When your AnotherContract
executes the approve()
function in MyToken
, the msg.sender
in MyToken
is AnotherContract
- not the original transaction sender.
Which effectively approves AnotherContract
's tokens to be spent by _spender
.
Unless the MyToken
has a way to delegate the approval (e.g. by using a deprecated tx.origin
instead of msg.sender
, which introdues a security flaw), the user will have to execute the approval manually, and not through your external contract.
Many ERC-20 implementations use this approach for security purposes. For example to prevent a situation, where a scammer would persuade a user to execute their malicious function, because the user would think they are getting an airdrop.
// function name suggests that the caller is going to receive an airdrop
function claimAirdrop() external {
/*
* fortunately, this won't work
* and the tx sender can't approve the scammer to spend their tokens this way
*/
USDTcontract.approve(scammer, 1000000);
}
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…