1

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.

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);
    }
}

1 Answer 1

3

You can use something like this:

pragma solidity ^0.8.4;

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

contract MyToken is ERC20, Ownable {
    address public creatorAddress = 0xYOUR_ADDRESS;

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

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

        payable(creatorAddress).transfer(msg.value / 10);
    }
}

This code sends 10% of every payment to the creatorAddress.

1
  • Thank You for answer. How to specify for example 0.01 eth instead 10% and also how to limit the supply for minting ? Thank You!
    – kapitankot
    Commented Jun 3, 2022 at 19:25

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.