@@ -43,28 +43,22 @@ public class ERC1644: IERC1644, ERC20BaseProperties {
4343 }
4444
4545 public func getBalance( account: EthereumAddress ) async throws -> BigUInt {
46- let contract = self . contract
47- self . transaction. callOnBlock = . latest
46+ transaction. callOnBlock = . latest
4847 let result = try await contract. createReadOperation ( " balanceOf " , parameters: [ account] as [ AnyObject ] , extraData: Data ( ) ) !. callContractMethod ( )
4948 guard let res = result [ " 0 " ] as? BigUInt else { throw Web3Error . processingError ( desc: " Failed to get result of expected type from the Ethereum node " ) }
5049 return res
5150 }
5251
5352 public func getAllowance( originalOwner: EthereumAddress , delegate: EthereumAddress ) async throws -> BigUInt {
54- let contract = self . contract
55- self . transaction. callOnBlock = . latest
53+ transaction. callOnBlock = . latest
5654 let result = try await contract. createReadOperation ( " allowance " , parameters: [ originalOwner, delegate] as [ AnyObject ] , extraData: Data ( ) ) !. callContractMethod ( )
5755 guard let res = result [ " 0 " ] as? BigUInt else { throw Web3Error . processingError ( desc: " Failed to get result of expected type from the Ethereum node " ) }
5856 return res
5957 }
6058
6159 public func transfer( from: EthereumAddress , to: EthereumAddress , amount: String ) async throws -> WriteOperation {
62- let contract = self . contract
63-
64- self . transaction. from = from
65- self . transaction. to = self . address
66- self . transaction. callOnBlock = . latest
67-
60+ transaction. callOnBlock = . latest
61+ updateTransactionAndContract ( from: from)
6862 // get the decimals manually
6963 let callResult = try await contract. createReadOperation ( " decimals " ) !. callContractMethod ( )
7064 var decimals = BigUInt ( 0 )
@@ -81,12 +75,8 @@ public class ERC1644: IERC1644, ERC20BaseProperties {
8175 }
8276
8377 public func transferFrom( from: EthereumAddress , to: EthereumAddress , originalOwner: EthereumAddress , amount: String ) async throws -> WriteOperation {
84- let contract = self . contract
85-
86- self . transaction. from = from
87- self . transaction. to = self . address
88- self . transaction. callOnBlock = . latest
89-
78+ transaction. callOnBlock = . latest
79+ updateTransactionAndContract ( from: from)
9080 // get the decimals manually
9181 let callResult = try await contract. createReadOperation ( " decimals " ) !. callContractMethod ( )
9282 var decimals = BigUInt ( 0 )
@@ -104,12 +94,8 @@ public class ERC1644: IERC1644, ERC20BaseProperties {
10494 }
10595
10696 public func setAllowance( from: EthereumAddress , to: EthereumAddress , newAmount: String ) async throws -> WriteOperation {
107- let contract = self . contract
108-
109- self . transaction. from = from
110- self . transaction. to = self . address
111- self . transaction. callOnBlock = . latest
112-
97+ transaction. callOnBlock = . latest
98+ updateTransactionAndContract ( from: from)
11399 // get the decimals manually
114100 let callResult = try await contract. createReadOperation ( " decimals " ) !. callContractMethod ( )
115101 var decimals = BigUInt ( 0 )
@@ -127,20 +113,15 @@ public class ERC1644: IERC1644, ERC20BaseProperties {
127113 }
128114
129115 public func totalSupply( ) async throws -> BigUInt {
130- let contract = self . contract
131- self . transaction. callOnBlock = . latest
116+ transaction. callOnBlock = . latest
132117 let result = try await contract. createReadOperation ( " totalSupply " , parameters: [ AnyObject] ( ) , extraData: Data ( ) ) !. callContractMethod ( )
133118 guard let res = result [ " 0 " ] as? BigUInt else { throw Web3Error . processingError ( desc: " Failed to get result of expected type from the Ethereum node " ) }
134119 return res
135120 }
136121
137122 public func approve( from: EthereumAddress , spender: EthereumAddress , amount: String ) async throws -> WriteOperation {
138- let contract = self . contract
139-
140- self . transaction. from = from
141- self . transaction. to = self . address
142- self . transaction. callOnBlock = . latest
143-
123+ transaction. callOnBlock = . latest
124+ updateTransactionAndContract ( from: from)
144125 // get the decimals manually
145126 let callResult = try await contract. createReadOperation ( " decimals " ) !. callContractMethod ( )
146127 var decimals = BigUInt ( 0 )
@@ -159,20 +140,15 @@ public class ERC1644: IERC1644, ERC20BaseProperties {
159140
160141 // ERC1644
161142 public func isControllable( ) async throws -> Bool {
162- let contract = self . contract
163- self . transaction. callOnBlock = . latest
143+ transaction. callOnBlock = . latest
164144 let result = try await contract. createReadOperation ( " isControllable " , parameters: [ AnyObject] ( ) , extraData: Data ( ) ) !. callContractMethod ( )
165145 guard let res = result [ " 0 " ] as? Bool else { throw Web3Error . processingError ( desc: " Failed to get result of expected type from the Ethereum node " ) }
166146 return res
167147 }
168148
169149 public func controllerTransfer( from: EthereumAddress , to: EthereumAddress , originalOwner: EthereumAddress , amount: String , data: [ UInt8 ] , operatorData: [ UInt8 ] ) async throws -> WriteOperation {
170- let contract = self . contract
171-
172- self . transaction. from = from
173- self . transaction. to = self . address
174- self . transaction. callOnBlock = . latest
175-
150+ transaction. callOnBlock = . latest
151+ updateTransactionAndContract ( from: from)
176152 // get the decimals manually
177153 let callResult = try await contract. createReadOperation ( " decimals " ) !. callContractMethod ( )
178154 var decimals = BigUInt ( 0 )
@@ -190,12 +166,8 @@ public class ERC1644: IERC1644, ERC20BaseProperties {
190166 }
191167
192168 public func controllerRedeem( from: EthereumAddress , tokenHolder: EthereumAddress , amount: String , data: [ UInt8 ] , operatorData: [ UInt8 ] ) async throws -> WriteOperation {
193- let contract = self . contract
194-
195- self . transaction. from = from
196- self . transaction. to = self . address
197- self . transaction. callOnBlock = . latest
198-
169+ transaction. callOnBlock = . latest
170+ updateTransactionAndContract ( from: from)
199171 // get the decimals manually
200172 let callResult = try await contract. createReadOperation ( " decimals " ) !. callContractMethod ( )
201173 var decimals = BigUInt ( 0 )
@@ -212,3 +184,15 @@ public class ERC1644: IERC1644, ERC20BaseProperties {
212184 return tx
213185 }
214186}
187+
188+ // MARK: - Private
189+
190+ extension ERC1644 {
191+
192+ private func updateTransactionAndContract( from: EthereumAddress ) {
193+ transaction. from = from
194+ transaction. to = address
195+ contract. transaction = transaction
196+ }
197+
198+ }
0 commit comments