3

This question is about web3.js

I'm working on a very simple dapp to test out making a token. There's only two functions in my contract - one to mint coins and one to transfer them. I'm trying to make a fountain site where people can go get my token

The part that's confusing me is how I'm accessing the visitors account. This code snippet works - it'll let me grab their coinbase and use it as their default address.

var contractAddress = '0x791E39A210B49811459531A54790377540eFfcde';
var myCoin = eth.contract(contractABI).at(contractAddress);
var wallet = web3.eth.defaultAccount = web3.eth.coinbase;

var transaction = myCoin.mint(wallet, value)

But if I change it to not set the default address, i get an invalid address error. Below code does NOT work, despite me being able to log web3.eth.coinbase in the console no problem.

var contractAddress = '0x791E39A210B49811459531A54790377540eFfcde';
var myCoin = eth.contract(contractABI).at(contractAddress);
var wallet = web3.eth.coinbase;

var transaction = myCoin.mint(wallet, value)

Can anyone explain this implementation choice in web3.js? Why is my contract method dependent on defaultAccount in this way?

2

Every transaction requires a sender, a from account that pays for the gas. An invalid address error usually means that the sender hasn't been specified.

You can explicitly pass from for every transaction, like:

myCoin.mint(wallet, value, {from: web3.eth.coinbase})

Or specify it just once using web3.eth.defaultAccount = web3.eth.coinbase. The defaultAccount is then used whenever the from is not explicitly provided.

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.