简体   繁体   English

ehtereum 智能合约从另一个合约中批准支出者

[英]ehtereum smart contract approve spender from another contract

I have a erc20 token and in another contract I want to create a token swap function.我有一个 erc20 代币,在另一个合约中我想创建一个代币交换 function。 So very easily, one send a usdc token and swap my erc20 token in 1:1 ratio.非常容易,发送一个 usdc 代币并以 1:1 的比例交换我的 erc20 代币。 Problem is how to approve to spend my erc20 token.问题是如何批准使用我的 erc20 代币。 I tried several times but can't find a way.我尝试了几次,但找不到方法。

interface IERC20 {...} 

contract AnotherContract {

function approve(address _spender, uint256 _amount) public returns(bool) {
    return IERC20(MyToken).approve(_spender, _amount);
}

I deployed this another contract and when I call approve function from it.我部署了另一个合同,当我调用它时,我批准 function。 So When I set '_spender' to this contract address.因此,当我将“_spender”设置为此合约地址时。 The result is weird.结果很奇怪。 So this contract is owner and spender both.. As I think a user should be as a owner and this contract should be a spender.所以这个合同既是所有者又是支出者。我认为用户应该是所有者,而这个合同应该是支出者。 But function calling from onchain.但是 function 从链上调用。 the msg.sender is going to be this contract address self. msg.sender 将成为这个合约地址。

I don't understand and am confusing.我不明白,我很困惑。 anybody knows or have some rescoures?有人知道或有一些资源吗? Thank you.谢谢你。

When your AnotherContract executes the approve() function in MyToken , the msg.sender in MyToken is AnotherContract - not the original transaction sender.当您的AnotherContract执行 MyToken 中的approve() function 时, MyToken中的msg.senderMyToken - AnotherContract不是原始交易发送者。

Which effectively approves AnotherContract 's tokens to be spent by _spender .这有效地批准AnotherContract的令牌由_spender


Unless the MyToken has a way to delegate the approval (eg 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.除非MyToken有办法委派批准(例如,通过使用已弃用的tx.origin而不是msg.sender ,这会引入安全漏洞),否则用户将不得不手动执行批准,而不是通过您的外部合同。

Many ERC-20 implementations use this approach for security purposes.许多 ERC-20 实现出于安全目的使用这种方法。 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 的情况,因为用户会认为他们正在获得空投。

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

Can you please tell me how you eventually solved this problem?你能告诉我你最终是如何解决这个问题的吗? Im needing this for a really nice project.我需要这个来做一个非常好的项目。 If you want you could even join in on it.如果你愿意,你甚至可以加入它。 You can also reach me disc here Viveraah#5948你也可以在这里找到我的光盘 Viveraah#5948

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM