-
Notifications
You must be signed in to change notification settings - Fork 170
/
Copy pathEthereumClientProtocol.swift
96 lines (76 loc) · 5.53 KB
/
EthereumClientProtocol.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
//
// web3.swift
// Copyright © 2022 Argent Labs Limited. All rights reserved.
//
import BigInt
import Foundation
public enum CallResolution {
case noOffchain(failOnExecutionError: Bool)
case offchainAllowed(maxRedirects: Int)
}
// MARK: EthereumClient (HTTP or Websocket)
public protocol EthereumClientProtocol: EthereumRPCProtocol, AnyObject {
// Legacy result-based API
func net_version(completionHandler: @escaping (Result<EthereumNetwork, EthereumClientError>) -> Void)
func eth_gasPrice(completionHandler: @escaping (Result<BigUInt, EthereumClientError>) -> Void)
func eth_blockNumber(completionHandler: @escaping (Result<Int, EthereumClientError>) -> Void)
func eth_getBalance(address: EthereumAddress, block: EthereumBlock, completionHandler: @escaping (Result<BigUInt, EthereumClientError>) -> Void)
func eth_getCode(address: EthereumAddress, block: EthereumBlock, completionHandler: @escaping (Result<String, EthereumClientError>) -> Void)
func eth_estimateGas(_ transaction: EthereumTransaction, completionHandler: @escaping (Result<BigUInt, EthereumClientError>) -> Void)
func eth_sendRawTransaction(_ transaction: EthereumTransaction, withAccount account: EthereumAccountProtocol, completionHandler: @escaping (Result<String, EthereumClientError>) -> Void)
func eth_getTransactionCount(address: EthereumAddress, block: EthereumBlock, completionHandler: @escaping (Result<Int, EthereumClientError>) -> Void)
func eth_getTransaction(byHash txHash: String, completionHandler: @escaping (Result<EthereumTransaction, EthereumClientError>) -> Void)
func eth_getTransactionReceipt(txHash: String, completionHandler: @escaping (Result<EthereumTransactionReceipt, EthereumClientError>) -> Void)
func eth_call(
_ transaction: EthereumTransaction,
block: EthereumBlock,
completionHandler: @escaping (Result<String, EthereumClientError>) -> Void
)
func eth_call(
_ transaction: EthereumTransaction,
resolution: CallResolution,
block: EthereumBlock,
completionHandler: @escaping (Result<String, EthereumClientError>) -> Void
)
func eth_getLogs(addresses: [EthereumAddress]?, topics: [String?]?, fromBlock: EthereumBlock, toBlock: EthereumBlock, completionHandler: @escaping (Result<[EthereumLog], EthereumClientError>) -> Void)
func eth_getLogs(addresses: [EthereumAddress]?, orTopics: [[String]?]?, fromBlock: EthereumBlock, toBlock: EthereumBlock, completionHandler: @escaping (Result<[EthereumLog], EthereumClientError>) -> Void)
func eth_getBlockByNumber(_ block: EthereumBlock, completionHandler: @escaping (Result<EthereumBlockInfo, EthereumClientError>) -> Void)
func getLogs(addresses: [EthereumAddress]?, topics: Topics?, fromBlock: EthereumBlock, toBlock: EthereumBlock) async throws -> [EthereumLog]
}
// MARK: - Websocket
#if canImport(NIO)
import NIOWebSocket
public protocol EthereumClientWebSocketProtocol: EthereumClientProtocol {
var delegate: EthereumWebSocketClientDelegate? { get set }
var currentState: WebSocketState { get }
func connect()
func disconnect(code: WebSocketErrorCode)
func refresh()
func subscribe(type: EthereumSubscriptionType, completionHandler: @escaping (Result<EthereumSubscription, EthereumClientError>) -> Void)
func subscribe(type: EthereumSubscriptionType) async throws -> EthereumSubscription
func unsubscribe(_ subscription: EthereumSubscription, completionHandler: @escaping (Result<Bool, EthereumClientError>) -> Void)
func unsubscribe(_ subscription: EthereumSubscription) async throws -> Bool
func pendingTransactions(onSubscribe: @escaping (Result<EthereumSubscription, EthereumClientError>) -> Void, onData: @escaping (String) -> Void)
func pendingTransactions(onData: @escaping (String) -> Void) async throws -> EthereumSubscription
func newBlockHeaders(onSubscribe: @escaping (Result<EthereumSubscription, EthereumClientError>) -> Void, onData: @escaping (EthereumHeader) -> Void)
func newBlockHeaders(onData: @escaping (EthereumHeader) -> Void) async throws -> EthereumSubscription
func logs(logsParams: LogsParams?, onSubscribe: @escaping (Result<EthereumSubscription, EthereumClientError>) -> Void, onData: @escaping (EthereumLog) -> Void)
func logs(logsParams: LogsParams?, onData: @escaping (EthereumLog) -> Void) async throws -> EthereumSubscription
func syncing(onSubscribe: @escaping (Result<EthereumSubscription, EthereumClientError>) -> Void, onData: @escaping (EthereumSyncStatus) -> Void)
func syncing(onData: @escaping (EthereumSyncStatus) -> Void) async throws -> EthereumSubscription
}
public protocol EthereumWebSocketClientDelegate: AnyObject {
func onNewPendingTransaction(subscription: EthereumSubscription, txHash: String)
func onNewBlockHeader(subscription: EthereumSubscription, header: EthereumHeader)
func onLog(subscription: EthereumSubscription, log: EthereumLog)
func onSyncing(subscription: EthereumSubscription, sync: EthereumSyncStatus)
func onWebSocketReconnect()
}
extension EthereumWebSocketClientDelegate {
func onNewPendingTransaction(subscription: EthereumSubscription, txHash: String) {}
func onNewBlockHeader(subscription: EthereumSubscription, header: EthereumHeader) {}
func onLog(subscription: EthereumSubscription, log: EthereumLog) {}
func onSyncing(subscription: EthereumSubscription, sync: EthereumSyncStatus) {}
func onWebSocketReconnect() {}
}
#endif