For example, a crowdsale contract has a function that creates an ERC20 token called "token" using a token contract that is already defined further up in the code being deployed. The constructor contains

token = createTokenContract();

And the createTokenContract() method contains:

function createTokenContract() internal returns (TierToken) {
    return new ERC20Token();

When inside a payable function in the crowdsale it now calls a function of "token" inside my crowdsale contract like

token.transfer(beneficiary, tokenAmount);

Inside of that call who is the msg.sender? The transfer function is from OpenZeppelins implementation so contains:

    function transfer(address _to, uint256 _value) public returns (bool success) {
      require(_to != address(0)); //not sending to burn address
      require(_value <= balances[msg.sender]); // If the sender has sufficient funds to send
      require(_value>0);// and the amount is not zero or negative

      // SafeMath.sub will throw if there is not enough balance.
      balances[msg.sender] = balances[msg.sender].sub(_value);
      balances[_to] = balances[_to].add(_value);
      Transfer(msg.sender, _to, _value);
      return true;

In these balance checks and transfers where it uses balances[msg.sender], is that sender now the address of the crowdsale contract that contained the call to this function? Or is the sender the user that sent the ether to the contract (which then made the call to the token function)?

This may be simple but I cannot find this explained well in the docs in terms of msg.sender relating to contracts calling functions of other contracts.

Thanks for any help.


If a contract's function calls a function of another contract within it, is the address of the contract the msg.sender?


tx.origin is the external account that sent the transaction.

From Solidity docs:

msg.sender (address): sender of the message (current call)

tx.origin (address): sender of the transaction (full call chain)

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.