How to Create a Fungible Token on Flow
For Cadence 0.42 go to Legacy Docs
This guide is an in-depth tutorial on launching a Fungible Token contract from scratch. To launch in 2 minutes using a tool check out Toucans
What are Fungible Tokens?​
Fungible tokens are digital assets that are interchangeable and indistinguishable with other tokens of the same type. This means that each token is identical in specification to every other token in circulation. Think of them like traditional money; every dollar bill has the same value as every other dollar bill. Fungible tokens play a crucial role in web3 ecosystems, serving as both a means of payment and an incentive for network participation. They can take on various roles including currencies, structured financial instruments, shares of index funds, and even voting rights in decentralized autonomous organizations.
Vaults on Flow​
On the Flow blockchain and in the Cadence programming language, fungible tokens are stored in structures called resources. Resources are objects in Cadence that store data, but have special restrictions about how they can be stored and transferred, making them perfect for representing digital objects with real value.
You can learn more about resources in the Cadence documentation and tutorials.
For Fungible Tokens specifically, tokens are represented by a resource type called a Vault
:
_10access(all) resource interface Vault {_10_10 /// Field that tracks the balance of a vault_10 access(all) var balance: UFix64_10_10}
Think of a Vault
as a digital piggy bank.
Users who own fungible tokens store vault objects that track their balances
directly in their account storage. This is opposed to languages
that track user balances in a central ledger smart contract.
When you transfer tokens from one vault to another:
- The transferor's vault creates a temporary vault holding the transfer amount.
- The original vault's balance decreases by the transfer amount.
- The recipient's vault receives the tokens from the temporary vault and adds the temporary vault's balance to the its own balance.
- The temporary vault is then destroyed.
This process ensures secure and accurate token transfers on the Flow blockchain.
Fungible Token Standard​
The Fungible Token Standard defines how a fungible token should behave on Flow. Wallets and other platforms need to recognize these tokens, so they adhere to a specific interface, which defines fields like balance, totalSupply, withdraw functionality, and more. This interface ensures that all fungible tokens on Flow have a consistent structure and behavior. Clink the link to the fungible token standard to see the full standard and learn about specific features and requirements.
Learn more about interfaces here.
Setting Up a Project​
To start creating a Fungible Token on the Flow blockchain, you'll first need some tools and configurations in place.
Installing Flow CLI​
The Flow CLI (Command Line Interface) provides a suite of tools that allow developers to interact seamlessly with the Flow blockchain.
If you haven't installed the Flow CLI yet and have Homebrew installed, you can run brew install flow-cli
. If you don’t have Homebrew, please follow the installation guide here.
Initializing a New Project​
💡 Note: Here is a link to the completed code if you want to skip ahead or reference as you follow along.
Once you have the Flow CLI installed, you can set up a new project using the flow setup
command. This command initializes the necessary directory structure and a flow.json
configuration file (a way to configure your project for contract sources, deployments, accounts, and more):
_10flow setup FooToken
Upon execution, the command will generate the following directory structure:
_10/cadence_10 /contracts_10 /scripts_10 /transactions_10 /tests_10flow.json
Now, navigate into the project directory:
_10cd FooToken
In our configuration file, called flow.json
, for the network we want to use, we are going to state the address the FungibleToken
contract is deployed to via aliases
in a new contracts
section. Since it is a standard contract, it has already been deployed to the emulator, a tool that runs and emulates a local development version of the Flow Blockchain, for us. You can find addresses for other networks, like Testnet and Mainnet, on the Fungible Token Standard repo.
We'll also need to add the addresses for ViewResolver
, MetadataViews
,
and FungibleTokenMetadataViews
, which are other important contracts to use.
_22"contracts": {_22 "FungibleToken": {_22 "aliases": {_22 "emulator": "0xee82856bf20e2aa6"_22 }_22 },_22 "FungibleTokenMetadataViews": {_22 "aliases": {_22 "emulator": "0xee82856bf20e2aa6"_22 }_22 },_22 "ViewResolver": {_22 "aliases": {_22 "emulator": "0xf8d6e0586b0a20c7"_22 }_22 },_22 "MetadataViews": {_22 "aliases": {_22 "emulator": "0xf8d6e0586b0a20c7"_22 }_22 },_22}
Writing Our Token Contract​
Next let's create a FooToken
contract at cadence/contract/FooToken.cdc
using the boilerplate generate
command from the Flow CLI:
_10flow generate contract FooToken
This will create a new file called FooToken.cdc
in the contracts
directory. Let's open it up and add some code.
In this contract file, we want to import our FungibleToken
contract that we've defined in flow.json
.
_10import "FungibleToken"
In this same file, let's create our contract which implements the FungibleToken
contract interface (it does so by setting it following the FooToken:
).
We'll also include fields for standard storage and public paths
for our resource definitions.
In our init
— which runs on the contracts first deployment and is used to set initial values — let’s set an starting total supply of 1,000 tokens for this example.
_16// ...previous code_16_16access(all) contract FooToken: FungibleToken {_16 access(all) var totalSupply: UFix64_16_16 access(all) let VaultStoragePath: StoragePath_16 access(all) let VaultPublicPath: PublicPath_16 access(all) let MinterStoragePath: StoragePath_16_16 init() {_16 self.totalSupply = 1000.0_16 self.VaultStoragePath = /storage/fooTokenVault_16 self.VaultPublicPath = /public/fooTokenVault_16 self.MinterStoragePath = /storage/fooTokenMinter _16 }_16}
Creating a Vault​
Inside of this contract, we'll need to create a resource for a Vault
.
The FungibleToken
standard requires that your vault implements the FungibleToken.Vault
interface.
This interface inherits from many other interfaces
which enforce different functionality that you can learn about in the standard.
_16import "FungibleToken"_16_16access(all) contract FooToken: FungibleToken {_16 // ...totalSupply code_16_16 access(all) resource Vault: FungibleToken.Vault {_16_16 access(all) var balance: UFix64_16_16 init(balance: UFix64) {_16 self.balance = balance_16 }_16 }_16_16 // ...init code_16}
In order to give an account a vault, we need to create a function
that creates a vault of our FooToken type and returns it to the account.
This function takes a vaultType: Type
argument that allows the caller
to specify which type of Vault
they want to create.
Contracts that implement multiple Vault
types can use this argument,
but since your contract is only implementing one Vault
type,
it can ignore the argument.
A simpler version of this function with no parameter
should also be added to your Vault
implementation.
_24import "FungibleToken"_24_24access(all) contract FooToken: FungibleToken {_24 // ...other code_24_24 access(all) resource Vault: FungibleToken.Vault {_24_24 // ...other vault code_24_24 access(all) fun createEmptyVault(): @FooToken.Vault {_24 return <-create Vault(balance: 0.0)_24 }_24_24 // ...vault init code_24 }_24_24 // ...other code_24_24 access(all) fun createEmptyVault(vaultType: Type): @FooToken.Vault {_24 return <- create Vault(balance: 0.0)_24 }_24_24 // ...FooToken.init() code_24}
Inside our Vault
resource, we also need a way to withdraw balances.
To do that, we need to add a withdraw()
function that returns a new vault
with the transfer amount and decrements the existing balance.
_20import "FungibleToken"_20_20access(all) contract FooToken: FungibleToken {_20_20 // ...previous code_20_20 access(all) resource Vault: FungibleToken.Vault {_20_20 // ...other vault code_20_20 access(FungibleToken.Withdraw) fun withdraw(amount: UFix64): @FooToken.Vault {_20 self.balance = self.balance - amount_20 return <-create Vault(balance: amount)_20 }_20_20 // ...vault init code_20 }_20_20 // ...additional code_20}
As you can see, this function has an access(FungibleToken.Withdraw)
access modifier.
This is an example of entitlements in Cadence.
Entitlements
are a way for developers to restrict access to privileged fields and functions
in a composite type like a resource when a reference is created for it.
In this example, the withdraw()
function is always accessible to code that
controls the full Vault
object, but if a reference is created for it,
the withdraw()
function can only be called if the reference
is authorized by the owner with FungibleToken.Withdraw
,
which is a standard entitlement
defined by the FungibleToken contract:
_10// Example of an authorized entitled reference to a FungibleToken.Vault_10<auth(FungibleToken.Withdraw) &{FungibleToken.Vault}>
Entitlements are important to understand because they are what protects privileged functionality in your resource objects from being accessed by third-parties. It is recommended to read the entitlements documentation to understand how to use the feature properly.
References can be freely up-casted and down-casted in Cadence, so it is important for privileged functionality to be protected by an entitlement so that it can only be accessed if it is authorized.
In addition to withdrawing, the vault also needs a way to deposit. We'll typecast to make sure we are dealing with the correct token, update the vault balance, and destroy the vault. Add this code to your resource:
_22import "FungibleToken"_22_22access(all) contract FooToken: FungibleToken {_22_22 // ...previous code_22_22 access(all) resource Vault: FungibleToken.Vault {_22_22 // ...other vault code_22_22 access(all) fun deposit(from: @{FungibleToken.Vault}) {_22 let vault <- from as! @FooToken.Vault_22 self.balance = self.balance + vault.balance_22 destroy vault_22 }_22_22 // ...vault init_22_22 }_22_22 // ...additional code_22}
Many projects rely on events the signal when withdrawals, deposits, or burns happen.
Luckily, the FungibleToken
standard handles the definition and emission
of events for projects, so there is no need for you to add any events
to your implementation for withdraw, deposit, and burn.
Here are the FungibleToken
event definitions:
_10/// The event that is emitted when tokens are withdrawn from a Vault_10access(all) event Withdrawn(type: String, amount: UFix64, from: Address?, fromUUID: UInt64, withdrawnUUID: UInt64, balanceAfter: UFix64)_10_10/// The event that is emitted when tokens are deposited to a Vault_10access(all) event Deposited(type: String, amount: UFix64, to: Address?, toUUID: UInt64, depositedUUID: UInt64, balanceAfter: UFix64)_10_10/// Event that is emitted when the global burn method is called with a non-zero balance_10access(all) event Burned(type: String, amount: UFix64, fromUUID: UInt64)
These events are emitted by the Vault
interface
in the FungibleToken
contract whenever the relevant function is called on any implementation.
One important piece to understand about the Burned
event in particular
is that in order for it to be emitted when a Vault
is burned, it needs to
be burnt via the Burner
contract's burn()
method.
This will call the resource's burnCallback()
function, which emits the event.
You'll need to also add this function to your token contract now:
_24import "FungibleToken"_24_24access(all) contract FooToken: FungibleToken {_24_24 // ...previous code_24_24 access(all) resource Vault: FungibleToken.Vault {_24_24 // ...other vault code_24_24 /// Called when a fungible token is burned via the `Burner.burn()` method_24 access(contract) fun burnCallback() {_24 if self.balance > 0.0 {_24 FooToken.totalSupply = FooToken.totalSupply - self.balance_24 }_24 self.balance = 0.0_24 }_24_24 // ...vault init_24_24 }_24_24 // ...additional code_24}
If you ever need to destroy a Vault
with a non-zero balance,
you should destroy it via the Burner.burn
method so this important function can be called.
There are three other utility methods that need to be added to your Vault
to get various information:
_33import "FungibleToken"_33_33access(all) contract FooToken: FungibleToken {_33_33 // ...previous code_33_33 access(all) resource Vault: FungibleToken.Vault {_33_33 // ...other vault code_33_33 /// getSupportedVaultTypes optionally returns a list of vault types that this receiver accepts_33 access(all) view fun getSupportedVaultTypes(): {Type: Bool} {_33 let supportedTypes: {Type: Bool} = {}_33 supportedTypes[self.getType()] = true_33 return supportedTypes_33 }_33_33 /// Says if the Vault can receive the provided type in the deposit method_33 access(all) view fun isSupportedVaultType(type: Type): Bool {_33 return self.getSupportedVaultTypes()[type] ?? false_33 }_33_33 /// Asks if the amount can be withdrawn from this vault_33 access(all) view fun isAvailableToWithdraw(amount: UFix64): Bool {_33 return amount <= self.balance_33 }_33_33 // ...vault init_33_33 }_33_33 // ...additional code_33}
Adding Support for Metadata Views​
The Fungible Token standard also enforces that implementations provide functionality to return a set of standard views about the tokens via the ViewResolver and FungibleTokenMetadataViews definitions. These provide developers with standard ways of representing metadata about a given token such as supply, token symbols, website links, and standard account paths and types that third-parties can access in a standard way. You can see the metadata views documentation for a more thorough guide using a NFT contract as an example.
For now, you can add this code to your contract to support the important metadata views:
_83import "FungibleToken"_83_83// Add these imports_83import "MetadataViews"_83import "FungibleTokenMetadataViews"_83_83access(all) contract FooToken: FungibleToken {_83 // ...other code_83_83 access(all) view fun getContractViews(resourceType: Type?): [Type] {_83 return [_83 Type<FungibleTokenMetadataViews.FTView>(),_83 Type<FungibleTokenMetadataViews.FTDisplay>(),_83 Type<FungibleTokenMetadataViews.FTVaultData>(),_83 Type<FungibleTokenMetadataViews.TotalSupply>()_83 ]_83 }_83_83 access(all) fun resolveContractView(resourceType: Type?, viewType: Type): AnyStruct? {_83 switch viewType {_83 case Type<FungibleTokenMetadataViews.FTView>():_83 return FungibleTokenMetadataViews.FTView(_83 ftDisplay: self.resolveContractView(resourceType: nil, viewType: Type<FungibleTokenMetadataViews.FTDisplay>()) as! FungibleTokenMetadataViews.FTDisplay?,_83 ftVaultData: self.resolveContractView(resourceType: nil, viewType: Type<FungibleTokenMetadataViews.FTVaultData>()) as! FungibleTokenMetadataViews.FTVaultData?_83 )_83 case Type<FungibleTokenMetadataViews.FTDisplay>():_83 let media = MetadataViews.Media(_83 file: MetadataViews.HTTPFile(_83 // Change this to your own SVG image_83 url: "https://assets.website-files.com/5f6294c0c7a8cdd643b1c820/5f6294c0c7a8cda55cb1c936_Flow_Wordmark.svg"_83 ),_83 mediaType: "image/svg+xml"_83 )_83 let medias = MetadataViews.Medias([media])_83 return FungibleTokenMetadataViews.FTDisplay(_83 // Change these to represent your own token_83 name: "Example Foo Token",_83 symbol: "EFT",_83 description: "This fungible token is used as an example to help you develop your next FT #onFlow.",_83 externalURL: MetadataViews.ExternalURL("https://developers.flow.com/build/guides/fungible-token"),_83 logos: medias,_83 socials: {_83 "twitter": MetadataViews.ExternalURL("https://twitter.com/flow_blockchain")_83 }_83 )_83 case Type<FungibleTokenMetadataViews.FTVaultData>():_83 return FungibleTokenMetadataViews.FTVaultData(_83 storagePath: self.VaultStoragePath,_83 receiverPath: self.VaultPublicPath,_83 metadataPath: self.VaultPublicPath,_83 receiverLinkedType: Type<&FooToken.Vault>(),_83 metadataLinkedType: Type<&FooToken.Vault>(),_83 createEmptyVaultFunction: (fun(): @{FungibleToken.Vault} {_83 return <-FooToken.createEmptyVault(vaultType: Type<@FooToken.Vault>())_83 })_83 )_83 case Type<FungibleTokenMetadataViews.TotalSupply>():_83 return FungibleTokenMetadataViews.TotalSupply(_83 totalSupply: FooToken.totalSupply_83 )_83 }_83 return nil_83 }_83_83 // ...other code_83_83 access(all) resource Vault: FungibleToken.Vault {_83_83 // ...other vault code_83_83 access(all) view fun getViews(): [Type] {_83 return FooToken.getContractViews(resourceType: nil)_83 }_83_83 access(all) fun resolveView(_ view: Type): AnyStruct? {_83 return FooToken.resolveContractView(resourceType: nil, viewType: view)_83 }_83_83 // ...other vault code_83 }_83_83 // ...other FooToken code_83}
Creating a Minter​
Let's create a minter resource which is used to mint vaults that have tokens in them. We can keep track of tokens we are minting with totalSupply
If we want the ability to create new tokens, we'll need a way to mint them. To do that, let's create another resource on the FooToken
contract. This will have a mintToken
function which can increase the total supply of the token.
_28import "FungibleToken"_28_28access(all) contract FooToken: FungibleToken {_28_28 // ...additional contract code_28 // Add this event_28 access(all) event TokensMinted(amount: UFix64, type: String)_28_28 /// Minter_28 ///_28 /// Resource object that token admin accounts can hold to mint new tokens._28 ///_28 access(all) resource Minter {_28 /// mintTokens_28 ///_28 /// Function that mints new tokens, adds them to the total supply,_28 /// and returns them to the calling context._28 ///_28 access(all) fun mintTokens(amount: UFix64): @FooToken.Vault {_28 FooToken.totalSupply = FooToken.totalSupply + amount_28 let vault <-create Vault(balance: amount)_28 emit TokensMinted(amount: amount, type: vault.getType().identifier)_28 return <-vault_28 }_28 }_28_28 // ...additional contract code_28}
We also want to decide which account/s we want to give this ability to. In our example, we'll give it to the account where the contract is deployed. We can set this in the contract init function below the setting of total supply so that when the contract is created the minter is stored on the same account.
_11import "FungibleToken"_11_11access(all) contract FooToken: FungibleToken {_11_11 // ...additional contract code_11_11 init() {_11 self.totalSupply = 1000.0 // existed before_11 self.account.save(<- create Minter(), to: /storage/fooTokenMinter)_11 }_11}
After each of these steps, your FooToken.cdc
contract file should now look like this:
_175import "FungibleToken"_175import "MetadataViews"_175import "FungibleTokenMetadataViews"_175_175access(all) contract FooToken: FungibleToken {_175_175 /// The event that is emitted when new tokens are minted_175 access(all) event TokensMinted(amount: UFix64, type: String)_175_175 /// Total supply of FooTokens in existence_175 access(all) var totalSupply: UFix64_175_175 /// Storage and Public Paths_175 access(all) let VaultStoragePath: StoragePath_175 access(all) let VaultPublicPath: PublicPath_175 access(all) let ReceiverPublicPath: PublicPath_175 access(all) let AdminStoragePath: StoragePath_175_175 access(all) view fun getContractViews(resourceType: Type?): [Type] {_175 return [_175 Type<FungibleTokenMetadataViews.FTView>(),_175 Type<FungibleTokenMetadataViews.FTDisplay>(),_175 Type<FungibleTokenMetadataViews.FTVaultData>(),_175 Type<FungibleTokenMetadataViews.TotalSupply>()_175 ]_175 }_175_175 access(all) fun resolveContractView(resourceType: Type?, viewType: Type): AnyStruct? {_175 switch viewType {_175 case Type<FungibleTokenMetadataViews.FTView>():_175 return FungibleTokenMetadataViews.FTView(_175 ftDisplay: self.resolveContractView(resourceType: nil, viewType: Type<FungibleTokenMetadataViews.FTDisplay>()) as! FungibleTokenMetadataViews.FTDisplay?,_175 ftVaultData: self.resolveContractView(resourceType: nil, viewType: Type<FungibleTokenMetadataViews.FTVaultData>()) as! FungibleTokenMetadataViews.FTVaultData?_175 )_175 case Type<FungibleTokenMetadataViews.FTDisplay>():_175 let media = MetadataViews.Media(_175 file: MetadataViews.HTTPFile(_175 // Change this to your own SVG image_175 url: "https://assets.website-files.com/5f6294c0c7a8cdd643b1c820/5f6294c0c7a8cda55cb1c936_Flow_Wordmark.svg"_175 ),_175 mediaType: "image/svg+xml"_175 )_175 let medias = MetadataViews.Medias([media])_175 return FungibleTokenMetadataViews.FTDisplay(_175 // Change these to represent your own token_175 name: "Example Foo Token",_175 symbol: "EFT",_175 description: "This fungible token is used as an example to help you develop your next FT #onFlow.",_175 externalURL: MetadataViews.ExternalURL("https://developers.flow.com/build/guides/fungible-token"),_175 logos: medias,_175 socials: {_175 "twitter": MetadataViews.ExternalURL("https://twitter.com/flow_blockchain")_175 }_175 )_175 case Type<FungibleTokenMetadataViews.FTVaultData>():_175 return FungibleTokenMetadataViews.FTVaultData(_175 storagePath: self.VaultStoragePath,_175 receiverPath: self.VaultPublicPath,_175 metadataPath: self.VaultPublicPath,_175 receiverLinkedType: Type<&FooToken.Vault>(),_175 metadataLinkedType: Type<&FooToken.Vault>(),_175 createEmptyVaultFunction: (fun(): @{FungibleToken.Vault} {_175 return <-FooToken.createEmptyVault(vaultType: Type<@FooToken.Vault>())_175 })_175 )_175 case Type<FungibleTokenMetadataViews.TotalSupply>():_175 return FungibleTokenMetadataViews.TotalSupply(_175 totalSupply: FooToken.totalSupply_175 )_175 }_175 return nil_175 }_175_175 access(all) resource Vault: FungibleToken.Vault {_175_175 /// The total balance of this vault_175 access(all) var balance: UFix64_175_175 // initialize the balance at resource creation time_175 init(balance: UFix64) {_175 self.balance = balance_175 }_175_175 /// Called when a fungible token is burned via the `Burner.burn()` method_175 access(contract) fun burnCallback() {_175 if self.balance > 0.0 {_175 FooToken.totalSupply = FooToken.totalSupply - self.balance_175 }_175 self.balance = 0.0_175 }_175_175 access(all) view fun getViews(): [Type] {_175 return FooToken.getContractViews(resourceType: nil)_175 }_175_175 access(all) fun resolveView(_ view: Type): AnyStruct? {_175 return FooToken.resolveContractView(resourceType: nil, viewType: view)_175 }_175_175 access(all) view fun getSupportedVaultTypes(): {Type: Bool} {_175 let supportedTypes: {Type: Bool} = {}_175 supportedTypes[self.getType()] = true_175 return supportedTypes_175 }_175_175 access(all) view fun isSupportedVaultType(type: Type): Bool {_175 return self.getSupportedVaultTypes()[type] ?? false_175 }_175_175 access(all) view fun isAvailableToWithdraw(amount: UFix64): Bool {_175 return amount <= self.balance_175 }_175_175 access(FungibleToken.Withdraw) fun withdraw(amount: UFix64): @FooToken.Vault {_175 self.balance = self.balance - amount_175 return <-create Vault(balance: amount)_175 }_175_175 access(all) fun deposit(from: @{FungibleToken.Vault}) {_175 let vault <- from as! @FooToken.Vault_175 self.balance = self.balance + vault.balance_175 vault.balance = 0.0_175 destroy vault_175 }_175_175 access(all) fun createEmptyVault(): @FooToken.Vault {_175 return <-create Vault(balance: 0.0)_175 }_175 }_175_175 access(all) resource Minter {_175 /// mintTokens_175 ///_175 /// Function that mints new tokens, adds them to the total supply,_175 /// and returns them to the calling context._175 ///_175 access(all) fun mintTokens(amount: UFix64): @FooToken.Vault {_175 FooToken.totalSupply = FooToken.totalSupply + amount_175 let vault <-create Vault(balance: amount)_175 emit TokensMinted(amount: amount, type: vault.getType().identifier)_175 return <-vault_175 }_175 }_175_175 access(all) fun createEmptyVault(vaultType: Type): @FooToken.Vault {_175 return <- create Vault(balance: 0.0)_175 }_175_175 init() {_175 self.totalSupply = 1000.0_175_175 self.VaultStoragePath = /storage/exampleTokenVault_175 self.VaultPublicPath = /public/exampleTokenVault_175 self.AdminStoragePath = /storage/exampleTokenAdmin _175_175 // Create the Vault with the total supply of tokens and save it in storage_175 //_175 let vault <- create Vault(balance: self.totalSupply)_175 emit TokensMinted(amount: vault.balance, type: vault.getType().identifier)_175_175 // Create a public capability to the stored Vault that exposes_175 // the `deposit` method and getAcceptedTypes method through the `Receiver` interface_175 // and the `balance` method through the `Balance` interface_175 //_175 let exampleTokenCap = self.account.capabilities.storage.issue<&FooToken.Vault>(self.VaultStoragePath)_175 self.account.capabilities.publish(exampleTokenCap, at: self.VaultPublicPath)_175 let receiverCap = self.account.capabilities.storage.issue<&FooToken.Vault>(self.VaultStoragePath)_175 self.account.capabilities.publish(receiverCap, at: self.ReceiverPublicPath)_175_175 self.account.storage.save(<-vault, to: /storage/exampleTokenVault)_175_175 let admin <- create Minter()_175 self.account.storage.save(<-admin, to: self.AdminStoragePath)_175 }_175}
Deploying the Contract​
In order to use the contract, we need to deploy it to the network we want to use it on. In our case we are going to deploy it to emulator while developing.
Back in our flow.json
, let's add our FooToken
to the contracts
after FungibleToken
with the path of the source code:
_10"FooToken": "cadence/contracts/FooToken.cdc"
Let's also add a new deployments
section to flow.json
with the network we want to deploy it to, emulator
, the account we want it deployed to emulator-account
, and the list of contracts we want deployed in the array.
_10"deployments": {_10 "emulator": {_10 "emulator-account": ["FooToken"]_10 }_10}
Next, using the Flow CLI, we will start the emulator. As mentioned, this will give us a local development environment for the Flow Blockchain.
_10flow emulator start
Open a new terminal and run the following to deploy your project:
_10flow project deploy
Congrats, you've deployed your contract to the Flow Blockchain emulator. To read more about deploying your project to other environments, see the CLI docs.
Reading the Token’s Total Supply​
Let's now check that our total supply was initialized with 1,000 FooTokens. Go ahead and create a script called get_total_supply.cdc
using the generate
command.
_10flow generate script get_total_supply
In cadence/scripts/get_total_supply.cdc
(which was just created), let's add this code which will log the totalSupply
value from the FooToken
contract:
_10import "FooToken"_10_10access(all) fun main() {_10 log(FooToken.totalSupply)_10}
To run this using the CLI, enter this in your terminal:
_10flow scripts execute cadence/scripts/get_total_supply.cdc
In the terminal where you started the emulator, you should see 1000.0
To learn more about running scripts using Flow CLI, see the docs.
Giving Accounts the Ability to Receive Tokens​
On Flow, newly created accounts cannot receive arbitrary assets. They need to be initialized to receive resources. In our case, we want to give accounts tokens and we’ll need to create a Vault
(which acts as a receiver) on each account that we want to have the ability to receive tokens. To do this, we'll need to run a transaction which will create the vault and set it in their storage using the createEmptyVault()
function we created earlier on the contract.
Let's first create the file at cadence/transactions/setup_ft_account.cdc
using the generate
command:
_10flow generate transaction setup_ft_account
Then add this code to it.
This will call the createEmptyVault
function, save it in storage,
and create a capability for the vault which will later allow us to read from it
(To learn more about capabilities, see the Cadence docs here).
_24import "FungibleToken"_24import "FooToken"_24_24transaction () {_24_24 prepare(signer: auth(BorrowValue, IssueStorageCapabilityController, PublishCapability, SaveValue) &Account) {_24_24 // Return early if the account already stores a FooToken Vault_24 if signer.storage.borrow<&FooToken.Vault>(from: FooToken.VaultStoragePath) != nil {_24 return_24 }_24_24 let vault <- FooToken.createEmptyVault(vaultType: Type<@FooToken.Vault>())_24_24 // Create a new FooToken Vault and put it in storage_24 signer.storage.save(<-vault, to: FooToken.VaultStoragePath)_24_24 // Create a public capability to the Vault that exposes the Vault interfaces_24 let vaultCap = signer.capabilities.storage.issue<&FooToken.Vault>(_24 FooToken.VaultStoragePath_24 )_24 signer.capabilities.publish(vaultCap, at: FooToken.VaultPublicPath)_24 }_24}
There are also examples of generic transactions that you can use to setup an account for ANY fungible token using metadata views! You should check those out and try to use generic transactions whenever it is possible.
Next let's create a new emulator account using the CLI. We'll use this account to create a new vault and mint tokens into it. Run:
_10flow accounts create
Let's call it test-acct
and select "Emulator" for the network:
_10test-acct
This will have added a new account, called test-acct
to your flow.json
.
To call our setup account transaction from the CLI, we'll run the following:
_10flow transactions send ./cadence/transactions/setup_ft_account.cdc --signer test-acct --network emulator
To learn more about running transactions using CLI, see the docs.
Reading a Vault’s Balance​
Let's now read the balance of the newly created account (test-acct
) to check it's zero.
Create this new script file cadence/scripts/get_fooToken_balance.cdc
:
_10flow generate script get_fooToken_balance
Add this code which attempts to borrow the capability from the account requested and logs the vault balance if permitted:
_13import "FungibleToken"_13import "FooToken"_13import "FungibleTokenMetadataViews"_13_13access(all) fun main(address: Address): UFix64 {_13 let vaultData = FooToken.resolveContractView(resourceType: nil, viewType: Type<FungibleTokenMetadataViews.FTVaultData>()) as! FungibleTokenMetadataViews.FTVaultData?_13 ?? panic("Could not get vault data view for the contract")_13_13 return getAccount(address).capabilities.borrow<&{FungibleToken.Balance}>(_13 vaultData.metadataPath_13 )?.balance_13 ?? panic("Could not borrow Balance reference to the Vault")_13}
To run this script using the CLI, enter the following in your terminal. Note: you'll need to replace 123
with the address created by CLI in your flow.json
for the test-acct
address.
_10flow scripts execute cadence/scripts/get_fooToken_balance.cdc 123 // change "123" to test-acct address
You should see a balance of zero logged.
Minting More Tokens​
Now that we have an account with a vault, let's mint some tokens into it using the Minter we created on the contract account.
To do this, let's create a new transaction file cadence/transactions/mint_fooToken.cdc
:
_10flow generate transaction mint_fooToken
Next, let's add the following code to the mint_fooToken.cdc
file. This code will attempt to borrow the minting capability and mint 20 new tokens into the receivers account.
_30import "FungibleToken"_30import "FooToken"_30_30transaction(recipient: Address, amount: UFix64) {_30_30 /// Reference to the Example Token Minter Resource object_30 let tokenMinter: &FooToken.Minter_30_30 /// Reference to the Fungible Token Receiver of the recipient_30 let tokenReceiver: &{FungibleToken.Receiver}_30_30 prepare(signer: auth(BorrowValue) &Account) {_30_30 // Borrow a reference to the admin object_30 self.tokenMinter = signer.storage.borrow<&FooToken.Minter>(from: FooToken.AdminStoragePath)_30 ?? panic("Signer is not the token admin")_30_30 self.tokenReceiver = getAccount(recipient).capabilities.borrow<&{FungibleToken.Receiver}>(FooToken.VaultPublicPath)_30 ?? panic("Could not borrow receiver reference to the Vault")_30 }_30_30 execute {_30_30 // Create mint tokens_30 let mintedVault <- self.tokenMinter.mintTokens(amount: amount)_30_30 // Deposit them to the receiever_30 self.tokenReceiver.deposit(from: <-mintedVault)_30 }_30}
To run this transaction, enter this in your terminal. Note: 123
should be replaced with address of test-acct
found in your flow.json
. This command also states to sign with our emulator-account
on the Emulator network.
_10flow transactions send ./cadence/transactions/mint_fooToken.cdc 123 20.0 --signer emulator-account --network emulator
Let's go ahead and read the vault again. Remember to replace 123
with the correct address.
_10flow scripts execute cadence/scripts/get_fooToken_balance.cdc 123
It should now say 20 tokens are in the vault.
Transferring Tokens Between Accounts​
The final functionality we'll add is the ability to transfer tokens from one account to another.
To do that, create a new cadence/transactions/transfer_fooToken.cdc
transaction file:
_10flow generate transaction transfer_fooToken
Let's add the code which states that the signer of the transaction will withdraw from their vault and put it into the receiver's vault which will be passed as a transaction argument.
_31import "FungibleToken"_31import "FooToken"_31_31transaction(amount: UFix64, to: Address) {_31_31 // The Vault resource that holds the tokens that are being transferred_31 let sentVault: @{FungibleToken.Vault}_31_31 prepare(signer: auth(BorrowValue) &Account) {_31_31 // Get a reference to the signer's stored vault_31 let vaultRef = signer.storage.borrow<auth(FungibleToken.Withdraw) &FooToken.Vault>(from: FooToken.VaultStoragePath)_31 ?? panic("Could not borrow reference to the owner's Vault!")_31_31 // Withdraw tokens from the signer's stored vault_31 self.sentVault <- vaultRef.withdraw(amount: amount)_31 }_31_31 execute {_31_31 // Get the recipient's public account object_31 let recipient = getAccount(to)_31_31 // Get a reference to the recipient's Receiver_31 let receiverRef = recipient.capabilities.borrow<&{FungibleToken.Receiver}>(FooToken.VaultPublicPath)_31 ?? panic("Could not borrow receiver reference to the recipient's Vault")_31_31 // Deposit the withdrawn tokens in the recipient's receiver_31 receiverRef.deposit(from: <-self.sentVault)_31 }_31}
To send our tokens, we'll need to create a new account to send them to. Let's make one more account on emulator. Run:
_10flow accounts create
And pick the name:
_10test-acct-2
Make sure to select Emulator as the network.
Don't forget the new account will need a vault added, so let's run the following transaction to add one:
_10flow transactions send ./cadence/transactions/setup_ft_account.cdc --signer test-acct-2 --network emulator
Now, let's send 1 token from our earlier account to the new account. Remember to replace 123
with account address of test-acct-2
.
_10flow transactions send ./cadence/transactions/transfer_fooToken.cdc 123 "1.0" --signer test-acct --network emulator
After that, read the balance of test-acct-2
(replace the address 123
).
_10flow scripts execute cadence/scripts/get_fooToken_balance.cdc 123
You should now see 1 token in test-acct-2
account!
The transfer transaction also has a generic version that developers are encouraged to use!