Skip to content

Commit

Permalink
Merge pull request #64 from currybab/feature/pendingTransactions
Browse files Browse the repository at this point in the history
adds txpool function and its local node test
  • Loading branch information
shamatar authored Oct 18, 2018
2 parents ef4a602 + 45a7dab commit 784473f
Show file tree
Hide file tree
Showing 7 changed files with 199 additions and 4 deletions.
8 changes: 8 additions & 0 deletions web3swift.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -200,6 +200,8 @@
81FECD5B211AECBD006DA367 /* Web3+Eth+ObjC.swift in Sources */ = {isa = PBXBuildFile; fileRef = 81FECD5A211AECBD006DA367 /* Web3+Eth+ObjC.swift */; };
81FECD5C211AECBD006DA367 /* Web3+Eth+ObjC.swift in Sources */ = {isa = PBXBuildFile; fileRef = 81FECD5A211AECBD006DA367 /* Web3+Eth+ObjC.swift */; };
81FECD5E211AEFCE006DA367 /* web3swift_ObjC_Tests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 81FECD5D211AEFCE006DA367 /* web3swift_ObjC_Tests.swift */; };
985BFD4A216CE8B100B28C14 /* Web3+TxPool.swift in Sources */ = {isa = PBXBuildFile; fileRef = 985BFD49216CE8B100B28C14 /* Web3+TxPool.swift */; };
985BFD4E216E2E0A00B28C14 /* Promise+Web3+TxPool.swift in Sources */ = {isa = PBXBuildFile; fileRef = 985BFD4D216E2E0A00B28C14 /* Promise+Web3+TxPool.swift */; };
B219DC172154F3EE0035BF94 /* ENSResolver.swift in Sources */ = {isa = PBXBuildFile; fileRef = B219DC162154F3EE0035BF94 /* ENSResolver.swift */; };
B2E668CE214F8A7B00C3CC2D /* ENS.swift in Sources */ = {isa = PBXBuildFile; fileRef = B2E668CD214F8A7B00C3CC2D /* ENS.swift */; };
B350A445E5DB35C60E59AD70 /* libPods-web3swift-macOS.a in Frameworks */ = {isa = PBXBuildFile; fileRef = 57F8C9C48884592DCF561393 /* libPods-web3swift-macOS.a */; };
Expand Down Expand Up @@ -362,6 +364,8 @@
81FECD63211AF162006DA367 /* web3swift-Bridging-Header.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = "web3swift-Bridging-Header.h"; sourceTree = "<group>"; };
8349531F1984454E50389370 /* libPods-web3swift-iOS_Tests.a */ = {isa = PBXFileReference; explicitFileType = archive.ar; includeInIndex = 0; path = "libPods-web3swift-iOS_Tests.a"; sourceTree = BUILT_PRODUCTS_DIR; };
8675751D91DB2DBC9E7A3469 /* libPods-web3swift-macOS_Tests.a */ = {isa = PBXFileReference; explicitFileType = archive.ar; includeInIndex = 0; path = "libPods-web3swift-macOS_Tests.a"; sourceTree = BUILT_PRODUCTS_DIR; };
985BFD49216CE8B100B28C14 /* Web3+TxPool.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "Web3+TxPool.swift"; sourceTree = "<group>"; };
985BFD4D216E2E0A00B28C14 /* Promise+Web3+TxPool.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "Promise+Web3+TxPool.swift"; sourceTree = "<group>"; };
A5E8AF69880F5141B4AC9DF0 /* libPods-web3swift-iOS.a */ = {isa = PBXFileReference; explicitFileType = archive.ar; includeInIndex = 0; path = "libPods-web3swift-iOS.a"; sourceTree = BUILT_PRODUCTS_DIR; };
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>"; };
B219DC162154F3EE0035BF94 /* ENSResolver.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ENSResolver.swift; sourceTree = "<group>"; };
Expand Down Expand Up @@ -687,6 +691,7 @@
81A1824720D7DDA20016741F /* Promise+Web3+Personal+Sign.swift */,
81A1824A20D7DF1B0016741F /* Promise+Web3+Personal+UnlockAccount.swift */,
81195AAF20D7FF8500ABC6B1 /* Promise+Web3+Contract+GetIndexedEvents.swift */,
985BFD4D216E2E0A00B28C14 /* Promise+Web3+TxPool.swift */,
);
path = Classes;
sourceTree = "<group>";
Expand Down Expand Up @@ -717,6 +722,7 @@
81C0FCFC20449D1400D82FAF /* Web3+TransactionIntermediate.swift */,
818D16CE204D42910084D2A4 /* Web3+EventParser.swift */,
81EB1E4A208173D7003BD47F /* Web3+Personal.swift */,
985BFD49216CE8B100B28C14 /* Web3+TxPool.swift */,
);
path = Classes;
sourceTree = "<group>";
Expand Down Expand Up @@ -1149,6 +1155,8 @@
81FECD55211AEB49006DA367 /* EthereumAddress+ObjC.swift in Sources */,
81C5DA282072E18200424CD6 /* NativeTypesEncoding+Extensions.swift in Sources */,
8123E1C7200CBAC200B6D3AB /* Dictionary+Extension.swift in Sources */,
985BFD4E216E2E0A00B28C14 /* Promise+Web3+TxPool.swift in Sources */,
985BFD4A216CE8B100B28C14 /* Web3+TxPool.swift in Sources */,
81D7D97820A61E3800A193EC /* EventFiltering.swift in Sources */,
8160E5CE20B8245A0070070B /* IBAN.swift in Sources */,
81A7B2872143DBF6004CD2C7 /* EIP681.swift in Sources */,
Expand Down
63 changes: 63 additions & 0 deletions web3swift/Promises/Classes/Promise+Web3+TxPool.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
//
// Promise+Web3+TxPool.swift
// web3swift-iOS
//
// Created by Jun Park on 10/10/2018.
// Copyright © 2018 The Matter Inc. All rights reserved.
//

