# Best Practice

### 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.

* Method: Get
* URL: <https://api.0xgen.io/v1/:chain/tokenList>
* Parameters:

<table data-header-hidden><thead><tr><th width="129"></th><th width="83"></th><th width="107"></th><th></th></tr></thead><tbody><tr><td>parameter</td><td>type</td><td>example</td><td>description</td></tr><tr><td>chain</td><td>string</td><td>bsc</td><td>eth, bsc, polygon, avax, arbitrum, fantom, xdai, moonriver, aurora, cronos, harmony, solana, sifchain, osmosis, optimism, ont, tron, okex, zksync, linea, scroll</td></tr></tbody></table>

**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:**

<pre class="language-javascript"><code class="lang-javascript"><strong>{  "code": 200,
</strong>    "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
        },...]
}
</code></pre>

**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.**

* Method: Get
* URL: <https://api.0xgen.io/v1/:chain/quote>
* Parameters:

  <table data-header-hidden><thead><tr><th width="119"></th><th width="74"></th><th width="233"></th><th></th></tr></thead><tbody><tr><td>parameter</td><td>type</td><td>example</td><td>description</td></tr><tr><td>chain</td><td>string</td><td>bsc</td><td>eth, bsc, polygon, avax, arbitrum, fantom, xdai, moonriver, aurora, cronos, harmony, solana, sifchain, osmosis, optimism, ont, tron, okex, zksync, linea, scroll</td></tr><tr><td>inTokenAddress</td><td>string</td><td>0x783C08b5F26E3daf8C4681F3bf49844e425b6393</td><td>token address</td></tr><tr><td>outTokenAddress</td><td>string</td><td>0xD81D45E7635400dDD9c028839e9a9eF479006B28</td><td>out token address</td></tr><tr><td>amount</td><td>string</td><td>5</td><td>token amount(without decimals)</td></tr><tr><td>gasPrice</td><td>string</td><td>1</td><td>without decimals</td></tr></tbody></table>

**Your request will be like this**

```javascript
   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:**

```javascript
  {
    "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.**

* Method: Get
* URL: <https://api.0xgen.io/v1/:chain/:dex/swap_quote>
* Parameters:

  | parameter       | type    | example                                    | description                                                                                                                                                      |
  | --------------- | ------- | ------------------------------------------ | ---------------------------------------------------------------------------------------------------------------------------------------------------------------- |
  | chain           | string  | bsc                                        | eth, bsc, polygon, avax, arbitrum, fantom, xdai, moonriver, aurora, cronos, harmony, solana, sifchain, osmosis, optimism, ont, tron, okex, zksync, linea, scroll |
  | dex             | string  | openOcean                                  | openOcean, 1inch, paraSwap, matcha, jupiter, 1sol                                                                                                                |
  | inTokenAddress  | string  | 0x783C08b5F26E3daf8C4681F3bf49844e425b6393 | token address                                                                                                                                                    |
  | outTokenAddress | string  | 0xD81D45E7635400dDD9c028839e9a9eF479006B28 | out token address                                                                                                                                                |
  | amount          | string  | 5                                          | token amount(without decimals)                                                                                                                                   |
  | gasPrice        | string  | 5                                          | without decimals                                                                                                                                                 |
  | slippage        | number  | 1                                          | 1 equals 1%, 1%-100%                                                                                                                                             |
  | account         | string  |                                            | user's address                                                                                                                                                   |
  | disableEstimate | boolean | true                                       | true/false(check balance and allowance)                                                                                                                          |
  | referrer        | string? | 0x0000000000000000000000000000000000000000 | Please contact us for a unique referrer parameter                                                                                                                |

**For example, you want to use openocean to obtain the on-chain transaction data;**

```javascript
{
    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:

```javascript
{
    "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.

```javascript
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
    }
}
```
