-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1 from torusresearch/promiseImplementation
Promise implementation
- Loading branch information
Showing
10 changed files
with
256 additions
and
12 deletions.
There are no files selected for viewing
Binary file modified
BIN
+126 KB
(160%)
...m/xcode/package.xcworkspace/xcuserdata/shubham.xcuserdatad/UserInterfaceState.xcuserstate
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,11 @@ | ||
# fetch-node-details | ||
|
||
A description of this package. | ||
|
||
## Installation | ||
|
||
### Swift Package Manage | ||
|
||
``` | ||
.package(url: "https://github.com/torusresearch/fetch-node-details-swift5.git", from: "0.0.1"), | ||
``` |
187 changes: 187 additions & 0 deletions
187
Sources/fetch-node-details/fetchNodeDetails+promise.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,187 @@ | ||
// | ||
// File.swift | ||
// | ||
// | ||
// Created by Shubham on 13/3/20. | ||
// | ||
|
||
import Foundation | ||
import web3swift | ||
import BigInt | ||
import PromiseKit | ||
|
||
extension FetchNodeDetails { | ||
|
||
public func getCurrentEpochPromise() throws -> Promise<Int>{ | ||
let contractMethod = "currentEpoch" // Contract method you want to call | ||
let parameters: [AnyObject] = [] // Parameters for contract method | ||
let extraData: Data = Data() // Extra data for contract method | ||
var options = TransactionOptions.defaultOptions | ||
options.from = walletAddress | ||
options.gasPrice = .automatic | ||
options.gasLimit = .automatic | ||
let tx = self.contract.read( | ||
contractMethod, | ||
parameters: parameters, | ||
extraData: extraData, | ||
transactionOptions: options)! | ||
|
||
let txPromise = tx.callPromise() | ||
let rp = Promise<Int>.pending() | ||
|
||
txPromise.done{ data in | ||
let epoch = data.first?.value | ||
guard let newEpoch = epoch else { throw "some error"} | ||
print(newEpoch) | ||
rp.resolver.fulfill(Int("\(newEpoch)")!) | ||
} | ||
|
||
return rp.promise | ||
} | ||
|
||
public func getEpochInfoPromise(epoch : Int) throws -> Promise<EpochInfo>{ | ||
let contractMethod = "getEpochInfo" | ||
let parameters: [AnyObject] = [epoch as AnyObject] // Parameters for contract method | ||
let extraData: Data = Data() // Extra data for contract method | ||
var options = TransactionOptions.defaultOptions | ||
options.from = walletAddress | ||
options.gasPrice = .automatic | ||
options.gasLimit = .automatic | ||
|
||
let tx = self.contract.read( | ||
contractMethod, | ||
parameters: parameters, | ||
extraData: extraData, | ||
transactionOptions: options)! | ||
// | ||
// let result = tx.callPromise() | ||
// let (promise, resolver) = Promise<EpochInfo>.pending() | ||
|
||
let returnPromise = Promise<EpochInfo>{ seal in | ||
let txPromise = tx.callPromise() | ||
txPromise.done{ response in | ||
var nodeList = response["nodeList"] as! Array<Encodable> //Unable to convert to Array<String> | ||
nodeList = nodeList.map{ (el) -> String in | ||
let address = el as! EthereumAddress | ||
return String(describing: address.address) | ||
} | ||
//print(nodeList) | ||
|
||
guard let id = response["id"] else { throw "Casting for id from Any? -> Any failed"} | ||
guard let n = response["n"] else { throw "Casting for n from Any? -> Any failed"} | ||
guard let k = response["k"] else { throw "Casting for k from Any? -> Any failed"} | ||
guard let t = response["t"] else { throw "Casting for t from Any? -> Any failed"} | ||
guard let prevEpoch = response["prevEpoch"] else { throw "Casting for prevEpoch from Any? -> Any failed"} | ||
guard let nextEpoch = response["nextEpoch"] else { throw "Casting for nextEpoch from Any? -> Any failed"} | ||
|
||
let object = EpochInfo(_id: "\(id)", _n: "\(n)", _k: "\(k)", _t: "\(t)", _nodeList: nodeList as! Array<String>, _prevEpoch: "\(prevEpoch)", _nextEpoch: "\(nextEpoch)") | ||
print() | ||
seal.fulfill(object) | ||
|
||
}.catch{error in | ||
seal.reject(error) | ||
} | ||
|
||
} | ||
return returnPromise | ||
} | ||
|
||
public func getNodeEndpointPromise(nodeEthAddress: String) throws -> Promise<NodeInfo> { | ||
let contractMethod = "getNodeDetails" | ||
let parameters: [AnyObject] = [nodeEthAddress as AnyObject] // Parameters for contract method | ||
let extraData: Data = Data() // Extra data for contract method | ||
var options = TransactionOptions.defaultOptions | ||
options.from = walletAddress | ||
options.gasPrice = .automatic | ||
options.gasLimit = .automatic | ||
//print(extraData) | ||
|
||
let tx = self.contract.read( | ||
contractMethod, | ||
parameters: parameters, | ||
extraData: extraData, | ||
transactionOptions: options)! | ||
|
||
let returnPromise = Promise<NodeInfo>{ seal in | ||
let txPromise = tx.callPromise() | ||
txPromise.done{ response in | ||
// Unwraping Any? -> Any | ||
guard let declaredIp = response["declaredIp"] else { throw "Casting for declaredIp from Any? to Any failed" } | ||
guard let position = response["position"] else { throw "Casting for position from Any? to Any failed" } | ||
guard let pubKx = response["pubKx"] else { throw "Casting for pubKx from Any? to Any failed" } | ||
guard let pubKy = response["pubKy"] else { throw "Casting for pubKy from Any? to Any failed" } | ||
guard let tmP2PListenAddress = response["tmP2PListenAddress"] else { throw "Casting for tmP2PListenAddress from Any? to Any failed" } | ||
guard let p2pListenAddress = response["p2pListenAddress"] else { throw "Casting for p2pListenAddress from Any? to Any failed" } | ||
|
||
let object = NodeInfo(_declaredIp: "\(declaredIp)", _position: "\(position)", _pubKx: "\(pubKx)", _pubKy: "\(pubKy)", _tmP2PListenAddress: "\(tmP2PListenAddress)", _p2pListenAddress: "\(p2pListenAddress)") | ||
seal.fulfill(object) | ||
}.catch{err in seal.reject(err)} | ||
} | ||
return returnPromise | ||
} | ||
|
||
public func getNodeDetailsPromise() throws -> Promise<Bool>{ | ||
//if(self.nodeDetails.getUpdated()) { return self.nodeDetails } | ||
//let (promise, resolver) = Promise<Int>.pending() | ||
print("ASDF") | ||
var currentEpoch: Int = -1; | ||
|
||
let returnPromise = Promise<Bool> { seal in | ||
let currentEpochPromise = try self.getCurrentEpochPromise(); | ||
currentEpochPromise.then{ response -> Promise<EpochInfo> in | ||
print("currentEpoch is", response) | ||
currentEpoch = response | ||
return try self.getEpochInfoPromise(epoch: response) | ||
}.then{epochInfo -> Guarantee<[Result<NodeInfo>]> in | ||
let nodelist = epochInfo.getNodeList(); | ||
print("nodeList is", nodelist) | ||
var torusIndexes:[BigInt] = Array() | ||
var nodeEndPoints:[NodeInfo] = Array() | ||
var getNodeInfoPromisesArray:[Promise<NodeInfo>] = Array() | ||
for i in 0..<nodelist.count{ | ||
torusIndexes.append(BigInt(i+1)) | ||
//print(BigInt(i+1)) | ||
getNodeInfoPromisesArray.append(try self.getNodeEndpointPromise(nodeEthAddress: nodelist[i])) | ||
} | ||
return when(resolved: getNodeInfoPromisesArray) | ||
}.done{ results in | ||
//print(results) | ||
var updatedEndpoints: Array<String> = Array() | ||
var updatedNodePub:Array<TorusNodePub> = Array() | ||
|
||
for result in results{ | ||
switch result { | ||
case .fulfilled(let value): | ||
// print(value) | ||
let endPointElement:NodeInfo = value; | ||
let endpoint = "https://" + endPointElement.getDeclaredIp().split(separator: ":")[0] + "/jrpc"; | ||
updatedEndpoints.append(endpoint) | ||
|
||
let hexPubX = String(BigInt(endPointElement.getPubKx(), radix:10)!, radix:16, uppercase: true) | ||
let hexPubY = String(BigInt(endPointElement.getPubKy(), radix:10)!, radix:16, uppercase: true) | ||
updatedNodePub.append(TorusNodePub(_X: hexPubX, _Y: hexPubY)) | ||
default: | ||
seal.reject("error with node info") | ||
} | ||
|
||
} | ||
print(updatedNodePub, updatedEndpoints) | ||
|
||
|
||
self.nodeDetails.setNodeListAddress(nodeListAddress: self.proxyAddress.address); | ||
self.nodeDetails.setCurrentEpoch(currentEpoch: String(currentEpoch)); | ||
self.nodeDetails.setTorusNodeEndpoints(torusNodeEndpoints: updatedEndpoints); | ||
self.nodeDetails.setTorusNodePub(torusNodePub: updatedNodePub); | ||
self.nodeDetails.setUpdated(updated: true); | ||
|
||
seal.fulfill(true) | ||
}.catch { error in | ||
print(error) | ||
seal.reject("get epoch info failed") | ||
} | ||
} | ||
|
||
return returnPromise | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters