Best Practice
Transferring token example
Step 1: Get Token Info
You can call the get Token List API to get all the available tokens we have on the blockchain you choose.
eth, bsc, polygon, avax, arbitrum, fantom, xdai, moonriver, aurora, cronos, harmony, solana, sifchain, osmosis, optimism, ont, tron, okex, zksync, linea, scroll
Your request will look like this:
async tokenList() {
const { data } = axios.get('https://api.0xgen.io/v1/bsc/tokenList');
}
And your response will look like this:
{ "code": 200,
"data": [
{
"id": 2048,
"code": "ezeth",
"name": "Renzo Restaked ETH",
"address": "0x2416092f143378750bb29b79eD961ab195CcEea5",
"decimals": 18,
"symbol": "ezETH",
"icon": "https://s3.0xgen.io/logos/1720409237571_35144943543615725.png",
"chain": "bsc",
"createtime": "2024-07-08T03:27:21.000Z",
"hot": null,
"sort": "2024-07-08T03:27:21.000Z",
"chainId": null,
"usd": null
},...]
}
Then you pick up the token you choose in the specific chain which is included. You need to save the token information you need for further operations
Step 2: Quote
You can obtain quotes from different platforms.
Parameters:
eth, bsc, polygon, avax, arbitrum, fantom, xdai, moonriver, aurora, cronos, harmony, solana, sifchain, osmosis, optimism, ont, tron, okex, zksync, linea, scroll
0x783C08b5F26E3daf8C4681F3bf49844e425b6393
0xD81D45E7635400dDD9c028839e9a9eF479006B28
token amount(without decimals)
Your request will be like this
let params = {
inTokenAddress: '0x9029FdFAe9A03135846381c7cE16595C3554e10A',
outTokenAddress: '0xEeeeeEeeeEeEeeEeEeEeeEEEeeeeEeeeeeeeEEeE',
amount: 5,
gasPrice: 1,
};
async quote(){
const { data } = await axios.get('https://api.0xgen.io/v1/bsc/quote', {params})
}
And your response will look like this:
{
"code": 200,
"data": {
"dexes": [
{
"name": "openOcean",
"amount": "1614773503060146858429"
},
{
"name": "1inch",
"amount": "1614222924617714762008"
}
],
"maxOutAmount": "1614773503060146858429",
"minOutAmount": "1614222924617714762008",
"inToken": {
"symbol": "BNB",
"name": "Binance Coin",
"address": "0xEeeeeEeeeEeEeeEeEeEeeEEEeeeeEeeeeeeeEEeE",
"decimals": 18
},
"outToken": {
"symbol": "1INCH",
"name": "1INCH Token",
"address": "0x111111111117dc0aa78b770fa6a738034120c302",
"decimals": 18
},
"inAmount": "5000000000000000000"
}
}
Step 3: Obtain on-chain transaction data.
You can obtain the on-chain transaction data from a specified platform.
Parameters:
eth, bsc, polygon, avax, arbitrum, fantom, xdai, moonriver, aurora, cronos, harmony, solana, sifchain, osmosis, optimism, ont, tron, okex, zksync, linea, scroll
openOcean, 1inch, paraSwap, matcha, jupiter, 1sol
0x783C08b5F26E3daf8C4681F3bf49844e425b6393
0xD81D45E7635400dDD9c028839e9a9eF479006B28
token amount(without decimals)
true/false(check balance and allowance)
0x0000000000000000000000000000000000000000
Please contact us for a unique referrer parameter
For example, you want to use openocean to obtain the on-chain transaction data;
{
let params = {
inTokenAddress: '0x9029FdFAe9A03135846381c7cE16595C3554e10A',
outTokenAddress: '0xEeeeeEeeeEeEeeEeEeEeeEEEeeeeEeeeeeeeEEeE',
amount: 5,
gasPrice: 1,
slippage:100,
account: ''// wallet address
};
const res = await axios.get('https://api.0xgen.io/v1/bsc/openOcean/swap_quote', {params})
}
response:
If you call it right, you will get a result like this:
{
"code": 200,
"data": {
"inToken": {
"symbol": "BNB",
"name": "Binance Coin",
"address": "0xEeeeeEeeeEeEeeEeEeEeeEEEeeeeEeeeeeeeEEeE",
"decimals": 18
},
"outToken": {
"symbol": "USDT",
"name": "Binance-Peg USD (T)",
"decimals": 18,
"address": "0x55d398326f99059ff775485246999027b3197955"
},
"inAmount": "5000000000000000",
"outAmount": "1533988843342961506",
"estimatedGas": "198245",
"minOutAmount": "1518648954909531891",
"from": "0x929B44e589AC4dD99c0282614e9a844Ea9483C69",
"to": "0x6352a56caadC4F1E25CD6c75970Fa768A3304e64",
"value": "5000000000000000",
"gasPrice": "5000000000",
"data": ""
}
}
sendTransaction
Here is a case for you to make a transaction on BNB Chain.
import { ethers } from "ethers";
import axios from "axios";
async function swap() {
const account = ''; // wallet address;
const private_key = ''; //private key;
const provider = new ethers.providers.JsonRpcProvider('');
const wallet = new ethers.Wallet(private_key, provider);
let params = {
inTokenAddress: '0x9029FdFAe9A03135846381c7cE16595C3554e10A',
outTokenAddress: '0xEeeeeEeeeEeEeeEeEeEeeEEEeeeeEeeeeeeeEEeE',
amount: 5,
gasPrice: 1,
slippage: 100,
account// wallet address
};
const res = await axios.get("https://api.0xgen.io/v1/bsc/openOcean/swap_quote", { params });
if (res) {
const { estimatedGas, data, gasPrice } = res.data.data;
const swapParams = {
from: '', // wallet address
to: '0x6352a56caadc4f1e25cd6c75970fa768a3304e64', //Please use the contract from the contract page
gas: estimatedGas,
gasPrice: gasPrice,
data
};
if (isNativeToken(params.inTokenAddress.toLowerCase())) {
swapParams.value = ethers.BigNumber.from(res.data.value);
}
const { hash } = await wallet.sendTransaction(swapParams)
} else {
return
}
}
Last updated