Smart Contract
ceBUSD
A.231cc0dbbcffc4b7.ceBUSD
1/// Canonical ceBUSD on Flow
2import FungibleToken from 0xf233dcee88fe0abe
3import FungibleTokenMetadataViews from 0xf233dcee88fe0abe
4import FTMinterBurner from 0x08dd120226ec2213
5
6access(all) contract ceBUSD: FungibleToken, FTMinterBurner {
7 // path for admin resource
8 access(all) let AdminPath: StoragePath
9 /// Total supply of tokens in existence, initial 0, and increase when new tokens are minted
10 access(all) var totalSupply: UFix64
11
12 /// TokensInitialized
13 ///
14 /// The event that is emitted when the contract is created
15 access(all) event TokensInitialized(initialSupply: UFix64)
16
17 /// TokensWithdrawn
18 ///
19 /// The event that is emitted when tokens are withdrawn from a Vault
20 access(all) event TokensWithdrawn(amount: UFix64, from: Address?)
21
22 /// TokensDeposited
23 ///
24 /// The event that is emitted when tokens are deposited to a Vault
25 access(all) event TokensDeposited(amount: UFix64, to: Address?)
26
27 /// TokensMinted
28 ///
29 /// The event that is emitted when new tokens are minted
30 access(all) event TokensMinted(amount: UFix64)
31
32 /// TokensBurned
33 ///
34 /// The event that is emitted when tokens are destroyed
35 access(all) event TokensBurned(amount: UFix64)
36
37 /// MinterCreated
38 ///
39 /// The event that is emitted when a new minter resource is created
40 access(all) event MinterCreated(allowedAmount: UFix64)
41
42 /// BurnerCreated
43 ///
44 /// The event that is emitted when a new burner resource is created
45 access(all) event BurnerCreated()
46
47 /// Vault
48 ///
49 /// Each user stores an instance of only the Vault in their storage
50 /// The functions in the Vault and governed by the pre and post conditions
51 /// in FungibleToken when they are called.
52 /// The checks happen at runtime whenever a function is called.
53 ///
54 /// Resources can only be created in the context of the contract that they
55 /// are defined in, so there is no way for a malicious user to create Vaults
56 /// out of thin air. A special Minter resource needs to be defined to mint
57 /// new tokens.
58 ///
59 access(all) resource Vault: FungibleToken.Vault {
60
61 /// The total balance of this vault
62 access(all) var balance: UFix64
63
64 // initialize the balance at resource creation time
65 init(balance: UFix64) {
66 self.balance = balance
67 }
68
69 /// withdraw
70 ///
71 /// Function that takes an amount as an argument
72 /// and withdraws that amount from the Vault.
73 ///
74 /// It creates a new temporary Vault that is used to hold
75 /// the money that is being transferred. It returns the newly
76 /// created Vault to the context that called so it can be deposited
77 /// elsewhere.
78 ///
79 access(FungibleToken.Withdraw) fun withdraw(amount: UFix64): @{FungibleToken.Vault} {
80 self.balance = self.balance - amount
81 emit TokensWithdrawn(amount: amount, from: self.owner?.address)
82 return <-create Vault(balance: amount)
83 }
84
85 /// deposit
86 ///
87 /// Function that takes a Vault object as an argument and adds
88 /// its balance to the balance of the owners Vault.
89 ///
90 /// It is allowed to destroy the sent Vault because the Vault
91 /// was a temporary holder of the tokens. The Vault's balance has
92 /// been consumed and therefore can be destroyed.
93 ///
94 access(all) fun deposit(from: @{FungibleToken.Vault}) {
95 let vault <- from as! @ceBUSD.Vault
96 self.balance = self.balance + vault.balance
97 emit TokensDeposited(amount: vault.balance, to: self.owner?.address)
98 vault.balance = 0.0
99 destroy vault
100 }
101
102 /// Called when a fungible token is burned via the `Burner.burn()` method
103 access(contract) fun burnCallback() {
104 if self.balance > 0.0 {
105 ceBUSD.totalSupply = ceBUSD.totalSupply - self.balance
106 self.balance = 0.0
107 }
108 }
109
110 access(all) fun createEmptyVault(): @{FungibleToken.Vault} {
111 return <-create Vault(balance: 0.0)
112 }
113
114 access(all) view fun isAvailableToWithdraw(amount: UFix64): Bool {
115 return amount <= self.balance
116 }
117
118 access(all) view fun getViews(): [Type] {
119 return ceBUSD.getContractViews(resourceType: nil)
120 }
121
122 access(all) fun resolveView(_ view: Type): AnyStruct? {
123 return ceBUSD.resolveContractView(resourceType: nil, viewType: view)
124 }
125}
126
127 /// createEmptyVault
128 ///
129 /// Function that creates a new Vault with a balance of zero
130 /// and returns it to the calling context. A user must call this function
131 /// and store the returned Vault in their storage in order to allow their
132 /// account to be able to receive deposits of this token type.
133 ///
134 access(all) fun createEmptyVault(vaultType: Type): @{FungibleToken.Vault} {
135 return <-create Vault(balance: 0.0)
136 }
137
138 access(all) resource Administrator {
139
140 /// createNewMinter
141 ///
142 /// Function that creates and returns a new minter resource
143 ///
144 access(all) fun createNewMinter(allowedAmount: UFix64): @{FTMinterBurner.Minter} {
145 emit MinterCreated(allowedAmount: allowedAmount)
146 return <-create Minter(allowedAmount: allowedAmount)
147 }
148
149 /// createNewBurner
150 ///
151 /// Function that creates and returns a new burner resource
152 ///
153 access(all) fun createNewBurner(): @{FTMinterBurner.Burner} {
154 emit BurnerCreated()
155 return <-create Burner()
156 }
157 }
158
159 /// Minter
160 ///
161 /// Resource object that token admin accounts can hold to mint new tokens.
162 ///
163 access(all) resource Minter: FTMinterBurner.Minter {
164
165 /// The amount of tokens that the minter is allowed to mint
166 access(all) var allowedAmount: UFix64
167
168 /// mintTokens
169 ///
170 /// Function that mints new tokens, adds them to the total supply,
171 /// and returns them to the calling context.
172 ///
173 access(all) fun mintTokens(amount: UFix64): @{FungibleToken.Vault} {
174 pre {
175 amount > 0.0: "Amount minted must be greater than zero"
176 amount <= self.allowedAmount: "Amount minted must be less than the allowed amount"
177 }
178 ceBUSD.totalSupply = ceBUSD.totalSupply + amount
179 self.allowedAmount = self.allowedAmount - amount
180 emit TokensMinted(amount: amount)
181 return <-create Vault(balance: amount)
182 }
183
184 init(allowedAmount: UFix64) {
185 self.allowedAmount = allowedAmount
186 }
187 }
188
189 /// Burner
190 ///
191 /// Resource object that token admin accounts can hold to burn tokens.
192 ///
193 access(all) resource Burner: FTMinterBurner.Burner {
194
195 /// burnTokens
196 ///
197 /// Function that destroys a Vault instance, effectively burning the tokens.
198 ///
199 /// Note: the burned tokens are automatically subtracted from the
200 /// total supply in the Vault destructor.
201 ///
202 access(all) fun burnTokens(from: @{FungibleToken.Vault}) {
203 let vault <- from as! @ceBUSD.Vault
204 let amount = vault.balance
205 destroy vault
206 emit TokensBurned(amount: amount)
207 }
208 }
209
210 init() {
211 self.totalSupply = 0.0
212
213 // account owner only has admin resource, no vault as tokens are only minted later
214 let admin <- create Administrator()
215 self.AdminPath = /storage/ceBUSDAdmin
216 self.account.storage.save(<-admin, to: self.AdminPath)
217
218 // Emit an event that shows that the contract was initialized
219 emit TokensInitialized(initialSupply: self.totalSupply)
220 }
221
222 access(all) view fun getContractViews(resourceType: Type?): [Type] {
223 return [Type<FungibleTokenMetadataViews.FTVaultData>()]
224 }
225
226 access(all) fun resolveContractView(resourceType: Type?, viewType: Type): AnyStruct? {
227 switch viewType {
228 case Type<FungibleTokenMetadataViews.FTVaultData>():
229 return FungibleTokenMetadataViews.FTVaultData(
230 storagePath: /storage/ceBUSDVault,
231 receiverPath: /public/ceBUSDVault,
232 metadataPath: /public/ceBUSDReceiver,
233 receiverLinkedType: Type<&ceBUSD.Vault>(),
234 metadataLinkedType: Type<&ceBUSD.Vault>(),
235 createEmptyVaultFunction: (fun(): @{FungibleToken.Vault} {
236 return <-self.createEmptyVault(vaultType: Type<@ceBUSD.Vault>())
237 })
238 )
239 }
240 return nil
241 }
242}