Skip to content

Commit 784473f

Browse files
authored
Merge pull request #64 from currybab/feature/pendingTransactions
adds txpool function and its local node test
2 parents ef4a602 + 45a7dab commit 784473f

File tree

7 files changed

+199
-4
lines changed

7 files changed

+199
-4
lines changed

web3swift.xcodeproj/project.pbxproj

+8
Original file line numberDiff line numberDiff line change
@@ -200,6 +200,8 @@
200200
81FECD5B211AECBD006DA367 /* Web3+Eth+ObjC.swift in Sources */ = {isa = PBXBuildFile; fileRef = 81FECD5A211AECBD006DA367 /* Web3+Eth+ObjC.swift */; };
201201
81FECD5C211AECBD006DA367 /* Web3+Eth+ObjC.swift in Sources */ = {isa = PBXBuildFile; fileRef = 81FECD5A211AECBD006DA367 /* Web3+Eth+ObjC.swift */; };
202202
81FECD5E211AEFCE006DA367 /* web3swift_ObjC_Tests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 81FECD5D211AEFCE006DA367 /* web3swift_ObjC_Tests.swift */; };
203+
985BFD4A216CE8B100B28C14 /* Web3+TxPool.swift in Sources */ = {isa = PBXBuildFile; fileRef = 985BFD49216CE8B100B28C14 /* Web3+TxPool.swift */; };
204+
985BFD4E216E2E0A00B28C14 /* Promise+Web3+TxPool.swift in Sources */ = {isa = PBXBuildFile; fileRef = 985BFD4D216E2E0A00B28C14 /* Promise+Web3+TxPool.swift */; };
203205
B219DC172154F3EE0035BF94 /* ENSResolver.swift in Sources */ = {isa = PBXBuildFile; fileRef = B219DC162154F3EE0035BF94 /* ENSResolver.swift */; };
204206
B2E668CE214F8A7B00C3CC2D /* ENS.swift in Sources */ = {isa = PBXBuildFile; fileRef = B2E668CD214F8A7B00C3CC2D /* ENS.swift */; };
205207
B350A445E5DB35C60E59AD70 /* libPods-web3swift-macOS.a in Frameworks */ = {isa = PBXBuildFile; fileRef = 57F8C9C48884592DCF561393 /* libPods-web3swift-macOS.a */; };
@@ -362,6 +364,8 @@
362364
81FECD63211AF162006DA367 /* web3swift-Bridging-Header.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = "web3swift-Bridging-Header.h"; sourceTree = "<group>"; };
363365
8349531F1984454E50389370 /* libPods-web3swift-iOS_Tests.a */ = {isa = PBXFileReference; explicitFileType = archive.ar; includeInIndex = 0; path = "libPods-web3swift-iOS_Tests.a"; sourceTree = BUILT_PRODUCTS_DIR; };
364366
8675751D91DB2DBC9E7A3469 /* libPods-web3swift-macOS_Tests.a */ = {isa = PBXFileReference; explicitFileType = archive.ar; includeInIndex = 0; path = "libPods-web3swift-macOS_Tests.a"; sourceTree = BUILT_PRODUCTS_DIR; };
367+
985BFD49216CE8B100B28C14 /* Web3+TxPool.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "Web3+TxPool.swift"; sourceTree = "<group>"; };
368+
985BFD4D216E2E0A00B28C14 /* Promise+Web3+TxPool.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "Promise+Web3+TxPool.swift"; sourceTree = "<group>"; };
365369
A5E8AF69880F5141B4AC9DF0 /* libPods-web3swift-iOS.a */ = {isa = PBXFileReference; explicitFileType = archive.ar; includeInIndex = 0; path = "libPods-web3swift-iOS.a"; sourceTree = BUILT_PRODUCTS_DIR; };
366370
A9ADDE40292A17C21B8D5516 /* Pods-web3swift-iOS.release.xcconfig */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = text.xcconfig; name = "Pods-web3swift-iOS.release.xcconfig"; path = "Pods/Target Support Files/Pods-web3swift-iOS/Pods-web3swift-iOS.release.xcconfig"; sourceTree = "<group>"; };
367371
B219DC162154F3EE0035BF94 /* ENSResolver.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ENSResolver.swift; sourceTree = "<group>"; };
@@ -687,6 +691,7 @@
687691
81A1824720D7DDA20016741F /* Promise+Web3+Personal+Sign.swift */,
688692
81A1824A20D7DF1B0016741F /* Promise+Web3+Personal+UnlockAccount.swift */,
689693
81195AAF20D7FF8500ABC6B1 /* Promise+Web3+Contract+GetIndexedEvents.swift */,
694+
985BFD4D216E2E0A00B28C14 /* Promise+Web3+TxPool.swift */,
690695
);
691696
path = Classes;
692697
sourceTree = "<group>";
@@ -717,6 +722,7 @@
717722
81C0FCFC20449D1400D82FAF /* Web3+TransactionIntermediate.swift */,
718723
818D16CE204D42910084D2A4 /* Web3+EventParser.swift */,
719724
81EB1E4A208173D7003BD47F /* Web3+Personal.swift */,
725+
985BFD49216CE8B100B28C14 /* Web3+TxPool.swift */,
720726
);
721727
path = Classes;
722728
sourceTree = "<group>";
@@ -1149,6 +1155,8 @@
11491155
81FECD55211AEB49006DA367 /* EthereumAddress+ObjC.swift in Sources */,
11501156
81C5DA282072E18200424CD6 /* NativeTypesEncoding+Extensions.swift in Sources */,
11511157
8123E1C7200CBAC200B6D3AB /* Dictionary+Extension.swift in Sources */,
1158+
985BFD4E216E2E0A00B28C14 /* Promise+Web3+TxPool.swift in Sources */,
1159+
985BFD4A216CE8B100B28C14 /* Web3+TxPool.swift in Sources */,
11521160
81D7D97820A61E3800A193EC /* EventFiltering.swift in Sources */,
11531161
8160E5CE20B8245A0070070B /* IBAN.swift in Sources */,
11541162
81A7B2872143DBF6004CD2C7 /* EIP681.swift in Sources */,
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
//
2+
// Promise+Web3+TxPool.swift
3+
// web3swift-iOS
4+
//
5+
// Created by Jun Park on 10/10/2018.
6+
// Copyright © 2018 The Matter Inc. All rights reserved.
7+
//
8+
9+
import Foundation
10+
import BigInt
11+
import PromiseKit
12+
13+
14+
extension web3.TxPool {
15+
public func getInspectPromise() -> Promise<[String:[String:[String:String]]]> {
16+
let request = JSONRPCRequestFabric.prepareRequest(.getTxPoolInspect, parameters: [])
17+
let rp = web3.dispatch(request)
18+
let queue = web3.requestDispatcher.queue
19+
return rp.map(on: queue ) { response in
20+
guard let value: [String:[String:[String:String]]] = response.getValue() else {
21+
if response.error != nil {
22+
throw Web3Error.nodeError(desc: response.error!.message)
23+
}
24+
throw Web3Error.nodeError(desc: "Invalid value from Ethereum node")
25+
}
26+
return value
27+
}
28+
}
29+
30+
public func getStatusPromise() -> Promise<[String: Int]> {
31+
let request = JSONRPCRequestFabric.prepareRequest(.getTxPoolStatus, parameters: [])
32+
let rp = web3.dispatch(request)
33+
let queue = web3.requestDispatcher.queue
34+
return rp.map(on: queue ) { response in
35+
guard let value: [String: String] = response.result as? [String: String] else {
36+
if response.error != nil {
37+
throw Web3Error.nodeError(desc: response.error!.message)
38+
}
39+
throw Web3Error.nodeError(desc: "Invalid value from Ethereum node")
40+
}
41+
var result: [String: Int] = [:]
42+
for (k, v) in value {
43+
result[k] = Int.init(v.stripHexPrefix(), radix: 16)
44+
}
45+
return result
46+
}
47+
}
48+
49+
public func getContentPromise() -> Promise<[String:[String:[String:[String:String?]]]]> {
50+
let request = JSONRPCRequestFabric.prepareRequest(.getTxPoolContent, parameters: [])
51+
let rp = web3.dispatch(request)
52+
let queue = web3.requestDispatcher.queue
53+
return rp.map(on: queue ) { response in
54+
guard let value: [String:[String:[String:[String:String?]]]] = response.getValue() else {
55+
if response.error != nil {
56+
throw Web3Error.nodeError(desc: response.error!.message)
57+
}
58+
throw Web3Error.nodeError(desc: "Invalid value from Ethereum node")
59+
}
60+
return value
61+
}
62+
}
63+
}

