3

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

I'm using web3@1.0.0-beta.29 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

  • If you want to send any tras it should be signed with your private key/password. I couldn't see in your code. – Jitendra Kumar. Balla Feb 3 '18 at 10:47
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 })
| improve this answer | |
  • 3
    the point is to set a defaultAccount and not have to specify the from in every send – Micha Roon Feb 3 '18 at 8:12
  • Default account is already set, its just a general value that you can use. – btc4cash Mar 29 '18 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.

| improve this answer | |
  • according to the documentation, the default account should be used for that. – Micha Roon Feb 3 '18 at 8:12
  • Please add the link to the documentation that mention that. – btc4cash Mar 29 '18 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. – Broots Waymb Apr 20 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.

| improve this answer | |
  • 1
    the goal is to have Web3 fill out the FROM automatically. The error has nothing to do with signing – Micha Roon Feb 6 '18 at 22:13

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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