6
6
7
7
import Foundation
8
8
9
- public enum JSONRPCmethod : String , Encodable {
9
+ public typealias Hash = String // 32 bytes hash of block
10
+ public typealias Receipt = Hash
11
+ public typealias Address = Hash // 20 bytes
12
+
13
+ /// Ethereum JSON RPC API Calls
14
+ public enum EthJSONRPC {
15
+ // MARK: - Official API
16
+ // 0 parameter in call
17
+ case gasPrice
18
+ case blockNumber
19
+ case getNetwork
20
+ case getAccounts
21
+ // ??
22
+ case estimateGas
23
+
24
+ case sendRawTransaction( Hash )
25
+ case sendTransaction( TransactionParameters )
26
+ case getTransactionByHash( Hash )
27
+ case getTransactionReceipt( Receipt )
28
+ case getLogs( EventFilterParameters )
29
+ case personalSign( Address , Data )
30
+ case call( EthereumTransaction )
31
+ case getTransactionCount( Address , BlockNumber )
32
+ case getBalance( Address , BlockNumber )
33
+
34
+ /// Returns the value from a storage position at a given address.
35
+ ///
36
+ /// - Parameters:
37
+ /// - Address: Address
38
+ /// - Storage: slot
39
+ /// - BlockNumber: sd
40
+ case getStorageAt( Address , Hash , BlockNumber )
41
+
42
+ case getCode( Address , BlockNumber )
43
+ case getBlockByHash( Hash , Bool )
44
+ case getBlockByNumber( Hash , Bool )
45
+
46
+ /// Returns fee history with a respect to given setup
47
+ ///
48
+ /// Generates and returns an estimate of how much gas is necessary to allow the transaction to complete.
49
+ /// The transaction will not be added to the blockchain. Note that the estimate may be significantly more
50
+ /// than the amount of gas actually used by the transaction, for a variety of reasons including EVM mechanics and node performance.
51
+ ///
52
+ /// - Parameters:
53
+ /// - UInt: Requested range of blocks. Clients will return less than the requested range if not all blocks are available.
54
+ /// - BlockNumber: Highest block of the requested range.
55
+ /// - [Double]: A monotonically increasing list of percentile values.
56
+ /// For each block in the requested range, the transactions will be sorted in ascending order
57
+ /// by effective tip per gas and the coresponding effective tip for the percentile will be determined, accounting for gas consumed."
58
+ case feeHistory( UInt , BlockNumber , [ Double ] )
59
+
60
+ // MARK: - Additional API
61
+ /// Creates new account.
62
+ ///
63
+ /// Note: it becomes the new current unlocked account. There can only be one unlocked account at a time.
64
+ ///
65
+ /// - Parameters:
66
+ /// - String: Password for the new account.
67
+ case createAccount( String ) // No in Eth API
68
+
69
+ /// Unlocks specified account for use.
70
+ ///
71
+ /// If permanent unlocking is disabled (the default) then the duration argument will be ignored,
72
+ /// and the account will be unlocked for a single signing.
73
+ /// With permanent locking enabled, the duration sets the number of seconds to hold the account open for.
74
+ /// It will default to 300 seconds. Passing 0 unlocks the account indefinitely.
75
+ ///
76
+ /// There can only be one unlocked account at a time.
77
+ ///
78
+ /// - Parameters:
79
+ /// - Address: The address of the account to unlock.
80
+ /// - String: Passphrase to unlock the account.
81
+ /// - UInt?: Duration in seconds how long the account should remain unlocked for.
82
+ case unlockAccount( Address , String , UInt ? )
83
+ case getTxPoolStatus // No in Eth API
84
+ case getTxPoolContent // No in Eth API
85
+ case getTxPoolInspect // No in Eth API
86
+ }
87
+
88
+ extension EthJSONRPC {
89
+ var call : String {
90
+ switch self {
91
+ case . gasPrice: return " eth_gasPrice "
92
+ case . blockNumber: return " eth_blockNumber "
93
+ case . getNetwork: return " net_version "
94
+ case . getAccounts: return " eth_accounts "
95
+ case . sendRawTransaction: return " eth_sendRawTransaction "
96
+ case . sendTransaction: return " eth_sendTransaction "
97
+ case . getTransactionByHash: return " eth_getTransactionByHash "
98
+ case . getTransactionReceipt: return " eth_getTransactionReceipt "
99
+ case . personalSign: return " eth_sign "
100
+ case . getLogs: return " eth_getLogs "
101
+ case . call: return " eth_call "
102
+ case . estimateGas: return " eth_estimateGas "
103
+ case . getTransactionCount: return " eth_getTransactionCount "
104
+ case . getBalance: return " eth_getBalance "
105
+ case . getStorageAt: return " eth_getStorageAt "
106
+ case . getCode: return " eth_getCode "
107
+ case . getBlockByHash: return " eth_getBlockByHash "
108
+ case . getBlockByNumber: return " eth_getBlockByNumber "
109
+ case . feeHistory: return " eth_feeHistory "
110
+
111
+ case . unlockAccount: return " personal_unlockAccount "
112
+ case . createAccount: return " personal_createAccount "
113
+ case . getTxPoolStatus: return " txpool_status "
114
+ case . getTxPoolContent: return " txpool_content "
115
+ case . getTxPoolInspect: return " txpool_inspect "
116
+ }
117
+ }
118
+ }
119
+
120
+ extension EthJSONRPC {
121
+ var responseType : JRONRPCResponstType . Type {
122
+ switch self {
123
+ default : return String . self
124
+ }
125
+ }
126
+ }
10
127
128
+ extension EthJSONRPC {
129
+ var method : REST {
130
+ switch self {
131
+ default : return . POST
132
+ }
133
+ }
134
+ }
135
+
136
+ extension EthJSONRPC {
137
+ static func sendRequest( with call: EthJSONRPC ) async throws -> some JRONRPCResponstType {
138
+ switch call {
139
+ case . gasPrice: return " eth_gasPrice "
140
+ case . blockNumber: return " eth_blockNumber "
141
+ case . getNetwork: return " net_version "
142
+ case . getAccounts: return " eth_accounts "
143
+ case . sendRawTransaction: return " eth_sendRawTransaction "
144
+ case . sendTransaction: return " eth_sendTransaction "
145
+ case . getTransactionByHash: return " eth_getTransactionByHash "
146
+ case . getTransactionReceipt: return " eth_getTransactionReceipt "
147
+ case . personalSign: return " eth_sign "
148
+ case . getLogs: return " eth_getLogs "
149
+ case . call: return " eth_call "
150
+ case . estimateGas: return " eth_estimateGas "
151
+ case . getTransactionCount: return " eth_getTransactionCount "
152
+ case . getBalance: return " eth_getBalance "
153
+ case . getStorageAt: return " eth_getStorageAt "
154
+ case . getCode: return " eth_getCode "
155
+ case . getBlockByHash: return " eth_getBlockByHash "
156
+ case . getBlockByNumber: return " eth_getBlockByNumber "
157
+ case . feeHistory: return " eth_feeHistory "
158
+
159
+ case . unlockAccount: return " personal_unlockAccount "
160
+ case . createAccount: return " personal_createAccount "
161
+ case . getTxPoolStatus: return " txpool_status "
162
+ case . getTxPoolContent: return " txpool_content "
163
+ case . getTxPoolInspect: return " txpool_inspect "
164
+ }
165
+ }
166
+ }
167
+
168
+ private extension EthJSONRPC {
169
+ static func setupRequest( for call: EthJSONRPC ) -> URLRequest {
170
+ let url = URL ( string: " https://mainnet.infura.io/v3/4406c3acf862426c83991f1752c46dd8 " ) !
171
+ var urlRequest = URLRequest ( url: url, cachePolicy: . reloadIgnoringCacheData)
172
+ urlRequest. httpMethod = call. method. rawValue
173
+ urlRequest. setValue ( " application/json " , forHTTPHeaderField: " Content-Type " )
174
+ urlRequest. setValue ( " application/json " , forHTTPHeaderField: " Accept " )
175
+ return urlRequest
176
+ }
177
+ }
178
+
179
+ public enum REST : String {
180
+ case POST
181
+ case GET
182
+ }
183
+
184
+ protocol JRONRPCResponstType : Decodable { }
185
+
186
+ extension String : JRONRPCResponstType { }
187
+
188
+
189
+ public enum JSONRPCmethod : String , Encodable {
11
190
// 0 parameter in call
12
191
case gasPrice = " eth_gasPrice "
13
192
case blockNumber = " eth_blockNumber "
@@ -16,7 +195,6 @@ public enum JSONRPCmethod: String, Encodable {
16
195
case getTxPoolStatus = " txpool_status "
17
196
case getTxPoolContent = " txpool_content "
18
197
case getTxPoolInspect = " txpool_inspect "
19
- case estimateGas = " eth_estimateGas "
20
198
21
199
// 1 parameter in call
22
200
case sendRawTransaction = " eth_sendRawTransaction "
@@ -30,6 +208,7 @@ public enum JSONRPCmethod: String, Encodable {
30
208
31
209
// 2 parameters in call
32
210
case call = " eth_call "
211
+ case estimateGas = " eth_estimateGas "
33
212
case getTransactionCount = " eth_getTransactionCount "
34
213
case getBalance = " eth_getBalance "
35
214
case getStorageAt = " eth_getStorageAt "
0 commit comments