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.
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;constswapParams= { from:this.address, to:'0x6352a56caadc4f1e25cd6c75970fa768a3304e64',//Please use the contract from the contract page gas: estimatedGas, gasPrice: gasPrice, data }; constresult=awaitthis.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.