@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.
@@ -103,4 +103,4 @@ access(all) contract AddressUtils {
103
103
  access(all) fun currentNetwork(): String {
104
104
  return self.getNetworkFromAddress(self.account.address) ?? panic("unknown network!")
105
105
  }
106
- }
106
+ }
@@ -1,120 +1,68 @@
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
- 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
- // ScopedFTProvider
44
- //
45
- // A ScopedFTProvider is a wrapped FungibleTokenProvider with
46
- // filters that can be defined by anyone using the ScopedFTProvider.
47
- access(all) resource ScopedFTProvider: FungibleToken.Provider {
48
- access(self) let provider: Capability<auth(FungibleToken.Withdraw) &{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?
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
- access(all) init(provider: Capability<auth(FungibleToken.Withdraw) &{FungibleToken.Provider}>, filters: [{FTFilter}], expiration: UFix64?) {
55
- self.provider = provider
56
- self.filters = filters
57
- self.expiration = expiration
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
- access(all) fun check(): Bool {
61
- return self.provider.check()
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
- access(all) view fun isExpired(): Bool {
65
- if let expiration = self.expiration {
66
- return getCurrentBlock().timestamp >= expiration
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
- access(all) 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
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
- access(FungibleToken.Withdraw) 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)
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
- access(all) 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
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
- access(all) fun createScopedFTProvider(
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
- 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}
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
- pub struct AllowanceFilter: FTFilter {
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
- pub fun canWithdrawAmount(_ amount: UFix64): Bool {
27
+ access(all) view fun canWithdrawAmount(_ amount: UFix64): Bool {
28
28
  return amount + self.allowanceUsed <= self.allowance
29
29
  }
30
30
 
31
- pub fun markAmountWithdrawn(_ amount: UFix64) {
31
+ access(FungibleToken.Withdraw) fun markAmountWithdrawn(_ amount: UFix64) {
32
32
  self.allowanceUsed = self.allowanceUsed + amount
33
33
  }
34
34
 
35
- pub fun getDetails(): {String: AnyStruct} {
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
- pub resource ScopedFTProvider: FungibleToken.Provider {
48
- access(self) let provider: Capability<&{FungibleToken.Provider}>
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
- pub init(provider: Capability<&{FungibleToken.Provider}>, filters: [{FTFilter}], expiration: UFix64?) {
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
- pub fun check(): Bool {
60
+ access(all) fun check(): Bool {
61
61
  return self.provider.check()
62
62
  }
63
63
 
64
- pub fun isExpired(): Bool {
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
- pub fun canWithdraw(_ amount: UFix64): Bool {
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
- pub fun withdraw(amount: UFix64): @FungibleToken.Vault {
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
- pub fun getDetails(): [{String: AnyStruct}] {
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
- pub fun createScopedFTProvider(
114
- provider: Capability<&{FungibleToken.Provider}>,
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.getID()] != nil && self.ids[nft.getID()] == true
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.getID()] = false
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
+
@@ -81,4 +81,4 @@ access(all) contract StringUtils {
81
81
  access(all) fun join(_ strs: [String], _ separator: String): String {
82
82
  return String.join(strs, separator: separator)
83
83
  }
84
- }
84
+ }
@@ -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.getID()
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.getBalance()
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.Withdrawable) &FlowToken.Vault {
395
- return self.account.storage.borrow<auth(FungibleToken.Withdrawable) &FlowToken.Vault>(from: /storage/flowTokenVault)!
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.Withdrawable) &{FungibleToken.Vault},
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.Withdrawable) &{FungibleToken.Vault},
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.Withdrawable) &{FungibleToken.Vault},
733
+ storagePayment: auth(FungibleToken.Withdraw) &{FungibleToken.Vault},
734
734
  flowTokenRepayment: Capability<&FlowToken.Vault>
735
735
  ) {
736
736
  if cap.check<&{NonFungibleToken.Collection}>() {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@flowtyio/flow-contracts",
3
- "version": "0.1.0-beta.6",
3
+ "version": "0.1.0-beta.7",
4
4
  "main": "index.json",
5
5
  "description": "An NPM package for common flow contracts",
6
6
  "author": "flowtyio",