Cadence Cookbook

Contribute

Vault Minter

Vault Minter

01 Apr 2022

Contributed by Flow Blockchain

Intermediate

This resource is used by the admin to mint more tokens.

Smart Contract Example
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 // VaultMinter // // Resource object that an admin can control to mint new tokens pub resource VaultMinter { // Function that mints new tokens and deposits into an account's vault // using their 'Receiver' reference. // We say '&AnyResource{Receiver}' to say that the recipient can be any resource // as long as it implements the Receiver interface pub fun mintTokens(amount: UFix64, recipient: Capability<&AnyResource{Receiver}>) { let recipientRef = recipient.borrow() ?? panic("Could not borrow a receiver reference to the vault") ExampleToken.totalSupply = ExampleToken.totalSupply + UFix64(amount) recipientRef.deposit(from: <-create Vault(balance: amount)) } }

This resource is created so that more tokens may be minted. Ideally not many people should have access to this, only admins.

The VaultMinter resource takes in an amount as well as a capability that implements the Receiver interface as its arguments.

It checks to make sure the capability exists to receive, and once it does that it adds the amount in the parameters to the total minted supply.

Afterwards that newly created balance is deposited into the receivers account.

Transaction Example
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 // Mint Tokens import ExampleToken from 0x01 // This transaction mints tokens and deposits them into account 2's vault transaction { // Local variable for storing the reference to the minter resource let mintingRef: &ExampleToken.VaultMinter // Local variable for storing the reference to the Vault of // the account that will receive the newly minted tokens var receiver: Capability<&ExampleToken.Vault{ExampleToken.Receiver}> prepare(acct: AuthAccount) { // Borrow a reference to the stored, private minter resource self.mintingRef = acct.borrow<&ExampleToken.VaultMinter>(from: /storage/MainMinter) ?? panic("Could not borrow a reference to the minter") // Get the public account object for account 0x02 let recipient = getAccount(0x02) // Get their public receiver capability self.receiver = recipient.getCapability<&ExampleToken.Vault{ExampleToken.Receiver}> (/public/MainReceiver) } execute { // Mint 30 tokens and deposit them into the recipient's Vault self.mintingRef.mintTokens(amount: 30.0, recipient: self.receiver) log("30 tokens minted and deposited to account 0x02") } }

When doing the transaction, you first need to check to see if there is a VaultMinter that can be referenced by the signer of the transaction.

If so, then you get an account that has the capability to receive tokens and once you execute the transaction you include the amount of tokens to be minted, as well as the receivers capability in the arguments.


ProgressWorking With Fungible Tokens

100%


Related Recipes

14 Oct 2022
Token Vault
Beginner
01 Apr 2022
Withdrawing Tokens
Beginner
01 Apr 2022
Creating a Vault
Intermediate