0

performs a specific action in my app, I add the number of tokens stored in the database to that account.

After that, I want to mint my erc20 to that account as much as the number accumulated in the database.

In this case, i need two things. The user who is issued the gas required for minting will pay, and it will not be minted more than the value stored in my database.

This code has not been tested and may not work it as I expected. Please check only for what purpose it was written regardless of whether it was executed or not.

// SPDX-License-Identifier: MIT

pragma solidity >=0.4.22 <0.9.0;

import "./ERC20.sol";
import "./SafeMath.sol";
import "./Ownable.sol";
import "./ERC20Burnable.sol";
import "./ERC20Pausable.sol";

contract TEST is ERC20,Ownable,ERC20Burnable,ERC20Pausable {
    using SafeMath for uint256;

    mapping(address => uint256) canMintCount;
    mapping(address => uint256) payedGas;

    constructor() ERC20("test","test") {
    }

    function mint(uint256 count) external payable{
        require(canMintCount[msg.sender] <= count);
        require(payedGas[msg.sender] == msg.value);
        payable(owner()).transfer(msg.value);
        _mint(msg.sender, count);
    }

    function addMintCount(address user,uint256 count,uint256 gas) external onlyOwner {
        canMintCount[user] = canMintCount[user] + count;
        payedGas[user] = payedGas[user] + gas;
    }


    function _beforeTokenTransfer(
        address from,
        address to,
        uint256 tokenId
    ) internal virtual override(ERC20, ERC20Pausable) {
        super._beforeTokenTransfer(from, to, tokenId);
    }
}

When a user requests, my server makes the database value 0 and calls addMintCount by that value. After that User calls Mint

This method solves the problem of minting more values ​​than the values ​​entered into the database. However, an additional problem arises.

  1. From the user's point of view, the transaction gas fee and the gas fee used when the server calls addMintCount are paid separately, so users will think that they pay a lot.

  2. If the user only uses the API that calls addMintCount to the server and does not sign the Mint transaction, we will only be using gas bills.

In general, please introduce the open source contract you use in cases like mine, or tell me how to solve the above two methods.

1 Answer 1

1

If you are already storing the amount of tokens the user is entitled to mint in your app database, then it is a wasteful step to also maintain it within the smart contract as well. As you can see, it is requiring separate transactions and additional gas usage.

In this case I would recommend deleting the addMintCount function and only keep the mint function callable only by owner. When the user wants to get ERC20 tokens, then the mintCount update maintained by your database and token creation happen in the same transaction. You could even remove the mintCount variables and checks and simply pass these as parameters when calling the mint function from your app.

2
  • So, how can you set users to pay for gas? Aug 24, 2022 at 0:30
  • 1
    If you are looking for an option when owner controls what parameters are used but user send the transaction then you can implement a function like mint(...arg, signature) external {}, where argument are the parameters for minting and signature is signed parameters from owner address. Then verify parameters and signature on-chain.
    – Bigbrotha
    Aug 24, 2022 at 13:47

Your Answer

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

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