Example - CAKE/BUSD rate on Pancaksewap
In this example we will read the Pancakeswap router contract to get the price from Cake using getAmountsOut contract function
Last updated
In this example we will read the Pancakeswap router contract to get the price from Cake using getAmountsOut contract function
Last updated
To get started, open the Pancakeswap router contract on bscscan and find the getAmountsOut function. This function calculates the rates for given tokens pair (CAKE/BUSD in our case) Open it and see what parameters it needs.
We will not get into details of how and why, but in short, the Parameter 1 must be the amount of token we want to get rate for, so in our case it is CAKE amount. Notice that it is uint256 type, it means that we must convert the amount to WEI. We will do the conversion by using the To Wei function from Utils element (see in the demo editor)
This must be an array of tokens contract addresses that we want to get rate for. Cake: 0x0e09fabb73bd3ade0a17ecc321fd13a19e81ce82 BUSD: 0xe9e7CEA3DedcA5984780Bafc599bD69ADd087D56 And write it in array type with square brackets, and use double quotes for each parameter in array
Use the Contract Read event The result will be available only after this event triggers. In our case, the result will come in an array format, where the first item is the amount we have indicated and the second is the rate.
Remember that the whole response is in text format, so this array is actually a simple text. So, using Bubble feature find and replace, we will extract the second item from array. See in demo editor how exactly. Now the rate is in WEI format. We will use the From Wei action from Utils element And here it is, the result of our read contract will be in From Wei state. The rate for 1 Cake to BUSD is (6 Dec 2022):
Now we can do whatever we could need, like convert it to number, round it to 2 decimals or save in database.