cjmling / findings

Notes on stuff i finds worth keeping for quick reference later on.
2 stars 0 forks source link

How to write request to BSC smart contract method. #328

Open cjmling opened 2 years ago

cjmling commented 2 years ago
  1. Download the contract abi file save as json. For example https://bscscan.com/address/0x8e33802adb469571e63a9677b453cfcd45852fe3#code

it will look something like this

[
  {
    "inputs": [
      { "internalType": "address", "name": "_strategy", "type": "address" }
    ],
    "stateMutability": "nonpayable",
    "type": "constructor"
  },
  {
    "anonymous": false,
cjmling commented 2 years ago
  1. Start contract instance

while web3 is

import Web3 from 'web3'
declare const window: any

const provider =
  process.browser && window !== undefined && window.ethereum !== undefined
    ? window.ethereum
    : 'https://bsc-dataseed.binance.org/'

const web3: any = new Web3(provider)

abi is the json file

import abiFile from '../../data/abis/filename.json'

address is the contract address , which in this case 0x8e33802adb469571e63a9677b453cfcd45852fe3

new web3.eth.Contract(abi, address);

  1. Call the method you want.

    const discountBP = await contract.methods.getDiscountBP(account).call()