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.
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.
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.