0

I wrote a code which can connect to a contract and reads its total supply. here is my code(I removed abi because it was too big and didnt copy it here):

const Web3 = require('web3')
const rpcURL = "https://bsc-dataseed1.binance.org:443"
const web3 = new Web3(rpcURL)
const abi = []
const c_address = "0xA964a6dab034A4b5985603f7e86a596c7e0eA96e"
const contract = new web3.eth.Contract(abi, c_address)
contract.methods.totalSupply().call((err, result) => { console.log( web3.utils.fromWei(result, 'ether')) })

I watched some tutorials to write this and the last line confuses me. how can I change the last part to make it display the total supply using document.write? I actually need to use this on a html page. can anyone help me? my javascript knowledge is very limited. thanks

1 Answer 1

1

you can place your javascript logic inside the fat arrow function.

For example:

contract.methods.totalSupply().call((err, result) => {
  if(err){
    console.error('Error: ', err);
    // handle the error here
  }

  let supply = web3.utils.fromWei(result, 'ether');
  // You can add supply now to whatever part
  // of your page you want it displayed
});

If you are using pure html and js then I would suggest using document.getElementById() instead of document.write() because the latter replaces the entire html page. Take a look on how to do that here You can also find tutorials on how to write on specific document elements here

1
  • { document.getElementById( web3.utils.fromWei(result, 'ether')) } like this? where should I add the id?
    – Sam
    Commented Oct 27, 2021 at 12:00

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.