-1

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.

Blockchain Kid
  • 315
  • 2
  • 9

2 Answers2

1

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.

Antonio Carito
  • 1,287
  • 1
  • 5
  • 10
1

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

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

Obot Ernest
  • 412
  • 8
  • 19