Override mint()
Method
Mint allowed only for Owner
You may have noticed while using the Openbrush wizard, that prior to adding the Security -> Ownable trait, the contract will not have the mint()
function overridden, so anyone is able to mint new tokens, by default.
However, after including the Ownable trait, the default mint()
function will be overridden, and restricted to being called by the contract owner, only.
#[overrider(PSP34Mintable)]
#[openbrush::modifiers(only_owner)]
fn mint(&mut self, account: AccountId, id: Id) -> Result<(), PSP34Error>{
psp34::InternalImpl::_mint_to(self, account, id)
}
The wizard also creates a line in the new()
constructor that sets the initial owner of the contract to the account address used to deploy it:
_instance._init_with_owner(_instance.env().caller());
At this stage we will make a few changes:
- We do not want tokens to be mintable by the contract owner, only. We would like anyone who paid a fee to be able to mint tokens, as well.
- We would like to charge a fee of 1 SDN token per token minted (or any other native token, depending on the network).
- The constructor should not call the mint function.
Make the mint() Function Payable
Making a function payable in an ink! contract is relatively straightforward. Simply add payable
to the ink! macro as follows:
#[ink(message, payable)]