I have 2 contracts. For example: Contract 1

contract Token {

  using SafeMath for uint256;

  function transfer(address _to, uint256 _value) returns (bool) {
      var test = msg.sender; //msg.sender == address Contract2 
    balances[msg.sender] = balances[msg.sender].sub(_value);
    balances[_to] = balances[_to].add(_value);
    Transfer(msg.sender, _to, _value);
    return true;


Contract 2

contract Contact2{
    function test() {
          address test = msg.sender; //msg.sender==address my wallets
          Token(0x5faf852d0bae90ae3adb58f68deae7fcfc065a6d).transfer(this, 1);

When I call test () from the contract2, msg.sender = the address of my wallet. But when running Token(0x5faf852d0bae90ae3adb58f68deae7fcfc065a6d).transfer(this, 1) then msg.sender = address Contract2

As in the contract Token get msg.sender = my wallet?


msg.sender is always the account that sent the transaction, in this case it's Contract 1 that is calling transfer. As @siid pointed out in the comments, the answer to this issue is the approve/transferFrom workflow part of the erc20 standard.

| improve this answer | |

You call contract 2 from your wallet, there, msg.sender is your wallet. Contract 2 call contract 1 transfer function, there, msg.sender is the address of contract2.

| improve this answer | |

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.