Links

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:
https://open-api.openocean.finance/v3/avax/tokenList
And your response will look like this:
{
code: 200,
data: [
{
"id": 1619,
"code": "avaware-usd",
"name": "Avaware USD",
"address": "0x783C08b5F26E3daf8C4681F3bf49844e425b6393",
"decimals": 18,
"symbol": "AUSD",
"icon": "https://cloudstorage.openocean.finance/images/1646208600388_8016544631355003.png",
"chain": "avax",
"createtime": "2022-03-02T08:10:07.000Z",
"hot": null,
"sort": "2022-03-02T08:10:07.000Z",
"chainId": 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: 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:
https://open-api.openocean.finance/v1/cross/getAllowance?chainId=56&account=0xf8953d8671644348303cfa8Ae408F5d9fb884761&inTokenAddress=0x55d398326f99059ff775485246999027b3197955,0x08ba0619b1e7a582e0bce5bbe9843322c954c340&contractAddress=0x6352a56caadC4F1E25CD6c75970Fa768A3304e64
And your response will look like this:
{
"code": 200,
"data": [
{
"symbol": "USDT",
"allowance": "0",
"raw": "0"
},
{
"symbol": "BMON",
"allowance": "0",
"raw": "0"
}
]
}
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.
const res = await axios.get( "https://open-api.openocean.finance/v3/bsc/quote", {
chain: 'bsc'
inTokenAddress: '0x9029FdFAe9A03135846381c7cE16595C3554e10A',
outTokenAddress: '0xEeeeeEeeeEeEeeEeEeEeeEEEeeeeEeeeeeeeEEeE',
amount: 10,
gasPrice: 5,
slippage:1,
}).then((res) => {
const result = res.data
}).catch((err) => {
throw new Error(err)
})
If you call it right, you will get a result like this:
{
code: 200,
data: {
"inToken": {
"symbol": "AUSD",
"name": "Avaware USD",
"address": "0x783C08b5F26E3daf8C4681F3bf49844e425b6393",
"decimals": 18
},
"outToken": {
"symbol": "EMBR",
"name": "EmbrToken",
"address": "0xD81D45E7635400dDD9c028839e9a9eF479006B28",
"decimals": 18
},
"inAmount": "5000000000000000000",
"outAmount": "126261357830302882735",
"estimatedGas": "189669",
"dexes": [
{
"dexIndex": 1,
"dexCode": "SushiSwap",
"swapAmount": "0"
},
...
],
"path": {
}
}
}
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.
async swap() {
if(this.address && this.inAmount > 0) {
let params = {
chain: 'bsc'
inTokenAddress: '0x9029FdFAe9A03135846381c7cE16595C3554e10A',
outTokenAddress: '0xEeeeeEeeeEeEeeEeEeEeeEEEeeeeEeeeeeeeEEeE',
amount: 5,
gasPrice: 5,
slippage:100,
};
const res = await axios.get("https://open-api.openocean.finance/v3/bsc/swap_quote?inTokenAddress=0xEeeeeEeeeEeEeeEeEeEeeEEEeeeeEeeeeeeeEEeE&outTokenAddress=0x55d398326f99059ff775485246999027b3197955&amount=5&gasPrice=5&slippage=100&account=0x929B44e589AC4dD99c0282614e9a844Ea9483C69");
if(res) {
const {estimatedGas,data,gasPrice} = res.data.data;
const swapParams = {
from:this.address,
to:'0x6352a56caadc4f1e25cd6c75970fa768a3304e64', //Please use the contract from the contract page
gas: estimatedGas,
gasPrice: gasPrice,
data
};
const result = await this.myWallet.sdk.eth.sendTransaction(swapParams)
};
else {
return
},
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.