import Foundation
import BigInt
import PromiseKit


extension web3.TxPool {
public func getInspectPromise() -> Promise<[String:[String:[String:String]]]> {
let request = JSONRPCRequestFabric.prepareRequest(.getTxPoolInspect, parameters: [])
let rp = web3.dispatch(request)
let queue = web3.requestDispatcher.queue
return rp.map(on: queue ) { response in
guard let value: [String:[String:[String:String]]] = response.getValue() else {
if response.error != nil {
throw Web3Error.nodeError(desc: response.error!.message)
}
throw Web3Error.nodeError(desc: "Invalid value from Ethereum node")
}
return value
}
}

public func getStatusPromise() -> Promise<[String: Int]> {
let request = JSONRPCRequestFabric.prepareRequest(.getTxPoolStatus, parameters: [])
let rp = web3.dispatch(request)
let queue = web3.requestDispatcher.queue
return rp.map(on: queue ) { response in
guard let value: [String: String] = response.result as? [String: String] else {
if response.error != nil {
throw Web3Error.nodeError(desc: response.error!.message)
}
throw Web3Error.nodeError(desc: "Invalid value from Ethereum node")
}
var result: [String: Int] = [:]
for (k, v) in value {
result[k] = Int.init(v.stripHexPrefix(), radix: 16)
}
return result
}
}

public func getContentPromise() -> Promise<[String:[String:[String:[String:String?]]]]> {
let request = JSONRPCRequestFabric.prepareRequest(.getTxPoolContent, parameters: [])
let rp = web3.dispatch(request)
let queue = web3.requestDispatcher.queue
return rp.map(on: queue ) { response in
guard let value: [String:[String:[String:[String:String?]]]] = response.getValue() else {
if response.error != nil {
throw Web3Error.nodeError(desc: response.error!.message)
}
throw Web3Error.nodeError(desc: "Invalid value from Ethereum node")
}
return value
}
}
}
24 changes: 24 additions & 0 deletions web3swift/Web3/Classes/Web3+Instance.swift
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,30 @@ public class web3: Web3OptionsInheritable {
}
}

var txPoolInstance: web3.TxPool?

/// Public web3.personal.* namespace.
public var txPool: web3.TxPool {
if (self.txPoolInstance != nil) {
return self.txPoolInstance!
}
self.txPoolInstance = web3.TxPool(provider : self.provider, web3: self)
return self.txPoolInstance!
}

public class TxPool: Web3OptionsInheritable {
var provider:Web3Provider
// weak var web3: web3?
var web3: web3
public var options: Web3Options {
return self.web3.options
}
public init(provider prov: Web3Provider, web3 web3instance: web3) {
provider = prov
web3 = web3instance
}
}

