@flowtyio/flow-contracts 0.1.0-beta.2 → 0.1.0-beta.4
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/contracts/Burner.cdc +44 -0
- package/contracts/FlowStorageFees.cdc +15 -15
- package/contracts/FlowToken.cdc +29 -78
- package/contracts/FungibleToken.cdc +80 -53
- package/contracts/FungibleTokenMetadataViews.cdc +13 -25
- package/contracts/MetadataViews.cdc +28 -44
- package/contracts/NonFungibleToken.cdc +102 -59
- package/contracts/ViewResolver.cdc +20 -16
- package/contracts/example/ExampleNFT.cdc +420 -0
- package/contracts/example/ExampleToken.cdc +235 -0
- package/flow.json +27 -1
- package/package.json +1 -1
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
/// Burner is a contract that can facilitate the destruction of any resource on flow.
|
|
2
|
+
///
|
|
3
|
+
/// Contributors
|
|
4
|
+
/// - Austin Kline - https://twitter.com/austin_flowty
|
|
5
|
+
/// - Deniz Edincik - https://twitter.com/bluesign
|
|
6
|
+
/// - Bastian Müller - https://twitter.com/turbolent
|
|
7
|
+
access(all) contract Burner {
|
|
8
|
+
/// When Crescendo (Cadence 1.0) is released, custom destructors will be removed from cadece.
|
|
9
|
+
/// Burnable is an interface meant to replace this lost feature, allowing anyone to add a callback
|
|
10
|
+
/// method to ensure they do not destroy something which is not meant to be,
|
|
11
|
+
/// or to add logic based on destruction such as tracking the supply of a FT Collection
|
|
12
|
+
///
|
|
13
|
+
/// NOTE: The only way to see benefit from this interface
|
|
14
|
+
/// is to always use the burn method in this contract. Anyone who owns a resource can always elect **not**
|
|
15
|
+
/// to destroy a resource this way
|
|
16
|
+
access(all) resource interface Burnable {
|
|
17
|
+
access(contract) fun burnCallback()
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
/// burn is a global method which will destroy any resource it is given.
|
|
21
|
+
/// If the provided resource implements the Burnable interface,
|
|
22
|
+
/// it will call the burnCallback method and then destroy afterwards.
|
|
23
|
+
access(all) fun burn(_ r: @AnyResource) {
|
|
24
|
+
if let s <- r as? @{Burnable} {
|
|
25
|
+
s.burnCallback()
|
|
26
|
+
destroy s
|
|
27
|
+
} else if let arr <- r as? @[AnyResource] {
|
|
28
|
+
while arr.length > 0 {
|
|
29
|
+
let item <- arr.removeFirst()
|
|
30
|
+
self.burn(<-item)
|
|
31
|
+
}
|
|
32
|
+
destroy arr
|
|
33
|
+
} else if let dict <- r as? @{HashableStruct: AnyResource} {
|
|
34
|
+
let keys = dict.keys
|
|
35
|
+
while keys.length > 0 {
|
|
36
|
+
let item <- dict.remove(key: keys.removeFirst())!
|
|
37
|
+
self.burn(<-item)
|
|
38
|
+
}
|
|
39
|
+
destroy dict
|
|
40
|
+
} else {
|
|
41
|
+
destroy r
|
|
42
|
+
}
|
|
43
|
+
}
|
|
44
|
+
}
|
|
@@ -1,17 +1,17 @@
|
|
|
1
1
|
/*
|
|
2
2
|
* The FlowStorageFees smart contract
|
|
3
3
|
*
|
|
4
|
-
* An account's storage capacity determines up to how much storage on chain it can use.
|
|
4
|
+
* An account's storage capacity determines up to how much storage on chain it can use.
|
|
5
5
|
* A storage capacity is calculated by multiplying the amount of reserved flow with `StorageFee.storageMegaBytesPerReservedFLOW`
|
|
6
6
|
* The minimum amount of flow tokens reserved for storage capacity is `FlowStorageFees.minimumStorageReservation` this is paid during account creation, by the creator.
|
|
7
|
-
*
|
|
8
|
-
* At the end of all transactions, any account that had any value changed in their storage
|
|
7
|
+
*
|
|
8
|
+
* At the end of all transactions, any account that had any value changed in their storage
|
|
9
9
|
* has their storage capacity checked against their storage used and their main flow token vault against the minimum reservation.
|
|
10
10
|
* If any account fails this check the transaction wil fail.
|
|
11
|
-
*
|
|
12
|
-
* An account moving/deleting its `FlowToken.Vault` resource will result
|
|
11
|
+
*
|
|
12
|
+
* An account moving/deleting its `FlowToken.Vault` resource will result
|
|
13
13
|
* in the transaction failing because the account will have no storage capacity.
|
|
14
|
-
*
|
|
14
|
+
*
|
|
15
15
|
*/
|
|
16
16
|
|
|
17
17
|
import "FungibleToken"
|
|
@@ -26,8 +26,8 @@ access(all) contract FlowStorageFees {
|
|
|
26
26
|
access(all) event MinimumStorageReservationChanged(_ minimumStorageReservation: UFix64)
|
|
27
27
|
|
|
28
28
|
// Defines how much storage capacity every account has per reserved Flow token.
|
|
29
|
-
// definition is written per unit of flow instead of the inverse,
|
|
30
|
-
// so there is no loss of precision calculating storage from flow,
|
|
29
|
+
// definition is written per unit of flow instead of the inverse,
|
|
30
|
+
// so there is no loss of precision calculating storage from flow,
|
|
31
31
|
// but there is loss of precision when calculating flow per storage.
|
|
32
32
|
access(all) var storageMegaBytesPerReservedFLOW: UFix64
|
|
33
33
|
|
|
@@ -68,7 +68,7 @@ access(all) contract FlowStorageFees {
|
|
|
68
68
|
let acct = getAccount(accountAddress)
|
|
69
69
|
|
|
70
70
|
if let balanceRef = acct.capabilities.borrow<&FlowToken.Vault>(/public/flowTokenBalance) {
|
|
71
|
-
balance = balanceRef.
|
|
71
|
+
balance = balanceRef.balance
|
|
72
72
|
}
|
|
73
73
|
|
|
74
74
|
return self.accountBalanceToAccountStorageCapacity(balance)
|
|
@@ -86,7 +86,7 @@ access(all) contract FlowStorageFees {
|
|
|
86
86
|
|
|
87
87
|
// getAccountsCapacityForTransactionStorageCheck returns the storage capacity of a batch of accounts
|
|
88
88
|
// This is used to check if a transaction will fail because of any account being over the storage capacity
|
|
89
|
-
// The payer is an exception as its storage capacity is derived from its balance minus the maximum possible transaction fees
|
|
89
|
+
// The payer is an exception as its storage capacity is derived from its balance minus the maximum possible transaction fees
|
|
90
90
|
// (transaction fees if the execution effort is at the execution efort limit, a.k.a.: computation limit, a.k.a.: gas limit)
|
|
91
91
|
access(all) fun getAccountsCapacityForTransactionStorageCheck(accountAddresses: [Address], payer: Address, maxTxFees: UFix64): [UFix64] {
|
|
92
92
|
let capacities: [UFix64] = []
|
|
@@ -97,13 +97,13 @@ access(all) contract FlowStorageFees {
|
|
|
97
97
|
if let balanceRef = acct.capabilities.borrow<&FlowToken.Vault>(/public/flowTokenBalance) {
|
|
98
98
|
if accountAddress == payer {
|
|
99
99
|
// if the account is the payer, deduct the maximum possible transaction fees from the balance
|
|
100
|
-
balance = balanceRef.
|
|
100
|
+
balance = balanceRef.balance.saturatingSubtract(maxTxFees)
|
|
101
101
|
} else {
|
|
102
|
-
balance = balanceRef.
|
|
102
|
+
balance = balanceRef.balance
|
|
103
103
|
}
|
|
104
104
|
}
|
|
105
105
|
|
|
106
|
-
capacities.append(self.accountBalanceToAccountStorageCapacity(balance))
|
|
106
|
+
capacities.append(self.accountBalanceToAccountStorageCapacity(balance))
|
|
107
107
|
}
|
|
108
108
|
return capacities
|
|
109
109
|
}
|
|
@@ -117,7 +117,7 @@ access(all) contract FlowStorageFees {
|
|
|
117
117
|
return 0.0
|
|
118
118
|
}
|
|
119
119
|
|
|
120
|
-
// return balance multiplied with megabytes per flow
|
|
120
|
+
// return balance multiplied with megabytes per flow
|
|
121
121
|
return self.flowToStorageCapacity(balance)
|
|
122
122
|
}
|
|
123
123
|
|
|
@@ -157,7 +157,7 @@ access(all) contract FlowStorageFees {
|
|
|
157
157
|
var balance = 0.0
|
|
158
158
|
|
|
159
159
|
if let balanceRef = acct.capabilities.borrow<&FlowToken.Vault>(/public/flowTokenBalance) {
|
|
160
|
-
balance = balanceRef.
|
|
160
|
+
balance = balanceRef.balance
|
|
161
161
|
}
|
|
162
162
|
|
|
163
163
|
// get how much should be reserved for storage
|
package/contracts/FlowToken.cdc
CHANGED
|
@@ -1,16 +1,13 @@
|
|
|
1
1
|
import "FungibleToken"
|
|
2
2
|
import "MetadataViews"
|
|
3
3
|
import "FungibleTokenMetadataViews"
|
|
4
|
-
import "
|
|
4
|
+
import "Burner"
|
|
5
5
|
|
|
6
|
-
access(all) contract FlowToken:
|
|
6
|
+
access(all) contract FlowToken: FungibleToken {
|
|
7
7
|
|
|
8
8
|
// Total supply of Flow tokens in existence
|
|
9
9
|
access(all) var totalSupply: UFix64
|
|
10
10
|
|
|
11
|
-
// Event that is emitted when the contract is created
|
|
12
|
-
access(all) event TokensInitialized(initialSupply: UFix64)
|
|
13
|
-
|
|
14
11
|
// Event that is emitted when tokens are withdrawn from a Vault
|
|
15
12
|
access(all) event TokensWithdrawn(amount: UFix64, from: Address?)
|
|
16
13
|
|
|
@@ -20,9 +17,6 @@ access(all) contract FlowToken: ViewResolver {
|
|
|
20
17
|
// Event that is emitted when new tokens are minted
|
|
21
18
|
access(all) event TokensMinted(amount: UFix64)
|
|
22
19
|
|
|
23
|
-
// Event that is emitted when tokens are destroyed
|
|
24
|
-
access(all) event TokensBurned(amount: UFix64)
|
|
25
|
-
|
|
26
20
|
// Event that is emitted when a new minter resource is created
|
|
27
21
|
access(all) event MinterCreated(allowedAmount: UFix64)
|
|
28
22
|
|
|
@@ -41,20 +35,24 @@ access(all) contract FlowToken: ViewResolver {
|
|
|
41
35
|
// out of thin air. A special Minter resource needs to be defined to mint
|
|
42
36
|
// new tokens.
|
|
43
37
|
//
|
|
44
|
-
access(all) resource Vault: FungibleToken.Vault
|
|
38
|
+
access(all) resource Vault: FungibleToken.Vault {
|
|
45
39
|
|
|
46
40
|
// holds the balance of a users tokens
|
|
47
41
|
access(all) var balance: UFix64
|
|
48
42
|
|
|
49
|
-
access(all) view fun getBalance(): UFix64 {
|
|
50
|
-
return self.balance
|
|
51
|
-
}
|
|
52
|
-
|
|
53
43
|
// initialize the balance at resource creation time
|
|
54
44
|
init(balance: UFix64) {
|
|
55
45
|
self.balance = balance
|
|
56
46
|
}
|
|
57
47
|
|
|
48
|
+
/// Called when a fungible token is burned via the `Burner.burn()` method
|
|
49
|
+
access(contract) fun burnCallback() {
|
|
50
|
+
if self.balance > 0.0 {
|
|
51
|
+
FlowToken.totalSupply = FlowToken.totalSupply - self.balance
|
|
52
|
+
}
|
|
53
|
+
self.balance = 0.0
|
|
54
|
+
}
|
|
55
|
+
|
|
58
56
|
/// getSupportedVaultTypes optionally returns a list of vault types that this receiver accepts
|
|
59
57
|
access(all) view fun getSupportedVaultTypes(): {Type: Bool} {
|
|
60
58
|
return {self.getType(): true}
|
|
@@ -64,14 +62,9 @@ access(all) contract FlowToken: ViewResolver {
|
|
|
64
62
|
if (type == self.getType()) { return true } else { return false }
|
|
65
63
|
}
|
|
66
64
|
|
|
67
|
-
///
|
|
68
|
-
access(all) view fun
|
|
69
|
-
return
|
|
70
|
-
}
|
|
71
|
-
|
|
72
|
-
/// Returns the public path where this vault should have a public capability
|
|
73
|
-
access(all) view fun getDefaultPublicPath(): PublicPath? {
|
|
74
|
-
return /public/flowTokenReceiver
|
|
65
|
+
/// Asks if the amount can be withdrawn from this vault
|
|
66
|
+
access(all) view fun isAvailableToWithdraw(amount: UFix64): Bool {
|
|
67
|
+
return amount <= self.balance
|
|
75
68
|
}
|
|
76
69
|
|
|
77
70
|
// withdraw
|
|
@@ -83,7 +76,7 @@ access(all) contract FlowToken: ViewResolver {
|
|
|
83
76
|
// created Vault to the context that called so it can be deposited
|
|
84
77
|
// elsewhere.
|
|
85
78
|
//
|
|
86
|
-
access(FungibleToken.
|
|
79
|
+
access(FungibleToken.Withdraw) fun withdraw(amount: UFix64): @{FungibleToken.Vault} {
|
|
87
80
|
self.balance = self.balance - amount
|
|
88
81
|
emit TokensWithdrawn(amount: amount, from: self.owner?.address)
|
|
89
82
|
return <-create Vault(balance: amount)
|
|
@@ -110,7 +103,7 @@ access(all) contract FlowToken: ViewResolver {
|
|
|
110
103
|
/// developers to know which parameter to pass to the resolveView() method.
|
|
111
104
|
///
|
|
112
105
|
access(all) view fun getViews(): [Type]{
|
|
113
|
-
return FlowToken.
|
|
106
|
+
return FlowToken.getContractViews(resourceType: nil)
|
|
114
107
|
}
|
|
115
108
|
|
|
116
109
|
/// Get a Metadata View from FlowToken
|
|
@@ -119,7 +112,7 @@ access(all) contract FlowToken: ViewResolver {
|
|
|
119
112
|
/// @return A structure representing the requested view.
|
|
120
113
|
///
|
|
121
114
|
access(all) fun resolveView(_ view: Type): AnyStruct? {
|
|
122
|
-
return FlowToken.
|
|
115
|
+
return FlowToken.resolveContractView(resourceType: nil, viewType: view)
|
|
123
116
|
}
|
|
124
117
|
|
|
125
118
|
access(all) fun createEmptyVault(): @{FungibleToken.Vault} {
|
|
@@ -134,11 +127,12 @@ access(all) contract FlowToken: ViewResolver {
|
|
|
134
127
|
// and store the returned Vault in their storage in order to allow their
|
|
135
128
|
// account to be able to receive deposits of this token type.
|
|
136
129
|
//
|
|
137
|
-
access(all) fun createEmptyVault(): @FlowToken.Vault {
|
|
130
|
+
access(all) fun createEmptyVault(vaultType: Type): @FlowToken.Vault {
|
|
138
131
|
return <-create Vault(balance: 0.0)
|
|
139
132
|
}
|
|
140
133
|
|
|
141
|
-
|
|
134
|
+
/// Gets a list of the metadata views that this contract supports
|
|
135
|
+
access(all) view fun getContractViews(resourceType: Type?): [Type] {
|
|
142
136
|
return [Type<FungibleTokenMetadataViews.FTView>(),
|
|
143
137
|
Type<FungibleTokenMetadataViews.FTDisplay>(),
|
|
144
138
|
Type<FungibleTokenMetadataViews.FTVaultData>(),
|
|
@@ -150,12 +144,12 @@ access(all) contract FlowToken: ViewResolver {
|
|
|
150
144
|
/// @param view: The Type of the desired view.
|
|
151
145
|
/// @return A structure representing the requested view.
|
|
152
146
|
///
|
|
153
|
-
access(all) fun
|
|
154
|
-
switch
|
|
147
|
+
access(all) fun resolveContractView(resourceType: Type?, viewType: Type): AnyStruct? {
|
|
148
|
+
switch viewType {
|
|
155
149
|
case Type<FungibleTokenMetadataViews.FTView>():
|
|
156
150
|
return FungibleTokenMetadataViews.FTView(
|
|
157
|
-
ftDisplay: self.
|
|
158
|
-
ftVaultData: self.
|
|
151
|
+
ftDisplay: self.resolveContractView(resourceType: nil, viewType: Type<FungibleTokenMetadataViews.FTDisplay>()) as! FungibleTokenMetadataViews.FTDisplay?,
|
|
152
|
+
ftVaultData: self.resolveContractView(resourceType: nil, viewType: Type<FungibleTokenMetadataViews.FTVaultData>()) as! FungibleTokenMetadataViews.FTVaultData?
|
|
159
153
|
)
|
|
160
154
|
case Type<FungibleTokenMetadataViews.FTDisplay>():
|
|
161
155
|
let media = MetadataViews.Media(
|
|
@@ -176,16 +170,16 @@ access(all) contract FlowToken: ViewResolver {
|
|
|
176
170
|
}
|
|
177
171
|
)
|
|
178
172
|
case Type<FungibleTokenMetadataViews.FTVaultData>():
|
|
173
|
+
let vaultRef = FlowToken.account.storage.borrow<auth(FungibleToken.Withdraw) &FlowToken.Vault>(from: /storage/flowTokenVault)
|
|
174
|
+
?? panic("Could not borrow reference to the contract's Vault!")
|
|
179
175
|
return FungibleTokenMetadataViews.FTVaultData(
|
|
180
176
|
storagePath: /storage/flowTokenVault,
|
|
181
177
|
receiverPath: /public/flowTokenReceiver,
|
|
182
178
|
metadataPath: /public/flowTokenBalance,
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
metadataLinkedType: Type<&FlowToken.Vault>(),
|
|
186
|
-
providerLinkedType: Type<&FlowToken.Vault>(),
|
|
179
|
+
receiverLinkedType: Type<&{FungibleToken.Receiver, FungibleToken.Vault}>(),
|
|
180
|
+
metadataLinkedType: Type<&{FungibleToken.Balance, FungibleToken.Vault}>(),
|
|
187
181
|
createEmptyVaultFunction: (fun (): @{FungibleToken.Vault} {
|
|
188
|
-
return <-
|
|
182
|
+
return <-vaultRef.createEmptyVault()
|
|
189
183
|
})
|
|
190
184
|
)
|
|
191
185
|
case Type<FungibleTokenMetadataViews.TotalSupply>():
|
|
@@ -203,15 +197,6 @@ access(all) contract FlowToken: ViewResolver {
|
|
|
203
197
|
emit MinterCreated(allowedAmount: allowedAmount)
|
|
204
198
|
return <-create Minter(allowedAmount: allowedAmount)
|
|
205
199
|
}
|
|
206
|
-
|
|
207
|
-
// createNewBurner
|
|
208
|
-
//
|
|
209
|
-
// Function that creates and returns a new burner resource
|
|
210
|
-
//
|
|
211
|
-
access(all) fun createNewBurner(): @Burner {
|
|
212
|
-
emit BurnerCreated()
|
|
213
|
-
return <-create Burner()
|
|
214
|
-
}
|
|
215
200
|
}
|
|
216
201
|
|
|
217
202
|
// Minter
|
|
@@ -244,34 +229,6 @@ access(all) contract FlowToken: ViewResolver {
|
|
|
244
229
|
}
|
|
245
230
|
}
|
|
246
231
|
|
|
247
|
-
// Burner
|
|
248
|
-
//
|
|
249
|
-
// Resource object that token admin accounts can hold to burn tokens.
|
|
250
|
-
//
|
|
251
|
-
access(all) resource Burner {
|
|
252
|
-
|
|
253
|
-
// burnTokens
|
|
254
|
-
//
|
|
255
|
-
// Function that destroys a Vault instance, effectively burning the tokens.
|
|
256
|
-
//
|
|
257
|
-
// Note: the burned tokens are automatically subtracted from the
|
|
258
|
-
// total supply in the Vault destructor.
|
|
259
|
-
//
|
|
260
|
-
access(all) fun burnTokens(from: @FlowToken.Vault) {
|
|
261
|
-
FlowToken.burnTokens(from: <-from)
|
|
262
|
-
}
|
|
263
|
-
}
|
|
264
|
-
|
|
265
|
-
access(all) fun burnTokens(from: @FlowToken.Vault) {
|
|
266
|
-
let vault <- from as! @FlowToken.Vault
|
|
267
|
-
let amount = vault.balance
|
|
268
|
-
destroy vault
|
|
269
|
-
if amount > 0.0 {
|
|
270
|
-
FlowToken.totalSupply = FlowToken.totalSupply - amount
|
|
271
|
-
emit TokensBurned(amount: amount)
|
|
272
|
-
}
|
|
273
|
-
}
|
|
274
|
-
|
|
275
232
|
/// Gets the Flow Logo XML URI from storage
|
|
276
233
|
access(all) fun getLogoURI(): String {
|
|
277
234
|
return FlowToken.account.storage.copy<String>(from: /storage/flowTokenLogoURI) ?? ""
|
|
@@ -284,9 +241,6 @@ access(all) contract FlowToken: ViewResolver {
|
|
|
284
241
|
//
|
|
285
242
|
let vault <- create Vault(balance: self.totalSupply)
|
|
286
243
|
|
|
287
|
-
// Example of how to resolve a metadata view for a Vault
|
|
288
|
-
let ftView = vault.resolveView(Type<FungibleTokenMetadataViews.FTView>())
|
|
289
|
-
|
|
290
244
|
adminAccount.storage.save(<-vault, to: /storage/flowTokenVault)
|
|
291
245
|
|
|
292
246
|
// Create a public capability to the stored Vault that only exposes
|
|
@@ -304,8 +258,5 @@ access(all) contract FlowToken: ViewResolver {
|
|
|
304
258
|
let admin <- create Administrator()
|
|
305
259
|
adminAccount.storage.save(<-admin, to: /storage/flowTokenAdmin)
|
|
306
260
|
|
|
307
|
-
// Emit an event that shows that the contract was initialized
|
|
308
|
-
emit TokensInitialized(initialSupply: self.totalSupply)
|
|
309
|
-
|
|
310
261
|
}
|
|
311
262
|
}
|
|
@@ -32,6 +32,7 @@ to the Provider interface.
|
|
|
32
32
|
*/
|
|
33
33
|
|
|
34
34
|
import "ViewResolver"
|
|
35
|
+
import "Burner"
|
|
35
36
|
|
|
36
37
|
/// FungibleToken
|
|
37
38
|
///
|
|
@@ -40,16 +41,28 @@ import "ViewResolver"
|
|
|
40
41
|
/// utility methods that many projects will still want to have on their contracts,
|
|
41
42
|
/// but they are by no means required. all that is required is that the token
|
|
42
43
|
/// implements the `Vault` interface
|
|
43
|
-
access(all) contract FungibleToken {
|
|
44
|
+
access(all) contract interface FungibleToken: ViewResolver {
|
|
44
45
|
|
|
45
46
|
// An entitlement for allowing the withdrawal of tokens from a Vault
|
|
46
|
-
access(all) entitlement
|
|
47
|
+
access(all) entitlement Withdraw
|
|
47
48
|
|
|
48
49
|
/// The event that is emitted when tokens are withdrawn from a Vault
|
|
49
|
-
access(all) event
|
|
50
|
+
access(all) event Withdrawn(type: String, amount: UFix64, from: Address?, fromUUID: UInt64, withdrawnUUID: UInt64)
|
|
50
51
|
|
|
51
52
|
/// The event that is emitted when tokens are deposited to a Vault
|
|
52
|
-
access(all) event
|
|
53
|
+
access(all) event Deposited(type: String, amount: UFix64, to: Address?, toUUID: UInt64, depositedUUID: UInt64)
|
|
54
|
+
|
|
55
|
+
/// Event that is emitted when the global burn method is called with a non-zero balance
|
|
56
|
+
access(all) event Burned(type: String, amount: UFix64, fromUUID: UInt64)
|
|
57
|
+
|
|
58
|
+
/// Balance
|
|
59
|
+
///
|
|
60
|
+
/// The interface that provides standard functions\
|
|
61
|
+
/// for getting balance information
|
|
62
|
+
///
|
|
63
|
+
access(all) resource interface Balance {
|
|
64
|
+
access(all) var balance: UFix64
|
|
65
|
+
}
|
|
53
66
|
|
|
54
67
|
/// Provider
|
|
55
68
|
///
|
|
@@ -62,27 +75,29 @@ access(all) contract FungibleToken {
|
|
|
62
75
|
///
|
|
63
76
|
access(all) resource interface Provider {
|
|
64
77
|
|
|
65
|
-
///
|
|
78
|
+
/// Function to ask a provider if a specific amount of tokens
|
|
79
|
+
/// is available to be withdrawn
|
|
80
|
+
/// This could be useful to avoid panicing when calling withdraw
|
|
81
|
+
/// when the balance is unknown
|
|
82
|
+
/// Additionally, if the provider is pulling from multiple vaults
|
|
83
|
+
/// it only needs to check some of the vaults until the desired amount
|
|
84
|
+
/// is reached, potentially helping with performance.
|
|
85
|
+
///
|
|
86
|
+
access(all) view fun isAvailableToWithdraw(amount: UFix64): Bool
|
|
87
|
+
|
|
88
|
+
/// withdraw subtracts tokens from the implementing resource
|
|
66
89
|
/// and returns a Vault with the removed tokens.
|
|
67
90
|
///
|
|
68
|
-
/// The function's access level is
|
|
69
|
-
///
|
|
70
|
-
///
|
|
71
|
-
///
|
|
72
|
-
/// The owner may grant other accounts access by creating a private
|
|
73
|
-
/// capability that allows specific other users to access
|
|
74
|
-
/// the provider resource through a reference.
|
|
75
|
-
///
|
|
76
|
-
/// The owner may also grant all accounts access by creating a public
|
|
77
|
-
/// capability that allows all users to access the provider
|
|
78
|
-
/// resource through a reference.
|
|
91
|
+
/// The function's access level is `access(Withdraw)`
|
|
92
|
+
/// So in order to access it, one would either need the object itself
|
|
93
|
+
/// or an entitled reference with `Withdraw`.
|
|
79
94
|
///
|
|
80
|
-
access(
|
|
95
|
+
access(Withdraw) fun withdraw(amount: UFix64): @{Vault} {
|
|
81
96
|
post {
|
|
82
97
|
// `result` refers to the return value
|
|
83
|
-
result.
|
|
98
|
+
result.balance == amount:
|
|
84
99
|
"Withdrawal amount must be the same as the balance of the withdrawn Vault"
|
|
85
|
-
emit
|
|
100
|
+
emit Withdrawn(type: self.getType().identifier, amount: amount, from: self.owner?.address, fromUUID: self.uuid, withdrawnUUID: result.uuid)
|
|
86
101
|
}
|
|
87
102
|
}
|
|
88
103
|
}
|
|
@@ -104,15 +119,11 @@ access(all) contract FungibleToken {
|
|
|
104
119
|
access(all) fun deposit(from: @{Vault})
|
|
105
120
|
|
|
106
121
|
/// getSupportedVaultTypes optionally returns a list of vault types that this receiver accepts
|
|
107
|
-
access(all) view fun getSupportedVaultTypes(): {Type: Bool}
|
|
108
|
-
pre { true: "dummy" }
|
|
109
|
-
}
|
|
122
|
+
access(all) view fun getSupportedVaultTypes(): {Type: Bool}
|
|
110
123
|
|
|
111
124
|
/// Returns whether or not the given type is accepted by the Receiver
|
|
112
125
|
/// A vault that can accept any type should just return true by default
|
|
113
|
-
access(all) view fun isSupportedVaultType(type: Type): Bool
|
|
114
|
-
pre { true: "dummy" }
|
|
115
|
-
}
|
|
126
|
+
access(all) view fun isSupportedVaultType(type: Type): Bool
|
|
116
127
|
}
|
|
117
128
|
|
|
118
129
|
/// Vault
|
|
@@ -120,17 +131,35 @@ access(all) contract FungibleToken {
|
|
|
120
131
|
/// Ideally, this interface would also conform to Receiver, Balance, Transferor, Provider, and Resolver
|
|
121
132
|
/// but that is not supported yet
|
|
122
133
|
///
|
|
123
|
-
access(all) resource interface Vault: Receiver, Provider, ViewResolver.Resolver {
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
134
|
+
access(all) resource interface Vault: Receiver, Provider, Balance, ViewResolver.Resolver, Burner.Burnable {
|
|
135
|
+
|
|
136
|
+
/// Field that tracks the balance of a vault
|
|
137
|
+
access(all) var balance: UFix64
|
|
138
|
+
|
|
139
|
+
/// Called when a fungible token is burned via the `Burner.burn()` method
|
|
140
|
+
/// Implementations can do any bookkeeping or emit any events
|
|
141
|
+
/// that should be emitted when a vault is destroyed.
|
|
142
|
+
/// Many implementations will want to update the token's total supply
|
|
143
|
+
/// to reflect that the tokens have been burned and removed from the supply.
|
|
144
|
+
/// Implementations also need to set the balance to zero before the end of the function
|
|
145
|
+
/// This is to prevent vault owners from spamming fake Burned events.
|
|
146
|
+
access(contract) fun burnCallback() {
|
|
147
|
+
pre {
|
|
148
|
+
emit Burned(type: self.getType().identifier, amount: self.balance, fromUUID: self.uuid)
|
|
149
|
+
}
|
|
150
|
+
post {
|
|
151
|
+
self.balance == 0.0: "The balance must be set to zero during the burnCallback method so that it cannot be spammed"
|
|
152
|
+
}
|
|
153
|
+
self.balance = 0.0
|
|
154
|
+
}
|
|
129
155
|
|
|
130
156
|
/// getSupportedVaultTypes optionally returns a list of vault types that this receiver accepts
|
|
157
|
+
/// The default implementation is included here because vaults are expected
|
|
158
|
+
/// to only accepted their own type, so they have no need to provide an implementation
|
|
159
|
+
/// for this function
|
|
131
160
|
access(all) view fun getSupportedVaultTypes(): {Type: Bool} {
|
|
132
161
|
// Below check is implemented to make sure that run-time type would
|
|
133
|
-
// only get returned when the parent resource conforms with `FungibleToken.Vault`.
|
|
162
|
+
// only get returned when the parent resource conforms with `FungibleToken.Vault`.
|
|
134
163
|
if self.getType().isSubtype(of: Type<@{FungibleToken.Vault}>()) {
|
|
135
164
|
return {self.getType(): true}
|
|
136
165
|
} else {
|
|
@@ -140,36 +169,25 @@ access(all) contract FungibleToken {
|
|
|
140
169
|
}
|
|
141
170
|
}
|
|
142
171
|
|
|
172
|
+
/// Checks if the given type is supported by this Vault
|
|
143
173
|
access(all) view fun isSupportedVaultType(type: Type): Bool {
|
|
144
174
|
return self.getSupportedVaultTypes()[type] ?? false
|
|
145
175
|
}
|
|
146
176
|
|
|
147
|
-
/// Returns the storage path where the vault should typically be stored
|
|
148
|
-
access(all) view fun getDefaultStoragePath(): StoragePath?
|
|
149
|
-
|
|
150
|
-
/// Returns the public path where this vault should have a public capability
|
|
151
|
-
access(all) view fun getDefaultPublicPath(): PublicPath?
|
|
152
|
-
|
|
153
|
-
/// Returns the public path where this vault's Receiver should have a public capability
|
|
154
|
-
/// Publishing a Receiver Capability at a different path enables alternate Receiver implementations to be used
|
|
155
|
-
/// in the same canonical namespace as the underlying Vault.
|
|
156
|
-
access(all) view fun getDefaultReceiverPath(): PublicPath? {
|
|
157
|
-
return nil
|
|
158
|
-
}
|
|
159
|
-
|
|
160
177
|
/// withdraw subtracts `amount` from the Vault's balance
|
|
161
178
|
/// and returns a new Vault with the subtracted balance
|
|
162
179
|
///
|
|
163
|
-
access(
|
|
180
|
+
access(Withdraw) fun withdraw(amount: UFix64): @{Vault} {
|
|
164
181
|
pre {
|
|
165
|
-
self.
|
|
182
|
+
self.balance >= amount:
|
|
166
183
|
"Amount withdrawn must be less than or equal than the balance of the Vault"
|
|
167
184
|
}
|
|
168
185
|
post {
|
|
186
|
+
result.getType() == self.getType(): "Must return the same vault type as self"
|
|
169
187
|
// use the special function `before` to get the value of the `balance` field
|
|
170
188
|
// at the beginning of the function execution
|
|
171
189
|
//
|
|
172
|
-
self.
|
|
190
|
+
self.balance == before(self.balance) - amount:
|
|
173
191
|
"New Vault balance must be the difference of the previous balance and the withdrawn Vault balance"
|
|
174
192
|
}
|
|
175
193
|
}
|
|
@@ -180,12 +198,12 @@ access(all) contract FungibleToken {
|
|
|
180
198
|
// Assert that the concrete type of the deposited vault is the same
|
|
181
199
|
// as the vault that is accepting the deposit
|
|
182
200
|
pre {
|
|
183
|
-
from.isInstance(self.getType()):
|
|
201
|
+
from.isInstance(self.getType()):
|
|
184
202
|
"Cannot deposit an incompatible token type"
|
|
185
|
-
emit
|
|
203
|
+
emit Deposited(type: from.getType().identifier, amount: from.balance, to: self.owner?.address, toUUID: self.uuid, depositedUUID: from.uuid)
|
|
186
204
|
}
|
|
187
205
|
post {
|
|
188
|
-
self.
|
|
206
|
+
self.balance == before(self.balance) + before(from.balance):
|
|
189
207
|
"New Vault balance must be the sum of the previous balance and the deposited Vault"
|
|
190
208
|
}
|
|
191
209
|
}
|
|
@@ -194,8 +212,17 @@ access(all) contract FungibleToken {
|
|
|
194
212
|
///
|
|
195
213
|
access(all) fun createEmptyVault(): @{Vault} {
|
|
196
214
|
post {
|
|
197
|
-
result.
|
|
215
|
+
result.balance == 0.0: "The newly created Vault must have zero balance"
|
|
198
216
|
}
|
|
199
217
|
}
|
|
200
218
|
}
|
|
201
|
-
|
|
219
|
+
|
|
220
|
+
/// createEmptyVault allows any user to create a new Vault that has a zero balance
|
|
221
|
+
///
|
|
222
|
+
access(all) fun createEmptyVault(vaultType: Type): @{FungibleToken.Vault} {
|
|
223
|
+
post {
|
|
224
|
+
result.getType() == vaultType: "The returned vault does not match the desired type"
|
|
225
|
+
result.balance == 0.0: "The newly created Vault must have zero balance"
|
|
226
|
+
}
|
|
227
|
+
}
|
|
228
|
+
}
|