Handling native tokens with 0x Swap API
When using the 0x Swap API to handle native tokens like ETH, the process differs from ERC-20 tokens. Here’s what you need to know to handle native tokens properly:
Skip the allowance check: Unlike ERC-20 tokens, native tokens do not require an allowance or approval step. Therefore, you can skip the
approve()
call when preparing to send native tokens.Set the transaction value: Ensure that the
value
field in your transaction parameters accurately reflects the amount of native token (in wei) you intend to send.
tip
⚡️ Quicklink
See a full Swap API headless example that demonstrates how to handle native tokens. See the running instructions here.
Code example
Here's a code snippet of how you can implement the above steps:
import { createWalletClient, http, parseEther, sendTransaction } from 'viem';
import { privateKeyToAccount } from 'viem/accounts';
import { mainnet } from 'viem/chains';
// Create a wallet client using your private key
const account = privateKeyToAccount('YOUR_PRIVATE_KEY');
const client = createWalletClient({
account,
chain: mainnet,
transport: http('https://eth-mainnet.g.alchemy.com/v2/YOUR_ALCHEMY_KEY'), // Provide an authenticated RPC URL to avoid rate-limiting
});
// Check if the sellToken is a native token (ETH) to skip the allowance check
if (eth.address === '0xEeeeeEeeeEeEeeEeEeEeeEEEeeeeEeeeeeeeEEeE') {
console.log('Native token detected, no need for an allowance check');
} else {
// Check if allowance is required for non-native tokens
if (price.issues.allowance !== null) {
try {
const { request } = await eth.simulate.approve([price.issues.allowance.spender, maxUint256]);
// Remainder of code...
} catch (error) {
console.error('Error approving token:', error);
}
}
}
// Check if it's a native token (like ETH)
if (eth.address === '0xEeeeeEeeeEeEeeEeEeEeeEEEeeeeEeeeeeeeEEeE') {
const nonce = await client.getTransactionCount({
address: client.account.address,
});
// Directly sign and send the native token transaction
const txHash = await client.sendTransaction({
account: client.account,
chain: client.chain,
gas: quote?.transaction.gas ? BigInt(quote.transaction.gas) : undefined,
to: quote?.transaction.to,
data: quote.transaction.data,
value: BigInt(quote.transaction.value), // Ensure value is set for native tokens
gasPrice: quote?.transaction.gasPrice ? BigInt(quote.transaction.gasPrice) : undefined,
nonce: nonce,
});
// Log the transaction hash
console.log('Transaction sent:', txHash);
}