@@ -54,28 +54,22 @@ public class ERC1594: IERC1594, ERC20BaseProperties {
54
54
}
55
55
56
56
public func getBalance( account: EthereumAddress ) async throws -> BigUInt {
57
- let contract = self . contract
58
57
transaction. callOnBlock = . latest
59
58
let result = try await contract. createReadOperation ( " balanceOf " , parameters: [ account] as [ AnyObject ] , extraData: Data ( ) ) !. callContractMethod ( )
60
59
guard let res = result [ " 0 " ] as? BigUInt else { throw Web3Error . processingError ( desc: " Failed to get result of expected type from the Ethereum node " ) }
61
60
return res
62
61
}
63
62
64
63
public func getAllowance( originalOwner: EthereumAddress , delegate: EthereumAddress ) async throws -> BigUInt {
65
- let contract = self . contract
66
64
transaction. callOnBlock = . latest
67
65
let result = try await contract. createReadOperation ( " allowance " , parameters: [ originalOwner, delegate] as [ AnyObject ] , extraData: Data ( ) ) !. callContractMethod ( )
68
66
guard let res = result [ " 0 " ] as? BigUInt else { throw Web3Error . processingError ( desc: " Failed to get result of expected type from the Ethereum node " ) }
69
67
return res
70
68
}
71
69
72
70
public func transfer( from: EthereumAddress , to: EthereumAddress , amount: String ) async throws -> WriteOperation {
73
- let contract = self . contract
74
-
75
- self . transaction. from = from
76
- self . transaction. to = self . address
77
71
transaction. callOnBlock = . latest
78
-
72
+ updateTransactionAndContract ( from : from )
79
73
// get the decimals manually
80
74
let callResult = try await contract. createReadOperation ( " decimals " ) !. callContractMethod ( )
81
75
var decimals = BigUInt ( 0 )
@@ -92,12 +86,8 @@ public class ERC1594: IERC1594, ERC20BaseProperties {
92
86
}
93
87
94
88
public func transferFrom( from: EthereumAddress , to: EthereumAddress , originalOwner: EthereumAddress , amount: String ) async throws -> WriteOperation {
95
- let contract = self . contract
96
-
97
- self . transaction. from = from
98
- self . transaction. to = self . address
99
89
transaction. callOnBlock = . latest
100
-
90
+ updateTransactionAndContract ( from : from )
101
91
// get the decimals manually
102
92
let callResult = try await contract. createReadOperation ( " decimals " ) !. callContractMethod ( )
103
93
var decimals = BigUInt ( 0 )
@@ -115,12 +105,8 @@ public class ERC1594: IERC1594, ERC20BaseProperties {
115
105
}
116
106
117
107
public func setAllowance( from: EthereumAddress , to: EthereumAddress , newAmount: String ) async throws -> WriteOperation {
118
- let contract = self . contract
119
-
120
- self . transaction. from = from
121
- self . transaction. to = self . address
122
108
transaction. callOnBlock = . latest
123
-
109
+ updateTransactionAndContract ( from : from )
124
110
// get the decimals manually
125
111
let callResult = try await contract. createReadOperation ( " decimals " ) !. callContractMethod ( )
126
112
var decimals = BigUInt ( 0 )
@@ -138,21 +124,15 @@ public class ERC1594: IERC1594, ERC20BaseProperties {
138
124
}
139
125
140
126
public func totalSupply( ) async throws -> BigUInt {
141
- let contract = self . contract
142
-
143
127
transaction. callOnBlock = . latest
144
128
let result = try await contract. createReadOperation ( " totalSupply " , parameters: [ AnyObject] ( ) , extraData: Data ( ) ) !. callContractMethod ( )
145
129
guard let res = result [ " 0 " ] as? BigUInt else { throw Web3Error . processingError ( desc: " Failed to get result of expected type from the Ethereum node " ) }
146
130
return res
147
131
}
148
132
149
133
public func approve( from: EthereumAddress , spender: EthereumAddress , amount: String ) async throws -> WriteOperation {
150
- let contract = self . contract
151
-
152
- self . transaction. from = from
153
- self . transaction. to = self . address
154
134
transaction. callOnBlock = . latest
155
-
135
+ updateTransactionAndContract ( from : from )
156
136
// get the decimals manually
157
137
let callResult = try await contract. createReadOperation ( " decimals " ) !. callContractMethod ( )
158
138
var decimals = BigUInt ( 0 )
@@ -171,12 +151,8 @@ public class ERC1594: IERC1594, ERC20BaseProperties {
171
151
172
152
// ERC1594
173
153
public func transferWithData( from: EthereumAddress , to: EthereumAddress , amount: String , data: [ UInt8 ] ) async throws -> WriteOperation {
174
- let contract = self . contract
175
-
176
- self . transaction. from = from
177
- self . transaction. to = self . address
178
154
transaction. callOnBlock = . latest
179
-
155
+ updateTransactionAndContract ( from : from )
180
156
// get the decimals manually
181
157
let callResult = try await contract. createReadOperation ( " decimals " ) !. callContractMethod ( )
182
158
var decimals = BigUInt ( 0 )
@@ -194,12 +170,8 @@ public class ERC1594: IERC1594, ERC20BaseProperties {
194
170
}
195
171
196
172
public func transferFromWithData( from: EthereumAddress , to: EthereumAddress , originalOwner: EthereumAddress , amount: String , data: [ UInt8 ] ) async throws -> WriteOperation {
197
- let contract = self . contract
198
-
199
- self . transaction. from = from
200
- self . transaction. to = self . address
201
173
transaction. callOnBlock = . latest
202
-
174
+ updateTransactionAndContract ( from : from )
203
175
// get the decimals manually
204
176
let callResult = try await contract. createReadOperation ( " decimals " ) !. callContractMethod ( )
205
177
var decimals = BigUInt ( 0 )
@@ -217,20 +189,15 @@ public class ERC1594: IERC1594, ERC20BaseProperties {
217
189
}
218
190
219
191
public func isIssuable( ) async throws -> Bool {
220
- let contract = self . contract
221
192
transaction. callOnBlock = . latest
222
193
let result = try await contract. createReadOperation ( " isIssuable " , parameters: [ AnyObject] ( ) , extraData: Data ( ) ) !. callContractMethod ( )
223
194
guard let res = result [ " 0 " ] as? Bool else { throw Web3Error . processingError ( desc: " Failed to get result of expected type from the Ethereum node " ) }
224
195
return res
225
196
}
226
197
227
198
public func issue( from: EthereumAddress , tokenHolder: EthereumAddress , amount: String , data: [ UInt8 ] ) async throws -> WriteOperation {
228
- let contract = self . contract
229
-
230
- self . transaction. from = from
231
- self . transaction. to = self . address
232
199
transaction. callOnBlock = . latest
233
-
200
+ updateTransactionAndContract ( from : from )
234
201
// get the decimals manually
235
202
let callResult = try await contract. createReadOperation ( " decimals " ) !. callContractMethod ( )
236
203
var decimals = BigUInt ( 0 )
@@ -248,12 +215,8 @@ public class ERC1594: IERC1594, ERC20BaseProperties {
248
215
}
249
216
250
217
public func redeem( from: EthereumAddress , amount: String , data: [ UInt8 ] ) async throws -> WriteOperation {
251
- let contract = self . contract
252
-
253
- self . transaction. from = from
254
- self . transaction. to = self . address
255
218
transaction. callOnBlock = . latest
256
-
219
+ updateTransactionAndContract ( from : from )
257
220
// get the decimals manually
258
221
let callResult = try await contract. createReadOperation ( " decimals " ) !. callContractMethod ( )
259
222
var decimals = BigUInt ( 0 )
@@ -271,12 +234,8 @@ public class ERC1594: IERC1594, ERC20BaseProperties {
271
234
}
272
235
273
236
public func redeemFrom( from: EthereumAddress , tokenHolder: EthereumAddress , amount: String , data: [ UInt8 ] ) async throws -> WriteOperation {
274
- let contract = self . contract
275
-
276
- self . transaction. from = from
277
- self . transaction. to = self . address
278
237
transaction. callOnBlock = . latest
279
-
238
+ updateTransactionAndContract ( from : from )
280
239
// get the decimals manually
281
240
let callResult = try await contract. createReadOperation ( " decimals " ) !. callContractMethod ( )
282
241
var decimals = BigUInt ( 0 )
@@ -294,7 +253,6 @@ public class ERC1594: IERC1594, ERC20BaseProperties {
294
253
}
295
254
296
255
public func canTransfer( to: EthereumAddress , amount: String , data: [ UInt8 ] ) async throws -> ( [ UInt8 ] , Data ) {
297
- let contract = self . contract
298
256
transaction. callOnBlock = . latest
299
257
300
258
// get the decimals manually
@@ -315,7 +273,6 @@ public class ERC1594: IERC1594, ERC20BaseProperties {
315
273
}
316
274
317
275
public func canTransferFrom( originalOwner: EthereumAddress , to: EthereumAddress , amount: String , data: [ UInt8 ] ) async throws -> ( [ UInt8 ] , Data ) {
318
- let contract = self . contract
319
276
transaction. callOnBlock = . latest
320
277
321
278
// get the decimals manually
@@ -335,3 +292,15 @@ public class ERC1594: IERC1594, ERC20BaseProperties {
335
292
return res
336
293
}
337
294
}
295
+
296
+ // MARK: - Private
297
+
298
+ extension ERC1594 {
299
+
300
+ private func updateTransactionAndContract( from: EthereumAddress ) {
301
+ transaction. from = from
302
+ transaction. to = address
303
+ contract. transaction = transaction
304
+ }
305
+
306
+ }
0 commit comments