简体   繁体   English

ERC20 代币转移到智能合约

[英]ERC20 token transfer to smart contract

Hi I've written a simple smart contract to transfer erc20 token from user to smart contract and from smart contract to user.嗨,我编写了一个简单的智能合约来将 erc20 代币从用户转移到智能合约以及从智能合约转移到用户。 I'm approving it before calling the transferFrom function but the transactions fails.我在调用 transferFrom 函数之前批准了它,但交易失败。 I've tried only calling approve and not calling transferFrom it works.我试过只调用批准而不是调用 transferFrom 它有效。 I'm testing on rinkeby testnet.我正在 rinkeby 测试网上进行测试。

// SPDX-License-Identifier: MIT
pragma solidity =0.7.6;
pragma abicoder v2;

interface IERC20 {
    function totalSupply() external view returns (uint256);
    function balanceOf(address account) external view returns (uint256);
    function allowance(address owner, address spender) external view returns (uint256);
    function transfer(address recipient, uint256 amount) external returns (bool);
    function approve(address spender, uint256 amount) external returns (bool);
    function transferFrom(address sender, address recipient, uint256 amount) external returns (bool);
    event Transfer(address indexed from, address indexed to, uint256 value);
    event Approval(address indexed owner, address indexed spender, uint256 value);

}

contract transferToContract {
    address public _WETH = 0x1f9840a85d5aF5bf1D1762F925BDADdC4201F984;
    IERC20 public WETH = IERC20(_WETH);

    function transferToC (uint amount) public
    {
        WETH.approve(msg.sender,amount);
        WETH.transferFrom(msg.sender,address(this),amount);
    }
    
    function transferFromC(uint amount) public{
        WETH.approve(address(this),amount);
        WETH.transferFrom(address(this),msg.sender,amount);
    }
    
    function getbal() public view returns(uint){
        return WETH.balanceOf(msg.sender);
    }
    
    
}

I'm testing on remix with web3 injected.我正在测试注入 web3 的 remix。 The error I'm getting is我得到的错误是

Gas estimation errored with the following message (see below). The transaction execution will likely fail. Do you want to force sending?
execution reverted: Uni::transferFrom: transfer amount exceeds spender allowance { "originalError": { "code": 3, "data": "0x08c379a00000000000000000000000000000000000000000000000000000000000000020000000000000000000000000000000000000000000000000000000000000003c556e693a3a7472616e7366657246726f6d3a207472616e7366657220616d6f756e742065786365656473207370656e64657220616c6c6f77616e636500000000", "message": "execution reverted: Uni::transferFrom: transfer amount exceeds spender allowance" } }

Also I have the token in my test wallet on rinkeby testnet and I'm deploying using remix web3 injected.此外,我在 rinkeby 测试网上的测试钱包中有令牌,我正在使用注入的 remix web3 进行部署。

WETH.approve(msg.sender,amount);

This line approves the user to spend your contract's tokens.此行批准用户使用您的合约代币。 Not the other way around.不是反过来。

If you want to approve the contract to manipulate the user's tokens, the user needs to execute approve(yourContractAddress, amount) directly on the WETH contract (not through your contract).如果要批准合约操作用户的代币,用户需要直接在WETH合约上执行approve(yourContractAddress, amount) (不是通过你的合约)。


I've covered a similar topic in another answer.我在另一个答案中涵盖了一个类似的主题。 The TLDR part is also relevant to your question - https://stackoverflow.com/a/67642090/1693192 TLDR 部分也与您的问题有关 - https://stackoverflow.com/a/67642090/1693192

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

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