FUSD Transactions & Scripts

Transaction templates for the FUSD stablecoin on Flow

Contract

The FUSD contract defines the Flow USD stablecoin token.

FUSD implements the standard FungibleToken contract interface on Flow.

NetworkContract Address
Testnet0xe223d8a629e49c68
Mainnet0x3c5959b568896393

Source: FUSD.cdc

Transactions & Scripts

Setup FUSD Vault & Receiver

This transaction configures the signer's account with an empty FUSD vault.

It also links the following capabilities:

  • FungibleToken.Receiver - This capability allows this account to accept FUSD deposits.
  • FungibleToken.Balance - This capability allows anybody to inspect the FUSD balance of this account.
1
// Mainnet
2
import FungibleToken from 0xf233dcee88fe0abe
3
import FUSD from 0x3c5959b568896393
4
5
// Testnet
6
// import FungibleToken from 0x9a0766d93b6608b7
7
// import FUSD from 0xe223d8a629e49c68
8
9
transaction {
10
11
prepare(signer: AuthAccount) {
12
13
// It's OK if the account already has a Vault, but we don't want to replace it
14
if(signer.borrow<&FUSD.Vault>(from: /storage/fusdVault) != nil) {
15
return
16
}
17
18
// Create a new FUSD Vault and put it in storage
19
signer.save(<-FUSD.createEmptyVault(), to: /storage/fusdVault)
20
21
// Create a public capability to the Vault that only exposes
22
// the deposit function through the Receiver interface
23
signer.link<&FUSD.Vault{FungibleToken.Receiver}>(
24
/public/fusdReceiver,
25
target: /storage/fusdVault
26
)
27
28
// Create a public capability to the Vault that only exposes
29
// the balance field through the Balance interface
30
signer.link<&FUSD.Vault{FungibleToken.Balance}>(
31
/public/fusdBalance,
32
target: /storage/fusdVault
33
)
34
}
35
}

Source: setup_fusd_vault.cdc

Transfer FUSD

This transaction withdraws FUSD from the signer's account and deposits it into a recipient account. This transaction will fail if the recipient does not have an FUSD receiver. No funds are transferred or lost if the transaction fails.

  • amount: The amount of FUSD transfer (e.g. 10.0)
  • recipient: The recipient account address.
1
// Mainnet
2
import FungibleToken from 0xf233dcee88fe0abe
3
import FUSD from 0x3c5959b568896393
4
5
// Testnet
6
// import FungibleToken from 0x9a0766d93b6608b7
7
// import FUSD from 0xe223d8a629e49c68
8
9
transaction(amount: UFix64, recipient: Address) {
10
11
// The Vault resource that holds the tokens that are being transfered
12
let sentVault: @FungibleToken.Vault
13
14
prepare(signer: AuthAccount) {
15
// Get a reference to the signer's stored vault
16
let vaultRef = signer.borrow<&FUSD.Vault>(from: /storage/fusdVault)
17
?? panic("Could not borrow reference to the owner's Vault!")
18
19
// Withdraw tokens from the signer's stored vault
20
self.sentVault <- vaultRef.withdraw(amount: amount)
21
}
22
23
execute {
24
// Get the recipient's public account object
25
let recipientAccount = getAccount(recipient)
26
27
// Get a reference to the recipient's Receiver
28
let receiverRef = recipientAccount.getCapability(/public/fusdReceiver)!
29
.borrow<&{FungibleToken.Receiver}>()
30
?? panic("Could not borrow receiver reference to the recipient's Vault")
31
32
// Deposit the withdrawn tokens in the recipient's receiver
33
receiverRef.deposit(from: <-self.sentVault)
34
}
35
}

Source: transfer_fusd.cdc

Get FUSD Balance for an Account

This script returns the FUSD balance of an account.

1
// Mainnet
2
import FungibleToken from 0xf233dcee88fe0abe
3
import FUSD from 0x3c5959b568896393
4
5
// Testnet
6
// import FungibleToken from 0x9a0766d93b6608b7
7
// import FUSD from 0xe223d8a629e49c68
8
9
pub fun main(address: Address): UFix64 {
10
let account = getAccount(address)
11
12
let vaultRef = account
13
.getCapability(/public/fusdBalance)
14
.borrow<&FUSD.Vault{FungibleToken.Balance}>()
15
?? panic("Could not borrow Balance capability")
16
17
return vaultRef.balance
18
}

Source: get_fusd_balance.cdc