@@ -41,28 +41,22 @@ public class ERC1633: IERC1633, ERC20BaseProperties {
4141 }
4242
4343 public func getBalance( account: EthereumAddress ) async throws -> BigUInt {
44- let contract = self . contract
4544 transaction. callOnBlock = . latest
4645 let result = try await contract. createReadOperation ( " balanceOf " , parameters: [ account] as [ AnyObject ] , extraData: Data ( ) ) !. callContractMethod ( )
4746 guard let res = result [ " 0 " ] as? BigUInt else { throw Web3Error . processingError ( desc: " Failed to get result of expected type from the Ethereum node " ) }
4847 return res
4948 }
5049
5150 public func getAllowance( originalOwner: EthereumAddress , delegate: EthereumAddress ) async throws -> BigUInt {
52- let contract = self . contract
5351 transaction. callOnBlock = . latest
5452 let result = try await contract. createReadOperation ( " allowance " , parameters: [ originalOwner, delegate] as [ AnyObject ] , extraData: Data ( ) ) !. callContractMethod ( )
5553 guard let res = result [ " 0 " ] as? BigUInt else { throw Web3Error . processingError ( desc: " Failed to get result of expected type from the Ethereum node " ) }
5654 return res
5755 }
5856
5957 public func transfer( from: EthereumAddress , to: EthereumAddress , amount: String ) async throws -> WriteOperation {
60- let contract = self . contract
61-
62- self . transaction. from = from
63- self . transaction. to = self . address
6458 transaction. callOnBlock = . latest
65-
59+ updateTransactionAndContract ( from : from )
6660 // get the decimals manually
6761 let callResult = try await contract. createReadOperation ( " decimals " ) !. callContractMethod ( )
6862 var decimals = BigUInt ( 0 )
@@ -79,12 +73,8 @@ public class ERC1633: IERC1633, ERC20BaseProperties {
7973 }
8074
8175 public func transferFrom( from: EthereumAddress , to: EthereumAddress , originalOwner: EthereumAddress , amount: String ) async throws -> WriteOperation {
82- let contract = self . contract
83-
84- self . transaction. from = from
85- self . transaction. to = self . address
8676 transaction. callOnBlock = . latest
87-
77+ updateTransactionAndContract ( from : from )
8878 // get the decimals manually
8979 let callResult = try await contract. createReadOperation ( " decimals " ) !. callContractMethod ( )
9080 var decimals = BigUInt ( 0 )
@@ -102,12 +92,8 @@ public class ERC1633: IERC1633, ERC20BaseProperties {
10292 }
10393
10494 public func setAllowance( from: EthereumAddress , to: EthereumAddress , newAmount: String ) async throws -> WriteOperation {
105- let contract = self . contract
106-
107- self . transaction. from = from
108- self . transaction. to = self . address
10995 transaction. callOnBlock = . latest
110-
96+ updateTransactionAndContract ( from : from )
11197 // get the decimals manually
11298 let callResult = try await contract. createReadOperation ( " decimals " ) !. callContractMethod ( )
11399 var decimals = BigUInt ( 0 )
@@ -125,20 +111,15 @@ public class ERC1633: IERC1633, ERC20BaseProperties {
125111 }
126112
127113 public func totalSupply( ) async throws -> BigUInt {
128- let contract = self . contract
129114 transaction. callOnBlock = . latest
130115 let result = try await contract. createReadOperation ( " totalSupply " , parameters: [ AnyObject] ( ) , extraData: Data ( ) ) !. callContractMethod ( )
131116 guard let res = result [ " 0 " ] as? BigUInt else { throw Web3Error . processingError ( desc: " Failed to get result of expected type from the Ethereum node " ) }
132117 return res
133118 }
134119
135120 public func approve( from: EthereumAddress , spender: EthereumAddress , amount: String ) async throws -> WriteOperation {
136- let contract = self . contract
137-
138- self . transaction. from = from
139- self . transaction. to = self . address
140121 transaction. callOnBlock = . latest
141-
122+ updateTransactionAndContract ( from : from )
142123 // get the decimals manually
143124 let callResult = try await contract. createReadOperation ( " decimals " ) !. callContractMethod ( )
144125 var decimals = BigUInt ( 0 )
@@ -156,27 +137,36 @@ public class ERC1633: IERC1633, ERC20BaseProperties {
156137 }
157138
158139 func parentToken( ) async throws -> EthereumAddress {
159- let contract = self . contract
160140 transaction. callOnBlock = . latest
161141 let result = try await contract. createReadOperation ( " parentToken " , parameters: [ ] as [ AnyObject ] , extraData: Data ( ) ) !. callContractMethod ( )
162142 guard let res = result [ " 0 " ] as? EthereumAddress else { throw Web3Error . processingError ( desc: " Failed to get result of expected type from the Ethereum node " ) }
163143 return res
164144 }
165145
166146 func parentTokenId( ) async throws -> BigUInt {
167- let contract = self . contract
168147 transaction. callOnBlock = . latest
169148 let result = try await contract. createReadOperation ( " parentTokenId " , parameters: [ ] as [ AnyObject ] , extraData: Data ( ) ) !. callContractMethod ( )
170149 guard let res = result [ " 0 " ] as? BigUInt else { throw Web3Error . processingError ( desc: " Failed to get result of expected type from the Ethereum node " ) }
171150 return res
172151 }
173152
174153 public func supportsInterface( interfaceID: String ) async throws -> Bool {
175- let contract = self . contract
176154 transaction. callOnBlock = . latest
177155 let result = try await contract. createReadOperation ( " supportsInterface " , parameters: [ interfaceID] as [ AnyObject ] , extraData: Data ( ) ) !. callContractMethod ( )
178156 guard let res = result [ " 0 " ] as? Bool else { throw Web3Error . processingError ( desc: " Failed to get result of expected type from the Ethereum node " ) }
179157 return res
180158 }
181159
182160}
161+
162+ // MARK: - Private
163+
164+ extension ERC1633 {
165+
166+ private func updateTransactionAndContract( from: EthereumAddress ) {
167+ transaction. from = from
168+ transaction. to = address
169+ contract. transaction = transaction
170+ }
171+
172+ }
0 commit comments