Skip to content

Commit 5ec2d21

Browse files
Refactor NetworkLayer in sake of consistency.
1 parent 35ba700 commit 5ec2d21

19 files changed

+466
-403
lines changed

Sources/Core/EthereumNetwork/APIRequestParameter.swift

Lines changed: 0 additions & 201 deletions
This file was deleted.
Lines changed: 111 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,111 @@
1+
//
2+
// APIRequest+ComputedProperties.swift
3+
//
4+
//
5+
// Created by Yaroslav Yashin on 12.07.2022.
6+
//
7+
8+
import Foundation
9+
10+
extension APIRequest {
11+
var method: REST {
12+
switch self {
13+
default: return .POST
14+
}
15+
}
16+
17+
public var encodedBody: Data {
18+
let request = RequestBody(method: self.call, params: self.parameters)
19+
// this is safe to force try this here
20+
// Because request must failed to compile if it not conformable with `Encodable` protocol
21+
return try! JSONEncoder().encode(request)
22+
}
23+
24+
var parameters: [RequestParameter] {
25+
switch self {
26+
case .gasPrice, .blockNumber, .getNetwork, .getAccounts, .getTxPoolStatus, .getTxPoolContent, .getTxPoolInspect:
27+
return [RequestParameter]()
28+
29+
case .estimateGas(let transactionParameters, let blockNumber):
30+
return [RequestParameter.transaction(transactionParameters), RequestParameter.string(blockNumber.stringValue)]
31+
32+
case let .sendRawTransaction(hash):
33+
return [RequestParameter.string(hash)]
34+
35+
case let .sendTransaction(transactionParameters):
36+
return [RequestParameter.transaction(transactionParameters)]
37+
38+
case .getTransactionByHash(let hash):
39+
return [RequestParameter.string(hash)]
40+
41+
case .getTransactionReceipt(let receipt):
42+
return [RequestParameter.string(receipt)]
43+
44+
case .getLogs(let eventFilterParameters):
45+
return [RequestParameter.eventFilter(eventFilterParameters)]
46+
47+
case .personalSign(let address, let string):
48+
return [RequestParameter.string(address), RequestParameter.string(string)]
49+
50+
case .call(let transactionParameters, let blockNumber):
51+
return [RequestParameter.transaction(transactionParameters), RequestParameter.string(blockNumber.stringValue)]
52+
53+
case .getTransactionCount(let address, let blockNumber):
54+
return [RequestParameter.string(address), RequestParameter.string(blockNumber.stringValue)]
55+
56+
case .getBalance(let address, let blockNumber):
57+
return [RequestParameter.string(address), RequestParameter.string(blockNumber.stringValue)]
58+
59+
case .getStorageAt(let address, let bigUInt, let blockNumber):
60+
return [RequestParameter.string(address), RequestParameter.string(bigUInt.hexString), RequestParameter.string(blockNumber.stringValue)]
61+
62+
case .getCode(let address, let blockNumber):
63+
return [RequestParameter.string(address), RequestParameter.string(blockNumber.stringValue)]
64+
65+
case .getBlockByHash(let hash, let bool):
66+
return [RequestParameter.string(hash), RequestParameter.bool(bool)]
67+
68+
case .getBlockByNumber(let block, let bool):
69+
return [RequestParameter.string(block.stringValue), RequestParameter.bool(bool)]
70+
71+
case .feeHistory(let uInt, let blockNumber, let array):
72+
return [RequestParameter.string(uInt.hexString), RequestParameter.string(blockNumber.stringValue), RequestParameter.doubleArray(array)]
73+
74+
case .createAccount(let string):
75+
return [RequestParameter.string(string)]
76+
77+
case .unlockAccount(let address, let string, let uInt):
78+
return [RequestParameter.string(address), RequestParameter.string(string), RequestParameter.uint(uInt ?? 0)]
79+
}
80+
}
81+
82+
public var call: String {
83+
switch self {
84+
case .gasPrice: return "eth_gasPrice"
85+
case .blockNumber: return "eth_blockNumber"
86+
case .getNetwork: return "net_version"
87+
case .getAccounts: return "eth_accounts"
88+
case .sendRawTransaction: return "eth_sendRawTransaction"
89+
case .sendTransaction: return "eth_sendTransaction"
90+
case .getTransactionByHash: return "eth_getTransactionByHash"
91+
case .getTransactionReceipt: return "eth_getTransactionReceipt"
92+
case .personalSign: return "eth_sign"
93+
case .getLogs: return "eth_getLogs"
94+
case .call: return "eth_call"
95+
case .estimateGas: return "eth_estimateGas"
96+
case .getTransactionCount: return "eth_getTransactionCount"
97+
case .getBalance: return "eth_getBalance"
98+
case .getStorageAt: return "eth_getStorageAt"
99+
case .getCode: return "eth_getCode"
100+
case .getBlockByHash: return "eth_getBlockByHash"
101+
case .getBlockByNumber: return "eth_getBlockByNumber"
102+
case .feeHistory: return "eth_feeHistory"
103+
104+
case .unlockAccount: return "personal_unlockAccount"
105+
case .createAccount: return "personal_createAccount"
106+
case .getTxPoolStatus: return "txpool_status"
107+
case .getTxPoolContent: return "txpool_content"
108+
case .getTxPoolInspect: return "txpool_inspect"
109+
}
110+
}
111+
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
//
2+
// File.swift
3+
//
4+
//
5+
// Created by Yaroslav Yashin on 12.07.2022.
6+
//
7+
8+
import Foundation
9+
10+
/// JSON RPC response structure for serialization and deserialization purposes.
11+
public struct APIResponse<Result>: Decodable where Result: APIResultType {
12+
public var id: Int
13+
public var jsonrpc = "2.0"
14+
public var result: Result
15+
}
16+
17+
enum REST: String {
18+
case POST
19+
case GET
20+
}
21+
22+
struct RequestBody: Encodable {
23+
var jsonrpc = "2.0"
24+
var id = Counter.increment()
25+
26+
var method: String
27+
var params: [RequestParameter]
28+
}

0 commit comments

Comments
 (0)