web3swift/Web3/Classes/Web3+Instance.swift

+24
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,30 @@ public class web3: Web3OptionsInheritable {
8686
}
8787
}
8888

89+
var txPoolInstance: web3.TxPool?
90+
91+
/// Public web3.personal.* namespace.
92+
public var txPool: web3.TxPool {
93+
if (self.txPoolInstance != nil) {
94+
return self.txPoolInstance!
95+
}
96+
self.txPoolInstance = web3.TxPool(provider : self.provider, web3: self)
97+
return self.txPoolInstance!
98+
}
99+
100+
public class TxPool: Web3OptionsInheritable {
101+
var provider:Web3Provider
102+
// weak var web3: web3?
103+
var web3: web3
104+
public var options: Web3Options {
105+
return self.web3.options
106+
}
107+
public init(provider prov: Web3Provider, web3 web3instance: web3) {
108+
provider = prov
109+
web3 = web3instance
110+
}
111+
}
112+
89113
var walletInstance: web3.Web3Wallet?
90114

91115
/// Public web3.wallet.* namespace.

web3swift/Web3/Classes/Web3+JSONRPC.swift

+7-2
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,8 @@ public struct JSONRPCresponse: Decodable{
108108
Int.self,
109109
Bool.self,
110110
[String:String].self,
111-
[String:Int].self]
111+
[String:Int].self,
112+
[String:[String:[String:[String]]]].self]
112113

