Skip to content

Commit 1f51147

Browse files
committed
Fix ERC1376
1 parent 0595214 commit 1f51147

File tree

1 file changed

+64
-105
lines changed

1 file changed

+64
-105
lines changed

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

Lines changed: 64 additions & 105 deletions
Original file line numberDiff line numberDiff line change
@@ -89,27 +89,22 @@ public class ERC1376: IERC1376, ERC20BaseProperties {
8989
}
9090

9191
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()
9594
guard let res = result["0"] as? BigUInt else {throw Web3Error.processingError(desc: "Failed to get result of expected type from the Ethereum node")}
9695
return res
9796
}
9897

9998
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()
103101
guard let res = result["0"] as? BigUInt else {throw Web3Error.processingError(desc: "Failed to get result of expected type from the Ethereum node")}
104102
return res
105103
}
106104

107105
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)
113108
// get the decimals manually
114109
let callResult = try await contract.createReadOperation("decimals" )!.callContractMethod()
115110
var decimals = BigUInt(0)
@@ -121,16 +116,13 @@ public class ERC1376: IERC1376, ERC20BaseProperties {
121116
guard let value = Utilities.parseToBigUInt(amount, decimals: intDecimals) else {
122117
throw Web3Error.inputError(desc: "Can not parse inputted amount")
123118
}
124-
let tx = contract.createWriteOperation("transfer", parameters: [to, value] as [AnyObject] )!
119+
let tx = contract.createWriteOperation("transfer", parameters: [to, value] as [AnyObject])!
125120
return tx
126121
}
127122

128123
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)
134126
// get the decimals manually
135127
let callResult = try await contract.createReadOperation("decimals" )!.callContractMethod()
136128
var decimals = BigUInt(0)
@@ -143,16 +135,13 @@ public class ERC1376: IERC1376, ERC20BaseProperties {
143135
throw Web3Error.inputError(desc: "Can not parse inputted amount")
144136
}
145137

146-
let tx = contract.createWriteOperation("transferFrom", parameters: [originalOwner, to, value] as [AnyObject] )!
138+
let tx = contract.createWriteOperation("transferFrom", parameters: [originalOwner, to, value] as [AnyObject])!
147139
return tx
148140
}
149141

150142
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)
156145
// get the decimals manually
157146
let callResult = try await contract.createReadOperation("decimals" )!.callContractMethod()
158147
var decimals = BigUInt(0)
@@ -165,16 +154,13 @@ public class ERC1376: IERC1376, ERC20BaseProperties {
165154
throw Web3Error.inputError(desc: "Can not parse inputted amount")
166155
}
167156

168-
let tx = contract.createWriteOperation("setAllowance", parameters: [to, value] as [AnyObject] )!
157+
let tx = contract.createWriteOperation("setAllowance", parameters: [to, value] as [AnyObject])!
169158
return tx
170159
}
171160

172161
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)
178164
// get the decimals manually
179165
let callResult = try await contract.createReadOperation("decimals" )!.callContractMethod()
180166
var decimals = BigUInt(0)
@@ -187,24 +173,20 @@ public class ERC1376: IERC1376, ERC20BaseProperties {
187173
throw Web3Error.inputError(desc: "Can not parse inputted amount")
188174
}
189175

190-
let tx = contract.createWriteOperation("approve", parameters: [spender, value] as [AnyObject] )!
176+
let tx = contract.createWriteOperation("approve", parameters: [spender, value] as [AnyObject])!
191177
return tx
192178
}
193179

194180
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()
198183
guard let res = result["0"] as? BigUInt else {throw Web3Error.processingError(desc: "Failed to get result of expected type from the Ethereum node")}
199184
return res
200185
}
201186

202187
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)
208190
// get the decimals manually
209191
let callResult = try await contract.createReadOperation("decimals" )!.callContractMethod()
210192
var decimals = BigUInt(0)
@@ -220,16 +202,13 @@ public class ERC1376: IERC1376, ERC20BaseProperties {
220202
throw Web3Error.inputError(desc: "Can not parse inputted amount")
221203
}
222204

