Cadence Cookbook

Contribute

Token Vault

Token Vault

14 Oct 2022

Contributed by Flow Blockchain

Beginner

This explains the vault resource that can be created in order for you to handle fungible tokens.

Smart Contract 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 37 38 39 //A vault resource to be stored in an account that tracks your balance of tokens pub resource Vault: Provider, Receiver, Balance { // keeps track of the total balance of the account's tokens pub var balance: UFix64 // initialize the balance at resource creation time init(balance: UFix64) { self.balance = balance } // withdraw // // Function that takes an integer amount as an argument // and withdraws that amount from the Vault. // // It creates a new temporary Vault that is used to hold // the money that is being transferred. It returns the newly // created Vault to the context that called so it can be deposited // elsewhere. // pub fun withdraw(amount: UFix64): @Vault { self.balance = self.balance - amount return <-create Vault(balance: amount) } // deposit // // Function that takes a Vault object as an argument and adds // its balance to the balance of the owners Vault. // // It is allowed to destroy the sent Vault because the Vault // was a temporary holder of the tokens. The Vault's balance has // been consumed and therefore can be destroyed. pub fun deposit(from: @Vault) { self.balance = self.balance + from.balance destroy from } }

This is a vault resource in a smart contract that can be stored in a users account.

It keeps track of the total balance of tokens as well as the functions to withdraw and deposit tokens.

Interfaces such as Provider, Receiver, and Balance are included that allow users to send you money when executing a transaction.

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 // Setup Account import ExampleToken from 0x01 // This transaction configures an account to store and receive tokens defined by // the ExampleToken contract. transaction { prepare(acct: AuthAccount) { // Create a new empty Vault object let vaultA <- ExampleToken.createEmptyVault() // Store the vault in the account storage acct.save<@ExampleToken.Vault>(<-vaultA, to: /storage/MainVault) log("Empty Vault stored") // Create a public Receiver capability to the Vault let ReceiverRef = acct.link<&ExampleToken.Vault{ExampleToken.Receiver, ExampleToken.Balance}>(/public/MainReceiver, target: /storage/MainVault) log("References created") } post { // Check that the capabilities were created correctly getAccount(0x02).getCapability<&ExampleToken.Vault{ExampleToken.Receiver}>(/public/MainReceiver) .check(): "Vault Receiver Reference was not created correctly" } }

Here we are creating a new Vault resource into an account.

When that is created and stored into their private storage, we then create public capabilities that others can use to check balance, or deposit tokens.

Once we do that, we check to make sure that the capabilities were indeed created and then we are done with the transaction.


ProgressWorking With Fungible Tokens

Up Next: Withdrawing Tokens

25%


Related Recipes

01 Apr 2022
Withdrawing Tokens
Beginner
01 Apr 2022
Creating a Vault
Intermediate
01 Apr 2022
Vault Minter
Intermediate