-
Notifications
You must be signed in to change notification settings - Fork 115
Expand file tree
/
Copy pathTVFCollector.swift
More file actions
129 lines (111 loc) · 3.8 KB
/
Copy pathTVFCollector.swift
File metadata and controls
129 lines (111 loc) · 3.8 KB
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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
import Foundation
// MARK: - TVFCollectorProtocol
public protocol TVFCollectorProtocol {
func collect(
rpcMethod: String,
rpcParams: AnyCodable,
chainID: Blockchain,
rpcResult: RPCResult?,
tag: Int
) -> TVFData?
}
// MARK: - TVFCollector
public class TVFCollector: TVFCollectorProtocol {
// MARK: - Tag Enum
enum Tag: Int {
case sessionRequest = 1108
case sessionResponse = 1109
}
private let chainCollectors: [ChainTVFCollector]
public init() {
self.chainCollectors = [
EVMTVFCollector(),
SolanaTVFCollector(),
AlgorandTVFCollector(),
TronTVFCollector(),
XRPLTVFCollector(),
HederaTVFCollector(),
CosmosTVFCollector(),
NearTVFCollector(),
BitcoinTVFCollector(),
StacksTVFCollector(),
SuiTVFCollector(),
PolkadotTVFCollector(),
TonTVFCollector(),
StellarTVFCollector()
]
}
// MARK: - Single Public Method
/// Collects TVF data based on the given parameters and the `tag`.
///
/// - Parameters:
/// - rpcMethod: The RPC method (e.g., `"eth_sendTransaction"`).
/// - rpcParams: An `AnyCodable` containing arbitrary JSON or primitive content.
/// - chainID: A `Blockchain` instance (e.g., `Blockchain("eip155:1")`).
/// - rpcResult: An optional `RPCResult` representing `.response(AnyCodable)` or `.error(...)`.
/// - tag: Integer that should map to `.sessionRequest (1108)` or `.sessionResponse (1109)`.
///
/// - Returns: `TVFData` if the tag is valid. When no chain-specific
/// collector supports the method, the returned `TVFData` will contain only
/// the provided method and chain information.
public func collect(
rpcMethod: String,
rpcParams: AnyCodable,
chainID: Blockchain,
rpcResult: RPCResult?,
tag: Int
) -> TVFData? {
// Convert the incoming 'tag' Int into the Tag enum
guard let theTag = Tag(rawValue: tag) else {
return nil
}
// Find a collector that supports this method, if any
let collector = chainCollectors.first { $0.supportsMethod(rpcMethod) }
// Extract contract addresses if this is a request
let contractAddresses = theTag == .sessionRequest ?
collector?.extractContractAddresses(rpcMethod: rpcMethod, rpcParams: rpcParams) : nil
// Parse transaction hashes if this is a response
let txHashes = theTag == .sessionResponse ?
collector?.parseTxHashes(rpcMethod: rpcMethod, rpcResult: rpcResult, rpcParams: rpcParams) : nil
return TVFData(
rpcMethods: [rpcMethod],
chainId: chainID,
txHashes: txHashes,
contractAddresses: contractAddresses
)
}
}
#if DEBUG
public class TVFCollectorMock: TVFCollectorProtocol {
public struct CollectCall {
let method: String
let params: AnyCodable
let chainID: Blockchain?
let result: RPCResult?
let tag: Int
}
private(set) public var collectCalls: [CollectCall] = []
public var mockResult: TVFData?
public init(mockResult: TVFData? = nil) {
self.mockResult = mockResult
}
public func collect(
rpcMethod: String,
rpcParams: AnyCodable,
chainID: Blockchain,
rpcResult: RPCResult?,
tag: Int
) -> TVFData? {
collectCalls.append(
CollectCall(
method: rpcMethod,
params: rpcParams,
chainID: chainID,
result: RPCResult.response(AnyCodable("")),
tag: tag
)
)
return mockResult
}
}
#endif