@flowtyio/flow-contracts 0.1.0-beta.6 → 0.1.0-beta.7
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/flow-utils/AddressUtils.cdc +1 -1
- package/contracts/flow-utils/ArrayUtils.cdc +50 -102
- package/contracts/flow-utils/ScopedFTProviders.cdc +24 -19
- package/contracts/flow-utils/ScopedNFTProviders.cdc +4 -3
- package/contracts/flow-utils/StringUtils.cdc +1 -1
- package/contracts/lost-and-found/LostAndFound.cdc +8 -8
- package/package.json +1 -1
|
@@ -1,120 +1,68 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
// ScopedProviders are meant to solve the issue of unbounded access FungibleToken vaults
|
|
10
|
-
// when a provider is called for.
|
|
11
|
-
access(all) contract ScopedFTProviders {
|
|
12
|
-
access(all) struct interface FTFilter {
|
|
13
|
-
access(all) fun canWithdrawAmount(_ amount: UFix64): Bool
|
|
14
|
-
access(FungibleToken.Withdraw) fun markAmountWithdrawn(_ amount: UFix64)
|
|
15
|
-
access(all) fun getDetails(): {String: AnyStruct}
|
|
16
|
-
}
|
|
17
|
-
|
|
18
|
-
access(all) 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
|
-
access(all) fun canWithdrawAmount(_ amount: UFix64): Bool {
|
|
28
|
-
return amount + self.allowanceUsed <= self.allowance
|
|
29
|
-
}
|
|
30
|
-
|
|
31
|
-
access(FungibleToken.Withdraw) fun markAmountWithdrawn(_ amount: UFix64) {
|
|
32
|
-
self.allowanceUsed = self.allowanceUsed + amount
|
|
33
|
-
}
|
|
34
|
-
|
|
35
|
-
access(all) fun getDetails(): {String: AnyStruct} {
|
|
36
|
-
return {
|
|
37
|
-
"allowance": self.allowance,
|
|
38
|
-
"allowanceUsed": self.allowanceUsed
|
|
39
|
-
}
|
|
1
|
+
// Copied from https://github.com/bluesign/flow-utils/blob/dnz/cadence/contracts/ArrayUtils.cdc with minor adjustments
|
|
2
|
+
|
|
3
|
+
access(all) contract ArrayUtils {
|
|
4
|
+
access(all) fun rangeFunc(_ start: Int, _ end: Int, _ f: fun (Int): Void) {
|
|
5
|
+
var current = start
|
|
6
|
+
while current < end {
|
|
7
|
+
f(current)
|
|
8
|
+
current = current + 1
|
|
40
9
|
}
|
|
41
10
|
}
|
|
42
11
|
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
// block timestamp that this provider can no longer be used after
|
|
52
|
-
access(self) let expiration: UFix64?
|
|
12
|
+
access(all) fun range(_ start: Int, _ end: Int): [Int] {
|
|
13
|
+
var res: [Int] = []
|
|
14
|
+
self.rangeFunc(start, end, fun (i: Int) {
|
|
15
|
+
res.append(i)
|
|
16
|
+
})
|
|
17
|
+
return res
|
|
18
|
+
}
|
|
53
19
|
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
20
|
+
access(all) fun reverse(_ array: [Int]): [Int] {
|
|
21
|
+
var res: [Int] = []
|
|
22
|
+
var i: Int = array.length - 1
|
|
23
|
+
while i >= 0 {
|
|
24
|
+
res.append(array[i])
|
|
25
|
+
i = i - 1
|
|
58
26
|
}
|
|
27
|
+
return res
|
|
28
|
+
}
|
|
59
29
|
|
|
60
|
-
|
|
61
|
-
|
|
30
|
+
access(all) fun transform(_ array: auth(Mutate) &[AnyStruct], _ f : fun (&AnyStruct, auth(Mutate) &[AnyStruct], Int)){
|
|
31
|
+
for i in self.range(0, array.length){
|
|
32
|
+
f(array[i], array, i)
|
|
62
33
|
}
|
|
34
|
+
}
|
|
63
35
|
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
36
|
+
access(all) fun iterate(_ array: [AnyStruct], _ f : fun (AnyStruct): Bool) {
|
|
37
|
+
for item in array{
|
|
38
|
+
if !f(item){
|
|
39
|
+
break
|
|
67
40
|
}
|
|
68
|
-
return false
|
|
69
41
|
}
|
|
42
|
+
}
|
|
70
43
|
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
for filter in self.filters {
|
|
77
|
-
if !filter.canWithdrawAmount(amount) {
|
|
78
|
-
return false
|
|
79
|
-
}
|
|
80
|
-
}
|
|
81
|
-
|
|
82
|
-
return true
|
|
44
|
+
access(all) fun map(_ array: [AnyStruct], _ f : fun (AnyStruct): AnyStruct) : [AnyStruct] {
|
|
45
|
+
var res : [AnyStruct] = []
|
|
46
|
+
for item in array{
|
|
47
|
+
res.append(f(item))
|
|
83
48
|
}
|
|
49
|
+
return res
|
|
50
|
+
}
|
|
84
51
|
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
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)
|
|
52
|
+
access(all) fun mapStrings(_ array: [String], _ f: fun (String) : String) : [String] {
|
|
53
|
+
var res : [String] = []
|
|
54
|
+
for item in array{
|
|
55
|
+
res.append(f(item))
|
|
101
56
|
}
|
|
57
|
+
return res
|
|
58
|
+
}
|
|
102
59
|
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
}
|
|
108
|
-
|
|
109
|
-
return details
|
|
60
|
+
access(all) fun reduce(_ array: [AnyStruct], _ initial: AnyStruct, _ f : fun (AnyStruct, AnyStruct): AnyStruct) : AnyStruct{
|
|
61
|
+
var res: AnyStruct = f(initial, array[0])
|
|
62
|
+
for i in self.range(1, array.length){
|
|
63
|
+
res = f(res, array[i])
|
|
110
64
|
}
|
|
65
|
+
return res
|
|
111
66
|
}
|
|
112
67
|
|
|
113
|
-
|
|
114
|
-
provider: Capability<auth(FungibleToken.Withdraw) &{FungibleToken.Provider}>,
|
|
115
|
-
filters: [{FTFilter}],
|
|
116
|
-
expiration: UFix64?
|
|
117
|
-
): @ScopedFTProvider {
|
|
118
|
-
return <- create ScopedFTProvider(provider: provider, filters: filters, expiration: expiration)
|
|
119
|
-
}
|
|
120
|
-
}
|
|
68
|
+
}
|
|
@@ -8,14 +8,14 @@ import "StringUtils"
|
|
|
8
8
|
//
|
|
9
9
|
// ScopedProviders are meant to solve the issue of unbounded access FungibleToken vaults
|
|
10
10
|
// when a provider is called for.
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
11
|
+
access(all) contract ScopedFTProviders {
|
|
12
|
+
access(all) struct interface FTFilter {
|
|
13
|
+
access(all) view fun canWithdrawAmount(_ amount: UFix64): Bool
|
|
14
|
+
access(FungibleToken.Withdraw) fun markAmountWithdrawn(_ amount: UFix64)
|
|
15
|
+
access(all) fun getDetails(): {String: AnyStruct}
|
|
16
16
|
}
|
|
17
17
|
|
|
18
|
-
|
|
18
|
+
access(all) struct AllowanceFilter: FTFilter {
|
|
19
19
|
access(self) let allowance: UFix64
|
|
20
20
|
access(self) var allowanceUsed: UFix64
|
|
21
21
|
|
|
@@ -24,15 +24,15 @@ pub contract ScopedFTProviders {
|
|
|
24
24
|
self.allowanceUsed = 0.0
|
|
25
25
|
}
|
|
26
26
|
|
|
27
|
-
|
|
27
|
+
access(all) view fun canWithdrawAmount(_ amount: UFix64): Bool {
|
|
28
28
|
return amount + self.allowanceUsed <= self.allowance
|
|
29
29
|
}
|
|
30
30
|
|
|
31
|
-
|
|
31
|
+
access(FungibleToken.Withdraw) fun markAmountWithdrawn(_ amount: UFix64) {
|
|
32
32
|
self.allowanceUsed = self.allowanceUsed + amount
|
|
33
33
|
}
|
|
34
34
|
|
|
35
|
-
|
|
35
|
+
access(all) fun getDetails(): {String: AnyStruct} {
|
|
36
36
|
return {
|
|
37
37
|
"allowance": self.allowance,
|
|
38
38
|
"allowanceUsed": self.allowanceUsed
|
|
@@ -44,31 +44,31 @@ pub contract ScopedFTProviders {
|
|
|
44
44
|
//
|
|
45
45
|
// A ScopedFTProvider is a wrapped FungibleTokenProvider with
|
|
46
46
|
// filters that can be defined by anyone using the ScopedFTProvider.
|
|
47
|
-
|
|
48
|
-
access(self) let provider: Capability
|
|
47
|
+
access(all) resource ScopedFTProvider: FungibleToken.Provider {
|
|
48
|
+
access(self) let provider: Capability<auth(FungibleToken.Withdraw) &{FungibleToken.Provider}>
|
|
49
49
|
access(self) var filters: [{FTFilter}]
|
|
50
50
|
|
|
51
51
|
// block timestamp that this provider can no longer be used after
|
|
52
52
|
access(self) let expiration: UFix64?
|
|
53
53
|
|
|
54
|
-
|
|
54
|
+
access(all) init(provider: Capability<auth(FungibleToken.Withdraw) &{FungibleToken.Provider}>, filters: [{FTFilter}], expiration: UFix64?) {
|
|
55
55
|
self.provider = provider
|
|
56
56
|
self.filters = filters
|
|
57
57
|
self.expiration = expiration
|
|
58
58
|
}
|
|
59
59
|
|
|
60
|
-
|
|
60
|
+
access(all) fun check(): Bool {
|
|
61
61
|
return self.provider.check()
|
|
62
62
|
}
|
|
63
63
|
|
|
64
|
-
|
|
64
|
+
access(all) view fun isExpired(): Bool {
|
|
65
65
|
if let expiration = self.expiration {
|
|
66
66
|
return getCurrentBlock().timestamp >= expiration
|
|
67
67
|
}
|
|
68
68
|
return false
|
|
69
69
|
}
|
|
70
70
|
|
|
71
|
-
|
|
71
|
+
access(all) view fun canWithdraw(_ amount: UFix64): Bool {
|
|
72
72
|
if self.isExpired() {
|
|
73
73
|
return false
|
|
74
74
|
}
|
|
@@ -82,7 +82,11 @@ pub contract ScopedFTProviders {
|
|
|
82
82
|
return true
|
|
83
83
|
}
|
|
84
84
|
|
|
85
|
-
|
|
85
|
+
access(all) view fun isAvailableToWithdraw(amount: UFix64): Bool {
|
|
86
|
+
return self.canWithdraw(amount)
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
access(FungibleToken.Withdraw | FungibleToken.Withdraw) fun withdraw(amount: UFix64): @{FungibleToken.Vault} {
|
|
86
90
|
pre {
|
|
87
91
|
!self.isExpired(): "provider has expired"
|
|
88
92
|
}
|
|
@@ -100,7 +104,7 @@ pub contract ScopedFTProviders {
|
|
|
100
104
|
return <-self.provider.borrow()!.withdraw(amount: amount)
|
|
101
105
|
}
|
|
102
106
|
|
|
103
|
-
|
|
107
|
+
access(all) fun getDetails(): [{String: AnyStruct}] {
|
|
104
108
|
let details: [{String: AnyStruct}] = []
|
|
105
109
|
for filter in self.filters {
|
|
106
110
|
details.append(filter.getDetails())
|
|
@@ -110,11 +114,12 @@ pub contract ScopedFTProviders {
|
|
|
110
114
|
}
|
|
111
115
|
}
|
|
112
116
|
|
|
113
|
-
|
|
114
|
-
provider: Capability
|
|
117
|
+
access(all) fun createScopedFTProvider(
|
|
118
|
+
provider: Capability<auth(FungibleToken.Withdraw) &{FungibleToken.Provider}>,
|
|
115
119
|
filters: [{FTFilter}],
|
|
116
120
|
expiration: UFix64?
|
|
117
121
|
): @ScopedFTProvider {
|
|
118
122
|
return <- create ScopedFTProvider(provider: provider, filters: filters, expiration: expiration)
|
|
119
123
|
}
|
|
120
124
|
}
|
|
125
|
+
|
|
@@ -32,11 +32,11 @@ access(all) contract ScopedNFTProviders {
|
|
|
32
32
|
}
|
|
33
33
|
|
|
34
34
|
access(all) fun canWithdraw(_ nft: &{NonFungibleToken.NFT}): Bool {
|
|
35
|
-
return self.ids[nft.
|
|
35
|
+
return self.ids[nft.id] != nil && self.ids[nft.id] == true
|
|
36
36
|
}
|
|
37
37
|
|
|
38
38
|
access(NonFungibleToken.Withdraw) fun markWithdrawn(_ nft: &{NonFungibleToken.NFT}) {
|
|
39
|
-
self.ids[nft.
|
|
39
|
+
self.ids[nft.id] = false
|
|
40
40
|
}
|
|
41
41
|
|
|
42
42
|
access(all) fun getDetails(): {String: AnyStruct} {
|
|
@@ -121,7 +121,7 @@ access(all) contract ScopedNFTProviders {
|
|
|
121
121
|
return self.provider.check()
|
|
122
122
|
}
|
|
123
123
|
|
|
124
|
-
access(NonFungibleToken.Withdraw) fun withdraw(withdrawID: UInt64): @{NonFungibleToken.NFT} {
|
|
124
|
+
access(NonFungibleToken.Withdraw | NonFungibleToken.Owner) fun withdraw(withdrawID: UInt64): @{NonFungibleToken.NFT} {
|
|
125
125
|
pre {
|
|
126
126
|
!self.isExpired(): "provider has expired"
|
|
127
127
|
}
|
|
@@ -160,3 +160,4 @@ access(all) contract ScopedNFTProviders {
|
|
|
160
160
|
return <- create ScopedNFTProvider(provider: provider, filters: filters, expiration: expiration)
|
|
161
161
|
}
|
|
162
162
|
}
|
|
163
|
+
|
|
@@ -125,7 +125,7 @@ access(all) contract LostAndFound {
|
|
|
125
125
|
if self.type.isSubtype(of: Type<@{NonFungibleToken.NFT}>()) {
|
|
126
126
|
let ref = (&self.item as &AnyResource?)!
|
|
127
127
|
let nft = ref as! &{NonFungibleToken.NFT}
|
|
128
|
-
return nft.
|
|
128
|
+
return nft.id
|
|
129
129
|
}
|
|
130
130
|
return nil
|
|
131
131
|
}
|
|
@@ -135,7 +135,7 @@ access(all) contract LostAndFound {
|
|
|
135
135
|
if self.type.isSubtype(of: Type<@{FungibleToken.Vault}>()) {
|
|
136
136
|
let ref = (&self.item as &AnyResource?)!
|
|
137
137
|
let ft = ref as! &{FungibleToken.Vault}
|
|
138
|
-
return ft.
|
|
138
|
+
return ft.balance
|
|
139
139
|
}
|
|
140
140
|
return nil
|
|
141
141
|
}
|
|
@@ -391,8 +391,8 @@ access(all) contract LostAndFound {
|
|
|
391
391
|
}
|
|
392
392
|
}
|
|
393
393
|
|
|
394
|
-
access(contract) fun getFlowProvider(): auth(FungibleToken.
|
|
395
|
-
return self.account.storage.borrow<auth(FungibleToken.
|
|
394
|
+
access(contract) fun getFlowProvider(): auth(FungibleToken.Withdraw) &FlowToken.Vault {
|
|
395
|
+
return self.account.storage.borrow<auth(FungibleToken.Withdraw) &FlowToken.Vault>(from: /storage/flowTokenVault)!
|
|
396
396
|
}
|
|
397
397
|
|
|
398
398
|
// ShelfManager is a light-weight wrapper to get our shelves into storage.
|
|
@@ -422,7 +422,7 @@ access(all) contract LostAndFound {
|
|
|
422
422
|
item: @AnyResource,
|
|
423
423
|
memo: String?,
|
|
424
424
|
display: MetadataViews.Display?,
|
|
425
|
-
storagePayment: auth(FungibleToken.
|
|
425
|
+
storagePayment: auth(FungibleToken.Withdraw) &{FungibleToken.Vault},
|
|
426
426
|
flowTokenRepayment: Capability<&FlowToken.Vault>?
|
|
427
427
|
) : UInt64 {
|
|
428
428
|
pre {
|
|
@@ -598,7 +598,7 @@ access(all) contract LostAndFound {
|
|
|
598
598
|
init(_ flowTokenRepayment: Capability<&FlowToken.Vault>, lowBalanceThreshold: UFix64?) {
|
|
599
599
|
self.flowTokenRepayment = flowTokenRepayment
|
|
600
600
|
|
|
601
|
-
let vault <- FlowToken.createEmptyVault()
|
|
601
|
+
let vault <- FlowToken.createEmptyVault(vaultType: Type<@FlowToken.Vault>())
|
|
602
602
|
self.flowTokenVault <- vault
|
|
603
603
|
self.lowBalanceThreshold = lowBalanceThreshold
|
|
604
604
|
}
|
|
@@ -713,7 +713,7 @@ access(all) contract LostAndFound {
|
|
|
713
713
|
item: @AnyResource,
|
|
714
714
|
memo: String?,
|
|
715
715
|
display: MetadataViews.Display?,
|
|
716
|
-
storagePayment: auth(FungibleToken.
|
|
716
|
+
storagePayment: auth(FungibleToken.Withdraw) &{FungibleToken.Vault},
|
|
717
717
|
flowTokenRepayment: Capability<&FlowToken.Vault>?
|
|
718
718
|
) : UInt64 {
|
|
719
719
|
pre {
|
|
@@ -730,7 +730,7 @@ access(all) contract LostAndFound {
|
|
|
730
730
|
cap: Capability,
|
|
731
731
|
memo: String?,
|
|
732
732
|
display: MetadataViews.Display?,
|
|
733
|
-
storagePayment: auth(FungibleToken.
|
|
733
|
+
storagePayment: auth(FungibleToken.Withdraw) &{FungibleToken.Vault},
|
|
734
734
|
flowTokenRepayment: Capability<&FlowToken.Vault>
|
|
735
735
|
) {
|
|
736
736
|
if cap.check<&{NonFungibleToken.Collection}>() {
|