'Solidity Faucet implementation
I wrote a smart contract for my own token which is based on the ERC20 standard. Now I wanted to add the "faucet" functionality and I´m not quite sure, if my implementation is "best practice". (I know that everyone can mint unlimited tokens all the time) The thing Im most concerned about is, that Im implementing the ERC20 token not only in the contract of my token but also in the abstract Faucet contract. Can I do this safely?
Token-Contract:
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.12;
import "@openzeppelin/contracts/token/ERC20/ERC20.sol";
import "@openzeppelin/contracts/access/Ownable.sol";
import "./Faucet.sol";
contract Anne is ERC20, Ownable, Faucet {
/**
* @dev Set max supply
*/
uint256 private _maxSupply = 18_000_000 * 10**decimals();
constructor(uint256 totalSupply)
ERC20("ANNE", "ANNE")
Faucet(500_000 * 10**decimals())
{
_mint(msg.sender, totalSupply * 10**decimals());
}
/**
* @dev Returns max supply
*/
function getMaxSupply() public view returns (uint256) {
return _maxSupply;
}
function drip(address account) public {
_drip(account);
}
/**
* @dev Only owner can mint new tokens and ensures
* that max supply is not exceeded
*/
function mint(address account, uint256 amount) public onlyOwner {
require(
totalSupply() + amount <= getMaxSupply(),
"Max Supply Exceeded"
);
_mint(account, amount);
}
}
Faucet-Contract:
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.12;
import "@openzeppelin/contracts/token/ERC20/ERC20.sol";
/**
* Implements Faucet funtionality for ERC20 Tokens
*/
abstract contract Faucet is ERC20 {
uint256 private _maxMintAmount;
constructor(uint256 maxMintAmount) {
_maxMintAmount = maxMintAmount;
}
function _drip(address account) internal {
_mint(account, _maxMintAmount);
}
/**
* @dev Returns the maximum amount to mint
*/
function getMaxMintAmount() public view returns (uint256) {
return _maxMintAmount;
}
}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
