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.

3 Answers 3

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?

0

https://trufflesuite.com/docs/truffle/how-to/contracts/interact-with-your-contracts/ says:

We passed an object as the third parameter to sendCoin. Note that the sendCoin function in our Solidity contract doesn't have a third parameter. What you see above is a special object that can always be passed as the last parameter to a function that lets you edit specific details about the transaction ("transaction params"). Here, we set the from address ensuring this transaction came from accounts[0]. The transaction params that you can set correspond to the fields in an Ethereum transaction:

  • from
  • to
  • gas
  • gasPrice
  • value
  • data
  • nonce

It is optional and as Richard Horrocks said, from defaults to accounts[0]. I guess that default is in the same way that the initial gas to migrate the contract comes from the same account.

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.