@flowtyio/flow-contracts 0.0.16 → 0.0.17
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/README.md +5 -0
- package/contracts/flow-utils/ScopedFTProviders.cdc +120 -0
- package/contracts/flow-utils/ScopedNFTProviders.cdc +162 -0
- package/flow.json +19 -1
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -57,6 +57,11 @@ Currently, the list includes:
|
|
|
57
57
|
| TokenForwarding | 0x51ea0e37c27a1f1a | 0xe544175ee0461c4b |
|
|
58
58
|
| DapperUtilityCoin | 0x82ec283f88a62e65 | 0xead892083b3e2c6c |
|
|
59
59
|
| FlowUtilityToken | 0x82ec283f88a62e65 | 0xead892083b3e2c6c |
|
|
60
|
+
| ArrayUtils| 0x31ad40c07a2a9788 | 0xa340dc0a4ec828ab |
|
|
61
|
+
| StringUtils| 0x31ad40c07a2a9788 | 0xa340dc0a4ec828ab |
|
|
62
|
+
| AddressUtils | 0x31ad40c07a2a9788 | 0xa340dc0a4ec828ab |
|
|
63
|
+
| ScopedNFTProviders | 0x31ad40c07a2a9788 | 0xa340dc0a4ec828ab |
|
|
64
|
+
| ScopedFTProviders | 0x31ad40c07a2a9788 | 0xa340dc0a4ec828ab |
|
|
60
65
|
|
|
61
66
|
|
|
62
67
|
## Using a contract
|
|
@@ -0,0 +1,120 @@
|
|
|
1
|
+
import "FungibleToken"
|
|
2
|
+
import "StringUtils"
|
|
3
|
+
|
|
4
|
+
// ScopedFTProviders
|
|
5
|
+
//
|
|
6
|
+
// TO AVOID RISK, PLEASE DEPLOY YOUR OWN VERSION OF THIS CONTRACT SO THAT
|
|
7
|
+
// MALICIOUS UPDATES ARE NOT POSSIBLE
|
|
8
|
+
//
|
|
9
|
+
// ScopedProviders are meant to solve the issue of unbounded access FungibleToken vaults
|
|
10
|
+
// when a provider is called for.
|
|
11
|
+
pub contract ScopedFTProviders {
|
|
12
|
+
pub struct interface FTFilter {
|
|
13
|
+
pub fun canWithdrawAmount(_ amount: UFix64): Bool
|
|
14
|
+
pub fun markAmountWithdrawn(_ amount: UFix64)
|
|
15
|
+
pub fun getDetails(): {String: AnyStruct}
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
pub struct AllowanceFilter: FTFilter {
|
|
19
|
+
access(self) let allowance: UFix64
|
|
20
|
+
access(self) var allowanceUsed: UFix64
|
|
21
|
+
|
|
22
|
+
init(_ allowance: UFix64) {
|
|
23
|
+
self.allowance = allowance
|
|
24
|
+
self.allowanceUsed = 0.0
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
pub fun canWithdrawAmount(_ amount: UFix64): Bool {
|
|
28
|
+
return amount + self.allowanceUsed <= self.allowance
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
pub fun markAmountWithdrawn(_ amount: UFix64) {
|
|
32
|
+
self.allowanceUsed = self.allowanceUsed + amount
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
pub fun getDetails(): {String: AnyStruct} {
|
|
36
|
+
return {
|
|
37
|
+
"allowance": self.allowance,
|
|
38
|
+
"allowanceUsed": self.allowanceUsed
|
|
39
|
+
}
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
// ScopedFTProvider
|
|
44
|
+
//
|
|
45
|
+
// A ScopedFTProvider is a wrapped FungibleTokenProvider with
|
|
46
|
+
// filters that can be defined by anyone using the ScopedFTProvider.
|
|
47
|
+
pub resource ScopedFTProvider: FungibleToken.Provider {
|
|
48
|
+
access(self) let provider: Capability<&{FungibleToken.Provider}>
|
|
49
|
+
access(self) var filters: [{FTFilter}]
|
|
50
|
+
|
|
51
|
+
// block timestamp that this provider can no longer be used after
|
|
52
|
+
access(self) let expiration: UFix64?
|
|
53
|
+
|
|
54
|
+
pub init(provider: Capability<&{FungibleToken.Provider}>, filters: [{FTFilter}], expiration: UFix64?) {
|
|
55
|
+
self.provider = provider
|
|
56
|
+
self.filters = filters
|
|
57
|
+
self.expiration = expiration
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
pub fun check(): Bool {
|
|
61
|
+
return self.provider.check()
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
pub fun isExpired(): Bool {
|
|
65
|
+
if let expiration = self.expiration {
|
|
66
|
+
return getCurrentBlock().timestamp >= expiration
|
|
67
|
+
}
|
|
68
|
+
return false
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
pub fun canWithdraw(_ amount: UFix64): Bool {
|
|
72
|
+
if self.isExpired() {
|
|
73
|
+
return false
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
for filter in self.filters {
|
|
77
|
+
if !filter.canWithdrawAmount(amount) {
|
|
78
|
+
return false
|
|
79
|
+
}
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
return true
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
pub fun withdraw(amount: UFix64): @FungibleToken.Vault {
|
|
86
|
+
pre {
|
|
87
|
+
!self.isExpired(): "provider has expired"
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
var i = 0
|
|
91
|
+
while i < self.filters.length {
|
|
92
|
+
if !self.filters[i].canWithdrawAmount(amount) {
|
|
93
|
+
panic(StringUtils.join(["cannot withdraw tokens. filter of type", self.filters[i].getType().identifier, "failed."], " "))
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
self.filters[i].markAmountWithdrawn(amount)
|
|
97
|
+
i = i + 1
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
return <-self.provider.borrow()!.withdraw(amount: amount)
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
pub fun getDetails(): [{String: AnyStruct}] {
|
|
104
|
+
let details: [{String: AnyStruct}] = []
|
|
105
|
+
for filter in self.filters {
|
|
106
|
+
details.append(filter.getDetails())
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
return details
|
|
110
|
+
}
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
pub fun createScopedFTProvider(
|
|
114
|
+
provider: Capability<&{FungibleToken.Provider}>,
|
|
115
|
+
filters: [{FTFilter}],
|
|
116
|
+
expiration: UFix64?
|
|
117
|
+
): @ScopedFTProvider {
|
|
118
|
+
return <- create ScopedFTProvider(provider: provider, filters: filters, expiration: expiration)
|
|
119
|
+
}
|
|
120
|
+
}
|
|
@@ -0,0 +1,162 @@
|
|
|
1
|
+
import "NonFungibleToken"
|
|
2
|
+
import "StringUtils"
|
|
3
|
+
|
|
4
|
+
// ScopedNFTProviders
|
|
5
|
+
//
|
|
6
|
+
// TO AVOID RISK, PLEASE DEPLOY YOUR OWN VERSION OF THIS CONTRACT SO THAT
|
|
7
|
+
// MALICIOUS UPDATES ARE NOT POSSIBLE
|
|
8
|
+
//
|
|
9
|
+
// ScopedNFTProviders are meant to solve the issue of unbounded access to NFT Collections.
|
|
10
|
+
// A provider can be given extensible filters which allow limited access to resources based on any trait on the NFT itself.
|
|
11
|
+
//
|
|
12
|
+
// By using a scoped provider, only a subset of assets can be taken if the provider leaks
|
|
13
|
+
// instead of the entire nft collection.
|
|
14
|
+
pub contract ScopedNFTProviders {
|
|
15
|
+
pub struct interface NFTFilter {
|
|
16
|
+
pub fun canWithdraw(_ nft: &NonFungibleToken.NFT): Bool
|
|
17
|
+
pub fun markWithdrawn(_ nft: &NonFungibleToken.NFT)
|
|
18
|
+
pub fun getDetails(): {String: AnyStruct}
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
pub struct NFTIDFilter: NFTFilter {
|
|
22
|
+
// the ids that are allowed to be withdrawn.
|
|
23
|
+
// If ids[num] is false, the id cannot be withdrawn anymore
|
|
24
|
+
access(self) let ids: {UInt64: Bool}
|
|
25
|
+
|
|
26
|
+
init(_ ids: [UInt64]) {
|
|
27
|
+
let d: {UInt64: Bool} = {}
|
|
28
|
+
for i in ids {
|
|
29
|
+
d[i] = true
|
|
30
|
+
}
|
|
31
|
+
self.ids = d
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
pub fun canWithdraw(_ nft: &NonFungibleToken.NFT): Bool {
|
|
35
|
+
return self.ids[nft.id] != nil && self.ids[nft.id] == true
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
pub fun markWithdrawn(_ nft: &NonFungibleToken.NFT) {
|
|
39
|
+
self.ids[nft.id] = false
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
pub fun getDetails(): {String: AnyStruct} {
|
|
43
|
+
return {
|
|
44
|
+
"ids": self.ids
|
|
45
|
+
}
|
|
46
|
+
}
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
pub struct UUIDFilter: NFTFilter {
|
|
50
|
+
// the ids that are allowed to be withdrawn.
|
|
51
|
+
// If ids[num] is false, the id cannot be withdrawn anymore
|
|
52
|
+
access(self) let uuids: {UInt64: Bool}
|
|
53
|
+
|
|
54
|
+
init(_ uuids: [UInt64]) {
|
|
55
|
+
let d: {UInt64: Bool} = {}
|
|
56
|
+
for i in uuids {
|
|
57
|
+
d[i] = true
|
|
58
|
+
}
|
|
59
|
+
self.uuids = d
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
pub fun canWithdraw(_ nft: &NonFungibleToken.NFT): Bool {
|
|
63
|
+
return self.uuids[nft.uuid] != nil && self.uuids[nft.uuid]! == true
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
pub fun markWithdrawn(_ nft: &NonFungibleToken.NFT) {
|
|
67
|
+
self.uuids[nft.uuid] = false
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
pub fun getDetails(): {String: AnyStruct} {
|
|
71
|
+
return {
|
|
72
|
+
"uuids": self.uuids
|
|
73
|
+
}
|
|
74
|
+
}
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
// ScopedNFTProvider
|
|
78
|
+
//
|
|
79
|
+
// Wrapper around an NFT Provider that is restricted to specific ids.
|
|
80
|
+
pub resource ScopedNFTProvider: NonFungibleToken.Provider {
|
|
81
|
+
access(self) let provider: Capability<&{NonFungibleToken.Provider, NonFungibleToken.CollectionPublic}>
|
|
82
|
+
access(self) let filters: [{NFTFilter}]
|
|
83
|
+
|
|
84
|
+
// block timestamp that this provider can no longer be used after
|
|
85
|
+
access(self) let expiration: UFix64?
|
|
86
|
+
|
|
87
|
+
pub fun isExpired(): Bool {
|
|
88
|
+
if let expiration = self.expiration {
|
|
89
|
+
return getCurrentBlock().timestamp >= expiration
|
|
90
|
+
}
|
|
91
|
+
return false
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
pub init(provider: Capability<&{NonFungibleToken.Provider, NonFungibleToken.CollectionPublic}>, filters: [{NFTFilter}], expiration: UFix64?) {
|
|
95
|
+
self.provider = provider
|
|
96
|
+
self.expiration = expiration
|
|
97
|
+
self.filters = filters
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
pub fun canWithdraw(_ id: UInt64): Bool {
|
|
101
|
+
if self.isExpired() {
|
|
102
|
+
return false
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
let nft = self.provider.borrow()!.borrowNFT(id: id)
|
|
106
|
+
if nft == nil {
|
|
107
|
+
return false
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
var i = 0
|
|
111
|
+
while i < self.filters.length {
|
|
112
|
+
if !self.filters[i].canWithdraw(nft) {
|
|
113
|
+
return false
|
|
114
|
+
}
|
|
115
|
+
i = i + 1
|
|
116
|
+
}
|
|
117
|
+
return true
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
pub fun check(): Bool {
|
|
121
|
+
return self.provider.check()
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
pub fun withdraw(withdrawID: UInt64): @NonFungibleToken.NFT {
|
|
125
|
+
pre {
|
|
126
|
+
!self.isExpired(): "provider has expired"
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
let nft <- self.provider.borrow()!.withdraw(withdrawID: withdrawID)
|
|
130
|
+
let ref = &nft as &NonFungibleToken.NFT
|
|
131
|
+
|
|
132
|
+
var i = 0
|
|
133
|
+
while i < self.filters.length {
|
|
134
|
+
if !self.filters[i].canWithdraw(ref) {
|
|
135
|
+
panic(StringUtils.join(["cannot withdraw nft. filter of type", self.filters[i].getType().identifier, "failed."], " "))
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
self.filters[i].markWithdrawn(ref)
|
|
139
|
+
i = i + 1
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
return <-nft
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
pub fun getDetails(): [{String: AnyStruct}] {
|
|
146
|
+
let details: [{String: AnyStruct}] = []
|
|
147
|
+
for f in self.filters {
|
|
148
|
+
details.append(f.getDetails())
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
return details
|
|
152
|
+
}
|
|
153
|
+
}
|
|
154
|
+
|
|
155
|
+
pub fun createScopedNFTProvider(
|
|
156
|
+
provider: Capability<&{NonFungibleToken.Provider, NonFungibleToken.CollectionPublic}>,
|
|
157
|
+
filters: [{NFTFilter}],
|
|
158
|
+
expiration: UFix64?
|
|
159
|
+
): @ScopedNFTProvider {
|
|
160
|
+
return <- create ScopedNFTProvider(provider: provider, filters: filters, expiration: expiration)
|
|
161
|
+
}
|
|
162
|
+
}
|
package/flow.json
CHANGED
|
@@ -291,6 +291,22 @@
|
|
|
291
291
|
"testnet": "0x31ad40c07a2a9788",
|
|
292
292
|
"mainnet": "0xa340dc0a4ec828ab"
|
|
293
293
|
}
|
|
294
|
+
},
|
|
295
|
+
"ScopedFTProviders": {
|
|
296
|
+
"source": "./contracts/flow-utils/ScopedFTProviders.cdc",
|
|
297
|
+
"aliases": {
|
|
298
|
+
"emulator": "0xf8d6e0586b0a20c7",
|
|
299
|
+
"testnet": "0x31ad40c07a2a9788",
|
|
300
|
+
"mainnet": "0xa340dc0a4ec828ab"
|
|
301
|
+
}
|
|
302
|
+
},
|
|
303
|
+
"ScopedNFTProviders": {
|
|
304
|
+
"source": "./contracts/flow-utils/ScopedNFTProviders.cdc",
|
|
305
|
+
"aliases": {
|
|
306
|
+
"emulator": "0xf8d6e0586b0a20c7",
|
|
307
|
+
"testnet": "0x31ad40c07a2a9788",
|
|
308
|
+
"mainnet": "0xa340dc0a4ec828ab"
|
|
309
|
+
}
|
|
294
310
|
}
|
|
295
311
|
},
|
|
296
312
|
"deployments": {
|
|
@@ -315,7 +331,9 @@
|
|
|
315
331
|
"TopShotLocking",
|
|
316
332
|
"ArrayUtils",
|
|
317
333
|
"StringUtils",
|
|
318
|
-
"AddressUtils"
|
|
334
|
+
"AddressUtils",
|
|
335
|
+
"ScopedNFTProviders",
|
|
336
|
+
"ScopedFTProviders"
|
|
319
337
|
],
|
|
320
338
|
"emulator-ft": [
|
|
321
339
|
"FungibleToken",
|