0

I have a deployed contract (contract A) which should act as a storage (let's say 0xD7ACd2a9FD159E69Bb102A1ca21C9a3e3A5F771B).

Now from my ERC20 Token contract (contract B), I am calling approve to the contract A to allow it to "withdraw" my tokens.

Would it be possible to call trasnferFrom of contract A inside another function of contract B?

I am trying with (inside the code of B)

address A = 0xD7ACd2a9FD159E69Bb102A1ca21C9a3e3A5F771B;
...
approve(A,_amount);
A.call(bytes4(keccak256("transferFrom(address, address, uint256)")), (_B, msg.sender, _amount));

But it doesn't work since I am giving too many arg to the call function.

What would be the best way to transfer automatically my token (contract A) to a deployed ERC20 contract from within the contract B itslef?

EDIT

So on my receiver contract A, I added the function:

    function getTokens (address from, uint256 value) public returns (bool) {
        transferFrom(from, address(this), value);
        return true;
    }

And on my sender contract B I am calling:

    address contract_address = 0x09943Fa8DD32C76f7b880627a0F6af73e8f5A595 ;
     
...
        approve(contract_address, _value);
        contract_address.call(abi.encode("getTokens(address, uint256)",msg.sender, _value));
...

But even if tx is succesfull, when I check balance of contract A, it's 0. I don't know exactly what I am missing.

4
  • Are you sure you are checking balanceOf and not just balance? Nov 25, 2020 at 13:58
  • Yes I am calling function balanceOf(address account) public view override returns (uint256) { return _balances[account]; } with the contract address as argument, but still balance is 0
    – 4NDR34
    Nov 25, 2020 at 14:31
  • I am having a doubt: since the two contract that interacts are deployed on VM (Remix), should the address variable or some of the inputs of the functions be indicated as memory? Cause If i check allowance on Contract B it's correct, but if I try to call "tranferFrom" from contract A, it doesn't work cause "value exceed balance".
    – 4NDR34
    Nov 25, 2020 at 15:43
  • Hi @4NDR34, Have you find any solution of this. I have the exactly the same problem. Any information would be of great help. Apr 2, 2021 at 21:22

1 Answer 1

0

I'm not 100% sure I understood your scenario correctly, but let's try to answer.

No you can't "push" tokens with transferFrom. That function needs to be called by the contract / EOA which wants to get the tokens (and has allowance to withdraw).

If you want to push tokens you can use the transfer function, but that has different semantics, as you probably know.

What you can do is add functionality in whatever contract wants the tokens, for example a new function getTheTokens. That function could then be called by whatever other contract and that function calls the transferFrom.

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.