0
votes

I have deployed contract A. Now I am creating gateway contract B and I want to send some tokens of contract A to user address X using owner address. Worth to mention that contract A owner is the same as contract B. I do the following

contract A is Ownable { // this one already deployed by owner
  constructor() {
    owner = msg.sender; // owner address is 0x123
    approve(msg.sender, totalSupply); // let's approve all tokens for owner
  }      

  function transferFrom(address from, address to, uint256 value) public returns (bool) {
    require(value <= allowed[from][msg.sender], "Not allowed!");
    // let's skip other logic
  }
}    

contract B is Ownable { // gateway contract will be deployed and executed by same owner
  A contractA = ETC20(0x111);
  address payable X = 0x333;

  constructor() {
    owner = msg.sender; // owner address is 0x123
  }

  function giveAwayTokens(uint256 value) {
    contractA.transferFrom(owner, X, value);
  }
}

When I execute "giveAwayTokens" function from owner address (0x123), I get error "Not allowed!". So I am a bit confused right now, because the allowance of owner from owner is max supply. Or maybe the msg.sender is contractB itself? Please enlighten me what I am doing wrong here, thank you

1
This example is incomplete because we do not know what msg.sender in in the constructor.Mikko Ohtamaa
@MikkoOhtamaa I've updated the example with owner set logic. So basically both contract owner is 0x123 address. Also, 0x123 executes the giveAwayTokens functionSaulius

1 Answers

1
votes

When ContractB calls ContractA, the msg.sender in ContractA is ContractB address.

Based on the code and the error message, the owner (0x123) didn't allow ContractB to spend their tokens.

You need to set the value of allowed[<owner>][<ContractB>] to be at least the amount of tokens that you want to send.

Most likely you have an approve() function (defined in the token standard) that you can use. In the linked example, the caller of the function would be the owner, the spender would be the ContractB and the value would be any value equal or higher than the amount of tokens that you want to send (mind the decimals).