Mid and Final term discussion

[dapp] Best practice for sender address

[dapp] Best practice for sender address

by MICHELE AGOSTINI -
Number of replies: 1

Hello, I saw that I can retrieve the address of the sender using web3.eth.getAccounts(), which returns a Promise object. It is way easier to ask the user to explicitly insert the address in a text form. Is it better to go through getAccounts() for some reasons (e.g., ux or security issues)?

Thanks for the help.

In reply to MICHELE AGOSTINI

Re: [dapp] Best practice for sender address

by ANDREA LISI -

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