223-
let tx = contract.createWriteOperation("approve", parameters: [spender, eValue, nValue] as [AnyObject] )!
205+
let tx = contract.createWriteOperation("approve", parameters: [spender, eValue, nValue] as [AnyObject])!
224206
return tx
225207
}
226208

227209
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)
233212
// get the decimals manually
234213
let callResult = try await contract.createReadOperation("decimals" )!.callContractMethod()
235214
var decimals = BigUInt(0)
@@ -242,16 +221,13 @@ public class ERC1376: IERC1376, ERC20BaseProperties {
242221
throw Web3Error.inputError(desc: "Can not parse inputted amount")
243222
}
244223

245-
let tx = contract.createWriteOperation("increaseAllowance", parameters: [spender, amount] as [AnyObject] )!
224+
let tx = contract.createWriteOperation("increaseAllowance", parameters: [spender, amount] as [AnyObject])!
246225
return tx
247226
}
248227

249228
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)
255231
// get the decimals manually
256232
let callResult = try await contract.createReadOperation("decimals" )!.callContractMethod()
257233
var decimals = BigUInt(0)
@@ -264,33 +240,26 @@ public class ERC1376: IERC1376, ERC20BaseProperties {
264240
throw Web3Error.inputError(desc: "Can not parse inputted amount")
265241
}
266242

267-
let tx = contract.createWriteOperation("decreaseAllowance", parameters: [spender, amount, strict] as [AnyObject] )!
243+
let tx = contract.createWriteOperation("decreaseAllowance", parameters: [spender, amount, strict] as [AnyObject])!
268244
return tx
269245
}
270246

271247
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])!
277250
return tx
278251
}
279252

280253
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()
284256
guard let res = result["0"] as? BigUInt else {throw Web3Error.processingError(desc: "Failed to get result of expected type from the Ethereum node")}
285257
return res
286258
}
287259

288260
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)
294263
// get the decimals manually
295264
let callResult = try await contract.createReadOperation("decimals" )!.callContractMethod()
296265
var decimals = BigUInt(0)
@@ -302,16 +271,13 @@ public class ERC1376: IERC1376, ERC20BaseProperties {
302271
guard let value = Utilities.parseToBigUInt(data, decimals: intDecimals) else {
303272
throw Web3Error.inputError(desc: "Can not parse inputted amount")
304273
}
305-
let tx = contract.createWriteOperation("transfer", parameters: [value] as [AnyObject] )!
274+
let tx = contract.createWriteOperation("transfer", parameters: [value] as [AnyObject])!
306275
return tx
307276
}
308277

309278
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)
315281
// get the decimals manually
316282
let callResult = try await contract.createReadOperation("decimals" )!.callContractMethod()
317283
var decimals = BigUInt(0)
@@ -328,29 +294,22 @@ public class ERC1376: IERC1376, ERC20BaseProperties {
328294
}
329295

330296
func nonceOf(owner: EthereumAddress) async throws -> BigUInt {
331-
let contract = self.contract
332-
self.transaction.callOnBlock = .latest
297+
transaction.callOnBlock = .latest
333298
let result = try await contract.createReadOperation("nonceOf", parameters: [owner] as [AnyObject], extraData: Data() )!.callContractMethod()
334299
guard let res = result["0"] as? BigUInt else {throw Web3Error.processingError(desc: "Failed to get result of expected type from the Ethereum node")}
335300
return res
336301
}
337302

338303
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)
344306
let tx = contract.createWriteOperation("increaseNonce", parameters: [] as [AnyObject] )!
345307
return tx
346308
}
347309

348310
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)
354313
// get the decimals manually
355314
let callResult = try await contract.createReadOperation("decimals" )!.callContractMethod()
356315
var decimals = BigUInt(0)
@@ -370,46 +329,46 @@ public class ERC1376: IERC1376, ERC20BaseProperties {
370329
}
371330

372331
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()
376334
guard let res = result["0"] as? DirectDebit else {throw Web3Error.processingError(desc: "Failed to get result of expected type from the Ethereum node")}
377335
return res
378336
}
379337

380338
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])!
386341
return tx
387342
}
388343

389344
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])!
395347
return tx
396348
}
397349

398350
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])!
404353
return tx
405354
}
406355

407356
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])!
413359
return tx
414360
}
415361
}
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

Comments
 (0)