Mid and Final term discussion

[dapp] Best practice for sender address

Re: [dapp] Best practice for sender address

by ANDREA LISI -
Number of replies: 0

Hi,

Unfortunately, every time from the front-end you want to retrieve Eth related information (smart contract, current Metamask account's address) functions return Promise objects.

But since you can retrieve the account's address (e.g. with getAddress()) you can use that result as parameter of a smart contract function (I guess you need it for the {from: 'address'} metadata for smart contract functions)

Another way to get the account's address is

    this.web3.eth.getCoinbase(async (err, account) => {
         // "account" is the current account's address (e.g. from Metamask)
         // store "account" in a higher scope variable (e.g. global / object variable)
         // keep coding...
    });

You can do in two ways

  1. Get the account's address every time you need to call a smart contract function (slow);
  2. Get the account's address during window's loading and store it in a variable (unfortunately, if you change the address in Metamask you need to refresh the page)

Hope I answered to your question and I did not go off-topic