Smart Contract
BasicToken
A.f7913d7a57ba6555.BasicToken
1// BasicToken.cdc
2//
3// The BasicToken contract is a sample implementation of a fungible token on Flow.
4//
5// Fungible tokens behave like everyday currencies -- they can be minted, transferred or
6// traded for digital goods.
7//
8// Follow the fungible tokens tutorial to learn more: https://developers.flow.com/cadence/tutorial/06-fungible-tokens
9//
10// This is a basic implementation of a Fungible Token and is NOT meant to be used in production
11// See the Flow Fungible Token standard for real examples: https://github.com/onflow/flow-ft
12
13pub contract BasicToken {
14
15 // Vault
16 //
17 // Each user stores an instance of only the Vault in their storage
18 // The functions in the Vault and governed by the pre and post conditions
19 // in the interfaces when they are called.
20 // The checks happen at runtime whenever a function is called.
21 //
22 // Resources can only be created in the context of the contract that they
23 // are defined in, so there is no way for a malicious user to create Vaults
24 // out of thin air. A special Minter resource or constructor function needs to be defined to mint
25 // new tokens.
26 //
27 pub resource Vault {
28
29 // keeps track of the total balance of the account's tokens
30 pub var balance: UFix64
31
32 // initialize the balance at resource creation time
33 init(balance: UFix64) {
34 self.balance = balance
35 }
36
37 // withdraw
38 //
39 // Function that takes an integer amount as an argument
40 // and withdraws that amount from the Vault.
41 //
42 // It creates a new temporary Vault that is used to hold
43 // the money that is being transferred. It returns the newly
44 // created Vault to the context that called so it can be deposited
45 // elsewhere.
46 //
47 pub fun withdraw(amount: UFix64): @Vault {
48 self.balance = self.balance - amount
49 return <-create Vault(balance: amount)
50 }
51
52 // deposit
53 //
54 // Function that takes a Vault object as an argument and adds
55 // its balance to the balance of the owners Vault.
56 //
57 // It is allowed to destroy the sent Vault because the Vault
58 // was a temporary holder of the tokens. The Vault's balance has
59 // been consumed and therefore can be destroyed.
60 pub fun deposit(from: @Vault) {
61 self.balance = self.balance + from.balance
62 destroy from
63 }
64 }
65
66 // createVault
67 //
68 // Function that creates a new Vault with an initial balance
69 // and returns it to the calling context. A user must call this function
70 // and store the returned Vault in their storage in order to allow their
71 // account to be able to receive deposits of this token type.
72 //
73 pub fun createVault(): @Vault {
74 return <-create Vault(balance: 30.0)
75 }
76
77 // The init function for the contract. All fields in the contract must
78 // be initialized at deployment. This is just an example of what
79 // an implementation could do in the init function. The numbers are arbitrary.
80 init() {
81 // create the Vault with the initial balance and put it in storage
82 // account.save saves an object to the specified `to` path
83 // The path is a literal path that consists of a domain and identifier
84 // The domain must be `storage`, `private`, or `public`
85 // the identifier can be any name
86 let vault <- self.createVault()
87 self.account.save(<-vault, to: /storage/CadenceFungibleTokenTutorialVault)
88 }
89}
90
91