dappuniversity / token_sale

Code Your Own Cryptocurrency & ICO on Ethereum | Tutorial
406 stars 387 forks source link

Where the amount goes off? #48

Closed rodurma closed 2 years ago

rodurma commented 2 years ago

I don't understand this part of code:

require(tokenContract.transfer(msg.sender, _numberOfTokens));

the msg.sender sold to himself?

When you calls function from imported contract the msg.sender is address of caller contract (DappTokenSale is this case) ?

  // Buy tokens
  function buyTokens(uint256 _numberOfTokens) public payable {
    // Require that value is equal to tokens
    require(msg.value == multiply(_numberOfTokens, tokenPrice));

    // Require that the contract has enough tokens
    require(tokenContract.balanceOf(address(this)) >= _numberOfTokens);

    // Require that a transfer is successful
    require(tokenContract.transfer(msg.sender, _numberOfTokens));

    // keep track of tokensSold
    tokensSold += _numberOfTokens;

    // Trigger Sell Event
    emit Sell(msg.sender, _numberOfTokens);
  }
rodurma commented 2 years ago

Now I know https://ethereum.stackexchange.com/questions/28972/who-is-msg-sender-when-calling-a-contract-from-a-contract/28977