@@ -44,28 +44,22 @@ public class ERC1643: IERC1643, ERC20BaseProperties {
4444 }
4545
4646 public func getBalance( account: EthereumAddress ) async throws -> BigUInt {
47- let contract = self . contract
48- self . transaction. callOnBlock = . latest
47+ transaction. callOnBlock = . latest
4948 let result = try await contract. createReadOperation ( " balanceOf " , parameters: [ account] as [ AnyObject ] , extraData: Data ( ) ) !. callContractMethod ( )
5049 guard let res = result [ " 0 " ] as? BigUInt else { throw Web3Error . processingError ( desc: " Failed to get result of expected type from the Ethereum node " ) }
5150 return res
5251 }
5352
5453 public func getAllowance( originalOwner: EthereumAddress , delegate: EthereumAddress ) async throws -> BigUInt {
55- let contract = self . contract
56- self . transaction. callOnBlock = . latest
54+ transaction. callOnBlock = . latest
5755 let result = try await contract. createReadOperation ( " allowance " , parameters: [ originalOwner, delegate] as [ AnyObject ] , extraData: Data ( ) ) !. callContractMethod ( )
5856 guard let res = result [ " 0 " ] as? BigUInt else { throw Web3Error . processingError ( desc: " Failed to get result of expected type from the Ethereum node " ) }
5957 return res
6058 }
6159
6260 public func transfer( from: EthereumAddress , to: EthereumAddress , amount: String ) async throws -> WriteOperation {
63- let contract = self . contract
64-
65- self . transaction. from = from
66- self . transaction. to = self . address
67- self . transaction. callOnBlock = . latest
68-
61+ transaction. callOnBlock = . latest
62+ updateTransactionAndContract ( from: from)
6963 // get the decimals manually
7064 let callResult = try await contract. createReadOperation ( " decimals " ) !. callContractMethod ( )
7165 var decimals = BigUInt ( 0 )
@@ -82,12 +76,8 @@ public class ERC1643: IERC1643, ERC20BaseProperties {
8276 }
8377
8478 public func transferFrom( from: EthereumAddress , to: EthereumAddress , originalOwner: EthereumAddress , amount: String ) async throws -> WriteOperation {
85- let contract = self . contract
86-
87- self . transaction. from = from
88- self . transaction. to = self . address
89- self . transaction. callOnBlock = . latest
90-
79+ transaction. callOnBlock = . latest
80+ updateTransactionAndContract ( from: from)
9181 // get the decimals manually
9282 let callResult = try await contract. createReadOperation ( " decimals " ) !. callContractMethod ( )
9383 var decimals = BigUInt ( 0 )
@@ -105,12 +95,8 @@ public class ERC1643: IERC1643, ERC20BaseProperties {
10595 }
10696
10797 public func setAllowance( from: EthereumAddress , to: EthereumAddress , newAmount: String ) async throws -> WriteOperation {
108- let contract = self . contract
109-
110- self . transaction. from = from
111- self . transaction. to = self . address
112- self . transaction. callOnBlock = . latest
113-
98+ transaction. callOnBlock = . latest
99+ updateTransactionAndContract ( from: from)
114100 // get the decimals manually
115101 let callResult = try await contract. createReadOperation ( " decimals " ) !. callContractMethod ( )
116102 var decimals = BigUInt ( 0 )
@@ -128,20 +114,15 @@ public class ERC1643: IERC1643, ERC20BaseProperties {
128114 }
129115
130116 public func totalSupply( ) async throws -> BigUInt {
131- let contract = self . contract
132- self . transaction. callOnBlock = . latest
117+ transaction. callOnBlock = . latest
133118 let result = try await contract. createReadOperation ( " totalSupply " , parameters: [ AnyObject] ( ) , extraData: Data ( ) ) !. callContractMethod ( )
134119 guard let res = result [ " 0 " ] as? BigUInt else { throw Web3Error . processingError ( desc: " Failed to get result of expected type from the Ethereum node " ) }
135120 return res
136121 }
137122
138123 public func approve( from: EthereumAddress , spender: EthereumAddress , amount: String ) async throws -> WriteOperation {
139- let contract = self . contract
140-
141- self . transaction. from = from
142- self . transaction. to = self . address
143- self . transaction. callOnBlock = . latest
144-
124+ transaction. callOnBlock = . latest
125+ updateTransactionAndContract ( from: from)
145126 // get the decimals manually
146127 let callResult = try await contract. createReadOperation ( " decimals " ) !. callContractMethod ( )
147128 var decimals = BigUInt ( 0 )
@@ -160,38 +141,41 @@ public class ERC1643: IERC1643, ERC20BaseProperties {
160141
161142 // ERC1643 methods
162143 public func getDocument( name: Data ) async throws -> ( String , Data ) {
163- let contract = self . contract
164- self . transaction. callOnBlock = . latest
144+ transaction. callOnBlock = . latest
165145 let result = try await contract. createReadOperation ( " getDocument " , parameters: [ name] as [ AnyObject ] , extraData: Data ( ) ) !. callContractMethod ( )
166146 guard let res = result [ " 0 " ] as? ( String , Data ) else { throw Web3Error . processingError ( desc: " Failed to get result of expected type from the Ethereum node " ) }
167147 return res
168148 }
169149
170150 public func setDocument( from: EthereumAddress , name: Data , uri: String , documentHash: Data ) throws -> WriteOperation {
171- let contract = self . contract
172-
173- self . transaction. from = from
174- self . transaction. to = self . address
175-
151+ updateTransactionAndContract ( from: from)
176152 let tx = contract. createWriteOperation ( " setDocument " , parameters: [ name, uri, documentHash] as [ AnyObject ] ) !
177153 return tx
178154 }
179155
180156 public func removeDocument( from: EthereumAddress , name: Data ) throws -> WriteOperation {
181- let contract = self . contract
182-
183- self . transaction. from = from
184- self . transaction. to = self . address
185-
157+ updateTransactionAndContract ( from: from)
186158 let tx = contract. createWriteOperation ( " removeDocument " , parameters: [ name] as [ AnyObject ] ) !
187159 return tx
188160 }
189161
190162 public func getAllDocuments( ) async throws -> [ Data ] {
191- let contract = self . contract
192- self . transaction. callOnBlock = . latest
163+ transaction. callOnBlock = . latest
193164 let result = try await contract. createReadOperation ( " getAllDocuments " , parameters: [ ] as [ AnyObject ] , extraData: Data ( ) ) !. callContractMethod ( )
194165 guard let res = result [ " 0 " ] as? [ Data ] else { throw Web3Error . processingError ( desc: " Failed to get result of expected type from the Ethereum node " ) }
195166 return res
196167 }
197168}
169+
170+ // MARK: - Private
171+
172+ extension ERC1643 {
173+
174+ private func updateTransactionAndContract( from: EthereumAddress ) {
175+ transaction. from = from
176+ transaction. to = address
177+ contract. transaction = transaction
178+ }
179+
180+ }
181+
0 commit comments