Skip to content

Commit 7d7428d

Browse files
authored
Merge pull request #333 from argentlabs/tech/zksync2client-protocols
[TIDY] Move RPC methods to EthereumRPCProtocol, so those can be shared with ZKSync
2 parents a0fc7cc + b12eab4 commit 7d7428d

File tree

17 files changed

+325
-321
lines changed

17 files changed

+325
-321
lines changed

Package.resolved

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

web3sTests/Client/EthereumClientTests.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,7 @@ class EthereumClientTests: XCTestCase {
116116
func testEthSendRawTransaction() async {
117117
do {
118118
let gasPrice = try await client?.eth_gasPrice()
119-
let tx = EthereumTransaction(from: nil, to: "0x3c1bd6b420448cf16a389c8b0115ccb3660bb854", value: BigUInt(1600000), data: nil, nonce: 2, gasPrice: gasPrice ?? BigUInt(9000000), gasLimit: BigUInt(500000), chainId: EthereumNetwork.goerli.intValue)
119+
let tx = EthereumTransaction(from: nil, to: "0x3c1bd6b420448cf16a389c8b0115ccb3660bb854", value: BigUInt(1), data: nil, nonce: 2, gasPrice: gasPrice ?? BigUInt(9000000), gasLimit: BigUInt(30000), chainId: EthereumNetwork.goerli.intValue)
120120

121121
let txHash = try await client?.eth_sendRawTransaction(tx, withAccount: account!)
122122
XCTAssertNotNil(txHash, "No tx hash, ensure key is valid in TestConfig.swift")

web3sTests/ENS/ENSOffchainTests.swift

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ class ENSOffchainTests: XCTestCase {
6060
ens: "resolver.eth",
6161
mode: .allowOffchainLookup
6262
)
63-
XCTAssertEqual(EthereumAddress("0x342cf18d3e41de491aa1a3067574c849ada6a2ad"), ens)
63+
XCTAssertEqual(EthereumAddress("0xd7a4f6473f32ac2af804b3686ae8f1932bc35750"), ens)
6464
} catch {
6565
XCTFail("Expected ens but failed \(error).")
6666
}
@@ -104,7 +104,7 @@ class ENSOffchainTests: XCTestCase {
104104
ens: "resolver.eth",
105105
mode: .allowOffchainLookup
106106
)
107-
XCTAssertEqual(EthereumAddress("0x342cf18d3e41de491aa1a3067574c849ada6a2ad"), ens)
107+
XCTAssertEqual(EthereumAddress("0xd7a4f6473f32ac2af804b3686ae8f1932bc35750"), ens)
108108
} catch {
109109
XCTFail("Expected ens but failed \(error).")
110110
}
@@ -139,7 +139,7 @@ class ENSOffchainTests: XCTestCase {
139139
ens: "resolver.eth",
140140
mode: .allowOffchainLookup
141141
)
142-
XCTAssertEqual(EthereumAddress("0x342cf18d3e41de491aa1a3067574c849ada6a2ad"), ens)
142+
XCTAssertEqual(EthereumAddress("0xd7a4f6473f32ac2af804b3686ae8f1932bc35750"), ens)
143143
} catch {
144144
XCTFail("Expected ens but failed \(error).")
145145
}

web3swift/src/Client/BaseEthereumClient.swift

Lines changed: 0 additions & 253 deletions
Original file line numberDiff line numberDiff line change
@@ -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()

web3swift/src/Client/Protocols/EthereumClientProtocol.swift

Lines changed: 4 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,12 @@ public enum CallResolution {
1111
case offchainAllowed(maxRedirects: Int)
1212
}
1313

14+
// MARK: EthereumClient (HTTP or Websocket)
15+
1416
public protocol EthereumClientProtocol: EthereumRPCProtocol, AnyObject {
1517
var network: EthereumNetwork? { get }
1618

19+
// Legacy result-based API
1720
func net_version(completionHandler: @escaping (Result<EthereumNetwork, EthereumClientError>) -> Void)
1821
func eth_gasPrice(completionHandler: @escaping (Result<BigUInt, EthereumClientError>) -> Void)
1922
func eth_blockNumber(completionHandler: @escaping (Result<Int, EthereumClientError>) -> Void)
@@ -39,31 +42,9 @@ public protocol EthereumClientProtocol: EthereumRPCProtocol, AnyObject {
3942
func eth_getLogs(addresses: [EthereumAddress]?, orTopics: [[String]?]?, fromBlock: EthereumBlock, toBlock: EthereumBlock, completionHandler: @escaping (Result<[EthereumLog], EthereumClientError>) -> Void)
4043
func eth_getBlockByNumber(_ block: EthereumBlock, completionHandler: @escaping (Result<EthereumBlockInfo, EthereumClientError>) -> Void)
4144
func getLogs(addresses: [EthereumAddress]?, topics: Topics?, fromBlock: EthereumBlock, toBlock: EthereumBlock) async throws -> [EthereumLog]
42-
43-
// Async/Await
44-
func net_version() async throws -> EthereumNetwork
45-
func eth_gasPrice() async throws -> BigUInt
46-
func eth_blockNumber() async throws -> Int
47-
func eth_getBalance(address: EthereumAddress, block: EthereumBlock) async throws -> BigUInt
48-
func eth_getCode(address: EthereumAddress, block: EthereumBlock) async throws -> String
49-
func eth_estimateGas(_ transaction: EthereumTransaction) async throws -> BigUInt
50-
func eth_sendRawTransaction(_ transaction: EthereumTransaction, withAccount account: EthereumAccountProtocol) async throws -> String
51-
func eth_getTransaction(byHash txHash: String) async throws -> EthereumTransaction
52-
func eth_getTransactionReceipt(txHash: String) async throws -> EthereumTransactionReceipt
53-
func eth_call(
54-
_ transaction: EthereumTransaction,
55-
block: EthereumBlock
56-
) async throws -> String
57-
func eth_call(
58-
_ transaction: EthereumTransaction,
59-
resolution: CallResolution,
60-
block: EthereumBlock
61-
) async throws -> String
62-
func eth_getLogs(addresses: [EthereumAddress]?, topics: [String?]?, fromBlock: EthereumBlock, toBlock: EthereumBlock) async throws -> [EthereumLog]
63-
func eth_getLogs(addresses: [EthereumAddress]?, orTopics: [[String]?]?, fromBlock: EthereumBlock, toBlock: EthereumBlock) async throws -> [EthereumLog]
64-
func eth_getBlockByNumber(_ block: EthereumBlock) async throws -> EthereumBlockInfo
6545
}
6646

47+
// MARK: - Websocket
6748
#if canImport(NIO)
6849
import NIOWebSocket
6950

0 commit comments

Comments
 (0)