@@ -54,28 +54,22 @@ public class ERC1594: IERC1594, ERC20BaseProperties {
5454 }
5555
5656 public func getBalance( account: EthereumAddress ) async throws -> BigUInt {
57- let contract = self . contract
5857 transaction. callOnBlock = . latest
5958 let result = try await contract. createReadOperation ( " balanceOf " , parameters: [ account] as [ AnyObject ] , extraData: Data ( ) ) !. callContractMethod ( )
6059 guard let res = result [ " 0 " ] as? BigUInt else { throw Web3Error . processingError ( desc: " Failed to get result of expected type from the Ethereum node " ) }
6160 return res
6261 }
6362
6463 public func getAllowance( originalOwner: EthereumAddress , delegate: EthereumAddress ) async throws -> BigUInt {
65- let contract = self . contract
6664 transaction. callOnBlock = . latest
6765 let result = try await contract. createReadOperation ( " allowance " , parameters: [ originalOwner, delegate] as [ AnyObject ] , extraData: Data ( ) ) !. callContractMethod ( )
6866 guard let res = result [ " 0 " ] as? BigUInt else { throw Web3Error . processingError ( desc: " Failed to get result of expected type from the Ethereum node " ) }
6967 return res
7068 }
7169
7270 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
7771 transaction. callOnBlock = . latest
78-
72+ updateTransactionAndContract ( from : from )
7973 // get the decimals manually
8074 let callResult = try await contract. createReadOperation ( " decimals " ) !. callContractMethod ( )
8175 var decimals = BigUInt ( 0 )
@@ -92,12 +86,8 @@ public class ERC1594: IERC1594, ERC20BaseProperties {
9286 }
9387
9488 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
9989 transaction. callOnBlock = . latest
100-
90+ updateTransactionAndContract ( from : from )
10191 // get the decimals manually
10292 let callResult = try await contract. createReadOperation ( " decimals " ) !. callContractMethod ( )
10393 var decimals = BigUInt ( 0 )
@@ -115,12 +105,8 @@ public class ERC1594: IERC1594, ERC20BaseProperties {
115105 }
116106
117107 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
122108 transaction. callOnBlock = . latest
123-
109+ updateTransactionAndContract ( from : from )
124110 // get the decimals manually
125111 let callResult = try await contract. createReadOperation ( " decimals " ) !. callContractMethod ( )
126112 var decimals = BigUInt ( 0 )
@@ -138,21 +124,15 @@ public class ERC1594: IERC1594, ERC20BaseProperties {
138124 }
139125
140126 public func totalSupply( ) async throws -> BigUInt {
141- let contract = self . contract
142-
143127 transaction. callOnBlock = . latest
144128 let result = try await contract. createReadOperation ( " totalSupply " , parameters: [ AnyObject] ( ) , extraData: Data ( ) ) !. callContractMethod ( )
145129 guard let res = result [ " 0 " ] as? BigUInt else { throw Web3Error . processingError ( desc: " Failed to get result of expected type from the Ethereum node " ) }
146130 return res
147131 }
148132
149133 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
154134 transaction. callOnBlock = . latest
155-
135+ updateTransactionAndContract ( from : from )
156136 // get the decimals manually
157137 let callResult = try await contract. createReadOperation ( " decimals " ) !. callContractMethod ( )
158138 var decimals = BigUInt ( 0 )
@@ -171,12 +151,8 @@ public class ERC1594: IERC1594, ERC20BaseProperties {
171151
172152 // ERC1594
173153 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
178154 transaction. callOnBlock = . latest
179-
155+ updateTransactionAndContract ( from : from )
180156 // get the decimals manually
181157 let callResult = try await contract. createReadOperation ( " decimals " ) !. callContractMethod ( )
182158 var decimals = BigUInt ( 0 )
@@ -194,12 +170,8 @@ public class ERC1594: IERC1594, ERC20BaseProperties {
194170 }
195171
196172 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
201173 transaction. callOnBlock = . latest
202-
174+ updateTransactionAndContract ( from : from )
203175 // get the decimals manually
204176 let callResult = try await contract. createReadOperation ( " decimals " ) !. callContractMethod ( )
205177 var decimals = BigUInt ( 0 )
@@ -217,20 +189,15 @@ public class ERC1594: IERC1594, ERC20BaseProperties {
217189 }
218190
219191 public func isIssuable( ) async throws -> Bool {
220- let contract = self . contract
221192 transaction. callOnBlock = . latest
222193 let result = try await contract. createReadOperation ( " isIssuable " , parameters: [ AnyObject] ( ) , extraData: Data ( ) ) !. callContractMethod ( )
223194 guard let res = result [ " 0 " ] as? Bool else { throw Web3Error . processingError ( desc: " Failed to get result of expected type from the Ethereum node " ) }
224195 return res
225196 }
226197
227198 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
232199 transaction. callOnBlock = . latest
233-
200+ updateTransactionAndContract ( from : from )
234201 // get the decimals manually
235202 let callResult = try await contract. createReadOperation ( " decimals " ) !. callContractMethod ( )
236203 var decimals = BigUInt ( 0 )
@@ -248,12 +215,8 @@ public class ERC1594: IERC1594, ERC20BaseProperties {
248215 }
249216
250217 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
255218 transaction. callOnBlock = . latest
256-
219+ updateTransactionAndContract ( from : from )
257220 // get the decimals manually
258221 let callResult = try await contract. createReadOperation ( " decimals " ) !. callContractMethod ( )
259222 var decimals = BigUInt ( 0 )
@@ -271,12 +234,8 @@ public class ERC1594: IERC1594, ERC20BaseProperties {
271234 }
272235
273236 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
278237 transaction. callOnBlock = . latest
279-
238+ updateTransactionAndContract ( from : from )
280239 // get the decimals manually
281240 let callResult = try await contract. createReadOperation ( " decimals " ) !. callContractMethod ( )
282241 var decimals = BigUInt ( 0 )
@@ -294,7 +253,6 @@ public class ERC1594: IERC1594, ERC20BaseProperties {
294253 }
295254
296255 public func canTransfer( to: EthereumAddress , amount: String , data: [ UInt8 ] ) async throws -> ( [ UInt8 ] , Data ) {
297- let contract = self . contract
298256 transaction. callOnBlock = . latest
299257
300258 // get the decimals manually
@@ -315,7 +273,6 @@ public class ERC1594: IERC1594, ERC20BaseProperties {
315273 }
316274
317275 public func canTransferFrom( originalOwner: EthereumAddress , to: EthereumAddress , amount: String , data: [ UInt8 ] ) async throws -> ( [ UInt8 ] , Data ) {
318- let contract = self . contract
319276 transaction. callOnBlock = . latest
320277
321278 // get the decimals manually
@@ -335,3 +292,15 @@ public class ERC1594: IERC1594, ERC20BaseProperties {
335292 return res
336293 }
337294}
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