如何将自定义令牌发送到另一个帐户?

发布于 2025-01-21 18:19:50 字数 1004 浏览 0 评论 0原文

因此,这是我制作的user1的自定义令牌

    address public deployer; //to save adress of the deployer
    
    constructor() ERC20('tokenA', 'TA') { //called by the deployer (once)
        _mint(msg.sender, 1000000000000 * 10 ** 10); //mint/create tokens - we have created 100000000000*10^18 tokens
        deployer = msg.sender;  //set the deployer
    }

    //total supply is fixed no more can be created ever

    function burn (uint amount) external {  //remove tokens by sending then to a zero address
        _burn(msg.sender, amount);
    }
}

是合同的所有者,并且拥有大量令牌,用户2中有3个令牌。 我必须编写一个函数,当单击传输1时,该功能将1个令牌发送给所有者。


    function transferOneToken () public payable {
        address token = //address of token contract;
        ERC20 paymentToken = ERC20(token);

        require(paymentToken.transferFrom(msg.sender, owner, 1), "transfer Failed"); //owner is set in constructor
        
    }

这就是我想到的,用户2具有3个令牌,只需要转移1个,但仍然估算出的错误错误会出现错误,说

我如何解决此问题?

so this is the custom token i made

    address public deployer; //to save adress of the deployer
    
    constructor() ERC20('tokenA', 'TA') { //called by the deployer (once)
        _mint(msg.sender, 1000000000000 * 10 ** 10); //mint/create tokens - we have created 100000000000*10^18 tokens
        deployer = msg.sender;  //set the deployer
    }

    //total supply is fixed no more can be created ever

    function burn (uint amount) external {  //remove tokens by sending then to a zero address
        _burn(msg.sender, amount);
    }
}

User1 is the owner of the contract and has tons of tokens, User2 has 3 tokens in his wallet.
I have to write a function which when clicked transfers 1 token to the owner that is User1.


    function transferOneToken () public payable {
        address token = //address of token contract;
        ERC20 paymentToken = ERC20(token);

        require(paymentToken.transferFrom(msg.sender, owner, 1), "transfer Failed"); //owner is set in constructor
        
    }

This is what I came up with, User 2 has 3 tokens and only has to transfer 1 but still gas estimation failed error pops up saying insufficient allowance

how do I solve this?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

雨的味道风的声音 2025-01-28 18:19:50

令牌持有人(用户2)需要aprative() Spender(实施transferOnetoken()函数的合同)在trasseffrom()之前花费了它们的令牌功能被调用。

令牌合同的and 函数进行2个参数:

  1. 支型地址
  2. 在没有批准机制的情况下允许花费的最大金额

,任何合同都可以拉出其用户的代币,这将是一个对于无法/不阅读合同代码的用户的不安全情况。

The token holder (User 2) needs to approve() the spender (the contract implementing the transferOneToken() function) to spend their tokens before the transferFrom() function is invoked.

The approve() function of the token contract takes 2 arguments:

  1. The spender address
  2. The max amount they're allowed to spend

Without the approval mechanism, any contract could pull their users' tokens, which would be an unsafe situation for users who can't/don't read the contract code.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文