2

When working through the MetaCoin sample project; within the MetaCoin.sol file we have this method:

function sendCoin(address receiver, uint amount) returns(bool sufficient) {
    if (balances[msg.sender] < amount) return false;
    balances[msg.sender] -= amount;
    balances[receiver] += amount;
    Transfer(msg.sender, receiver, amount);
    return true;
}

Inside the test script; when calling the sendCoin method; the code is passing a third parameter: {from: account_one}

return meta.sendCoin(account_two, amount, {from: account_one});

I can understand the code needs to know from what account to send the coin from; but I am not understanding how this is working as the sendCoin method signature has two parameters.

4

in truffle you always need to add this object parameter representing the values you need for transactions in Ethereum. like in:

eth.sendTransaction({from: '0x036a03fc47084741f83938296a1c8ef67f6e34fa', to: '0xa8ade7feab1ece71446bed25fa0cf6745c19c3d5', value: web3.toWei(1, "ether")})

the to is provided by Truffle, which is of course the contract's address. But things like from, gas and value can/should/must be defined by hand. So the last arg is an object containing these parameters. Hint: When simply calling (read-only) a function, you can provide the from information, too, to pass a specific wallet address to a contract and get desired information returned.

2

The snippet of contract code you're looking at uses msg.sender, which is a special variable that always exists in the global namespace.

Normally when you call a contract, the msg.sender value will reflect the address of the default account of the caller (i.e. account[0]).

If you don't want to use the default account, then you can explicitly specify a different account using the {from: <a_different_account>} method. In your case the account_one variable can be set to point to any address you own.

Edit:

Also see this previous thread: Can I execute functions within a contract (transact) from multiple accounts?

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.