Skip to main content

Resource Vault


_10
resource Vault {
_10
_10
balance: UFix64
_10
}

Each user stores an instance of only the Vault in their storage The functions in the Vault and governed by the pre and post conditions in FungibleToken when they are called. The checks happen at runtime whenever a function is called.

Resources can only be created in the context of the contract that they are defined in, so there is no way for a malicious user to create Vaults out of thin air. A special Minter resource needs to be defined to mint new tokens.

Implemented Interfaces:

  • FungibleToken.Provider
  • FungibleToken.Receiver
  • FungibleToken.Balance
  • MetadataViews.Resolver

Initializer


_10
func init(balance UFix64)

Functions

fun withdraw()


_10
func withdraw(amount UFix64): FungibleToken.Vault

Function that takes an 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.

Parameters:

  • amount : The amount of tokens to be withdrawn from the vault

Returns: The Vault resource containing the withdrawn funds


fun deposit()


_10
func deposit(from FungibleToken.Vault)

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.

Parameters:

  • from : The Vault resource containing the funds that will be deposited

fun getViews()


_10
func getViews(): [Type]

The way of getting all the Metadata Views implemented by ExampleToken

developers to know which parameter to pass to the resolveView() method.

Returns: An array of Types defining the implemented views. This value will be used by


fun resolveView()


_10
func resolveView(_ Type): AnyStruct?

The way of getting a Metadata View out of the ExampleToken

Parameters:

  • view : The Type of the desired view.

Returns: A structure representing the requested view.