Creating a Vault
Creating a Vault
01 Apr 2022
Contributed by Flow Blockchain
Intermediate
This explains the function that would be used for you to create a new vault in others accounts.
Smart Contract Example
1
2
3
4
//creating an empty vault
pub fun createEmptyVault(): @Vault {
return <-create Vault(balance: 0.0)
}
This function is included in the smart contract so that anyone can create a vault resource and then save it to their account.
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
// Setup Account
import ExampleToken from 0x01
// This transaction configures an account to store and receive tokens defined by
// the ExampleToken contract.
transaction {
prepare(acct: AuthAccount) {
// Create a new empty Vault object
let vaultA <- ExampleToken.createEmptyVault()
// Store the vault in the account storage
acct.save<@ExampleToken.Vault>(<-vaultA, to: /storage/MainVault)
log("Empty Vault stored")
// Create a public Receiver capability to the Vault
let ReceiverRef = acct.link<&ExampleToken.Vault{ExampleToken.Receiver, ExampleToken.Balance}>(/public/MainReceiver, target: /storage/MainVault)
log("References created")
}
post {
// Check that the capabilities were created correctly
getAccount(0x02).getCapability<&ExampleToken.Vault{ExampleToken.Receiver}>(/public/MainReceiver)
.check():
"Vault Receiver Reference was not created correctly"
}
}
Here we are creating a new Vault resource into an account.
When that is created and stored into their private storage, we then create public capabilities that others can use to check balance, or deposit tokens.
Once we do that, we check to make sure that the capabilities were indeed created and then we are done with the transaction.
ProgressWorking With Fungible Tokens
Up Next: Vault Minter
75%