简体   繁体   English

如何创建智能合约来转发 ERC20?

[英]How can I create smart contract for forwarding ERC20 with solidity?

I try to to create a contract that receive ERC20 token, then forward to destination address with below code, but it seems like it is not working correctly.我尝试创建一个接收 ERC20 令牌的合约,然后使用以下代码转发到目标地址,但它似乎无法正常工作。

receive() external payable
    {
        emit Deposit(msg.sender, msg.value);
        ERC20 token = ERC20(TOKEN_ADDRESS);
        token.transfer(DESTINATION_ADDRESS,token.balanceOf(address(this));
    }
  • Is there anyway to create contract like this with smartcontract?反正有没有用智能合约创建这样的合约?
  • How I can change above code to make it work?我如何更改上面的代码以使其工作?

Thanks!谢谢!

Is there anyway to create contract like this with smartcontract?反正有没有用智能合约创建这样的合约?

Yes.是的。

How I can change above code to make it work?我如何更改上面的代码以使其工作?

You need to use approve() and transferFrom() of ERC-20 token standard to process token payments in smart contracts.您需要使用 ERC-20 代币标准的approve()transferFrom()来处理智能合约中的代币支付。 payable keyword only concerns ETH payments, not token payments. payable关键字只涉及 ETH 支付,不涉及代币支付。

For more information how approve() mechanism work can be for example found here , though multiple tutorials exist on this topic.有关approve()机制如何工作的更多信息,例如可以在此处找到,尽管该主题存在多个教程。

Ok, I got it.好,我知道了。 The point that this receive() function is not trigger when sending token to the contract, so this code is not working at all.这个 receive() function 在向合约发送代币时没有触发,所以这段代码根本不起作用。

And in my case, I think I have to create a contract to let the owner call and use ERC20 transfer function to transfer the token, because he is the token owner already, then he do not need to call approve and transferFrom function.而在我的情况下,我认为我必须创建一个合约让所有者调用并使用 ERC20 转移 function 来转移令牌,因为他已经是令牌所有者,那么他不需要调用批准和 transferFrom function。

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

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