@@ -89,27 +89,22 @@ public class ERC1376: IERC1376, ERC20BaseProperties {
89
89
}
90
90
91
91
public func getBalance( account: EthereumAddress ) async throws -> BigUInt {
92
- let contract = self . contract
93
- self . transaction. callOnBlock = . latest
94
- let result = try await contract. createReadOperation ( " balanceOf " , parameters: [ account] as [ AnyObject ] , extraData: Data ( ) ) !. callContractMethod ( )
92
+ transaction. callOnBlock = . latest
93
+ let result = try await contract. createReadOperation ( " balanceOf " , parameters: [ account] as [ AnyObject ] , extraData: Data ( ) ) !. callContractMethod ( )
95
94
guard let res = result [ " 0 " ] as? BigUInt else { throw Web3Error . processingError ( desc: " Failed to get result of expected type from the Ethereum node " ) }
96
95
return res
97
96
}
98
97
99
98
public func getAllowance( originalOwner: EthereumAddress , delegate: EthereumAddress ) async throws -> BigUInt {
100
- let contract = self . contract
101
- self . transaction. callOnBlock = . latest
102
- let result = try await contract. createReadOperation ( " allowance " , parameters: [ originalOwner, delegate] as [ AnyObject ] , extraData: Data ( ) ) !. callContractMethod ( )
99
+ transaction. callOnBlock = . latest
100
+ let result = try await contract. createReadOperation ( " allowance " , parameters: [ originalOwner, delegate] as [ AnyObject ] , extraData: Data ( ) ) !. callContractMethod ( )
103
101
guard let res = result [ " 0 " ] as? BigUInt else { throw Web3Error . processingError ( desc: " Failed to get result of expected type from the Ethereum node " ) }
104
102
return res
105
103
}
106
104
107
105
public func transfer( from: EthereumAddress , to: EthereumAddress , amount: String ) async throws -> WriteOperation {
108
- let contract = self . contract
109
- self . transaction. from = from
110
- self . transaction. to = self . address
111
- self . transaction. callOnBlock = . latest
112
-
106
+ transaction. callOnBlock = . latest
107
+ updateTransactionAndContract ( from: from)
113
108
// get the decimals manually
114
109
let callResult = try await contract. createReadOperation ( " decimals " ) !. callContractMethod ( )
115
110
var decimals = BigUInt ( 0 )
@@ -121,16 +116,13 @@ public class ERC1376: IERC1376, ERC20BaseProperties {
121
116
guard let value = Utilities . parseToBigUInt ( amount, decimals: intDecimals) else {
122
117
throw Web3Error . inputError ( desc: " Can not parse inputted amount " )
123
118
}
124
- let tx = contract. createWriteOperation ( " transfer " , parameters: [ to, value] as [ AnyObject ] ) !
119
+ let tx = contract. createWriteOperation ( " transfer " , parameters: [ to, value] as [ AnyObject ] ) !
125
120
return tx
126
121
}
127
122
128
123
public func transferFrom( from: EthereumAddress , to: EthereumAddress , originalOwner: EthereumAddress , amount: String ) async throws -> WriteOperation {
129
- let contract = self . contract
130
- self . transaction. from = from
131
- self . transaction. to = self . address
132
- self . transaction. callOnBlock = . latest
133
-
124
+ transaction. callOnBlock = . latest
125
+ updateTransactionAndContract ( from: from)
134
126
// get the decimals manually
135
127
let callResult = try await contract. createReadOperation ( " decimals " ) !. callContractMethod ( )
136
128
var decimals = BigUInt ( 0 )
@@ -143,16 +135,13 @@ public class ERC1376: IERC1376, ERC20BaseProperties {
143
135
throw Web3Error . inputError ( desc: " Can not parse inputted amount " )
144
136
}
145
137
146
- let tx = contract. createWriteOperation ( " transferFrom " , parameters: [ originalOwner, to, value] as [ AnyObject ] ) !
138
+ let tx = contract. createWriteOperation ( " transferFrom " , parameters: [ originalOwner, to, value] as [ AnyObject ] ) !
147
139
return tx
148
140
}
149
141
150
142
public func setAllowance( from: EthereumAddress , to: EthereumAddress , newAmount: String ) async throws -> WriteOperation {
151
- let contract = self . contract
152
- self . transaction. from = from
153
- self . transaction. to = self . address
154
- self . transaction. callOnBlock = . latest
155
-
143
+ transaction. callOnBlock = . latest
144
+ updateTransactionAndContract ( from: from)
156
145
// get the decimals manually
157
146
let callResult = try await contract. createReadOperation ( " decimals " ) !. callContractMethod ( )
158
147
var decimals = BigUInt ( 0 )
@@ -165,16 +154,13 @@ public class ERC1376: IERC1376, ERC20BaseProperties {
165
154
throw Web3Error . inputError ( desc: " Can not parse inputted amount " )
166
155
}
167
156
168
- let tx = contract. createWriteOperation ( " setAllowance " , parameters: [ to, value] as [ AnyObject ] ) !
157
+ let tx = contract. createWriteOperation ( " setAllowance " , parameters: [ to, value] as [ AnyObject ] ) !
169
158
return tx
170
159
}
171
160
172
161
public func approve( from: EthereumAddress , spender: EthereumAddress , amount: String ) async throws -> WriteOperation {
173
- let contract = self . contract
174
- self . transaction. from = from
175
- self . transaction. to = self . address
176
- self . transaction. callOnBlock = . latest
177
-
162
+ transaction. callOnBlock = . latest
163
+ updateTransactionAndContract ( from: from)
178
164
// get the decimals manually
179
165
let callResult = try await contract. createReadOperation ( " decimals " ) !. callContractMethod ( )
180
166
var decimals = BigUInt ( 0 )
@@ -187,24 +173,20 @@ public class ERC1376: IERC1376, ERC20BaseProperties {
187
173
throw Web3Error . inputError ( desc: " Can not parse inputted amount " )
188
174
}
189
175
190
- let tx = contract. createWriteOperation ( " approve " , parameters: [ spender, value] as [ AnyObject ] ) !
176
+ let tx = contract. createWriteOperation ( " approve " , parameters: [ spender, value] as [ AnyObject ] ) !
191
177
return tx
192
178
}
193
179
194
180
public func totalSupply( ) async throws -> BigUInt {
195
- let contract = self . contract
196
- self . transaction. callOnBlock = . latest
197
- let result = try await contract. createReadOperation ( " totalSupply " , parameters: [ AnyObject] ( ) , extraData: Data ( ) ) !. callContractMethod ( )
181
+ transaction. callOnBlock = . latest
182
+ let result = try await contract. createReadOperation ( " totalSupply " , parameters: [ AnyObject] ( ) , extraData: Data ( ) ) !. callContractMethod ( )
198
183
guard let res = result [ " 0 " ] as? BigUInt else { throw Web3Error . processingError ( desc: " Failed to get result of expected type from the Ethereum node " ) }
199
184
return res
200
185
}
201
186
202
187
func approve( from: EthereumAddress , spender: EthereumAddress , expectedValue: String , newValue: String ) async throws -> WriteOperation {
203
- let contract = self . contract
204
- self . transaction. from = from
205
- self . transaction. to = self . address
206
- self . transaction. callOnBlock = . latest
207
-
188
+ transaction. callOnBlock = . latest
189
+ updateTransactionAndContract ( from: from)
208
190
// get the decimals manually
209
191
let callResult = try await contract. createReadOperation ( " decimals " ) !. callContractMethod ( )
210
192
var decimals = BigUInt ( 0 )
@@ -220,16 +202,13 @@ public class ERC1376: IERC1376, ERC20BaseProperties {
220
202
throw Web3Error . inputError ( desc: " Can not parse inputted amount " )
221
203
}
222
204
223
- let tx = contract. createWriteOperation ( " approve " , parameters: [ spender, eValue, nValue] as [ AnyObject ] ) !
205
+ let tx = contract. createWriteOperation ( " approve " , parameters: [ spender, eValue, nValue] as [ AnyObject ] ) !
224
206
return tx
225
207
}
226
208
227
209
func increaseAllowance( from: EthereumAddress , spender: EthereumAddress , value: String ) async throws -> WriteOperation {
228
- let contract = self . contract
229
- self . transaction. from = from
230
- self . transaction. to = self . address
231
- self . transaction. callOnBlock = . latest
232
-
210
+ transaction. callOnBlock = . latest
211
+ updateTransactionAndContract ( from: from)
233
212
// get the decimals manually
234
213
let callResult = try await contract. createReadOperation ( " decimals " ) !. callContractMethod ( )
235
214
var decimals = BigUInt ( 0 )
@@ -242,16 +221,13 @@ public class ERC1376: IERC1376, ERC20BaseProperties {
242
221
throw Web3Error . inputError ( desc: " Can not parse inputted amount " )
243
222
}
244
223
245
- let tx = contract. createWriteOperation ( " increaseAllowance " , parameters: [ spender, amount] as [ AnyObject ] ) !
224
+ let tx = contract. createWriteOperation ( " increaseAllowance " , parameters: [ spender, amount] as [ AnyObject ] ) !
246
225
return tx
247
226
}
248
227
249
228
func decreaseAllowance( from: EthereumAddress , spender: EthereumAddress , value: String , strict: Bool ) async throws -> WriteOperation {
250
- let contract = self . contract
251
- self . transaction. from = from
252
- self . transaction. to = self . address
253
- self . transaction. callOnBlock = . latest
254
-
229
+ transaction. callOnBlock = . latest
230
+ updateTransactionAndContract ( from: from)
255
231
// get the decimals manually
256
232
let callResult = try await contract. createReadOperation ( " decimals " ) !. callContractMethod ( )
257
233
var decimals = BigUInt ( 0 )
@@ -264,33 +240,26 @@ public class ERC1376: IERC1376, ERC20BaseProperties {
264
240
throw Web3Error . inputError ( desc: " Can not parse inputted amount " )
265
241
}
266
242
267
- let tx = contract. createWriteOperation ( " decreaseAllowance " , parameters: [ spender, amount, strict] as [ AnyObject ] ) !
243
+ let tx = contract. createWriteOperation ( " decreaseAllowance " , parameters: [ spender, amount, strict] as [ AnyObject ] ) !
268
244
return tx
269
245
}
270
246
271
247
func setERC20ApproveChecking( from: EthereumAddress , approveChecking: Bool ) throws -> WriteOperation {
272
- let contract = self . contract
273
- self . transaction. from = from
274
- self . transaction. to = self . address
275
-
276
- let tx = contract. createWriteOperation ( " setERC20ApproveChecking " , parameters: [ approveChecking] as [ AnyObject ] ) !
248
+ updateTransactionAndContract ( from: from)
249
+ let tx = contract. createWriteOperation ( " setERC20ApproveChecking " , parameters: [ approveChecking] as [ AnyObject ] ) !
277
250
return tx
278
251
}
279
252
280
253
func spendableAllowance( owner: EthereumAddress , spender: EthereumAddress ) async throws -> BigUInt {
281
- let contract = self . contract
282
- self . transaction. callOnBlock = . latest
283
- let result = try await contract. createReadOperation ( " spendableAllowance " , parameters: [ owner, spender] as [ AnyObject ] , extraData: Data ( ) ) !. callContractMethod ( )
254
+ transaction. callOnBlock = . latest
255
+ let result = try await contract. createReadOperation ( " spendableAllowance " , parameters: [ owner, spender] as [ AnyObject ] , extraData: Data ( ) ) !. callContractMethod ( )
284
256
guard let res = result [ " 0 " ] as? BigUInt else { throw Web3Error . processingError ( desc: " Failed to get result of expected type from the Ethereum node " ) }
285
257
return res
286
258
}
287
259
288
260
func transfer( from: EthereumAddress , data: String ) async throws -> WriteOperation {
289
- let contract = self . contract
290
- self . transaction. from = from
291
- self . transaction. to = self . address
292
- self . transaction. callOnBlock = . latest
293
-
261
+ transaction. callOnBlock = . latest
262
+ updateTransactionAndContract ( from: from)
294
263
// get the decimals manually
295
264
let callResult = try await contract. createReadOperation ( " decimals " ) !. callContractMethod ( )
296
265
var decimals = BigUInt ( 0 )
@@ -302,16 +271,13 @@ public class ERC1376: IERC1376, ERC20BaseProperties {
302
271
guard let value = Utilities . parseToBigUInt ( data, decimals: intDecimals) else {
303
272
throw Web3Error . inputError ( desc: " Can not parse inputted amount " )
304
273
}
305
- let tx = contract. createWriteOperation ( " transfer " , parameters: [ value] as [ AnyObject ] ) !
274
+ let tx = contract. createWriteOperation ( " transfer " , parameters: [ value] as [ AnyObject ] ) !
306
275
return tx
307
276
}
308
277
309
278
func transferAndCall( from: EthereumAddress , to: EthereumAddress , value: String , data: [ UInt8 ] ) async throws -> WriteOperation {
310
- let contract = self . contract
311
- self . transaction. from = from
312
- self . transaction. to = self . address
313
- self . transaction. callOnBlock = . latest
314
-
279
+ transaction. callOnBlock = . latest
280
+ updateTransactionAndContract ( from: from)
315
281
// get the decimals manually
316
282
let callResult = try await contract. createReadOperation ( " decimals " ) !. callContractMethod ( )
317
283
var decimals = BigUInt ( 0 )
@@ -328,29 +294,22 @@ public class ERC1376: IERC1376, ERC20BaseProperties {
328
294
}
329
295
330
296
func nonceOf( owner: EthereumAddress ) async throws -> BigUInt {
331
- let contract = self . contract
332
- self . transaction. callOnBlock = . latest
297
+ transaction. callOnBlock = . latest
333
298
let result = try await contract. createReadOperation ( " nonceOf " , parameters: [ owner] as [ AnyObject ] , extraData: Data ( ) ) !. callContractMethod ( )
334
299
guard let res = result [ " 0 " ] as? BigUInt else { throw Web3Error . processingError ( desc: " Failed to get result of expected type from the Ethereum node " ) }
335
300
return res
336
301
}
337
302
338
303
func increaseNonce( from: EthereumAddress ) throws -> WriteOperation {
339
- let contract = self . contract
340
- self . transaction. from = from
341
- self . transaction. to = self . address
342
- self . transaction. callOnBlock = . latest
343
-
304
+ transaction. callOnBlock = . latest
305
+ updateTransactionAndContract ( from: from)
344
306
let tx = contract. createWriteOperation ( " increaseNonce " , parameters: [ ] as [ AnyObject ] ) !
345
307
return tx
346
308
}
347
309
348
310
func delegateTransferAndCall( from: EthereumAddress , nonce: BigUInt , fee: BigUInt , gasAmount: BigUInt , to: EthereumAddress , value: String , data: [ UInt8 ] , mode: IERC1376DelegateMode , v: UInt8 , r: Data , s: Data ) async throws -> WriteOperation {
349
- let contract = self . contract
350
- self . transaction. from = from
351
- self . transaction. to = self . address
352
- self . transaction. callOnBlock = . latest
353
-
311
+ transaction. callOnBlock = . latest
312
+ updateTransactionAndContract ( from: from)
354
313
// get the decimals manually
355
314
let callResult = try await contract. createReadOperation ( " decimals " ) !. callContractMethod ( )
356
315
var decimals = BigUInt ( 0 )
@@ -370,46 +329,46 @@ public class ERC1376: IERC1376, ERC20BaseProperties {
370
329
}
371
330
372
331
func directDebit( debtor: EthereumAddress , receiver: EthereumAddress ) async throws -> DirectDebit {
373
- let contract = self . contract
374
- self . transaction. callOnBlock = . latest
375
- let result = try await contract. createReadOperation ( " directDebit " , parameters: [ debtor, receiver] as [ AnyObject ] , extraData: Data ( ) ) !. callContractMethod ( )
332
+ transaction. callOnBlock = . latest
333
+ let result = try await contract. createReadOperation ( " directDebit " , parameters: [ debtor, receiver] as [ AnyObject ] , extraData: Data ( ) ) !. callContractMethod ( )
376
334
guard let res = result [ " 0 " ] as? DirectDebit else { throw Web3Error . processingError ( desc: " Failed to get result of expected type from the Ethereum node " ) }
377
335
return res
378
336
}
379
337
380
338
func setupDirectDebit( from: EthereumAddress , receiver: EthereumAddress , info: DirectDebitInfo ) throws -> WriteOperation {
381
- let contract = self . contract
382
- self . transaction. from = from
383
- self . transaction. to = self . address
384
-
385
- let tx = contract. createWriteOperation ( " setupDirectDebit " , parameters: [ receiver, info] as [ AnyObject ] ) !
339
+ updateTransactionAndContract ( from: from)
340
+ let tx = contract. createWriteOperation ( " setupDirectDebit " , parameters: [ receiver, info] as [ AnyObject ] ) !
386
341
return tx
387
342
}
388
343
389
344
func terminateDirectDebit( from: EthereumAddress , receiver: EthereumAddress ) throws -> WriteOperation {
390
- let contract = self . contract
391
- self . transaction. from = from
392
- self . transaction. to = self . address
393
-
394
- let tx = contract. createWriteOperation ( " terminateDirectDebit " , parameters: [ receiver] as [ AnyObject ] ) !
345
+ updateTransactionAndContract ( from: from)
346
+ let tx = contract. createWriteOperation ( " terminateDirectDebit " , parameters: [ receiver] as [ AnyObject ] ) !
395
347
return tx
396
348
}
397
349
398
350
func withdrawDirectDebit( from: EthereumAddress , debtor: EthereumAddress ) throws -> WriteOperation {
399
- let contract = self . contract
400
- self . transaction. from = from
401
- self . transaction. to = self . address
402
-
403
- let tx = contract. createWriteOperation ( " withdrawDirectDebit " , parameters: [ debtor] as [ AnyObject ] ) !
351
+ updateTransactionAndContract ( from: from)
352
+ let tx = contract. createWriteOperation ( " withdrawDirectDebit " , parameters: [ debtor] as [ AnyObject ] ) !
404
353
return tx
405
354
}
406
355
407
356
func withdrawDirectDebit( from: EthereumAddress , debtors: [ EthereumAddress ] , strict: Bool ) throws -> WriteOperation {
408
- let contract = self . contract
409
- self . transaction. from = from
410
- self . transaction. to = self . address
411
-
412
- let tx = contract. createWriteOperation ( " withdrawDirectDebit " , parameters: [ debtors, strict] as [ AnyObject ] ) !
357
+ updateTransactionAndContract ( from: from)
358
+ let tx = contract. createWriteOperation ( " withdrawDirectDebit " , parameters: [ debtors, strict] as [ AnyObject ] ) !
413
359
return tx
414
360
}
415
361
}
362
+
363
+ // MARK: - Private
364
+
365
+ extension ERC1376 {
366
+
367
+ private func updateTransactionAndContract( from: EthereumAddress ) {
368
+ transaction. from = from
369
+ transaction. to = address
370
+ contract. transaction = transaction
371
+ }
372
+
373
+ }
374
+
0 commit comments