I would like to create a token which any wallet cannot hold more than 100 of.
Is this possible while this token, also is mintable?
Yes, this is possible.
I'm not sure if you mean that there should never be more than 100 tokens total, or if no one wallet should be able to have a balance of more than 100 tokens. We'll go through both.
First, you mentioned a mint
function. I assume this means you understand that without mint
, it's pretty simple - just premint 100 tokens and there's no way to make more.
It's still pretty simple even with a mint
function.
The ERC20 standard specifies a totalSupply()
which is used to show how many tokens are in circulation.
If you query totalSupply
in mint
(before you actually mint the token), you can see how many tokens are already in circulation. (Remember to adjust for the decimals. If you have 18 decimal places, and totalSupply()
returns 1
, it doesn't mean 1 token, it means 0.000000000000000001 tokens.) If there are already 100 tokens, mint
should revert, if there are less than 100, but the amount to mint would push it over 100, it should mint 100 - totalSupply
tokens, and otherwise, the full amount.
This works very similar to the above, but instead of using totalSupply
, you'll use the balance
of the address receiving the tokens. (There is a function balanceOf
which takes an address as an argument which you can use.) Note that here you'll need checks in mint
and also in transfer
and transferFrom
. (If there is an internal _transfer
function used by both, like in OpenZeppelin's ERC20, putting the checks in _transfer
should be enough for both.)
Hope that helps!