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.
Method: Get
URL: https://open-api.openocean.finance/v3/:chain/tokenList
Parameters:
Parameter | Type | Example | Description |
---|---|---|---|
chain | string | bsc | Please refer to "supported chains" for the corresponding chain codes/chain ID |
Your request will look like this:
And your response will look like this:
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: Check Approve
Approving assets is necessary for Defi users to authorize the contract to use their tokens to swap. As with the getBalance method, you can use the wallet method or directly use our SDK to get a specific token approved for trading. You can use the getAllowance API to query the allowance data from our server.
Method: Get
URL: https://open-api.openocean.finance/v1/cross/getAllowance
Parameters:
Parameter | Type | Parameter | Description |
---|---|---|---|
account | string | 0xf8953d8671644348303cfa8Ae408F5d9fb884761 | wallet address |
chainId | number | 56 | chain id (1/56/100/137/250/42161/43114) In particular, we have defined Terra as 400 |
inTokenAddress | string | 0x9029FdFAe9A03135846381c7cE16595C3554e10A,0x08ba0619b1e7a582e0bce5bbe9843322c954c340 | token address |
contractAddress | string | 0x6352a56caadC4F1E25CD6c75970Fa768A3304e64 | contract address |
Your request will be like this:
And your response will look like this:
Step 3: Quote transaction
You can directly use Open API to quote the token exchange amount.
Method: Get
URL: https://open-api.openocean.finance/v3/:chain/quote
Parameters:
Parameter | Type | Example | Description |
---|---|---|---|
chain | string | bsc, avax, fantom | Please refer to "supported chains" for the corresponding chain codes/chain ID |
inTokenAddress | string | 0x9029FdFAe9A03135846381c7cE16595C3554e10A | The token address you want to sell. |
outTokenAddress | string | 0xEeeeeEeeeEeEeeEeEeEeeEEEeeeeEeeeeeeeEEeE | The token address you want to receive. |
amount | number | 10 | Sell amount. |
gasPrice | number | 5 | you can set it yourself or get it through GetGasPrice. |
slippage | number | 1 | The percentage of difference you are accepted in the reveive amount(higher or lower). |
For example, you want to pick up the price between OOE and BNB on BNB chain.
If you call it right, you will get a result like this:
Step 4: Send transaction
Here is the last step! Now you have several ways to swap the token you selected. You can use the swap_quote API to get the transaction body from our API server. Here is a case for you to make a transaction on BNB Chain.
The work flow we recommand for API users, is using Swapquote API to get transaction body, then use the wallet to request your transaction on chain.
Once your wallet plugin was triggered, which means you are successfully using the sendTransaction function and all params are set. You can swap your token and the transaction hash can be gotten once your transaction is sent to the public chain.
Last updated