1

I used the official code to create a token contract,and I created a new contract. Now I hope to use this new contract to invoke token contract, transfer token from A account to B account, and encounter the problem of no transferable quota.

pragma solidity ^0.4.17;

interface Token {
    function approve(address spender, uint256 value) public returns (bool);
    function transferFrom(address from, address to, uint256 value) public returns (bool);   
}


/**
 * The TrxCoin contract does this and that...
 */
contract TrxCoin {

    Token token = Token(0xAc08fe3C9F442990C52904dE997D9972499bD3E6);

    function getContractAddr() view public returns (address) {
        return this;
    }

    function approve(address spender, uint256 value) public {
        require(token.approve(spender, value));
    }

    function transfer(address _to, uint value) public payable {
        require(token.transferFrom(msg.sender, _to, value));
    }
}

When I use the token contract to call the approve method directly, I can transfer through the new contract, but I can't allocate the quota directly by calling the approve method with the new contract.

Why is this? Thank you for the answer!

user94559
  • 59,196
  • 6
  • 103
  • 103
Leo
  • 11
  • 2

1 Answers1

1

You're hitting this problem because you're attempting to approve the transfer of tokens from your contract, not the actual owner's address.

The ERC20 approve method writes to it's state that the requester is giving permission to let the spender perform the transaction. It does this with something like allowed[msg.sender][_spender] = _value;.

When you are invoking the token contract (C) from your account (A), msg.sender is set to address(A). However, when you're calling the token contract from TrxCoin, you've now introduced a new contract (B) as a middleman. The chain is now A->B->C. In this case, the msg.sender that C receives is now address(B). At this point, the Token contract state is still set to not allow any tokens owned by A to be transferred to spender.

There's no reason to delegate through the TrxCoin contract for the approval. Just invoke the Token contract directly.

Adam Kipnis
  • 10,175
  • 10
  • 35
  • 48
  • Thank you for your answer. Can I not be able to make a transfer operation if I use approre to set the address of the contract TrxCoin with the address of the address A? – Leo Dec 29 '17 at 03:00