I'm creating a contract where the user would transfer X amounts of tokens A and B to my smart contract for a task. The problem with this is that in principle, the user would have to sign and send 3 transactions: 2 for approvals of the tokens, and then 1 more for the smart contract that it would call transferFrom(user, address(this), amount)
. To improve user experience, would it be possible instead to have the user call the function:
function transferFromUser(address token, uint256 value) {
// 0xa9059cbb = transfer function selector
(bool success, bytes memory data) = token.delegatecall(abi.encodeWithSelector(0xa9059cbb, address(this), value));
}
This way the context when calling transfer via delegatecall
would mark the user address as msg.sender
and hence it should work without the need for approval right? The token smart contract would think that the user itself is calling that function so there would be no need to sign on the approval. Is this correct?
There is a similar question being asked here Transfer ERC20 via onother smart contract (call transfer as delegate) but I opened this one because I don't entirely agree with the answer marked as accepted. In my case, contract A is my smart contract and contrat B is the token's contract. If user calls contractA, which calls contractB's transfer()
function with delegatecall()
, it would be as if the user calls transfer()
themselves right?
tx.origin
:)delegatecall
- is how the proxies work. And for your case - look into thepermits
. Collect the permits from the user, and then only 1 transaction is required, instead of 3. If tokens support EIP-2612permit2
contract that can be used with all the ERC20 tokens even if they are notpermit
compliant, although I see on the docs that it saysBefore integrating contracts can request users’ tokens through Permit2, users must approve the Permit2 contract through the specific token contract
. Does this mean that the user should, at least the first time, send a transaction specifically to allow permits for them in that token? Thanks!