I'm making a subcurrency with solidity. I'm confused at implementing the transfer mechanism.

Here's my code:

function SubCoin(){
      balances[msg.sender] = totalPoints;

function send(address to, uint256 valueInmWard) {
    if (balances[msg.sender] >= valueInmWard) {
      balances[to] += valueInmWard;
      balances[msg.sender] -= valueInmWard;
      PointTransfer(msg.sender, to, valueInmWard);

I'm stuck at setting the msg.sender address. I only know I can use: web3.eth.defaultAccount = web3.eth.coinbase; to send from eth.accounts[0]. I call SubCoin.send.sendTransaction(web3.eth.accounts[1], 1000); in web3 to perform the action.

If I would like to send among eth.accounts[1], eth.accounts[2]..., how should I specify the msg.sender in web3?


sendTransaction expects an optional transaction object as first parameter after the method arguments. Among other things this allows you to specify the sender address via the from attribute.


var tx = {from: web3.eth.accounts[1]};
SubCoin.send.sendTransaction(web3.eth.accounts[0], 1000, tx);

The same transaction object can not only be passed to contract.method.sendTransaction but also to contract.method.call, web3.eth.sendTransaction, and web3.eth.call.

More information can be found in the documentation.

  • 1
    Hi, I'm studying DAPP. I have a question.. How can it be allowed changing msg.sender parameter from who calling api? Is it possible on main network? I'm confused how it can be safe.. Oct 23 '18 at 1:54
  • msg.sender is recovered from the transaction signature. It should only be possible to create the signature for a given address if you have the private key to that address.
    – JuliSmz
    Jun 19 '21 at 17:04

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.