Cadence Cookbook

Contribute

Withdrawing Tokens

Withdrawing Tokens

01 Apr 2022

Contributed by Flow Blockchain

Beginner

This is included in your smart contract when you would like to implement token withdrawls. Also useful for transferring tokens between accounts.

Smart Contract Example
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 pub resource interface Provider { // withdraw // // Function that subtracts tokens from the owner's Vault // and returns a Vault resource (@Vault) with the removed tokens. // // The function's access level is public, but this isn't a problem // because even the public functions are not fully public at first. // anyone in the network can call them, but only if the owner grants // them access by publishing a resource that exposes the withdraw // function. // pub fun withdraw(amount: UFix64): @Vault { post { // 'result' refers to the return value of the function result.balance == UFix64(amount): "Withdrawal amount must be the same as the balance of the withdrawn Vault" } } }

When setting up a withdraw function in your contract, you can create an interface that's only accessible to your private/storage path in your account so that others aren't able to withdraw funds without you approving.

Here we are making sure that after the withdrawl occurs your balance that was withdrawn was the same as the balance of the withdrawn vault.

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 import ExampleToken from 0x01 // This transaction is a template for a transaction that // could be used by anyone to send tokens to another account // that owns a Vault transaction { // Temporary Vault object that holds the balance that is being transferred var temporaryVault: @ExampleToken.Vault prepare(acct: AuthAccount) { // withdraw tokens from your vault by borrowing a reference to it // and calling the withdraw function with that reference let vaultRef = acct.borrow<&ExampleToken.Vault>(from: /storage/MainVault) ?? panic("Could not borrow a reference to the owner's vault") self.temporaryVault <- vaultRef.withdraw(amount: 10.0) } execute { // get the recipient's public account object let recipient = getAccount(0x01) // get the recipient's Receiver reference to their Vault // by borrowing the reference from the public capability let receiverRef = recipient.getCapability(/public/MainReceiver) .borrow<&ExampleToken.Vault{ExampleToken.Receiver}>() ?? panic("Could not borrow a reference to the receiver") // deposit your tokens to their Vault receiverRef.deposit(from: <-self.temporaryVault) log("Transfer succeeded!") } }

This transaction is showing us how a transfer occurs from one vault to the other. It includes both withdrawing of tokens from an account and depositing into the receiving account.

Before we execute the transfer we create a temporary vault that holds our balance being transfered. Afterwards we borrow a reference to the vault and then call a function that says to withdraw 10 tokens from it.

Afterwards we execute the transaction by getting the account of the receiver, making sure they have the capability to receive, and then depositing the balance into their account.

Note that the withdrawn balance returns a resource, which is why in order for it to be deposited you need to move a resource into the deposit function.


ProgressWorking With Fungible Tokens

Up Next: Creating a Vault

50%


Related Recipes

14 Oct 2022
Token Vault
Beginner
01 Apr 2022
Creating a Vault
Intermediate
01 Apr 2022
Vault Minter
Intermediate