var walletInstance: web3.Web3Wallet?

/// Public web3.wallet.* namespace.
Expand Down
9 changes: 7 additions & 2 deletions web3swift/Web3/Classes/Web3+JSONRPC.swift
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,8 @@ public struct JSONRPCresponse: Decodable{
Int.self,
Bool.self,
[String:String].self,
[String:Int].self]
[String:Int].self,
[String:[String:[String:[String]]]].self]

public init(from decoder: Decoder) throws {
let container = try decoder.container(keyedBy: JSONRPCresponseKeys.self)
Expand Down Expand Up @@ -152,7 +153,11 @@ public struct JSONRPCresponse: Decodable{
result = rawValue
} else if let rawValue = try? container.decodeIfPresent([String: Int].self, forKey: .result) {
result = rawValue
}
} else if let rawValue = try? container.decodeIfPresent([String:[String:[String:String]]].self, forKey: .result) {
result = rawValue
} else if let rawValue = try? container.decodeIfPresent([String:[String:[String:[String:String?]]]].self, forKey: .result) {
result = rawValue
}
self.init(id: id, jsonrpc: jsonrpc, result: result, error: nil)
}

Expand Down
9 changes: 9 additions & 0 deletions web3swift/Web3/Classes/Web3+Methods.swift
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,9 @@ public enum JSONRPCmethod: String, Encodable {
case personalSign = "eth_sign"
case unlockAccount = "personal_unlockAccount"
case getLogs = "eth_getLogs"
case getTxPoolInspect = "txpool_inspect"
case getTxPoolStatus = "txpool_status"
case getTxPoolContent = "txpool_content"

public var requiredNumOfParameters: Int {
get {
Expand All @@ -55,6 +58,12 @@ public enum JSONRPCmethod: String, Encodable {
return 0
case .getAccounts:
return 0
case .getTxPoolStatus:
return 0
case .getTxPoolContent:
return 0
case .getTxPoolInspect:
return 0
default:
return 1
}
Expand Down
50 changes: 50 additions & 0 deletions web3swift/Web3/Classes/Web3+TxPool.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
//
// Web3+TxPool.swift
// web3swift-iOS
//
// Created by Jun Park on 09/10/2018.
// Copyright © 2018 The Matter Inc. All rights reserved.
//

import Foundation
import Result
import BigInt


extension web3.TxPool {
public func getInspect() -> Result<[String:[String:[String:String]]], Web3Error> {
do {
let result = try self.getInspectPromise().wait()
return Result(result)
} catch {
if let err = error as? Web3Error {
return Result.failure(err)
}
return Result.failure(Web3Error.generalError(err: error))
}
}

public func getStatus() -> Result<[String: Int], Web3Error> {
do {
let result = try self.getStatusPromise().wait()
return Result(result)
} catch {
if let err = error as? Web3Error {
return Result.failure(err)
}
return Result.failure(Web3Error.generalError(err: error))
}
}

public func getContent() -> Result<[String:[String:[String:[String:String?]]]], Web3Error> {
do {
let result = try self.getContentPromise().wait()
return Result(result)
} catch {
if let err = error as? Web3Error {
return Result.failure(err)
}
return Result.failure(Web3Error.generalError(err: error))
}
}
}
40 changes: 38 additions & 2 deletions web3swiftTests/web3swift_local_node_Tests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,42 @@ class web3swift_local_node_Tests: XCTestCase {
// print(result)
// }
// }


func testTxPoolStatus() {
let web3 = Web3.new(URL.init(string: "http://127.0.0.1:8545")!)!
let result = web3.txPool.getStatus()

switch result {
case .failure(let error):
print(error)
XCTFail()
case .success(let response):
print(response)
}
}

func testTxPoolInspect() {
let web3 = Web3.new(URL.init(string: "http://127.0.0.1:8545")!)!
let result = web3.txPool.getInspect()

switch result {
case .failure(let error):
print(error)
XCTFail()
case .success(let response):
print(response)
}
}

func testTxPoolContent() {
let web3 = Web3.new(URL.init(string: "http://127.0.0.1:8545")!)!
let result = web3.txPool.getContent()

switch result {
case .failure(let error):
print(error)
XCTFail()
case .success(let response):
print(response)
}
}
}

0 comments on commit 784473f

Please sign in to comment.