Smart Contract with Mint and Payable Function (ERC20)

Hey,

I would like to create smart contract where You can mint new tokens (accesible by anyone calling) and charge a fee during that mint payable to contract creator. Something like a donation, but with the mint new tokens function at the same time.

I came up with something like this (changed onlyOwner to paybale), but how to set I believe msg.value to call a specific amount to pay during the mint. I would appreciate every answer as Im struggling with that for hours now … thank You.

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

import "@openzeppelin/contracts/token/ERC20/ERC20.sol";
import "@openzeppelin/contracts/access/Ownable.sol";

contract MyToken is ERC20, Ownable {
    constructor() ERC20("MyToken", "MTK") {
        _mint(msg.sender, 10000 * 10 ** decimals());
    }

    function mint(address to, uint256 amount) public payble {
        _mint(to, amount);
    }
}

you can add receiver.transfer(msg.value) after _mint() and define the receiver address somewhere in the contract.

Can You please show the example code. Thank You

In your mint function you can use require.

require(msg.value == amount);
1 Like

Including what glad mentioned, your mint function will look like this, where amount value and receiver address are already defined in the contract.

    function mint(address to, uint256 amount) public payble {
        require(msg.value == amount);
        _mint(to, amount);
        receiver.transfer(msg.value);
    }
1 Like