1

I am looking for a way to implement smart contract that can spend user's ETH or erc-20 tokens with just one approve, without anything else required from user.

USECASE

  • 1.User approves token spending(usdt for example) he has 10 USDT.

  • 2.WITHOUT USER INTERACTION our smart contract can transfer this 10 usdt from user to wherever else it is coded to transfer it. (This function spend_user_usdt should be invoked with either something like chainlink cron job or manually by contract creator.

    1. Since User already approved token spending it doesn't require anything to do, tokens are transfered from his wallet automatically

I know this is possible because sometimes smart contracts get hacked and users have to revoke tokens approval, or their tokens are gone without additional manual confirmation.

But I wasn't able to find decent information on it.

PS: Just in case I don't want to write malicious smart contracts - that in not in the zone of my interests. This mechanics could be used to build very interesting projects that simplify user experience in many areas.

2 Answers 2

3

This is part of the ERC20 specification.

See here, the transferFrom() function. https://eips.ethereum.org/EIPS/eip-20#methods

Users send a transaction to the token contract to approve your contract to spend an allowance, the $10 over which your contract would have control.

How your contract exercises that control is entirely an application concern. I see no reason why a chainLink keeper or other external signal couldn't trigger the "approved" contract to take action, spending the user's funds.

Hope it helps.

1
  • So basically just to be sure that we are on the same page: Let's look at USDT. User calls approve function of USDT smart contract with my_contract_address with amount =10$. After this, my contract could without any actions needed for user spend his $10. ? Commented Dec 8, 2023 at 19:46
2

As Rob Hitchens said, this is an already existing feature of Erc20 tokens. However, i would like to add that you can't do that with ETH, since it's not a token. But you can do this with wrapped eth, and it's the same very simple mecanism.

4
  • hi, thanks for addition, I will add this comment to your answer too: just to be sure that we are on the same page: Let's look at USDT. User calls approve function of USDT smart contract where he specifies my_custom_contract_address and amount =10$. After this, my contract could without any actions needed from user spend his $10 ? Commented Dec 8, 2023 at 19:51
  • Yes, that is correct.
    – Yann
    Commented Dec 8, 2023 at 20:49
  • awesome thanks, i will try and let you know Commented Dec 8, 2023 at 21:21
  • 1
    Thanks, Yann. Yes, ETH is the exception since it's not an ERC20. Commented Dec 9, 2023 at 9:59

Your Answer

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

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