Gasless Transactions
We're introducing gasless transactions to make DeFi more accessible and user-friendly on Swing.
Gas fees have long been one of the biggest barriers to widespread DeFi adoption. Every transaction on Ethereum and other blockchain networks requires users to pay gas fees, which can be particularly burdensome during periods of network congestion. The solution to this problem is gasless transactions, which allow users to execute trades and approvals without directly paying gas fees.
What Are Gasless Transactions?
Gasless transactions improve the user experience (UX) by representing a paradigm shift in how users interact with blockchain networks. Instead of requiring users to hold native tokens (like ETH) to pay for transaction fees, gasless transactions enable users to execute trades and approvals without directly spending gas from their wallets.
The Technical Foundation: EIP-2612
Gasless transactions are enabled through EIP-2612, an Ethereum Improvement Proposal that introduces permit functionality to ERC-20 tokens. This standard allows users to grant permission to others to spend their tokens through cryptographic signatures rather than on-chain transactions.
Key benefits of EIP-2612 include:
- Simplified User Experience: Users only need to approve token transfers once instead of for each individual transfer
- Enhanced Security: Permit approvals include expiration times and unique nonces, providing better security than traditional approvals
- Reduced Gas Costs: Permit approvals require fewer transactions, resulting in significant gas savings
How Agent-Based Best Execution Works
The magic of gasless trading lies in agent-based best execution. Here's how the process works:
- User Submits Trade: Users can submit a trade without paying gas to the blockchain
- Token Permit: Users only need to permit the token spend, which is done without paying gas through cryptographic signatures
- Agent Execution: Specialized agents execute the trade on behalf of the user
- Gas Payment: The gas amount is typically deducted from the sending amount, so users never need to hold native tokens for gas
This approach eliminates the need for users to maintain gas token balances across multiple chains, significantly lowering the barrier to entry for DeFi participation.
Benefits for Users
Lower Barrier to Entry
- No need to hold native tokens for gas across multiple chains
- Simplified wallet management
- Reduced complexity for new DeFi users
Cost Efficiency
- Gas costs are automatically optimized through agent execution
- No need to estimate gas prices or worry about failed transactions due to insufficient gas
- Potential for better pricing through professional market makers
Enhanced User Experience
- One-click trading without gas management
- Seamless cross-chain operations
- Reduced transaction failures
Supported Routes and Chains
Gasless transactions are supported through multiple routing protocols, each offering different chain coverage and capabilities:
Feature | Velora Delta | Bebob Gasless |
---|---|---|
Transaction Types | Same-chain swaps Cross-chain transfers and swaps | Same-chain swaps only |
Supported Chains | • Ethereum • Polygon • BSC (Binance Smart Chain) • Avalanche • Arbitrum • Optimism (OP) • Base | • Ethereum • Arbitrum • Base • Blast • BSC (Binance Smart Chain) • Mode • Optimism (OP) • Polygon • Scroll • Taiko • zkSync |
How to Get Gasless Quotes
Gasless Transactions are supported on our SDK and API integrations.
Request Parameters
When requesting a quote from supported routing protocols, simply set the mode
parameter to gasless
.
mode: 'gasless'
API Sample Request
const result = await axios.post(
'https://swap.prod.swing.xyz/v0/transfer/quote',
{
fromChain: 'polygon',
tokenSymbol: 'USDC',
fromTokenAddress: '0xcbe56b00d173a26a5978ce90db2e33622fd95a28',
fromUserAddress: '0x018c15DA1239B84b08283799B89045CD476BBbBb',
toChain: 'ethereum',
toTokenSymbol: 'ETH',
toTokenAddress: '0x0000000000000000000000000000000000000000',
toUserAddress: '0x018c15DA1239B84b08283799B89045CD476BBbBb',
tokenAmount: '1000000000',
projectId: 'replug',
mode: 'gasless', // Set mode to gasless
},
);
SDK Sample Request
import type { TransferParams } from '@swing.xyz/sdk';
const transferParams: TransferParams = {
fromChain: 'ethereum',
fromToken: 'USDC',
fromUserAddress: '0x0000',
amount: '100',
toChain: 'polygon',
toToken: 'USDC',
toUserAddress: '0x0000',
maxSlippage: 0.01,
mode: 'gasless', // Set mode to gasless
};
const quote = await swingSDK.getQuote(transferParams);
Execute a Gasless Transaction
After receiving a gasless quote, executing the transaction requires handling EIP-712 typed data signatures instead of regular transaction data. The API response will contain different formats depending on whether gasless execution is available.
Understanding Response Types
When you request a quote with mode: 'gasless'
, the API may return two different transaction formats:
Regular Transaction Response
{
"tx": {
"from": "0x018c15DA1239B84b08283799B89045CD476BBbBb",
"to": "0x39E3e49C99834C9573c9FC7Ff5A4B226cD7B0E63",
"data": "0x301a3720000000000000000000000000eeeeeeeeeeee...",
"value": "0x0e35fa931a0000",
"gas": "0x06a02f"
}
}
Gasless Transaction Response
{
"tx": {
"from": "0x018c15DA1239B84b08283799B89045CD476BBbBb",
"to": "0xbeb0b0623f66bE8cE162EbDfA2ec543A522F4ea6",
"meta": {
"domain": {
"name": "JamSettlement",
"version": "2",
"chainId": 137,
"verifyingContract": "0xbeb0b0623f66bE8cE162EbDfA2ec543A522F4ea6"
},
"types": {
"JamOrder": [
{ "name": "taker", "type": "address" },
{ "name": "receiver", "type": "address" },
// ... additional type definitions
]
},
"value": {
"taker": "0x018c15DA1239B84b08283799B89045CD476BBbBb",
"receiver": "0x018c15DA1239B84b08283799B89045CD476BBbBb",
"expiry": 1750260501,
// ... order parameters
}
},
"data": "0x",
"value": "0x00",
"gas": "797387"
}
}
The presence of the meta
field containing domain
, types
, and value
indicates this is a gasless transaction that requires EIP-712 signature handling.
Implementation
Detecting Transaction Type
function isGaslessTransaction(tx) {
return tx.meta && tx.meta.domain && tx.meta.types && tx.meta.value;
}
Handling Regular Transactions
async function executeRegularTransaction(tx) {
const signer = await getSigner();
const txData = {
data: tx.data,
from: tx.from,
to: tx.to,
value: tx.value,
gasLimit: tx.gas,
};
const txResponse = await signer.sendTransaction(txData);
const receipt = await txResponse.wait();
return txResponse.hash;
}
Handling Gasless Transactions
async function executeGaslessTransaction(tx) {
const signer = await getSigner();
const { domain, types, value } = tx.meta;
// Sign the EIP-712 typed data
const signature = await signer.signTypedData(domain, types, value);
// Submit signed order for gasless execution: TODO
return signature;
}
Complete Handler
import { ethers } from 'ethers';
async function getSigner() {
const provider = new ethers.providers.Web3Provider(window.ethereum);
return provider.getSigner();
}
async function executeSwingTransaction(apiResponse) {
const { tx } = apiResponse;
if (isGaslessTransaction(tx)) {
console.log("Executing gasless transaction");
return await executeGaslessTransaction(tx);
} else {
console.log("Executing regular transaction");
return await executeRegularTransaction(tx);
}
}
// Usage
const quoteResponse = await getQuote({ mode: 'gasless' });
const txHash = await executeSwingTransaction(quoteResponse);
SDK Implementation
The Swing SDK handles gasless transaction detection and execution automatically:
import { SwingSDK } from '@swing.xyz/sdk';
const swingSDK = new SwingSDK({
projectId: 'your-project-id',
debug: true,
});
// Get gasless quote
const quote = await swingSDK.getQuote({
fromChain: 'polygon',
fromToken: 'USDC',
fromUserAddress: '0x018c15DA1239B84b08283799B89045CD476BBbBb',
toChain: 'ethereum',
toToken: 'ETH',
toUserAddress: '0x018c15DA1239B84b08283799B89045CD476BBbBb',
amount: '1000',
mode: 'gasless'
});
// Execute transaction (SDK handles gasless detection automatically)
const transfer = await swingSDK.transfer(quote);
Important Notes
- Signature Security: EIP-712 signatures include expiration times and nonces for security
- Network Requirements: Gasless execution is only available on supported chains and routing protocols
- Fallback Handling: Always implement fallback logic for cases where gasless execution fails
- Gas Deduction: Gas costs are typically deducted from the sending token amount, so users receive slightly less of their destination token
Error Handling
async function executeWithFallback(apiResponse) {
try {
return await executeSwingTransaction(apiResponse);
} catch (error) {
console.error('Gasless execution failed:', error);
// Fallback to regular transaction if gasless fails
if (isGaslessTransaction(apiResponse.tx)) {
console.log('Attempting fallback to regular execution');
// Request regular quote and execute
const regularQuote = await getQuote({ mode: 'regular' });
return await executeSwingTransaction(regularQuote);
}
throw error;
}
}
Next Steps
SDK Guides
Build your own any-to-any cross-chain UI and interact with Swing API & web3 wallets.
🎯 A great choice for JavaScript developers looking to build quickly
Widget Guides
Learn how to embed the cross-chain widget into your app with a few lines of code.
🎯 A slick pre-built plug and play UI for developers looking to quickly go to market.
API Guides
Learn the key API principles and how to integrate endpoints into dApps, web or mobile apps.
🎯 Perfect for developers who aren't using a JavaScript stack!