Skip to content

Commit 7178a20

Browse files
committed
Fix ERC777
1 parent 68372ed commit 7178a20

File tree

1 file changed

+59
-94
lines changed

1 file changed

+59
-94
lines changed

Sources/web3swift/Tokens/ERC777/Web3+ERC777.swift

Lines changed: 59 additions & 94 deletions
Original file line numberDiff line numberDiff line change
@@ -57,43 +57,36 @@ public class ERC777: IERC777, ERC20BaseProperties {
5757
}
5858

5959
public func getGranularity() async throws -> BigUInt {
60-
let contract = self.contract
61-
self.transaction.callOnBlock = .latest
60+
transaction.callOnBlock = .latest
6261
let result = try await contract.createReadOperation("granularity", parameters: [] as [AnyObject], extraData: Data() )!.callContractMethod()
6362
guard let res = result["0"] as? BigUInt else {throw Web3Error.processingError(desc: "Failed to get result of expected type from the Ethereum node")}
6463
return res
6564
}
6665

6766
public func getDefaultOperators() async throws -> [EthereumAddress] {
68-
let contract = self.contract
69-
self.transaction.callOnBlock = .latest
67+
transaction.callOnBlock = .latest
7068
let result = try await contract.createReadOperation("defaultOperators", parameters: [] as [AnyObject], extraData: Data() )!.callContractMethod()
7169
guard let res = result["0"] as? [EthereumAddress] else {throw Web3Error.processingError(desc: "Failed to get result of expected type from the Ethereum node")}
7270
return res
7371
}
7472

7573
public func getBalance(account: EthereumAddress) async throws -> BigUInt {
76-
let contract = self.contract
77-
self.transaction.callOnBlock = .latest
74+
transaction.callOnBlock = .latest
7875
let result = try await contract.createReadOperation("balanceOf", parameters: [account] as [AnyObject], extraData: Data() )!.callContractMethod()
7976
guard let res = result["0"] as? BigUInt else {throw Web3Error.processingError(desc: "Failed to get result of expected type from the Ethereum node")}
8077
return res
8178
}
8279

8380
public func getAllowance(originalOwner: EthereumAddress, delegate: EthereumAddress) async throws -> BigUInt {
84-
let contract = self.contract
85-
self.transaction.callOnBlock = .latest
81+
transaction.callOnBlock = .latest
8682
let result = try await contract.createReadOperation("allowance", parameters: [originalOwner, delegate] as [AnyObject], extraData: Data() )!.callContractMethod()
8783
guard let res = result["0"] as? BigUInt else {throw Web3Error.processingError(desc: "Failed to get result of expected type from the Ethereum node")}
8884
return res
8985
}
9086

9187
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)
9790
// get the decimals manually
9891
let callResult = try await contract.createReadOperation("decimals" )!.callContractMethod()
9992
var decimals = BigUInt(0)
@@ -105,16 +98,13 @@ public class ERC777: IERC777, ERC20BaseProperties {
10598
guard let value = Utilities.parseToBigUInt(amount, decimals: intDecimals) else {
10699
throw Web3Error.inputError(desc: "Can not parse inputted amount")
107100
}
108-
let tx = contract.createWriteOperation("transfer", parameters: [to, value] as [AnyObject] )!
101+
let tx = contract.createWriteOperation("transfer", parameters: [to, value] as [AnyObject])!
109102
return tx
110103
}
111104

112105
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)
118108
// get the decimals manually
119109
let callResult = try await contract.createReadOperation("decimals" )!.callContractMethod()
120110
var decimals = BigUInt(0)
@@ -127,16 +117,13 @@ public class ERC777: IERC777, ERC20BaseProperties {
127117
throw Web3Error.inputError(desc: "Can not parse inputted amount")
128118
}
129119

130-
let tx = contract.createWriteOperation("transferFrom", parameters: [originalOwner, to, value] as [AnyObject] )!
120+
let tx = contract.createWriteOperation("transferFrom", parameters: [originalOwner, to, value] as [AnyObject])!
131121
return tx
132122
}
133123

134124
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)
140127
// get the decimals manually
141128
let callResult = try await contract.createReadOperation("decimals" )!.callContractMethod()
142129
var decimals = BigUInt(0)
@@ -149,53 +136,42 @@ public class ERC777: IERC777, ERC20BaseProperties {
149136
throw Web3Error.inputError(desc: "Can not parse inputted amount")
150137
}
151138

152-
let tx = contract.createWriteOperation("setAllowance", parameters: [to, value] as [AnyObject] )!
139+
let tx = contract.createWriteOperation("setAllowance", parameters: [to, value] as [AnyObject])!
153140
return tx
154141
}
155142

156143
public func totalSupply() async throws -> BigUInt {
157-
let contract = self.contract
158-
self.transaction.callOnBlock = .latest
144+
transaction.callOnBlock = .latest
159145
let result = try await contract.createReadOperation("totalSupply", parameters: [AnyObject](), extraData: Data() )!.callContractMethod()
160146
guard let res = result["0"] as? BigUInt else {throw Web3Error.processingError(desc: "Failed to get result of expected type from the Ethereum node")}
161147
return res
162148
}
163149

164150
// ERC777 methods
165151
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])!
172155
return tx
173156
}
174157

175158
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])!
182162
return tx
183163
}
184164

185165
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
188167
let result = try await contract.createReadOperation("isOperatorFor", parameters: [user, tokenHolder] as [AnyObject], extraData: Data() )!.callContractMethod()
189168
guard let res = result["0"] as? Bool else {throw Web3Error.processingError(desc: "Failed to get result of expected type from the Ethereum node")}
190169
return res
191170
}
192171

