Web3 & MetaMask - Bubble plugin
Get PluginAll our plugins
  • Overview
  • Quick Start Example
  • Elements details
    • Web3 & MetaMask
      • Connect To MetaMask
      • Disconnect MetaMask
      • Send token With MetaMask
      • Get Balance of a Token
      • Get Balance of Multiple Tokens
      • Get Token Symbol
      • Sign
      • Check signature
      • Sign Typed Data
      • Check signature-Typed Data
      • Smart Contract - Read
        • Example - CAKE/BUSD rate on Pancaksewap
      • Smart Contract - Write
        • Example - Write a function
      • Trigger Switch Chain
      • Approve - Set Allowance
      • Revoke Allowance
      • Deploy Contract
      • Add Chain to MetaMask
      • Get NFT Metadata
      • Check NFT Balance
      • Smart Contract - Read
      • Smart Contract - Write
    • Web3 Utils
      • Get Transaction confirmations
      • Get Transaction Details
      • To Wei
      • From Wei
      • Add Token to MetaMask
      • Get ENS data
      • Get ENS Domain from Address
  • Use with other Wallets & Plugins
  • Sign up a user with signature
  • How to use on Mobile
Powered by GitBook
On this page
  • Action name
  • Parameter 1
  • Parameter 2
  • Result
  1. Elements details
  2. Web3 & MetaMask
  3. Smart Contract - Read

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

PreviousSmart Contract - ReadNextSmart Contract - Write

Last updated 2 years ago

This demo is available

To get started, open the 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.

Action name

getAmountsOut

Parameter 1

Web3 Utils To Wei Result

Parameter 2

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

["0x0e09fabb73bd3ade0a17ecc321fd13a19e81ce82","0xe9e7CEA3DedcA5984780Bafc599bD69ADd087D56"]

Result

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.

["1000000000000000000","3939845677320195487"]
3.939845677320195487

Now we can do whatever we could need, like convert it to number, round it to 2 decimals or save in database.

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 (see in the demo editor)

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

To Wei function from Utils element
From Wei action from Utils element
here
Pancakeswap router contract on bscscan
Join the Web3 NoCode Discord Server!Discord
Our team is active on Discord. Join for discussions and support
Logo