113114
public init(from decoder: Decoder) throws {
114115
let container = try decoder.container(keyedBy: JSONRPCresponseKeys.self)
@@ -152,7 +153,11 @@ public struct JSONRPCresponse: Decodable{
152153
result = rawValue
153154
} else if let rawValue = try? container.decodeIfPresent([String: Int].self, forKey: .result) {
154155
result = rawValue
155-
}
156+
} else if let rawValue = try? container.decodeIfPresent([String:[String:[String:String]]].self, forKey: .result) {
157+
result = rawValue
158+
} else if let rawValue = try? container.decodeIfPresent([String:[String:[String:[String:String?]]]].self, forKey: .result) {
159+
result = rawValue
160+
}
156161
self.init(id: id, jsonrpc: jsonrpc, result: result, error: nil)
157162
}
158163

web3swift/Web3/Classes/Web3+Methods.swift

+9
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,9 @@ public enum JSONRPCmethod: String, Encodable {
2929
case personalSign = "eth_sign"
3030
case unlockAccount = "personal_unlockAccount"
3131
case getLogs = "eth_getLogs"
32+
case getTxPoolInspect = "txpool_inspect"
33+
case getTxPoolStatus = "txpool_status"
34+
case getTxPoolContent = "txpool_content"
3235

3336
public var requiredNumOfParameters: Int {
3437
get {
@@ -55,6 +58,12 @@ public enum JSONRPCmethod: String, Encodable {
5558
return 0
5659
case .getAccounts:
5760
return 0
61+
case .getTxPoolStatus:
62+
return 0
63+
case .getTxPoolContent:
64+
return 0
65+
case .getTxPoolInspect:
66+
return 0
5867
default:
5968
return 1
6069
}
+50
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
//
2+
// Web3+TxPool.swift
3+
// web3swift-iOS
4+
//
5+
// Created by Jun Park on 09/10/2018.
6+
// Copyright © 2018 The Matter Inc. All rights reserved.
7+
//
8+
9+
import Foundation
10+
import Result
11+
import BigInt
12+
13+
14+
extension web3.TxPool {
15+
public func getInspect() -> Result<[String:[String:[String:String]]], Web3Error> {
16+
do {
17+
let result = try self.getInspectPromise().wait()
18+
return Result(result)
19+
} catch {
20+
if let err = error as? Web3Error {
21+
return Result.failure(err)
22+
}
23+
return Result.failure(Web3Error.generalError(err: error))
24+
}
25+
}
26+
27+
public func getStatus() -> Result<[String: Int], Web3Error> {
28+
do {
29+
let result = try self.getStatusPromise().wait()
30+
return Result(result)
31+
} catch {
32+
if let err = error as? Web3Error {
33+
return Result.failure(err)
34+
}
35+
return Result.failure(Web3Error.generalError(err: error))
36+
}
37+
}
38+
39+
public func getContent() -> Result<[String:[String:[String:[String:String?]]]], Web3Error> {
40+
do {
41+
let result = try self.getContentPromise().wait()
42+
return Result(result)
43+
} catch {
44+
if let err = error as? Web3Error {
45+
return Result.failure(err)
46+
}
47+
return Result.failure(Web3Error.generalError(err: error))
48+
}
49+
}
50+
}

web3swiftTests/web3swift_local_node_Tests.swift

+38-2
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,42 @@ class web3swift_local_node_Tests: XCTestCase {
104104
// print(result)
105105
// }
106106
// }
107-
108-
107+
func testTxPoolStatus() {
108+
let web3 = Web3.new(URL.init(string: "http://127.0.0.1:8545")!)!
109+
let result = web3.txPool.getStatus()
110+
111+
switch result {
112+
case .failure(let error):
113+
print(error)
114+
XCTFail()
115+
case .success(let response):
116+
print(response)
117+
}
118+
}
119+
120+
func testTxPoolInspect() {
121+
let web3 = Web3.new(URL.init(string: "http://127.0.0.1:8545")!)!
122+
let result = web3.txPool.getInspect()
123+
124+
switch result {
125+
case .failure(let error):
126+
print(error)
127+
XCTFail()
128+
case .success(let response):
129+
print(response)
130+
}
131+
}
132+
133+
func testTxPoolContent() {
134+
let web3 = Web3.new(URL.init(string: "http://127.0.0.1:8545")!)!
135+
let result = web3.txPool.getContent()
136+
137+
switch result {
138+
case .failure(let error):
139+
print(error)
140+
XCTFail()
141+
case .success(let response):
142+
print(response)
143+
}
144+
}
109145
}

0 commit comments

Comments
 (0)