Solidity – How to Execute transferFrom Function of an ERC20 Token?

erc-20solidity

I am trying to accept ERC20 Tokens as payment in a contract. As I understand it the sender will have to use the approve function to give my contract an allowance first. So far so good. What I do not understand is how does my contract call the transferFrom function to credit itself.

Specifically I want to test if the 'allowance' approved for my contract is equal to a value and if the remaining balance of the sender is equal or greater than my contract's allowance. Conditionally to that I want to execute the 'transferFrom' function of the ERC20 Token.

I am trying to do the following:

if (
         && _Token.call(bytes4(sha3("allowance(owner, this)"))) = 100
         && _Token.call(bytes4(sha3("balanceOf(owner)"))) >= 100
         )
         {
         _Token.call(bytes4(sha3("transferFrom(owner, this, 100)")));

But the Myst compiler says Expected primary expression.

 && _Token.call(bytes4(sha3("allowance(owner, this)"))) = 100

Many thanks.

Best Answer

Your code fragment has a _Token variable, which you're treating as a generic address (using call() on it). If you know that this address is an ERC20 token, you can use Solidity to cast that variable to the appropriate type, and then it's a more intuitive function call, rather than figuring out signature hashes:

contract ERC20Token {
  function transferFrom(address from, address to, uint value);
}

contract MyContract {
  function myFunction(address tokenAddr) {
    ERC20Token tok = ERC20Token(tokenAddr);
    tok.transferFrom(_owner, _recipient, 100);
  }
}
Related Topic