且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

Ehtereum智能合同批准来自另一份合同的消费者

更新时间:2022-12-11 14:18:51

当您的AnotherContract执行MyToken中的approve()函数时,MyToken中的msg.senderAnotherContract--不是原始事务发送方。

有效地批准AnotherContract的令牌由_spender使用。


除非MyToken有办法委派审批(例如,使用已弃用的tx.origin而不是引入安全漏洞的msg.sender),否则用户必须手动执行审批,而不是通过您的外部合同。

许多ERC-20实现出于安全目的使用此方法。例如,为了防止出现这样的情况,诈骗者说服用户执行他们的恶意功能,因为用户会认为他们得到了空投。

// 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);
}