@@ -23,237 +23,6 @@ public struct Counter {
23
23
}
24
24
}
25
25
26
- /// JSON RPC request structure for serialization and deserialization purposes.
27
- //public struct JSONRPCrequest<T: Encodable>: Encodable {
28
- public struct JSONRPCrequest : Encodable {
29
- public var jsonrpc : String = " 2.0 "
30
- public var method : JSONRPCmethod ?
31
- public var params : [ RequestParameter ] = [ ]
32
- public var id : UInt = Counter . increment ( )
33
-
34
- enum CodingKeys : String , CodingKey {
35
- case jsonrpc
36
- case method
37
- case params
38
- case id
39
- }
40
-
41
- public func encode( to encoder: Encoder ) throws {
42
- var container = encoder. container ( keyedBy: CodingKeys . self)
43
- try container. encode ( jsonrpc, forKey: . jsonrpc)
44
- try container. encode ( method? . rawValue, forKey: . method)
45
- try container. encode ( params, forKey: . params)
46
- try container. encode ( id, forKey: . id)
47
- }
48
-
49
- public var isValid : Bool {
50
- get {
51
- if self . method == nil {
52
- return false
53
- }
54
- guard let method = self . method else { return false }
55
- return method. requiredNumOfParameters == self . params. count
56
- }
57
- }
58
- }
59
-
60
- /// JSON RPC batch request structure for serialization and deserialization purposes.
61
- public struct JSONRPCrequestBatch : Encodable {
62
- var requests : [ JSONRPCrequest ]
63
-
64
- public func encode( to encoder: Encoder ) throws {
65
- var container = encoder. singleValueContainer ( )
66
- try container. encode ( self . requests)
67
- }
68
- }
69
-
70
- /// JSON RPC response structure for serialization and deserialization purposes.
71
- public struct JSONRPCresponse : Decodable {
72
- public var id : Int
73
- public var jsonrpc = " 2.0 "
74
- public var result : Any ?
75
- public var error : ErrorMessage ?
76
- public var message : String ?
77
-
78
- enum JSONRPCresponseKeys : String , CodingKey {
79
- case id = " id "
80
- case jsonrpc = " jsonrpc "
81
- case result = " result "
82
- case error = " error "
83
- }
84
-
85
- public init ( id: Int , jsonrpc: String , result: Any ? , error: ErrorMessage ? ) {
86
- self . id = id
87
- self . jsonrpc = jsonrpc
88
- self . result = result
89
- self . error = error
90
- }
91
-
92
- public struct ErrorMessage : Decodable {
93
- public var code : Int
94
- public var message : String
95
- }
96
-
97
- internal var decodableTypes : [ Decodable . Type ] = [
98
- [ EventLog ] . self,
99
- [ TransactionDetails ] . self,
100
- [ TransactionReceipt ] . self,
101
- [ Block ] . self,
102
- [ String ] . self,
103
- [ Int ] . self,
104
- [ Bool ] . self,
105
- EventLog . self,
106
- TransactionDetails . self,
107
- TransactionReceipt . self,
108
- Block . self,
109
- String . self,
110
- Int . self,
111
- Bool . self,
112
- [ String : String ] . self,
113
- [ String : Int ] . self,
114
- [ String : [ String : [ String : [ String ] ] ] ] . self,
115
- Web3 . Oracle. FeeHistory. self
116
- ]
117
-
118
- // FIXME: Make me a real generic
119
- public init ( from decoder: Decoder ) throws {
120
- let container = try decoder. container ( keyedBy: JSONRPCresponseKeys . self)
121
- let id : Int = try container. decode ( Int . self, forKey: . id)
122
- let jsonrpc : String = try container. decode ( String . self, forKey: . jsonrpc)
123
- let errorMessage = try container. decodeIfPresent ( ErrorMessage . self, forKey: . error)
124
- if errorMessage != nil {
125
- self . init ( id: id, jsonrpc: jsonrpc, result: nil , error: errorMessage)
126
- return
127
- }
128
- // TODO: make me generic (DecodableFromHex or Decodable)
129
- /// This is all available types of `result` to init
130
- /// Now its type is `Any?`, since you're typecasting this any to exact each response,
131
- /// coz you're knowing response to what request it would be,
132
- /// and you're could be sure about API Response structure.
133
- ///
134
- /// So since we're typecasting this explicitly each tyme, `result` property could be set
135
- /// to protocol type like `Decodable` or `DevodableFromHex` or something,
136
- /// and this code could be reduced so hard.
137
- var result : Any ? = nil
138
- if let rawValue = try ? container. decodeIfPresent ( String . self, forKey: . result) {
139
- result = rawValue
140
- } else if let rawValue = try ? container. decodeIfPresent ( Int . self, forKey: . result) {
141
- result = rawValue
142
- } else if let rawValue = try ? container. decodeIfPresent ( Bool . self, forKey: . result) {
143
- result = rawValue
144
- } else if let rawValue = try ? container. decodeIfPresent ( EventLog . self, forKey: . result) {
145
- result = rawValue
146
- } else if let rawValue = try ? container. decodeIfPresent ( Block . self, forKey: . result) {
147
- result = rawValue
148
- } else if let rawValue = try ? container. decodeIfPresent ( TransactionReceipt . self, forKey: . result) {
149
- result = rawValue
150
- } else if let rawValue = try ? container. decodeIfPresent ( TransactionDetails . self, forKey: . result) {
151
- result = rawValue
152
- } else if let rawValue = try ? container. decodeIfPresent ( [ EventLog ] . self, forKey: . result) {
153
- result = rawValue
154
- } else if let rawValue = try ? container. decodeIfPresent ( [ Block ] . self, forKey: . result) {
155
- result = rawValue
156
- } else if let rawValue = try ? container. decodeIfPresent ( [ TransactionReceipt ] . self, forKey: . result) {
157
- result = rawValue
158
- } else if let rawValue = try ? container. decodeIfPresent ( [ TransactionDetails ] . self, forKey: . result) {
159
- result = rawValue
160
- } else if let rawValue = try ? container. decodeIfPresent ( TxPoolStatus . self, forKey: . result) {
161
- result = rawValue
162
- } else if let rawValue = try ? container. decodeIfPresent ( TxPoolContent . self, forKey: . result) {
163
- result = rawValue
164
- } else if let rawValue = try ? container. decodeIfPresent ( [ Bool ] . self, forKey: . result) {
165
- result = rawValue
166
- } else if let rawValue = try ? container. decodeIfPresent ( [ Int ] . self, forKey: . result) {
167
- result = rawValue
168
- } else if let rawValue = try ? container. decodeIfPresent ( [ String ] . self, forKey: . result) {
169
- result = rawValue
170
- } else if let rawValue = try ? container. decodeIfPresent ( [ String : String ] . self, forKey: . result) {
171
- result = rawValue
172
- } else if let rawValue = try ? container. decodeIfPresent ( [ String : Int ] . self, forKey: . result) {
173
- result = rawValue
174
- } else if let rawValue = try ? container. decodeIfPresent ( [ String : [ String : [ String : String ] ] ] . self, forKey: . result) {
175
- result = rawValue
176
- } else if let rawValue = try ? container. decodeIfPresent ( [ String : [ String : [ String : [ String : String ? ] ] ] ] . self, forKey: . result) {
177
- result = rawValue
178
- } else if let rawValue = try ? container. decodeIfPresent ( Web3 . Oracle. FeeHistory. self, forKey: . result) {
179
- result = rawValue
180
- }
181
- self . init ( id: id, jsonrpc: jsonrpc, result: result, error: nil )
182
- }
183
-
184
- // FIXME: Make me a real generic
185
- // MARK: - This fits for DecodableFromHex
186
- /// Get the JSON RCP reponse value by deserializing it into some native <T> class.
187
- ///
188
- /// Returns nil if serialization fails
189
- public func getValue< T> ( ) -> T ? {
190
- let type = T . self
191
-
192
- if type == BigUInt . self {
193
- guard let string = self . result as? String else { return nil }
194
- guard let value = BigUInt ( string. stripHexPrefix ( ) , radix: 16 ) else { return nil }
195
- return value as? T
196
- } else if type == BigInt . self {
197
- guard let string = self . result as? String else { return nil }
198
- guard let value = BigInt ( string. stripHexPrefix ( ) , radix: 16 ) else { return nil }
199
- return value as? T
200
- } else if type == Data . self {
201
- guard let string = self . result as? String else { return nil }
202
- guard let value = Data . fromHex ( string) else { return nil }
203
- return value as? T
204
- } else if type == EthereumAddress . self {
205
- guard let string = self . result as? String else { return nil }
206
- guard let value = EthereumAddress ( string, ignoreChecksum: true ) else { return nil }
207
- return value as? T
208
- }
209
- // else if slf == String.self {
210
- // guard let value = self.result as? T else {return nil}
211
- // return value
212
- // } else if slf == Int.self {
213
- // guard let value = self.result as? T else {return nil}
214
- // return value
215
- // }
216
- else if type == [ BigUInt ] . self {
217
- guard let string = self . result as? [ String ] else { return nil }
218
- let values = string. compactMap { ( str) -> BigUInt ? in
219
- return BigUInt ( str. stripHexPrefix ( ) , radix: 16 )
220
- }
221
- return values as? T
222
- } else if type == [ BigInt ] . self {
223
- guard let string = self . result as? [ String ] else { return nil }
224
- let values = string. compactMap { ( str) -> BigInt ? in
225
- return BigInt ( str. stripHexPrefix ( ) , radix: 16 )
226
- }
227
- return values as? T
228
- } else if type == [ Data ] . self {
229
- guard let string = self . result as? [ String ] else { return nil }
230
- let values = string. compactMap { ( str) -> Data ? in
231
- return Data . fromHex ( str)
232
- }
233
- return values as? T
234
- } else if type == [ EthereumAddress ] . self {
235
- guard let string = self . result as? [ String ] else { return nil }
236
- let values = string. compactMap { ( str) -> EthereumAddress ? in
237
- return EthereumAddress ( str, ignoreChecksum: true )
238
- }
239
- return values as? T
240
- }
241
- guard let value = self . result as? T else { return nil }
242
- return value
243
- }
244
- }
245
-
246
- /// JSON RPC batch response structure for serialization and deserialization purposes.
247
- public struct JSONRPCresponseBatch : Decodable {
248
- var responses : [ JSONRPCresponse ]
249
-
250
- public init ( from decoder: Decoder ) throws {
251
- let container = try decoder. singleValueContainer ( )
252
- let responses = try container. decode ( [ JSONRPCresponse ] . self)
253
- self . responses = responses
254
- }
255
- }
256
-
257
26
/// Transaction parameters JSON structure for interaction with Ethereum node.
258
27
public struct TransactionParameters : Codable {
259
28
/// accessList parameter JSON structure
0 commit comments