0

Assuming standard flow of events

  1. User, who is owner of address A approves to spend X ERC20 tokens, using approve method to a smart-contract S from address A.
  2. Then, user calls S to spend that amount X and do its work.

What if in-between 1 and 2, someone else, who can call S will invoke spending from address A?

Is there any protection from that in ETH itself, e.g. to check if S is called by the user who gave the approval (and not someone else)?

What if someone watching me, when I use uniswap, e.g. and calls its smart-contract instead of me? After I give approval, but before the swap tx sinks in?

What is the standard way to handle that risk, if it exists?

4
  • 1
    "Is there any protection from that in ETH itself" -> no! The logic of Contract S decides who is allowed to do that and who is not. People give on uniswap MAX allowance because they trust the protocol and only they can trigger swap.
    – Majd TL
    Commented Nov 2, 2022 at 10:22
  • @MajdTL so if anyone can trigger a smart contract, that has access to my funds → means anyone can spend my funds? Commented Nov 2, 2022 at 13:41
  • @MajdTL also, does that mean, if someone who can call uniswap contract goes wild — they can call its methods with users' wallets, who still have valid approvals? Commented Nov 2, 2022 at 13:47
  • Smart Contract S must have a method that calls the "transferFrom" method from the ERC20 Contract. This method in Contract S has a logic and this logic says who can call it and what will happen if it is called. If this method has a bug or is implemented wrong then your money is at risk.
    – Majd TL
    Commented Nov 2, 2022 at 14:40

2 Answers 2

0

Yes there is, the approve function is based on your address.

Usually in the form of

approve(spender, amount)

This means the contract can spend "amount" quantity of tokens that lies in "spender"s wallet.

Another user cannot act on behalf of the smart contract you approved for. (Remember: You are approve the contract to use the funds in your wallet)

The only way someone can use this is if they hack the smart contract in some way, then calls a function within the smart contract to move your funds. In this way the "blockchain" will think the contract is moving the funds.

2
  • Why is it "Another user cannot act on behalf of the smart contract you approved for"? I though, unless checked in contract itself, anyone can call it. Commented Nov 2, 2022 at 13:42
  • A user cannot act on behalf of the smart contract as the logic of the functions in the smart contract prohibit this. F.E TransferFrom function will check if the msg.sender is allowed to move funds..if not, they cant, if yes, they can. If this check is not in the smart contract, then yes, the contract can be drained. Let's say a contract has a function called withdrawETHER() which sends a certain amount of ETH to the requester based on their deposit, IF this contract did not track everyones deposits and knows that a requester may just request a certain amount, then 1 person can drain it Commented Nov 2, 2022 at 13:53
0

It's not trivial but yes look at the anyswap hacks. Not to mention many contracts have admins with control privileges and "rescue" functions that may be exploited. Many users use protocols that have upgradable proxy contracts. If things like this concern you, you should be extra careful.

One way to handle this is to have more than one wallet and limit the amount of exposure across them. Have one that grants no allowances and acts as a vault, with another that buffers your allowances and dapp interactions.

I wrote a short article that touches on this: https://toshokan.samurais.io/web3-safety-the-hot-the-hard-the-cold/

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.