I am trying to transfer tokens from EOA to contractA.My TokenContract is ERC20. I have to call approve and transferFrom. But is it possible to combine them into 1 call deposit(). I tried using delegatecall for approve but getting errors? Can someone suggest the best approach to transfer in a single call?

function deposit(uint _tokens) public  {
        require (_tokens > 0);
        //Approve before calling this
        msg.sender.delegatecall(bytes4(keccak256(strConcat(addressToAsciiString(tokenContract),".approve(address _spender, uint256 _value)"))), _tokens);
        require (tokenContract.allowance(msg.sender, this) >= _tokens);
        require (tokenContract.transferFrom(msg.sender, tokenStore, _tokens));

    }

Please check the documentation on delegatecall. It says next:

There exists a special variant of a message call, named delegatecall which is identical to a message call apart from the fact that the code at the target address is executed in the context of the calling contract and msg.sender and msg.value do not change their values.

Which means delegatecall will just try execute approve function in scope of your calling contract. Only code will be used.

Just imagine what security hole will we have if with delegatecall could do calls to other contracts on behalf of msg.sender. ERC20 approved was designed with purpose that only owner of tokens may approve transfer & no one else.

BTW, delegatecall usage seems to not be correct as well. As I know there is no need for argument names, spaces & address of course.

If ERC20 contract is not yet deployed you may take a look at ERC827 which is extension of ERC20. It was designed for simmilar issue you got. But flow will be a little bit different: you call approveAndCall on token from your client / web3 which will then calls deposit and deposit will transferFrom tokens then. In this case, you need to create deposit payload on client side.

Your Answer

 

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

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