如何根据我的坚固合同中的付款使用我的ERC20自定义令牌?

发布于 2025-02-01 22:33:38 字数 457 浏览 4 评论 0原文

我创建了一个自定义的ERC20令牌,目前已在TestNet上部署,将来会在Polygon上启动它。

固体性

 uint256 public cost = 0.001 ether;
 function test(uint256 _mintAmount) public payable {
               require(msg.value >= cost * _mintAmount);
               //some code
    }

我想使用我的自定义令牌代替以太币。我该怎么做?有什么直截了当的方法吗?如果要使用react dapp,我该怎么做? 目前,对于以太坊,我的React Dapp被配置为以下内容 -

"WEI_COST":  1000000000000000,

请帮助。

I have created a custom ERC20 token and that is currently deployed on testnet and will launch it on polygon in future.

Solidity

 uint256 public cost = 0.001 ether;
 function test(uint256 _mintAmount) public payable {
               require(msg.value >= cost * _mintAmount);
               //some code
    }

I want to use my custom token in place of ether. How do I do it? Is there any straight forward way for it? And if want to use a react dapp how may I do that?
Currently for Ethereum, my react dapp is configured as follow-

"WEI_COST":  1000000000000000,

Please help.

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

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

发布评论

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

评论(2

尹雨沫 2025-02-08 22:33:38

您可以与IERC20接口进行交互,该接口允许您处理ERC20令牌。为了解决您的问题,您可以看到此智能合同代码:

// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;

import "@openzeppelin/contracts/token/ERC20/IERC20.sol";

contract MintWithERC20 {
    IERC20 token;
    uint256 public cost = 1 * 10**18;

    // NOTE: Pass it the token address that your contract must accept like deposit 
    constructor(address _addressToken) {
        token = IERC20(_addressToken);
    }

    // NOTE: Deposit token that you specificied into smart contract constructor 
    function depositToken(uint _amount, uint _mintAmount) public {
        require(_amount >= cost * _mintAmount);
        token.transferFrom(msg.sender, address(this), _amount);
    }

    // NOTE: You can check how many tokens have your smart contract balance   
    function getSmartContractBalance() external view returns(uint) {
        return token.balanceOf(address(this));
    }
} 

我为您做了更好的理解记录。

注意:当您调用readittoken()功能时,请记住批准您的智能合约以允许其在钱包中访问并传输令牌。

You can interact with IERC20 interface that allows you to handle ERC20 token. To solve your issue you can see this smart contract code:

// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;

import "@openzeppelin/contracts/token/ERC20/IERC20.sol";

contract MintWithERC20 {
    IERC20 token;
    uint256 public cost = 1 * 10**18;

    // NOTE: Pass it the token address that your contract must accept like deposit 
    constructor(address _addressToken) {
        token = IERC20(_addressToken);
    }

    // NOTE: Deposit token that you specificied into smart contract constructor 
    function depositToken(uint _amount, uint _mintAmount) public {
        require(_amount >= cost * _mintAmount);
        token.transferFrom(msg.sender, address(this), _amount);
    }

    // NOTE: You can check how many tokens have your smart contract balance   
    function getSmartContractBalance() external view returns(uint) {
        return token.balanceOf(address(this));
    }
} 

I put some notes for understand to you better what I done.

Attention: When you'll call depositToken() function remember to approve your smart contract for give it permission to access in your wallet and transfer the tokens.

清醇 2025-02-08 22:33:38

您可以批准智能合约来转让您的自定义令牌。

//批准智能合约转移令牌
token.approve(地址(this),_Amount);

You can approve the smart contract to transfer your custom token.

//approving smart contract to transfer token
token.approve(address(this), _amount);

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