3

I would like to set a default account for my web3 operations with web3js but it does not work.

I'm using [email protected] and here is my code.

  const Web3 = require('web3')
  const Token = require('./Token.json')

  const web3 = new Web3(new Web3.providers.HttpProvider())//provider.web3
  web3.eth.defaultAccount = '0x4615ff6690a3bb23bd85051c5c69abba4092bbb4'
  const tkn = new web3.eth.Contract(Token.abi, '0xba9d4199fab4f26efe3551d490e3821486f135ba')

  tkn.methods.transfer('0x4615ff6690a3bb23bd85051c5c69abba4092bbb4', 1e9)
    .send()
    .then(console.log)
    .catch(console.log)

the output of this is

    Error: No "from" address specified in neither the given options, nor the default options.

the documentation says that this should not happen

1
  • If you want to send any tras it should be signed with your private key/password. I couldn't see in your code. Feb 3, 2018 at 10:47

3 Answers 3

1

I ran into the same problem. I had to explicitly specify the address on the send function call, like this:

tkn.methods.transfer('0x4615ff6690a3bb23bd85051c5c69abba4092bbb4', 1e9)
    .send({ from: web3.eth.defaultAccount })
2
  • 3
    the point is to set a defaultAccount and not have to specify the from in every send
    – Micha Roon
    Feb 3, 2018 at 8:12
  • Default account is already set, its just a general value that you can use.
    – btc4cash
    Mar 29, 2018 at 1:38
0

When creating the contract instance , you can set the "from" field in the options. Then all your transactions will default to that address. You could give the from address as the default Account.

3
  • according to the documentation, the default account should be used for that.
    – Micha Roon
    Feb 3, 2018 at 8:12
  • Please add the link to the documentation that mention that.
    – btc4cash
    Mar 29, 2018 at 1:38
  • @btc4cash - The docs appear inconsistent. defaultAccount says it should be used if from is missing. However, send does not list from as optional. Apr 20, 2020 at 0:34
0

Your correct, but if you want to send any transaction to eth network. That should be signed with your account/unlocked your account.

//unlock your account before call transfer web3.personal.unlockAccount(web3.eth.accounts[1],'abc123');

Or use private key to send information.

1
  • 1
    the goal is to have Web3 fill out the FROM automatically. The error has nothing to do with signing
    – Micha Roon
    Feb 6, 2018 at 22:13

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.