Skip to content

Commit c68eb0f

Browse files
Merge pull request #368 from SwiftyLimi/develop
Added Promise: Support for getCode function
2 parents 65f0090 + 57e93ff commit c68eb0f

File tree

1 file changed

+33
-0
lines changed

1 file changed

+33
-0
lines changed
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
//
2+
// Promise+Web3+Eth+GetCode.swift
3+
// web3swift
4+
//
5+
// Created by Ndriqim Haxhaj on 8/25/21.
6+
//
7+
8+
import Foundation
9+
import PromiseKit
10+
import BigInt
11+
12+
extension web3.Eth {
13+
public func getCodePromise(address: EthereumAddress, onBlock: String = "latest") -> Promise<String> {
14+
let addr = address.address
15+
return getCodePromise(address: addr, onBlock: onBlock)
16+
}
17+
public func getCodePromise(address: String, onBlock: String = "latest") -> Promise<String> {
18+
let request = JSONRPCRequestFabric.prepareRequest(.getCode, parameters: [address.lowercased(), onBlock])
19+
let rp = web3.dispatch(request)
20+
let queue = web3.requestDispatcher.queue
21+
return rp.map(on: queue ) { response in
22+
guard let value: String = response.getValue() else {
23+
if response.error != nil {
24+
throw Web3Error.nodeError(desc: response.error!.message)
25+
}
26+
throw Web3Error.nodeError(desc: "Invalid value from Ethereum node")
27+
}
28+
return value
29+
}
30+
}
31+
}
32+
33+

0 commit comments

Comments
 (0)