FLOW for Wallets & Custodians
Creating an Account
A user needs a Flow account in order to receive, hold and send FLOW tokens. The accounts & keys documentation provides a detailed overview of how accounts work on Flow.
You can create an account using templates and helper code from one of the Flow SDKs:
Receiving FLOW Deposits
Every Flow account supports the FLOW token by default. Once an account is created, it is already provisioned to receive FLOW deposits from other users.
FLOW, like any other FungibleToken
on Flow, is stored in a special resource called a FungibleToken.Vault
.
Every new account is created with an empty FLOW vault stored at the /storage/flowTokenVault
storage path.
_10let vault = account.borrow<&FlowToken.Vault>(from: /storage/flowTokenVault)
Conceptually, a vault is like a mailbox with a lock. Anybody can deposit tokens
but only the account holder can withdraw them. This functionality is made possible by
resource capabilities in Cadence. Each account publishes a FungibleToken.Receiver
interface
that points to its FLOW vault. The receiver is the mail slot; it allows others to
deposit FLOW into a vault without stealing what's inside.
Here's how you deposit FLOW into an account:
_10let receiver = account_10 .getCapability(/public/flowTokenReceiver)_10 .borrow<&{FungibleToken.Receiver}>()_10 ?? panic("Could not borrow FungibleToken.Receiver reference")_10_10receiver.deposit(from: <-senderVault)
Detecting Deposits
The FlowToken
contract emits a FlowToken.TokensDeposited
event whenever tokens
move between accounts.
_10pub event TokensDeposited(amount: UFix64, to: Address?)
You can query for this event to detect when tokens are deposited into a user's account.
Getting the Balance of an Account
Detailed below is an example of how to query the balance of a FlowToken.Vault
instance.
From Cadence
Similar to the token receiver, each account publishes a FungibleToken.Balance
capability
that allows anybody to read the balance of an account. This allows Cadence programs
to fetch the balance of an account directly in code.
_10let balanceRef = account_10 .getCapability(/public/flowTokenBalance)_10 .borrow<&FlowToken.Vault{FungibleToken.Balance}>()_10 ?? panic("Could not borrow FungibleToken.Balance reference")_10_10log(balanceRef.balance)
The above code can be executed as part of a read-only Cadence script.
From the Access API
The FLOW Access API makes it easy to query an account's balance without writing any Cadence code.
The GetAccount RPC method includes a balance
field, which holds the FLOW token balance
for the requested account.
_12import (_12 "github.com/onflow/flow-go-sdk"_12 "github.com/onflow/flow-go-sdk/client"_12)_12_12func main() {_12 flowClient, _ := client.New(accessAPIHost)_12_12 account, _ := flowClient.GetAccount(ctx, address)_12_12 fmt.Println(account.Balance)_12}
Sending FLOW
Below is an example of a transaction that transfers FLOW from one account to another.
_32import FungibleToken from 0xFUNGIBLE_TOKEN_ADDRESS_32import FlowToken from 0xFLOW_TOKEN_ADDRESS_32_32transaction(amount: UFix64, to: Address) {_32_32 // The FungibleToken.Vault resource that holds the tokens to be transferred_32 let sentVault: @FungibleToken.Vault_32_32 prepare(sender: AuthAccount) {_32 // Get a reference to the sender's stored vault_32 let vault = sender._32 borrow<&ExampleToken.Vault>(from: /storage/flowTokenVault)_32 ?? panic("Could not borrow reference to the owner's Vault!")_32_32 // Withdraw tokens from the sender's stored vault_32 self.sentVault <- vault.withdraw(amount: amount)_32 }_32_32 execute {_32 // Get the recipient's public account object_32 let recipient = getAccount(to)_32_32 // Get a reference to the recipient's FungibleToken.Receiver_32 let receiver = recipient_32 .getCapability(/public/flowTokenReceiver)_32 .borrow<&{FungibleToken.Receiver}>()_32 ?? panic("Could not borrow receiver reference to the recipient's Vault")_32_32 // Deposit the withdrawn tokens in the recipient's receiver_32 receiver.deposit(from: <-self.sentVault)_32 }_32}
This transaction template is available for use in our SDKs:
- Transfer Tokens with the FCL (Flow Client Library)
- Coming soon: Transfer Tokens with the Go SDK
Staking FLOW
The FLOW staking documentation outlines the steps a custodian can take to support staking through a trusted node operator.