193172
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)
199175
// get the decimals manually
200176
let callResult = try await contract.createReadOperation("decimals" )!.callContractMethod()
201177
var decimals = BigUInt(0)
@@ -207,16 +183,13 @@ public class ERC777: IERC777, ERC20BaseProperties {
207183
guard let value = Utilities.parseToBigUInt(amount, decimals: intDecimals) else {
208184
throw Web3Error.inputError(desc: "Can not parse inputted amount")
209185
}
210-
let tx = contract.createWriteOperation("send", parameters: [to, value, data] as [AnyObject] )!
186+
let tx = contract.createWriteOperation("send", parameters: [to, value, data] as [AnyObject])!
211187
return tx
212188
}
213189

214190
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)
220193
// get the decimals manually
221194
let callResult = try await contract.createReadOperation("decimals" )!.callContractMethod()
222195
var decimals = BigUInt(0)
@@ -233,11 +206,8 @@ public class ERC777: IERC777, ERC20BaseProperties {
233206
}
234207

235208
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)
241211
// get the decimals manually
242212
let callResult = try await contract.createReadOperation("decimals" )!.callContractMethod()
243213
var decimals = BigUInt(0)
@@ -249,16 +219,13 @@ public class ERC777: IERC777, ERC20BaseProperties {
249219
guard let value = Utilities.parseToBigUInt(amount, decimals: intDecimals) else {
250220
throw Web3Error.inputError(desc: "Can not parse inputted amount")
251221
}
252-
let tx = contract.createWriteOperation("burn", parameters: [value, data] as [AnyObject] )!
222+
let tx = contract.createWriteOperation("burn", parameters: [value, data] as [AnyObject])!
253223
return tx
254224
}
255225

256226
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)
262229
// get the decimals manually
263230
let callResult = try await contract.createReadOperation("decimals" )!.callContractMethod()
264231
var decimals = BigUInt(0)
@@ -270,67 +237,53 @@ public class ERC777: IERC777, ERC20BaseProperties {
270237
guard let value = Utilities.parseToBigUInt(amount, decimals: intDecimals) else {
271238
throw Web3Error.inputError(desc: "Can not parse inputted amount")
272239
}
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])!
274241
return tx
275242
}
276243

277244
public func canImplementInterfaceForAddress(interfaceHash: Data, addr: EthereumAddress) async throws -> Data {
278-
let contract = self.contract
279-
self.transaction.callOnBlock = .latest
245+
transaction.callOnBlock = .latest
280246
let result = try await contract.createReadOperation("canImplementInterfaceForAddress", parameters: [interfaceHash, addr] as [AnyObject], extraData: Data() )!.callContractMethod()
281247
guard let res = result["0"] as? Data else {throw Web3Error.processingError(desc: "Failed to get result of expected type from the Ethereum node")}
282248
return res
283249
}
284250

285251
public func getInterfaceImplementer(addr: EthereumAddress, interfaceHash: Data) async throws -> EthereumAddress {
286-
let contract = self.contract
287-
self.transaction.callOnBlock = .latest
252+
transaction.callOnBlock = .latest
288253
let result = try await contract.createReadOperation("getInterfaceImplementer", parameters: [addr, interfaceHash] as [AnyObject], extraData: Data() )!.callContractMethod()
289254
guard let res = result["0"] as? EthereumAddress else {throw Web3Error.processingError(desc: "Failed to get result of expected type from the Ethereum node")}
290255
return res
291256
}
292257

293258
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])!
299261
return tx
300262
}
301263

302264
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])!
308267
return tx
309268
}
310269

311270
public func interfaceHash(interfaceName: String) async throws -> Data {
312-
let contract = self.contract
313-
self.transaction.callOnBlock = .latest
271+
transaction.callOnBlock = .latest
314272
let result = try await contract.createReadOperation("interfaceHash", parameters: [interfaceName] as [AnyObject], extraData: Data() )!.callContractMethod()
315273
guard let res = result["0"] as? Data else {throw Web3Error.processingError(desc: "Failed to get result of expected type from the Ethereum node")}
316274
return res
317275
}
318276

277+
// FIXME: might want to rename contract param here
319278
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])!
325281
return tx
326282
}
327283

328284
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)
334287
// get the decimals manually
335288
let callResult = try await contract.createReadOperation("decimals" )!.callContractMethod()
336289
var decimals = BigUInt(0)
@@ -343,16 +296,28 @@ public class ERC777: IERC777, ERC20BaseProperties {
343296
throw Web3Error.inputError(desc: "Can not parse inputted amount")
344297
}
345298

346-
let tx = contract.createWriteOperation("approve", parameters: [spender, value] as [AnyObject] )!
299+
let tx = contract.createWriteOperation("approve", parameters: [spender, value] as [AnyObject])!
347300
return tx
348301
}
349302

350303
public func supportsInterface(interfaceID: String) async throws -> Bool {
351-
let contract = self.contract
352-
self.transaction.callOnBlock = .latest
304+
transaction.callOnBlock = .latest
353305
let result = try await contract.createReadOperation("supportsInterface", parameters: [interfaceID] as [AnyObject], extraData: Data() )!.callContractMethod()
354306
guard let res = result["0"] as? Bool else {throw Web3Error.processingError(desc: "Failed to get result of expected type from the Ethereum node")}
355307
return res
356308
}
357309

358310
}
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

Comments
 (0)