@@ -41,259 +41,6 @@ open class BaseEthereumClient: EthereumClientProtocol {
4141 }
4242 }
4343
44- public func net_version( ) async throws -> EthereumNetwork {
45- let emptyParams : [ Bool ] = [ ]
46- do {
47- let data = try await networkProvider. send ( method: " net_version " , params: emptyParams, receive: String . self)
48-
49- if let resString = data as? String {
50- let network = EthereumNetwork . fromString ( resString)
51- return network
52- } else {
53- throw EthereumClientError . unexpectedReturnValue
54- }
55- } catch {
56- throw failureHandler ( error)
57- }
58- }
59-
60- public func eth_gasPrice( ) async throws -> BigUInt {
61- let emptyParams : [ Bool ] = [ ]
62-
63- do {
64- let data = try await networkProvider. send ( method: " eth_gasPrice " , params: emptyParams, receive: String . self)
65- if let hexString = data as? String , let bigUInt = BigUInt ( hex: hexString) {
66- return bigUInt
67- } else {
68- throw EthereumClientError . unexpectedReturnValue
69- }
70- } catch {
71- throw failureHandler ( error)
72- }
73- }
74-
75- public func eth_blockNumber( ) async throws -> Int {
76- let emptyParams : [ Bool ] = [ ]
77-
78- do {
79- let data = try await networkProvider. send ( method: " eth_blockNumber " , params: emptyParams, receive: String . self)
80- if let hexString = data as? String {
81- if let integerValue = Int ( hex: hexString) {
82- return integerValue
83- } else {
84- throw EthereumClientError . decodeIssue
85- }
86- } else {
87- throw EthereumClientError . unexpectedReturnValue
88- }
89- } catch {
90- throw failureHandler ( error)
91- }
92- }
93-
94- public func eth_getBalance( address: EthereumAddress , block: EthereumBlock ) async throws -> BigUInt {
95- do {
96- let data = try await networkProvider. send ( method: " eth_getBalance " , params: [ address. asString ( ) , block. stringValue] , receive: String . self)
97- if let resString = data as? String , let balanceInt = BigUInt ( hex: resString. web3. noHexPrefix) {
98- return balanceInt
99- } else {
100- throw EthereumClientError . unexpectedReturnValue
101- }
102- } catch {
103- throw failureHandler ( error)
104- }
105- }
106-
107- public func eth_getCode( address: EthereumAddress , block: EthereumBlock = . Latest) async throws -> String {
108- do {
109- let data = try await networkProvider. send ( method: " eth_getCode " , params: [ address. asString ( ) , block. stringValue] , receive: String . self)
110- if let resDataString = data as? String {
111- return resDataString
112- } else {
113- throw EthereumClientError . unexpectedReturnValue
114- }
115- } catch {
116- throw failureHandler ( error)
117- }
118- }
119-
120- public func eth_estimateGas( _ transaction: EthereumTransaction ) async throws -> BigUInt {
121- struct CallParams : Encodable {
122- let from : String ?
123- let to : String
124- let value : String ?
125- let data : String ?
126-
127- enum TransactionCodingKeys : String , CodingKey {
128- case from
129- case to
130- case value
131- case data
132- }
133-
134- func encode( to encoder: Encoder ) throws {
135- var container = encoder. unkeyedContainer ( )
136- var nested = container. nestedContainer ( keyedBy: TransactionCodingKeys . self)
137- if let from = from {
138- try nested. encode ( from, forKey: . from)
139- }
140- try nested. encode ( to, forKey: . to)
141-
142- let jsonRPCAmount : ( String ) -> String = { amount in
143- amount == " 0x00 " ? " 0x0 " : amount
144- }
145-
146- if let value = value. map ( jsonRPCAmount) {
147- try nested. encode ( value, forKey: . value)
148- }
149- if let data = data {
150- try nested. encode ( data, forKey: . data)
151- }
152- }
153- }
154-
155- let value : BigUInt ?
156- if let txValue = transaction. value, txValue > . zero {
157- value = txValue
158- } else {
159- value = nil
160- }
161-
162- let params = CallParams (
163- from: transaction. from? . asString ( ) ,
164- to: transaction. to. asString ( ) ,
165- value: value? . web3. hexStringNoLeadingZeroes,
166- data: transaction. data? . web3. hexString
167- )
168-
169- do {
170- let data = try await networkProvider. send ( method: " eth_estimateGas " , params: params, receive: String . self)
171- if let gasHex = data as? String , let gas = BigUInt ( hex: gasHex) {
172- return gas
173- } else {
174- throw EthereumClientError . unexpectedReturnValue
175- }
176- } catch {
177- throw failureHandler ( error)
178- }
179- }
180-
181- public func eth_sendRawTransaction( _ transaction: EthereumTransaction , withAccount account: EthereumAccountProtocol ) async throws -> String {
182- do {
183- // Inject pending nonce
184- let nonce = try await eth_getTransactionCount ( address: account. address, block: . Pending)
185-
186- var transaction = transaction
187- transaction. nonce = nonce
188-
189- if transaction. chainId == nil , let network = network {
190- transaction. chainId = network. intValue
191- }
192-
193- guard let _ = transaction. chainId, let signedTx = ( try ? account. sign ( transaction: transaction) ) , let transactionHex = signedTx. raw? . web3. hexString else {
194- throw EthereumClientError . encodeIssue
195- }
196-
197- let data = try await networkProvider. send ( method: " eth_sendRawTransaction " , params: [ transactionHex] , receive: String . self)
198- if let resDataString = data as? String {
199- return resDataString
200- } else {
201- throw EthereumClientError . unexpectedReturnValue
202- }
203- } catch {
204- throw failureHandler ( error)
205- }
206- }
207-
208- public func eth_getTransaction( byHash txHash: String ) async throws -> EthereumTransaction {
209- do {
210- let data = try await networkProvider. send ( method: " eth_getTransactionByHash " , params: [ txHash] , receive: EthereumTransaction . self)
211- if let transaction = data as? EthereumTransaction {
212- return transaction
213- } else {
214- throw EthereumClientError . unexpectedReturnValue
215- }
216- } catch {
217- throw failureHandler ( error)
218- }
219- }
220-
221- public func eth_getTransactionReceipt( txHash: String ) async throws -> EthereumTransactionReceipt {
222- do {
223- let data = try await networkProvider. send ( method: " eth_getTransactionReceipt " , params: [ txHash] , receive: EthereumTransactionReceipt . self)
224- if let receipt = data as? EthereumTransactionReceipt {
225- return receipt
226- } else {
227- throw EthereumClientError . unexpectedReturnValue
228- }
229- } catch {
230- throw failureHandler ( error)
231- }
232- }
233-
234- public func eth_getLogs( addresses: [ EthereumAddress ] ? , topics: [ String ? ] ? , fromBlock from: EthereumBlock = . Earliest, toBlock to: EthereumBlock = . Latest) async throws -> [ EthereumLog ] {
235- try await RecursiveLogCollector ( ethClient: self ) . getAllLogs ( addresses: addresses, topics: topics. map ( Topics . plain) , from: from, to: to)
236- }
237-
238- public func eth_getLogs( addresses: [ EthereumAddress ] ? , orTopics topics: [ [ String ] ? ] ? , fromBlock from: EthereumBlock = . Earliest, toBlock to: EthereumBlock = . Latest) async throws -> [ EthereumLog ] {
239- try await RecursiveLogCollector ( ethClient: self ) . getAllLogs ( addresses: addresses, topics: topics. map ( Topics . composed) , from: from, to: to)
240- }
241-
242- public func getLogs( addresses: [ EthereumAddress ] ? , topics: Topics ? , fromBlock: EthereumBlock , toBlock: EthereumBlock ) async throws -> [ EthereumLog ] {
243- struct CallParams : Encodable {
244- var fromBlock : String
245- var toBlock : String
246- let address : [ EthereumAddress ] ?
247- let topics : Topics ?
248- }
249-
250- let params = CallParams ( fromBlock: fromBlock. stringValue, toBlock: toBlock. stringValue, address: addresses, topics: topics)
251-
252- do {
253- let data = try await networkProvider. send ( method: " eth_getLogs " , params: [ params] , receive: [ EthereumLog ] . self)
254-
255- if let logs = data as? [ EthereumLog ] {
256- return logs
257- } else {
258- throw EthereumClientError . unexpectedReturnValue
259- }
260- } catch {
261- if let error = error as? JSONRPCError ,
262- case let . executionError( innerError) = error,
263- innerError. error. code == JSONRPCErrorCode . tooManyResults {
264- throw EthereumClientError . tooManyResults
265- } else {
266- throw EthereumClientError . unexpectedReturnValue
267- }
268- }
269- }
270-
271- public func eth_getBlockByNumber( _ block: EthereumBlock ) async throws -> EthereumBlockInfo {
272- struct CallParams : Encodable {
273- let block : EthereumBlock
274- let fullTransactions : Bool
275-
276- func encode( to encoder: Encoder ) throws {
277- var container = encoder. unkeyedContainer ( )
278- try container. encode ( block. stringValue)
279- try container. encode ( fullTransactions)
280- }
281- }
282-
283- let params = CallParams ( block: block, fullTransactions: false )
284-
285- do {
286- let data = try await networkProvider. send ( method: " eth_getBlockByNumber " , params: params, receive: EthereumBlockInfo . self)
287- if let blockData = data as? EthereumBlockInfo {
288- return blockData
289- } else {
290- throw EthereumClientError . unexpectedReturnValue
291- }
292- } catch {
293- throw failureHandler ( error)
294- }
295- }
296-
29744 private func fetchNetwork( ) async -> EthereumNetwork ? {
29845 do {
29946 return try await net_version ( )
0 commit comments