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