@@ -57,43 +57,36 @@ public class ERC777: IERC777, ERC20BaseProperties {
57
57
}
58
58
59
59
public func getGranularity( ) async throws -> BigUInt {
60
- let contract = self . contract
61
- self . transaction. callOnBlock = . latest
60
+ transaction. callOnBlock = . latest
62
61
let result = try await contract. createReadOperation ( " granularity " , parameters: [ ] as [ AnyObject ] , extraData: Data ( ) ) !. callContractMethod ( )
63
62
guard let res = result [ " 0 " ] as? BigUInt else { throw Web3Error . processingError ( desc: " Failed to get result of expected type from the Ethereum node " ) }
64
63
return res
65
64
}
66
65
67
66
public func getDefaultOperators( ) async throws -> [ EthereumAddress ] {
68
- let contract = self . contract
69
- self . transaction. callOnBlock = . latest
67
+ transaction. callOnBlock = . latest
70
68
let result = try await contract. createReadOperation ( " defaultOperators " , parameters: [ ] as [ AnyObject ] , extraData: Data ( ) ) !. callContractMethod ( )
71
69
guard let res = result [ " 0 " ] as? [ EthereumAddress ] else { throw Web3Error . processingError ( desc: " Failed to get result of expected type from the Ethereum node " ) }
72
70
return res
73
71
}
74
72
75
73
public func getBalance( account: EthereumAddress ) async throws -> BigUInt {
76
- let contract = self . contract
77
- self . transaction. callOnBlock = . latest
74
+ transaction. callOnBlock = . latest
78
75
let result = try await contract. createReadOperation ( " balanceOf " , parameters: [ account] as [ AnyObject ] , extraData: Data ( ) ) !. callContractMethod ( )
79
76
guard let res = result [ " 0 " ] as? BigUInt else { throw Web3Error . processingError ( desc: " Failed to get result of expected type from the Ethereum node " ) }
80
77
return res
81
78
}
82
79
83
80
public func getAllowance( originalOwner: EthereumAddress , delegate: EthereumAddress ) async throws -> BigUInt {
84
- let contract = self . contract
85
- self . transaction. callOnBlock = . latest
81
+ transaction. callOnBlock = . latest
86
82
let result = try await contract. createReadOperation ( " allowance " , parameters: [ originalOwner, delegate] as [ AnyObject ] , extraData: Data ( ) ) !. callContractMethod ( )
87
83
guard let res = result [ " 0 " ] as? BigUInt else { throw Web3Error . processingError ( desc: " Failed to get result of expected type from the Ethereum node " ) }
88
84
return res
89
85
}
90
86
91
87
public func transfer( from: EthereumAddress , to: EthereumAddress , amount: String ) async throws -> WriteOperation {
92
- let contract = self . contract
93
- self . transaction. from = from
94
- self . transaction. to = self . address
95
- self . transaction. callOnBlock = . latest
96
-
88
+ transaction. callOnBlock = . latest
89
+ updateTransactionAndContract ( from: from)
97
90
// get the decimals manually
98
91
let callResult = try await contract. createReadOperation ( " decimals " ) !. callContractMethod ( )
99
92
var decimals = BigUInt ( 0 )
@@ -105,16 +98,13 @@ public class ERC777: IERC777, ERC20BaseProperties {
105
98
guard let value = Utilities . parseToBigUInt ( amount, decimals: intDecimals) else {
106
99
throw Web3Error . inputError ( desc: " Can not parse inputted amount " )
107
100
}
108
- let tx = contract. createWriteOperation ( " transfer " , parameters: [ to, value] as [ AnyObject ] ) !
101
+ let tx = contract. createWriteOperation ( " transfer " , parameters: [ to, value] as [ AnyObject ] ) !
109
102
return tx
110
103
}
111
104
112
105
public func transferFrom( from: EthereumAddress , to: EthereumAddress , originalOwner: EthereumAddress , amount: String ) async throws -> WriteOperation {
113
- let contract = self . contract
114
- self . transaction. from = from
115
- self . transaction. to = self . address
116
- self . transaction. callOnBlock = . latest
117
-
106
+ transaction. callOnBlock = . latest
107
+ updateTransactionAndContract ( from: from)
118
108
// get the decimals manually
119
109
let callResult = try await contract. createReadOperation ( " decimals " ) !. callContractMethod ( )
120
110
var decimals = BigUInt ( 0 )
@@ -127,16 +117,13 @@ public class ERC777: IERC777, ERC20BaseProperties {
127
117
throw Web3Error . inputError ( desc: " Can not parse inputted amount " )
128
118
}
129
119
130
- let tx = contract. createWriteOperation ( " transferFrom " , parameters: [ originalOwner, to, value] as [ AnyObject ] ) !
120
+ let tx = contract. createWriteOperation ( " transferFrom " , parameters: [ originalOwner, to, value] as [ AnyObject ] ) !
131
121
return tx
132
122
}
133
123
134
124
public func setAllowance( from: EthereumAddress , to: EthereumAddress , newAmount: String ) async throws -> WriteOperation {
135
- let contract = self . contract
136
- self . transaction. from = from
137
- self . transaction. to = self . address
138
- self . transaction. callOnBlock = . latest
139
-
125
+ transaction. callOnBlock = . latest
126
+ updateTransactionAndContract ( from: from)
140
127
// get the decimals manually
141
128
let callResult = try await contract. createReadOperation ( " decimals " ) !. callContractMethod ( )
142
129
var decimals = BigUInt ( 0 )
@@ -149,53 +136,42 @@ public class ERC777: IERC777, ERC20BaseProperties {
149
136
throw Web3Error . inputError ( desc: " Can not parse inputted amount " )
150
137
}
151
138
152
- let tx = contract. createWriteOperation ( " setAllowance " , parameters: [ to, value] as [ AnyObject ] ) !
139
+ let tx = contract. createWriteOperation ( " setAllowance " , parameters: [ to, value] as [ AnyObject ] ) !
153
140
return tx
154
141
}
155
142
156
143
public func totalSupply( ) async throws -> BigUInt {
157
- let contract = self . contract
158
- self . transaction. callOnBlock = . latest
144
+ transaction. callOnBlock = . latest
159
145
let result = try await contract. createReadOperation ( " totalSupply " , parameters: [ AnyObject] ( ) , extraData: Data ( ) ) !. callContractMethod ( )
160
146
guard let res = result [ " 0 " ] as? BigUInt else { throw Web3Error . processingError ( desc: " Failed to get result of expected type from the Ethereum node " ) }
161
147
return res
162
148
}
163
149
164
150
// ERC777 methods
165
151
public func authorize( from: EthereumAddress , operator user: EthereumAddress ) throws -> WriteOperation {
166
- let contract = self . contract
167
- self . transaction. from = from
168
- self . transaction. to = self . address
169
- self . transaction. callOnBlock = . latest
170
-
171
- let tx = contract. createWriteOperation ( " authorizeOperator " , parameters: [ user] as [ AnyObject ] ) !
152
+ transaction. callOnBlock = . latest
153
+ updateTransactionAndContract ( from: from)
154
+ let tx = contract. createWriteOperation ( " authorizeOperator " , parameters: [ user] as [ AnyObject ] ) !
172
155
return tx
173
156
}
174
157
175
158
public func revoke( from: EthereumAddress , operator user: EthereumAddress ) throws -> WriteOperation {
176
- let contract = self . contract
177
- self . transaction. from = from
178
- self . transaction. to = self . address
179
- self . transaction. callOnBlock = . latest
180
-
181
- let tx = contract. createWriteOperation ( " revokeOperator " , parameters: [ user] as [ AnyObject ] ) !
159
+ transaction. callOnBlock = . latest
160
+ updateTransactionAndContract ( from: from)
161
+ let tx = contract. createWriteOperation ( " revokeOperator " , parameters: [ user] as [ AnyObject ] ) !
182
162
return tx
183
163
}
184
164
185
165
public func isOperatorFor( operator user: EthereumAddress , tokenHolder: EthereumAddress ) async throws -> Bool {
186
- let contract = self . contract
187
- self . transaction. callOnBlock = . latest
166
+ transaction. callOnBlock = . latest
188
167
let result = try await contract. createReadOperation ( " isOperatorFor " , parameters: [ user, tokenHolder] as [ AnyObject ] , extraData: Data ( ) ) !. callContractMethod ( )
189
168
guard let res = result [ " 0 " ] as? Bool else { throw Web3Error . processingError ( desc: " Failed to get result of expected type from the Ethereum node " ) }
190
169
return res
191
170
}
192
171
193
172
public func send( from: EthereumAddress , to: EthereumAddress , amount: String , data: [ UInt8 ] ) async throws -> WriteOperation {
194
- let contract = self . contract
195
- self . transaction. from = from
196
- self . transaction. to = self . address
197
- self . transaction. callOnBlock = . latest
198
-
173
+ transaction. callOnBlock = . latest
174
+ updateTransactionAndContract ( from: from)
199
175
// get the decimals manually
200
176
let callResult = try await contract. createReadOperation ( " decimals " ) !. callContractMethod ( )
201
177
var decimals = BigUInt ( 0 )
@@ -207,16 +183,13 @@ public class ERC777: IERC777, ERC20BaseProperties {
207
183
guard let value = Utilities . parseToBigUInt ( amount, decimals: intDecimals) else {
208
184
throw Web3Error . inputError ( desc: " Can not parse inputted amount " )
209
185
}
210
- let tx = contract. createWriteOperation ( " send " , parameters: [ to, value, data] as [ AnyObject ] ) !
186
+ let tx = contract. createWriteOperation ( " send " , parameters: [ to, value, data] as [ AnyObject ] ) !
211
187
return tx
212
188
}
213
189
214
190
public func operatorSend( from: EthereumAddress , to: EthereumAddress , originalOwner: EthereumAddress , amount: String , data: [ UInt8 ] , operatorData: [ UInt8 ] ) async throws -> WriteOperation {
215
- let contract = self . contract
216
- self . transaction. from = from
217
- self . transaction. to = self . address
218
- self . transaction. callOnBlock = . latest
219
-
191
+ transaction. callOnBlock = . latest
192
+ updateTransactionAndContract ( from: from)
220
193
// get the decimals manually
221
194
let callResult = try await contract. createReadOperation ( " decimals " ) !. callContractMethod ( )
222
195
var decimals = BigUInt ( 0 )
@@ -233,11 +206,8 @@ public class ERC777: IERC777, ERC20BaseProperties {
233
206
}
234
207
235
208
public func burn( from: EthereumAddress , amount: String , data: [ UInt8 ] ) async throws -> WriteOperation {
236
- let contract = self . contract
237
- self . transaction. from = from
238
- self . transaction. to = self . address
239
- self . transaction. callOnBlock = . latest
240
-
209
+ transaction. callOnBlock = . latest
210
+ updateTransactionAndContract ( from: from)
241
211
// get the decimals manually
242
212
let callResult = try await contract. createReadOperation ( " decimals " ) !. callContractMethod ( )
243
213
var decimals = BigUInt ( 0 )
@@ -249,16 +219,13 @@ public class ERC777: IERC777, ERC20BaseProperties {
249
219
guard let value = Utilities . parseToBigUInt ( amount, decimals: intDecimals) else {
250
220
throw Web3Error . inputError ( desc: " Can not parse inputted amount " )
251
221
}
252
- let tx = contract. createWriteOperation ( " burn " , parameters: [ value, data] as [ AnyObject ] ) !
222
+ let tx = contract. createWriteOperation ( " burn " , parameters: [ value, data] as [ AnyObject ] ) !
253
223
return tx
254
224
}
255
225
256
226
public func operatorBurn( from: EthereumAddress , amount: String , originalOwner: EthereumAddress , data: [ UInt8 ] , operatorData: [ UInt8 ] ) async throws -> WriteOperation {
257
- let contract = self . contract
258
- self . transaction. from = from
259
- self . transaction. to = self . address
260
- self . transaction. callOnBlock = . latest
261
-
227
+ transaction. callOnBlock = . latest
228
+ updateTransactionAndContract ( from: from)
262
229
// get the decimals manually
263
230
let callResult = try await contract. createReadOperation ( " decimals " ) !. callContractMethod ( )
264
231
var decimals = BigUInt ( 0 )
@@ -270,67 +237,53 @@ public class ERC777: IERC777, ERC20BaseProperties {
270
237
guard let value = Utilities . parseToBigUInt ( amount, decimals: intDecimals) else {
271
238
throw Web3Error . inputError ( desc: " Can not parse inputted amount " )
272
239
}
273
- let tx = contract. createWriteOperation ( " burn " , parameters: [ originalOwner, value, data, operatorData] as [ AnyObject ] ) !
240
+ let tx = contract. createWriteOperation ( " burn " , parameters: [ originalOwner, value, data, operatorData] as [ AnyObject ] ) !
274
241
return tx
275
242
}
276
243
277
244
public func canImplementInterfaceForAddress( interfaceHash: Data , addr: EthereumAddress ) async throws -> Data {
278
- let contract = self . contract
279
- self . transaction. callOnBlock = . latest
245
+ transaction. callOnBlock = . latest
280
246
let result = try await contract. createReadOperation ( " canImplementInterfaceForAddress " , parameters: [ interfaceHash, addr] as [ AnyObject ] , extraData: Data ( ) ) !. callContractMethod ( )
281
247
guard let res = result [ " 0 " ] as? Data else { throw Web3Error . processingError ( desc: " Failed to get result of expected type from the Ethereum node " ) }
282
248
return res
283
249
}
284
250
285
251
public func getInterfaceImplementer( addr: EthereumAddress , interfaceHash: Data ) async throws -> EthereumAddress {
286
- let contract = self . contract
287
- self . transaction. callOnBlock = . latest
252
+ transaction. callOnBlock = . latest
288
253
let result = try await contract. createReadOperation ( " getInterfaceImplementer " , parameters: [ addr, interfaceHash] as [ AnyObject ] , extraData: Data ( ) ) !. callContractMethod ( )
289
254
guard let res = result [ " 0 " ] as? EthereumAddress else { throw Web3Error . processingError ( desc: " Failed to get result of expected type from the Ethereum node " ) }
290
255
return res
291
256
}
292
257
293
258
public func setInterfaceImplementer( from: EthereumAddress , addr: EthereumAddress , interfaceHash: Data , implementer: EthereumAddress ) throws -> WriteOperation {
294
- let contract = self . contract
295
- self . transaction. from = from
296
- self . transaction. to = self . address
297
-
298
- let tx = contract. createWriteOperation ( " setInterfaceImplementer " , parameters: [ addr, interfaceHash, implementer] as [ AnyObject ] ) !
259
+ updateTransactionAndContract ( from: from)
260
+ let tx = contract. createWriteOperation ( " setInterfaceImplementer " , parameters: [ addr, interfaceHash, implementer] as [ AnyObject ] ) !
299
261
return tx
300
262
}
301
263
302
264
public func setManager( from: EthereumAddress , addr: EthereumAddress , newManager: EthereumAddress ) throws -> WriteOperation {
303
- let contract = self . contract
304
- self . transaction. from = from
305
- self . transaction. to = self . address
306
-
307
- let tx = contract. createWriteOperation ( " setManager " , parameters: [ addr, newManager] as [ AnyObject ] ) !
265
+ updateTransactionAndContract ( from: from)
266
+ let tx = contract. createWriteOperation ( " setManager " , parameters: [ addr, newManager] as [ AnyObject ] ) !
308
267
return tx
309
268
}
310
269
311
270
public func interfaceHash( interfaceName: String ) async throws -> Data {
312
- let contract = self . contract
313
- self . transaction. callOnBlock = . latest
271
+ transaction. callOnBlock = . latest
314
272
let result = try await contract. createReadOperation ( " interfaceHash " , parameters: [ interfaceName] as [ AnyObject ] , extraData: Data ( ) ) !. callContractMethod ( )
315
273
guard let res = result [ " 0 " ] as? Data else { throw Web3Error . processingError ( desc: " Failed to get result of expected type from the Ethereum node " ) }
316
274
return res
317
275
}
318
276
277
+ // FIXME: might want to rename contract param here
319
278
public func updateERC165Cache( from: EthereumAddress , contract: EthereumAddress , interfaceId: [ UInt8 ] ) throws -> WriteOperation {
320
- let contract = self . contract
321
- self . transaction. from = from
322
- self . transaction. to = self . address
323
-
324
- let tx = contract. createWriteOperation ( " updateERC165Cache " , parameters: [ contract, interfaceId] as [ AnyObject ] ) !
279
+ updateTransactionAndContract ( from: from)
280
+ let tx = self . contract. createWriteOperation ( " updateERC165Cache " , parameters: [ contract, interfaceId] as [ AnyObject ] ) !
325
281
return tx
326
282
}
327
283
328
284
public func approve( from: EthereumAddress , spender: EthereumAddress , amount: String ) async throws -> WriteOperation {
329
- let contract = self . contract
330
- self . transaction. from = from
331
- self . transaction. to = self . address
332
- self . transaction. callOnBlock = . latest
333
-
285
+ transaction. callOnBlock = . latest
286
+ updateTransactionAndContract ( from: from)
334
287
// get the decimals manually
335
288
let callResult = try await contract. createReadOperation ( " decimals " ) !. callContractMethod ( )
336
289
var decimals = BigUInt ( 0 )
@@ -343,16 +296,28 @@ public class ERC777: IERC777, ERC20BaseProperties {
343
296
throw Web3Error . inputError ( desc: " Can not parse inputted amount " )
344
297
}
345
298
346
- let tx = contract. createWriteOperation ( " approve " , parameters: [ spender, value] as [ AnyObject ] ) !
299
+ let tx = contract. createWriteOperation ( " approve " , parameters: [ spender, value] as [ AnyObject ] ) !
347
300
return tx
348
301
}
349
302
350
303
public func supportsInterface( interfaceID: String ) async throws -> Bool {
351
- let contract = self . contract
352
- self . transaction. callOnBlock = . latest
304
+ transaction. callOnBlock = . latest
353
305
let result = try await contract. createReadOperation ( " supportsInterface " , parameters: [ interfaceID] as [ AnyObject ] , extraData: Data ( ) ) !. callContractMethod ( )
354
306
guard let res = result [ " 0 " ] as? Bool else { throw Web3Error . processingError ( desc: " Failed to get result of expected type from the Ethereum node " ) }
355
307
return res
356
308
}
357
309
358
310
}
311
+
312
+ // MARK: - Private
313
+
314
+ extension ERC777 {
315
+
316
+ private func updateTransactionAndContract( from: EthereumAddress ) {
317
+ transaction. from = from
318
+ transaction. to = address
319
+ contract. transaction = transaction
320
+ }
321
+
322
+ }
323
+
0 commit comments