Handle Native Tokens

View as Markdown

Understanding Native Tokens

Native tokens are the base currencies of their blockchains—for example, ETH on Ethereum, BNB on BNB Chain, and POL on Polygon. Because native tokens do not have contract addresses like ERC-20 tokens, the placeholder address: 0xEeeeeEeeeEeEeeEeEeEeeEEEeeeeEeeeeeeeEEeE is commonly used to represent native tokens in blockchain transactions.

Notes on Native Token Representation

For most of the EVM-compatible chains that we support, the native token is represented using the placeholder address 0xEeeeeEeeeEeEeeEeEeEeeEEEeeeeEeeeeeeeEEeE. This convention exists because native assets (e.g., ETH on Ethereum) do not have contract addresses like ERC-20 tokens.

Mantle Network Exception

Mantle’s native token, MNT, does not follow this convention. On the Mantle EVM chain, MNT has a designated contract address: 0xdeaddeaddeaddeaddeaddeaddeaddeaddead0000.

This address applies only to the native MNT token on Mantle and should not be used for wrapped or bridged versions on other networks. Refer to the linked guide for additional differences between Ethereum and Mantle.

For more context on native token addresses:

Steps for Handling Native Tokens

1

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.

2

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.

See a full Swap API headless example that demonstrates how to handle native tokens.

Code example

Here’s a code snippet of how you can implement the above steps:

expandable
1import { createWalletClient, http, parseEther, sendTransaction } from "viem";
2import { privateKeyToAccount } from "viem/accounts";
3import { mainnet } from "viem/chains";
4
5// Create a wallet client using your private key
6const account = privateKeyToAccount("YOUR_PRIVATE_KEY");
7const client = createWalletClient({
8 account,
9 chain: mainnet,
10 transport: http("https://eth-mainnet.g.alchemy.com/v2/YOUR_ALCHEMY_KEY"), // Provide an authenticated RPC URL to avoid rate-limiting
11});
12
13// Check if the sellToken is a native token (ETH) to skip the allowance check
14if (eth.address === "0xEeeeeEeeeEeEeeEeEeEeeEEEeeeeEeeeeeeeEEeE") {
15 console.log("Native token detected, no need for an allowance check");
16} else {
17 // Check if allowance is required for non-native tokens
18 if (price.issues.allowance !== null) {
19 try {
20 const { request } = await eth.simulate.approve([
21 price.issues.allowance.spender,
22 maxUint256,
23 ]);
24 // Remainder of code...
25 } catch (error) {
26 console.error("Error approving token:", error);
27 }
28 }
29}
30
31// Check if it's a native token (like ETH)
32if (eth.address === "0xEeeeeEeeeEeEeeEeEeEeeEEEeeeeEeeeeeeeEEeE") {
33 const nonce = await client.getTransactionCount({
34 address: client.account.address,
35 });
36 // Directly sign and send the native token transaction
37 const txHash = await client.sendTransaction({
38 account: client.account,
39 chain: client.chain,
40 gas: quote?.transaction.gas ? BigInt(quote.transaction.gas) : undefined,
41 to: quote?.transaction.to,
42 data: quote.transaction.data,
43 value: BigInt(quote.transaction.value), // Ensure value is set for native tokens
44 gasPrice: quote?.transaction.gasPrice
45 ? BigInt(quote.transaction.gasPrice)
46 : undefined,
47 nonce: nonce,
48 });
49 // Log the transaction hash
50 console.log("Transaction sent:", txHash);
51}

Wrapping and Unwrapping between ETH and WETH

Easily wrap and unwrap between ETH and WETH by requesting a swap quote by setting sellToken and buyToken as either the contract address for WETH or ETH. The swap quote returned will provide the calldata to directly interact with the WETH9 contract and not with 0x Settler contract.

Wrap ETH

GET

$curl --request GET \
>https://api.0x.org/swap/allowance-holder/quote?buyToken=0xc02aaa39b223fe8d0a0e5c4f27ead9083c756cc2&sellToken=0xEeeeeEeeeEeEeeEeEeEeeEEEeeeeEeeeeeeeEEeE&buyAmount=10000000" \
> --header '0x-api-key: YOUR_API_KEY' \
> --header '0x-version: v2'

Unwrap ETH

GET

$curl --request GET \
>https://api.0x.org/swap/allowance-holder/quote?buyToken=0xEeeeeEeeeEeEeeEeEeEeeEEEeeeeEeeeeeeeEEeE&sellToken=0xc02aaa39b223fe8d0a0e5c4f27ead9083c756cc2&buyAmount=10000000" \
> --header '0x-api-key: YOUR_API_KEY' \
> --header '0x-version: v2'