@ryanrfox/sdk 0.0.2 → 2.0.4
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +145 -29
- package/package.json +84 -8
- package/src/_esm/actions/index.d.ts +8 -0
- package/src/_esm/actions/index.d.ts.map +1 -0
- package/src/_esm/actions/index.js +8 -0
- package/src/_esm/actions/index.js.map +1 -0
- package/src/_esm/actions/sendTransactionBatch.d.ts +77 -0
- package/src/_esm/actions/sendTransactionBatch.d.ts.map +1 -0
- package/src/_esm/actions/sendTransactionBatch.js +245 -0
- package/src/_esm/actions/sendTransactionBatch.js.map +1 -0
- package/src/_esm/chains/chainConfig.d.ts +30 -0
- package/src/_esm/chains/chainConfig.d.ts.map +1 -0
- package/src/_esm/chains/chainConfig.js +30 -0
- package/src/_esm/chains/chainConfig.js.map +1 -0
- package/src/_esm/chains/index.d.ts +4 -0
- package/src/_esm/chains/index.d.ts.map +1 -0
- package/src/_esm/chains/index.js +4 -0
- package/src/_esm/chains/index.js.map +1 -0
- package/src/_esm/chains/radius.d.ts +64 -0
- package/src/_esm/chains/radius.d.ts.map +1 -0
- package/src/_esm/chains/radius.js +39 -0
- package/src/_esm/chains/radius.js.map +1 -0
- package/src/_esm/chains/radiusTestnet.d.ts +61 -0
- package/src/_esm/chains/radiusTestnet.d.ts.map +1 -0
- package/src/_esm/chains/radiusTestnet.js +45 -0
- package/src/_esm/chains/radiusTestnet.js.map +1 -0
- package/src/_esm/decorators/index.d.ts +8 -0
- package/src/_esm/decorators/index.d.ts.map +1 -0
- package/src/_esm/decorators/index.js +8 -0
- package/src/_esm/decorators/index.js.map +1 -0
- package/src/_esm/decorators/radius.d.ts +66 -0
- package/src/_esm/decorators/radius.d.ts.map +1 -0
- package/src/_esm/decorators/radius.js +36 -0
- package/src/_esm/decorators/radius.js.map +1 -0
- package/src/_esm/errors/account.d.ts +78 -0
- package/src/_esm/errors/account.d.ts.map +1 -0
- package/src/_esm/errors/account.js +97 -0
- package/src/_esm/errors/account.js.map +1 -0
- package/src/_esm/errors/base.d.ts +56 -0
- package/src/_esm/errors/base.d.ts.map +1 -0
- package/src/_esm/errors/base.js +51 -0
- package/src/_esm/errors/base.js.map +1 -0
- package/src/_esm/errors/contract.d.ts +51 -0
- package/src/_esm/errors/contract.d.ts.map +1 -0
- package/src/_esm/errors/contract.js +65 -0
- package/src/_esm/errors/contract.js.map +1 -0
- package/src/_esm/errors/index.d.ts +63 -0
- package/src/_esm/errors/index.d.ts.map +1 -0
- package/src/_esm/errors/index.js +40 -0
- package/src/_esm/errors/index.js.map +1 -0
- package/src/_esm/errors/transaction.d.ts +132 -0
- package/src/_esm/errors/transaction.d.ts.map +1 -0
- package/src/_esm/errors/transaction.js +143 -0
- package/src/_esm/errors/transaction.js.map +1 -0
- package/src/_esm/events/decodeEventLogs.d.ts +136 -0
- package/src/_esm/events/decodeEventLogs.d.ts.map +1 -0
- package/src/_esm/events/decodeEventLogs.js +93 -0
- package/src/_esm/events/decodeEventLogs.js.map +1 -0
- package/src/_esm/events/getLogs.d.ts +137 -0
- package/src/_esm/events/getLogs.d.ts.map +1 -0
- package/src/_esm/events/getLogs.js +171 -0
- package/src/_esm/events/getLogs.js.map +1 -0
- package/src/_esm/events/index.d.ts +17 -0
- package/src/_esm/events/index.d.ts.map +1 -0
- package/src/_esm/events/index.js +24 -0
- package/src/_esm/events/index.js.map +1 -0
- package/src/_esm/events/watchApproval.d.ts +184 -0
- package/src/_esm/events/watchApproval.d.ts.map +1 -0
- package/src/_esm/events/watchApproval.js +254 -0
- package/src/_esm/events/watchApproval.js.map +1 -0
- package/src/_esm/events/watchBlock.d.ts +192 -0
- package/src/_esm/events/watchBlock.d.ts.map +1 -0
- package/src/_esm/events/watchBlock.js +162 -0
- package/src/_esm/events/watchBlock.js.map +1 -0
- package/src/_esm/events/watchLogs.d.ts +128 -0
- package/src/_esm/events/watchLogs.d.ts.map +1 -0
- package/src/_esm/events/watchLogs.js +100 -0
- package/src/_esm/events/watchLogs.js.map +1 -0
- package/src/_esm/events/watchTransfer.d.ts +164 -0
- package/src/_esm/events/watchTransfer.d.ts.map +1 -0
- package/src/_esm/events/watchTransfer.js +234 -0
- package/src/_esm/events/watchTransfer.js.map +1 -0
- package/src/_esm/index.d.ts +16 -0
- package/src/_esm/index.d.ts.map +1 -0
- package/src/_esm/index.js +22 -0
- package/src/_esm/index.js.map +1 -0
- package/src/_esm/package.json +1 -0
- package/src/_esm/transport/index.d.ts +9 -0
- package/src/_esm/transport/index.d.ts.map +1 -0
- package/src/_esm/transport/index.js +9 -0
- package/src/_esm/transport/index.js.map +1 -0
- package/src/_esm/transport/interceptor.d.ts +48 -0
- package/src/_esm/transport/interceptor.d.ts.map +1 -0
- package/src/_esm/transport/interceptor.js +209 -0
- package/src/_esm/transport/interceptor.js.map +1 -0
- package/src/_esm/transport/types.d.ts +29 -0
- package/src/_esm/transport/types.d.ts.map +1 -0
- package/src/_esm/transport/types.js +2 -0
- package/src/_esm/transport/types.js.map +1 -0
- package/src/_esm/transport/websocket.d.ts +51 -0
- package/src/_esm/transport/websocket.d.ts.map +1 -0
- package/src/_esm/transport/websocket.js +70 -0
- package/src/_esm/transport/websocket.js.map +1 -0
- package/src/_types/actions/index.d.ts +8 -0
- package/src/_types/actions/index.d.ts.map +1 -0
- package/src/_types/actions/sendTransactionBatch.d.ts +77 -0
- package/src/_types/actions/sendTransactionBatch.d.ts.map +1 -0
- package/src/_types/chains/chainConfig.d.ts +30 -0
- package/src/_types/chains/chainConfig.d.ts.map +1 -0
- package/src/_types/chains/index.d.ts +4 -0
- package/src/_types/chains/index.d.ts.map +1 -0
- package/src/_types/chains/radius.d.ts +64 -0
- package/src/_types/chains/radius.d.ts.map +1 -0
- package/src/_types/chains/radiusTestnet.d.ts +61 -0
- package/src/_types/chains/radiusTestnet.d.ts.map +1 -0
- package/src/_types/decorators/index.d.ts +8 -0
- package/src/_types/decorators/index.d.ts.map +1 -0
- package/src/_types/decorators/radius.d.ts +66 -0
- package/src/_types/decorators/radius.d.ts.map +1 -0
- package/src/_types/errors/account.d.ts +78 -0
- package/src/_types/errors/account.d.ts.map +1 -0
- package/src/_types/errors/base.d.ts +56 -0
- package/src/_types/errors/base.d.ts.map +1 -0
- package/src/_types/errors/contract.d.ts +51 -0
- package/src/_types/errors/contract.d.ts.map +1 -0
- package/src/_types/errors/index.d.ts +63 -0
- package/src/_types/errors/index.d.ts.map +1 -0
- package/src/_types/errors/transaction.d.ts +132 -0
- package/src/_types/errors/transaction.d.ts.map +1 -0
- package/src/_types/events/decodeEventLogs.d.ts +136 -0
- package/src/_types/events/decodeEventLogs.d.ts.map +1 -0
- package/src/_types/events/getLogs.d.ts +137 -0
- package/src/_types/events/getLogs.d.ts.map +1 -0
- package/src/_types/events/index.d.ts +17 -0
- package/src/_types/events/index.d.ts.map +1 -0
- package/src/_types/events/watchApproval.d.ts +184 -0
- package/src/_types/events/watchApproval.d.ts.map +1 -0
- package/src/_types/events/watchBlock.d.ts +192 -0
- package/src/_types/events/watchBlock.d.ts.map +1 -0
- package/src/_types/events/watchLogs.d.ts +128 -0
- package/src/_types/events/watchLogs.d.ts.map +1 -0
- package/src/_types/events/watchTransfer.d.ts +164 -0
- package/src/_types/events/watchTransfer.d.ts.map +1 -0
- package/src/_types/index.d.ts +16 -0
- package/src/_types/index.d.ts.map +1 -0
- package/src/_types/transport/index.d.ts +9 -0
- package/src/_types/transport/index.d.ts.map +1 -0
- package/src/_types/transport/interceptor.d.ts +48 -0
- package/src/_types/transport/interceptor.d.ts.map +1 -0
- package/src/_types/transport/types.d.ts +29 -0
- package/src/_types/transport/types.d.ts.map +1 -0
- package/src/_types/transport/websocket.d.ts +51 -0
- package/src/_types/transport/websocket.d.ts.map +1 -0
|
@@ -0,0 +1,234 @@
|
|
|
1
|
+
import { decodeEventLog, erc20Abi } from 'viem';
|
|
2
|
+
/**
|
|
3
|
+
* Watches for ERC-20 Transfer events in real-time.
|
|
4
|
+
* Automatically decodes Transfer events and provides type-safe callbacks.
|
|
5
|
+
*
|
|
6
|
+
* @param client - The PublicClient to use (WebSocket transport recommended)
|
|
7
|
+
* @param params - Transfer event watching parameters
|
|
8
|
+
* @returns An unwatch function to stop the subscription
|
|
9
|
+
*
|
|
10
|
+
* @example
|
|
11
|
+
* ```typescript
|
|
12
|
+
* import { createPublicClient } from 'viem';
|
|
13
|
+
* import { createWebSocketTransport, watchTransfer } from '@radiustechsystems/sdk/events';
|
|
14
|
+
* import { radiusTestnet } from '@radiustechsystems/sdk/chains';
|
|
15
|
+
*
|
|
16
|
+
* const client = createPublicClient({
|
|
17
|
+
* chain: radiusTestnet,
|
|
18
|
+
* transport: createWebSocketTransport(radiusTestnet),
|
|
19
|
+
* });
|
|
20
|
+
*
|
|
21
|
+
* // Watch all transfers for a token
|
|
22
|
+
* const unwatch = watchTransfer(client, {
|
|
23
|
+
* address: '0x...', // Token address
|
|
24
|
+
* onTransfer: (events) => {
|
|
25
|
+
* events.forEach(event => {
|
|
26
|
+
* console.log(`Transfer: ${event.value} from ${event.from} to ${event.to}`);
|
|
27
|
+
* });
|
|
28
|
+
* },
|
|
29
|
+
* });
|
|
30
|
+
*
|
|
31
|
+
* // Watch transfers to a specific address
|
|
32
|
+
* const unwatchToAddress = watchTransfer(client, {
|
|
33
|
+
* address: '0x...', // Token address
|
|
34
|
+
* to: '0x...', // Recipient address
|
|
35
|
+
* onTransfer: (events) => {
|
|
36
|
+
* console.log(`Received ${events.length} transfers`);
|
|
37
|
+
* },
|
|
38
|
+
* });
|
|
39
|
+
*
|
|
40
|
+
* // Stop watching
|
|
41
|
+
* unwatch();
|
|
42
|
+
* ```
|
|
43
|
+
*
|
|
44
|
+
* @remarks
|
|
45
|
+
* - Requires WebSocket transport for real-time subscriptions
|
|
46
|
+
* - Automatically decodes Transfer events using ERC-20 ABI
|
|
47
|
+
* - Filters by from/to addresses if provided
|
|
48
|
+
* - Event signature: Transfer(address indexed from, address indexed to, uint256 value)
|
|
49
|
+
* - Subscriptions consume gas from your RPC key on Radius (10 GAS/sec)
|
|
50
|
+
*/
|
|
51
|
+
export function watchTransfer(client, params) {
|
|
52
|
+
// Build event filter args based on from/to parameters
|
|
53
|
+
const args = {};
|
|
54
|
+
if (params.from)
|
|
55
|
+
args.from = params.from;
|
|
56
|
+
if (params.to)
|
|
57
|
+
args.to = params.to;
|
|
58
|
+
return client.watchContractEvent({
|
|
59
|
+
address: params.address,
|
|
60
|
+
abi: erc20Abi,
|
|
61
|
+
eventName: 'Transfer',
|
|
62
|
+
args: Object.keys(args).length > 0 ? args : undefined,
|
|
63
|
+
onLogs: (logs) => {
|
|
64
|
+
// Decode and transform logs to TransferEvent format
|
|
65
|
+
const events = logs
|
|
66
|
+
.map((log) => {
|
|
67
|
+
try {
|
|
68
|
+
const decoded = decodeEventLog({
|
|
69
|
+
abi: erc20Abi,
|
|
70
|
+
data: log.data,
|
|
71
|
+
topics: log.topics,
|
|
72
|
+
});
|
|
73
|
+
return {
|
|
74
|
+
from: decoded.args.from,
|
|
75
|
+
to: decoded.args.to,
|
|
76
|
+
value: decoded.args.value,
|
|
77
|
+
log: log,
|
|
78
|
+
};
|
|
79
|
+
}
|
|
80
|
+
catch (error) {
|
|
81
|
+
// Skip logs that can't be decoded
|
|
82
|
+
if (params.onError) {
|
|
83
|
+
params.onError(error instanceof Error ? error : new Error('Failed to decode Transfer event'));
|
|
84
|
+
}
|
|
85
|
+
return null;
|
|
86
|
+
}
|
|
87
|
+
})
|
|
88
|
+
.filter((event) => event !== null);
|
|
89
|
+
if (events.length > 0) {
|
|
90
|
+
params.onTransfer(events);
|
|
91
|
+
}
|
|
92
|
+
},
|
|
93
|
+
onError: params.onError,
|
|
94
|
+
pollingInterval: params.pollingInterval,
|
|
95
|
+
});
|
|
96
|
+
}
|
|
97
|
+
/**
|
|
98
|
+
* Watches for Transfer events involving a specific address (as sender or receiver).
|
|
99
|
+
* Convenience wrapper around watchTransfer for monitoring a single address.
|
|
100
|
+
*
|
|
101
|
+
* @param client - The PublicClient to use (WebSocket transport recommended)
|
|
102
|
+
* @param params - Transfer watching parameters for specific address
|
|
103
|
+
* @returns An unwatch function to stop the subscription
|
|
104
|
+
*
|
|
105
|
+
* @example
|
|
106
|
+
* ```typescript
|
|
107
|
+
* import { createPublicClient } from 'viem';
|
|
108
|
+
* import { createWebSocketTransport, watchTransferForAddress } from '@radiustechsystems/sdk/events';
|
|
109
|
+
* import { radiusTestnet } from '@radiustechsystems/sdk/chains';
|
|
110
|
+
*
|
|
111
|
+
* const client = createPublicClient({
|
|
112
|
+
* chain: radiusTestnet,
|
|
113
|
+
* transport: createWebSocketTransport(radiusTestnet),
|
|
114
|
+
* });
|
|
115
|
+
*
|
|
116
|
+
* // Watch all transfers involving an address (sent or received)
|
|
117
|
+
* const unwatch = watchTransferForAddress(client, {
|
|
118
|
+
* tokenAddress: '0x...', // Token address
|
|
119
|
+
* watchAddress: '0x...', // Address to monitor
|
|
120
|
+
* onTransfer: (events) => {
|
|
121
|
+
* events.forEach(event => {
|
|
122
|
+
* if (event.from === watchAddress) {
|
|
123
|
+
* console.log(`Sent ${event.value} to ${event.to}`);
|
|
124
|
+
* } else {
|
|
125
|
+
* console.log(`Received ${event.value} from ${event.from}`);
|
|
126
|
+
* }
|
|
127
|
+
* });
|
|
128
|
+
* },
|
|
129
|
+
* });
|
|
130
|
+
*
|
|
131
|
+
* // Watch only transfers sent from an address
|
|
132
|
+
* const unwatchSent = watchTransferForAddress(client, {
|
|
133
|
+
* tokenAddress: '0x...',
|
|
134
|
+
* watchAddress: '0x...',
|
|
135
|
+
* senderOnly: true,
|
|
136
|
+
* onTransfer: (events) => {
|
|
137
|
+
* console.log(`Sent ${events.length} transfers`);
|
|
138
|
+
* },
|
|
139
|
+
* });
|
|
140
|
+
*
|
|
141
|
+
* // Stop watching
|
|
142
|
+
* unwatch();
|
|
143
|
+
* ```
|
|
144
|
+
*
|
|
145
|
+
* @remarks
|
|
146
|
+
* - If neither senderOnly nor receiverOnly is set, watches both directions
|
|
147
|
+
* - Cannot set both senderOnly and receiverOnly to true
|
|
148
|
+
* - More efficient than watching all transfers and filtering client-side
|
|
149
|
+
* - Server-side filtering reduces network traffic and processing
|
|
150
|
+
*/
|
|
151
|
+
export function watchTransferForAddress(client, params) {
|
|
152
|
+
// Validate parameters
|
|
153
|
+
if (params.senderOnly && params.receiverOnly) {
|
|
154
|
+
throw new Error('Cannot set both senderOnly and receiverOnly to true');
|
|
155
|
+
}
|
|
156
|
+
// Determine filter parameters
|
|
157
|
+
let from;
|
|
158
|
+
let to;
|
|
159
|
+
if (params.senderOnly) {
|
|
160
|
+
from = params.watchAddress;
|
|
161
|
+
}
|
|
162
|
+
else if (params.receiverOnly) {
|
|
163
|
+
to = params.watchAddress;
|
|
164
|
+
}
|
|
165
|
+
else {
|
|
166
|
+
// Watch both: need to create two separate subscriptions
|
|
167
|
+
// This is a limitation of eth_subscribe - can't do OR filters
|
|
168
|
+
// Use deduplication to prevent duplicate callbacks for the same event
|
|
169
|
+
const seenEvents = new Set();
|
|
170
|
+
const MAX_SEEN_EVENTS = 10000;
|
|
171
|
+
// Create a unique key for each event (using tx hash + log index)
|
|
172
|
+
const getEventKey = (event) => {
|
|
173
|
+
const txHash = event.log.transactionHash ?? 'pending';
|
|
174
|
+
const logIndex = event.log.logIndex ?? 0;
|
|
175
|
+
return `${txHash}-${logIndex}`;
|
|
176
|
+
};
|
|
177
|
+
// Wrapper that deduplicates events before calling the callback
|
|
178
|
+
const deduplicatedCallback = (events) => {
|
|
179
|
+
const newEvents = events.filter((event) => {
|
|
180
|
+
const key = getEventKey(event);
|
|
181
|
+
if (seenEvents.has(key)) {
|
|
182
|
+
return false;
|
|
183
|
+
}
|
|
184
|
+
seenEvents.add(key);
|
|
185
|
+
return true;
|
|
186
|
+
});
|
|
187
|
+
// Bound the cache size to prevent memory leaks for long-running subscriptions
|
|
188
|
+
if (seenEvents.size > MAX_SEEN_EVENTS) {
|
|
189
|
+
// Remove oldest half of entries
|
|
190
|
+
const iterator = seenEvents.values();
|
|
191
|
+
for (let i = 0; i < MAX_SEEN_EVENTS / 2; i++) {
|
|
192
|
+
const next = iterator.next();
|
|
193
|
+
if (next.done)
|
|
194
|
+
break;
|
|
195
|
+
seenEvents.delete(next.value);
|
|
196
|
+
}
|
|
197
|
+
}
|
|
198
|
+
if (newEvents.length > 0) {
|
|
199
|
+
params.onTransfer(newEvents);
|
|
200
|
+
}
|
|
201
|
+
};
|
|
202
|
+
const unwatchFrom = watchTransfer(client, {
|
|
203
|
+
address: params.tokenAddress,
|
|
204
|
+
from: params.watchAddress,
|
|
205
|
+
onTransfer: deduplicatedCallback,
|
|
206
|
+
onError: params.onError,
|
|
207
|
+
sync: params.sync,
|
|
208
|
+
pollingInterval: params.pollingInterval,
|
|
209
|
+
});
|
|
210
|
+
const unwatchTo = watchTransfer(client, {
|
|
211
|
+
address: params.tokenAddress,
|
|
212
|
+
to: params.watchAddress,
|
|
213
|
+
onTransfer: deduplicatedCallback,
|
|
214
|
+
onError: params.onError,
|
|
215
|
+
sync: params.sync,
|
|
216
|
+
pollingInterval: params.pollingInterval,
|
|
217
|
+
});
|
|
218
|
+
// Return combined unwatch function
|
|
219
|
+
return () => {
|
|
220
|
+
unwatchFrom();
|
|
221
|
+
unwatchTo();
|
|
222
|
+
};
|
|
223
|
+
}
|
|
224
|
+
return watchTransfer(client, {
|
|
225
|
+
address: params.tokenAddress,
|
|
226
|
+
from,
|
|
227
|
+
to,
|
|
228
|
+
onTransfer: params.onTransfer,
|
|
229
|
+
onError: params.onError,
|
|
230
|
+
sync: params.sync,
|
|
231
|
+
pollingInterval: params.pollingInterval,
|
|
232
|
+
});
|
|
233
|
+
}
|
|
234
|
+
//# sourceMappingURL=watchTransfer.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"watchTransfer.js","sourceRoot":"","sources":["../../events/watchTransfer.ts"],"names":[],"mappings":"AAKA,OAAO,EAAiC,cAAc,EAAE,QAAQ,EAAE,MAAM,MAAM,CAAC;A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AAM,CAAC,YAAY;YAC5B,IAAI,EAAE,MAAM,CAAC,YAAY;YACzB,UAAU,EAAE,oBAAoB;YAChC,OAAO,EAAE,MAAM,CAAC,OAAO;YACvB,IAAI,EAAE,MAAM,CAAC,IAAI;YACjB,eAAe,EAAE,MAAM,CAAC,eAAe;SACvC,CAAC,CAAC;QAEH,MAAM,SAAS,GAAG,aAAa,CAAC,MAAM,EAAE;YACvC,OAAO,EAAE,MAAM,CAAC,YAAY;YAC5B,EAAE,EAAE,MAAM,CAAC,YAAY;YACvB,UAAU,EAAE,oBAAoB;YAChC,OAAO,EAAE,MAAM,CAAC,OAAO;YACvB,IAAI,EAAE,MAAM,CAAC,IAAI;YACjB,eAAe,EAAE,MAAM,CAAC,eAAe;SACvC,CAAC,CAAC;QAEH,mCAAmC;QACnC,OAAO,GAAG,EAAE;YACX,WAAW,EAAE,CAAC;YACd,SAAS,EAAE,CAAC;QACb,CAAC,CAAC;IACH,CAAC;IAED,OAAO,aAAa,CAAC,MAAM,EAAE;QAC5B,OAAO,EAAE,MAAM,CAAC,YAAY;QAC5B,IAAI;QACJ,EAAE;QACF,UAAU,EAAE,MAAM,CAAC,UAAU;QAC7B,OAAO,EAAE,MAAM,CAAC,OAAO;QACvB,IAAI,EAAE,MAAM,CAAC,IAAI;QACjB,eAAe,EAAE,MAAM,CAAC,eAAe;KACvC,CAAC,CAAC;AACJ,CAAC"}
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Radius TypeScript SDK v2
|
|
3
|
+
*
|
|
4
|
+
* A viem-based SDK for interacting with the Radius platform.
|
|
5
|
+
*
|
|
6
|
+
* @packageDocumentation
|
|
7
|
+
*/
|
|
8
|
+
export type { Abi, Address, Chain, Hash, Hex, LocalAccount, TransactionReceipt, Transport, } from 'viem';
|
|
9
|
+
export { createPublicClient, createWalletClient, http } from 'viem';
|
|
10
|
+
export { privateKeyToAccount } from 'viem/accounts';
|
|
11
|
+
export { type SendTransactionBatchParameters, type SendTransactionBatchReturnType, sendTransactionBatch, } from './actions/index.js';
|
|
12
|
+
export { MAX_GAS, radius, radiusTestnet } from './chains/index.js';
|
|
13
|
+
export { type RadiusWalletActions, radiusWalletActions } from './decorators/index.js';
|
|
14
|
+
export * from './errors/index.js';
|
|
15
|
+
export * from './transport/index.js';
|
|
16
|
+
//# sourceMappingURL=index.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../index.ts"],"names":[],"mappings":"AAAA;;;;;;GAMG;AAGH,YAAY,EACX,GAAG,EACH,OAAO,EACP,KAAK,EACL,IAAI,EACJ,GAAG,EACH,YAAY,EACZ,kBAAkB,EAClB,SAAS,GACT,MAAM,MAAM,CAAC;AAEd,OAAO,EAAE,kBAAkB,EAAE,kBAAkB,EAAE,IAAI,EAAE,MAAM,MAAM,CAAC;AAEpE,OAAO,EAAE,mBAAmB,EAAE,MAAM,eAAe,CAAC;AAEpD,OAAO,EACN,KAAK,8BAA8B,EACnC,KAAK,8BAA8B,EACnC,oBAAoB,GACpB,MAAM,oBAAoB,CAAC;AAE5B,OAAO,EAAE,OAAO,EAAE,MAAM,EAAE,aAAa,EAAE,MAAM,mBAAmB,CAAC;AAEnE,OAAO,EAAE,KAAK,mBAAmB,EAAE,mBAAmB,EAAE,MAAM,uBAAuB,CAAC;AAGtF,cAAc,mBAAmB,CAAC;AAGlC,cAAc,sBAAsB,CAAC"}
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Radius TypeScript SDK v2
|
|
3
|
+
*
|
|
4
|
+
* A viem-based SDK for interacting with the Radius platform.
|
|
5
|
+
*
|
|
6
|
+
* @packageDocumentation
|
|
7
|
+
*/
|
|
8
|
+
// Re-export viem transport and client factories for convenience
|
|
9
|
+
export { createPublicClient, createWalletClient, http } from 'viem';
|
|
10
|
+
// Re-export viem account utilities
|
|
11
|
+
export { privateKeyToAccount } from 'viem/accounts';
|
|
12
|
+
// Actions
|
|
13
|
+
export { sendTransactionBatch, } from './actions/index.js';
|
|
14
|
+
// Chains
|
|
15
|
+
export { MAX_GAS, radius, radiusTestnet } from './chains/index.js';
|
|
16
|
+
// Decorators
|
|
17
|
+
export { radiusWalletActions } from './decorators/index.js';
|
|
18
|
+
// Errors
|
|
19
|
+
export * from './errors/index.js';
|
|
20
|
+
// Transport
|
|
21
|
+
export * from './transport/index.js';
|
|
22
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../index.ts"],"names":[],"mappings":"AAAA;;;;;;GAMG;AAaH,gEAAgE;AAChE,OAAO,EAAE,kBAAkB,EAAE,kBAAkB,EAAE,IAAI,EAAE,MAAM,MAAM,CAAC;AACpE,mCAAmC;AACnC,OAAO,EAAE,mBAAmB,EAAE,MAAM,eAAe,CAAC;AACpD,UAAU;AACV,OAAO,EAGN,oBAAoB,GACpB,MAAM,oBAAoB,CAAC;AAC5B,SAAS;AACT,OAAO,EAAE,OAAO,EAAE,MAAM,EAAE,aAAa,EAAE,MAAM,mBAAmB,CAAC;AACnE,aAAa;AACb,OAAO,EAA4B,mBAAmB,EAAE,MAAM,uBAAuB,CAAC;AAEtF,SAAS;AACT,cAAc,mBAAmB,CAAC;AAElC,YAAY;AACZ,cAAc,sBAAsB,CAAC"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"type": "module","sideEffects":false}
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Transport module for the Radius SDK.
|
|
3
|
+
* Provides HTTP request/response interceptors and middleware for
|
|
4
|
+
* JSON-RPC communication with Radius.
|
|
5
|
+
*/
|
|
6
|
+
export * from './interceptor.js';
|
|
7
|
+
export * from './types.js';
|
|
8
|
+
export * from './websocket.js';
|
|
9
|
+
//# sourceMappingURL=index.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../transport/index.ts"],"names":[],"mappings":"AAAA;;;;GAIG;AACH,cAAc,kBAAkB,CAAC;AACjC,cAAc,YAAY,CAAC;AAC3B,cAAc,gBAAgB,CAAC"}
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Transport module for the Radius SDK.
|
|
3
|
+
* Provides HTTP request/response interceptors and middleware for
|
|
4
|
+
* JSON-RPC communication with Radius.
|
|
5
|
+
*/
|
|
6
|
+
export * from './interceptor.js';
|
|
7
|
+
export * from './types.js';
|
|
8
|
+
export * from './websocket.js';
|
|
9
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../transport/index.ts"],"names":[],"mappings":"AAAA;;;;GAIG;AACH,cAAc,kBAAkB,CAAC;AACjC,cAAc,YAAY,CAAC;AAC3B,cAAc,gBAAgB,CAAC"}
|
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
import { type Transport } from 'viem';
|
|
2
|
+
import type { Interceptor, Logger, RequestHandler } from './types';
|
|
3
|
+
/**
|
|
4
|
+
* A RequestHandler implementation that intercepts HTTP requests and responses.
|
|
5
|
+
* Provides request logging and response modification capabilities.
|
|
6
|
+
*/
|
|
7
|
+
export declare class InterceptingRequestHandler implements RequestHandler {
|
|
8
|
+
private readonly interceptor?;
|
|
9
|
+
private readonly logger?;
|
|
10
|
+
private readonly proxied;
|
|
11
|
+
/**
|
|
12
|
+
* Creates a new InterceptingRequestHandler.
|
|
13
|
+
* @param interceptor Optional function to intercept and modify responses
|
|
14
|
+
* @param logger Optional logging function to record requests and responses
|
|
15
|
+
* @param proxied Underlying RequestHandler implementation (defaults to fetch-based implementation)
|
|
16
|
+
*/
|
|
17
|
+
constructor(interceptor?: Interceptor | undefined, logger?: Logger | undefined, proxied?: RequestHandler);
|
|
18
|
+
/**
|
|
19
|
+
* Sends a request and handles interception and logging of the response.
|
|
20
|
+
* @param request The HTTP request to send
|
|
21
|
+
* @returns The HTTP response, potentially modified by the interceptor
|
|
22
|
+
*/
|
|
23
|
+
handle(request: Request): Promise<Response>;
|
|
24
|
+
/**
|
|
25
|
+
* Parse the body of a request, cloning the request to avoid modifying the original.
|
|
26
|
+
* @private
|
|
27
|
+
*/
|
|
28
|
+
private parseRequestBody;
|
|
29
|
+
}
|
|
30
|
+
/**
|
|
31
|
+
* Options for creating an intercepting transport.
|
|
32
|
+
*/
|
|
33
|
+
export interface InterceptingTransportOptions {
|
|
34
|
+
/** The RPC URL to connect to */
|
|
35
|
+
url: string;
|
|
36
|
+
/** Optional function to intercept and modify responses */
|
|
37
|
+
interceptor?: Interceptor;
|
|
38
|
+
/** Optional logging function */
|
|
39
|
+
logger?: Logger;
|
|
40
|
+
/** Request timeout in milliseconds. Default: 10000 (10 seconds) */
|
|
41
|
+
timeout?: number;
|
|
42
|
+
/** Number of retry attempts. Default: 3 */
|
|
43
|
+
retryCount?: number;
|
|
44
|
+
/** Base delay between retries in milliseconds. Default: 150 */
|
|
45
|
+
retryDelay?: number;
|
|
46
|
+
}
|
|
47
|
+
export declare function createInterceptingTransport(options: InterceptingTransportOptions): Transport;
|
|
48
|
+
//# sourceMappingURL=interceptor.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"interceptor.d.ts","sourceRoot":"","sources":["../../transport/interceptor.ts"],"names":[],"mappings":"AAAA,OAAO,EAAuC,KAAK,SAAS,EAAE,MAAM,MAAM,CAAC;AAC3E,OAAO,KAAK,EAAE,WAAW,EAAE,MAAM,EAAE,cAAc,EAAE,MAAM,SAAS,CAAC;AAEnE;;;GAGG;AACH,qBAAa,0BAA2B,YAAW,cAAc;IAQ/D,OAAO,CAAC,QAAQ,CAAC,WAAW,CAAC;IAC7B,OAAO,CAAC,QAAQ,CAAC,MAAM,CAAC;IACxB,OAAO,CAAC,QAAQ,CAAC,OAAO;IATzB;;;;;OAKG;gBAEe,WAAW,CAAC,EAAE,WAAW,YAAA,EACzB,MAAM,CAAC,EAAE,MAAM,YAAA,EACf,OAAO,GAAE,cAA4C;IAGvE;;;;OAIG;IACG,MAAM,CAAC,OAAO,EAAE,OAAO,GAAG,OAAO,CAAC,QAAQ,CAAC;IA4CjD;;;OAGG;YACW,gBAAgB;CAY9B;AAsBD;;GAEG;AACH,MAAM,WAAW,4BAA4B;IAC5C,gCAAgC;IAChC,GAAG,EAAE,MAAM,CAAC;IACZ,0DAA0D;IAC1D,WAAW,CAAC,EAAE,WAAW,CAAC;IAC1B,gCAAgC;IAChC,MAAM,CAAC,EAAE,MAAM,CAAC;IAChB,mEAAmE;IACnE,OAAO,CAAC,EAAE,MAAM,CAAC;IACjB,2CAA2C;IAC3C,UAAU,CAAC,EAAE,MAAM,CAAC;IACpB,+DAA+D;IAC/D,UAAU,CAAC,EAAE,MAAM,CAAC;CACpB;AA4CD,wBAAgB,2BAA2B,CAAC,OAAO,EAAE,4BAA4B,GAAG,SAAS,CAyE5F"}
|
|
@@ -0,0 +1,209 @@
|
|
|
1
|
+
import { custom, http } from 'viem';
|
|
2
|
+
/**
|
|
3
|
+
* A RequestHandler implementation that intercepts HTTP requests and responses.
|
|
4
|
+
* Provides request logging and response modification capabilities.
|
|
5
|
+
*/
|
|
6
|
+
export class InterceptingRequestHandler {
|
|
7
|
+
interceptor;
|
|
8
|
+
logger;
|
|
9
|
+
proxied;
|
|
10
|
+
/**
|
|
11
|
+
* Creates a new InterceptingRequestHandler.
|
|
12
|
+
* @param interceptor Optional function to intercept and modify responses
|
|
13
|
+
* @param logger Optional logging function to record requests and responses
|
|
14
|
+
* @param proxied Underlying RequestHandler implementation (defaults to fetch-based implementation)
|
|
15
|
+
*/
|
|
16
|
+
constructor(interceptor, logger, proxied = new DefaultRequestHandler()) {
|
|
17
|
+
this.interceptor = interceptor;
|
|
18
|
+
this.logger = logger;
|
|
19
|
+
this.proxied = proxied;
|
|
20
|
+
}
|
|
21
|
+
/**
|
|
22
|
+
* Sends a request and handles interception and logging of the response.
|
|
23
|
+
* @param request The HTTP request to send
|
|
24
|
+
* @returns The HTTP response, potentially modified by the interceptor
|
|
25
|
+
*/
|
|
26
|
+
async handle(request) {
|
|
27
|
+
const reqBody = await this.parseRequestBody(request);
|
|
28
|
+
if (this.logger) {
|
|
29
|
+
this.logger('Request:', {
|
|
30
|
+
url: request.url,
|
|
31
|
+
method: request.method,
|
|
32
|
+
body: reqBody,
|
|
33
|
+
});
|
|
34
|
+
}
|
|
35
|
+
let response;
|
|
36
|
+
try {
|
|
37
|
+
response = await this.proxied.handle(request);
|
|
38
|
+
const body = await response.clone().text();
|
|
39
|
+
if (this.logger) {
|
|
40
|
+
this.logger('Response:', {
|
|
41
|
+
status: response.status,
|
|
42
|
+
body,
|
|
43
|
+
});
|
|
44
|
+
}
|
|
45
|
+
response = new Response(body, {
|
|
46
|
+
status: response.status,
|
|
47
|
+
statusText: response.statusText,
|
|
48
|
+
headers: response.headers,
|
|
49
|
+
});
|
|
50
|
+
}
|
|
51
|
+
catch (error) {
|
|
52
|
+
if (this.logger) {
|
|
53
|
+
this.logger('Request failed', {
|
|
54
|
+
error: error instanceof Error ? error.message : String(error),
|
|
55
|
+
});
|
|
56
|
+
}
|
|
57
|
+
throw error;
|
|
58
|
+
}
|
|
59
|
+
if (this.interceptor) {
|
|
60
|
+
return this.interceptor(reqBody, response);
|
|
61
|
+
}
|
|
62
|
+
return response;
|
|
63
|
+
}
|
|
64
|
+
/**
|
|
65
|
+
* Parse the body of a request, cloning the request to avoid modifying the original.
|
|
66
|
+
* @private
|
|
67
|
+
*/
|
|
68
|
+
async parseRequestBody(request) {
|
|
69
|
+
if (!request.body) {
|
|
70
|
+
return '';
|
|
71
|
+
}
|
|
72
|
+
try {
|
|
73
|
+
const clone = request.clone();
|
|
74
|
+
return await clone.text();
|
|
75
|
+
}
|
|
76
|
+
catch (error) {
|
|
77
|
+
throw new Error(`Failed to parse request body: ${error}`);
|
|
78
|
+
}
|
|
79
|
+
}
|
|
80
|
+
}
|
|
81
|
+
/**
|
|
82
|
+
* A simple implementation of RequestHandler that uses the Fetch API with timeout.
|
|
83
|
+
* @private
|
|
84
|
+
*/
|
|
85
|
+
class DefaultRequestHandler {
|
|
86
|
+
timeout;
|
|
87
|
+
constructor(timeout = 10000) {
|
|
88
|
+
this.timeout = timeout;
|
|
89
|
+
}
|
|
90
|
+
async handle(request) {
|
|
91
|
+
const controller = new AbortController();
|
|
92
|
+
const timeoutId = setTimeout(() => controller.abort(), this.timeout);
|
|
93
|
+
try {
|
|
94
|
+
const response = await fetch(request, { signal: controller.signal });
|
|
95
|
+
return response;
|
|
96
|
+
}
|
|
97
|
+
finally {
|
|
98
|
+
clearTimeout(timeoutId);
|
|
99
|
+
}
|
|
100
|
+
}
|
|
101
|
+
}
|
|
102
|
+
/**
|
|
103
|
+
* Creates a viem-compatible transport that supports request interception and logging.
|
|
104
|
+
*
|
|
105
|
+
* When only logging is needed (no interceptor), this uses viem's native http() transport
|
|
106
|
+
* with `onFetchRequest`/`onFetchResponse` callbacks, which provides:
|
|
107
|
+
* - Built-in retry logic with exponential backoff
|
|
108
|
+
* - Timeout enforcement
|
|
109
|
+
* - Request batching support
|
|
110
|
+
*
|
|
111
|
+
* When response interception is needed, a custom transport is used with:
|
|
112
|
+
* - Timeout support via AbortController
|
|
113
|
+
* - Basic retry logic
|
|
114
|
+
*
|
|
115
|
+
* @param options Configuration options for the transport
|
|
116
|
+
* @returns A viem Transport that can be used with createPublicClient
|
|
117
|
+
*
|
|
118
|
+
* @example
|
|
119
|
+
* ```typescript
|
|
120
|
+
* // Logging only - uses viem's http() for best performance
|
|
121
|
+
* const transport = createInterceptingTransport({
|
|
122
|
+
* url: 'https://rpc.testnet.radiustech.xyz',
|
|
123
|
+
* logger: console.log,
|
|
124
|
+
* });
|
|
125
|
+
*
|
|
126
|
+
* // With response interception
|
|
127
|
+
* const transport = createInterceptingTransport({
|
|
128
|
+
* url: 'https://rpc.testnet.radiustech.xyz',
|
|
129
|
+
* interceptor: async (reqBody, response) => {
|
|
130
|
+
* // Modify response if needed
|
|
131
|
+
* return response;
|
|
132
|
+
* },
|
|
133
|
+
* });
|
|
134
|
+
*
|
|
135
|
+
* const client = createPublicClient({
|
|
136
|
+
* chain: radiusTestnet,
|
|
137
|
+
* transport,
|
|
138
|
+
* });
|
|
139
|
+
* ```
|
|
140
|
+
*/
|
|
141
|
+
/** Counter for generating unique request IDs */
|
|
142
|
+
let requestIdCounter = 0;
|
|
143
|
+
export function createInterceptingTransport(options) {
|
|
144
|
+
const { url, interceptor, logger, timeout = 10000, retryCount = 3, retryDelay = 150 } = options;
|
|
145
|
+
// If no interceptor, use viem's native http() transport with callbacks
|
|
146
|
+
// This provides retry logic, timeout, and better performance
|
|
147
|
+
if (!interceptor) {
|
|
148
|
+
return http(url, {
|
|
149
|
+
timeout,
|
|
150
|
+
retryCount,
|
|
151
|
+
retryDelay,
|
|
152
|
+
onFetchRequest: logger
|
|
153
|
+
? (request) => {
|
|
154
|
+
logger('Request:', {
|
|
155
|
+
url: request.url,
|
|
156
|
+
method: request.method,
|
|
157
|
+
});
|
|
158
|
+
}
|
|
159
|
+
: undefined,
|
|
160
|
+
onFetchResponse: logger
|
|
161
|
+
? (response) => {
|
|
162
|
+
logger('Response:', {
|
|
163
|
+
status: response.status,
|
|
164
|
+
});
|
|
165
|
+
}
|
|
166
|
+
: undefined,
|
|
167
|
+
});
|
|
168
|
+
}
|
|
169
|
+
// With interceptor, use custom transport that supports response modification
|
|
170
|
+
const defaultHandler = new DefaultRequestHandler(timeout);
|
|
171
|
+
const requestHandler = new InterceptingRequestHandler(interceptor, logger, defaultHandler);
|
|
172
|
+
const request = async ({ method, params }) => {
|
|
173
|
+
const body = JSON.stringify({
|
|
174
|
+
jsonrpc: '2.0',
|
|
175
|
+
id: ++requestIdCounter,
|
|
176
|
+
method,
|
|
177
|
+
params,
|
|
178
|
+
});
|
|
179
|
+
const httpRequest = new Request(url, {
|
|
180
|
+
method: 'POST',
|
|
181
|
+
headers: {
|
|
182
|
+
'content-type': 'application/json',
|
|
183
|
+
},
|
|
184
|
+
body,
|
|
185
|
+
});
|
|
186
|
+
// Simple retry logic
|
|
187
|
+
let lastError;
|
|
188
|
+
for (let attempt = 0; attempt <= retryCount; attempt++) {
|
|
189
|
+
try {
|
|
190
|
+
const response = await requestHandler.handle(httpRequest.clone());
|
|
191
|
+
const result = await response.json();
|
|
192
|
+
if (result.error) {
|
|
193
|
+
throw new Error(result.error.message || 'RPC Error');
|
|
194
|
+
}
|
|
195
|
+
return result.result;
|
|
196
|
+
}
|
|
197
|
+
catch (error) {
|
|
198
|
+
lastError = error instanceof Error ? error : new Error(String(error));
|
|
199
|
+
if (attempt < retryCount) {
|
|
200
|
+
// Wait before retrying with exponential backoff
|
|
201
|
+
await new Promise((resolve) => setTimeout(resolve, retryDelay * 2 ** attempt));
|
|
202
|
+
}
|
|
203
|
+
}
|
|
204
|
+
}
|
|
205
|
+
throw lastError;
|
|
206
|
+
};
|
|
207
|
+
return custom({ request });
|
|
208
|
+
}
|
|
209
|
+
//# sourceMappingURL=interceptor.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"interceptor.js","sourceRoot":"","sources":["../../transport/interceptor.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,MAAM,EAAyB,IAAI,EAAkB,MAAM,MAAM,CAAC;AAG3E;;;GAGG;AACH,MAAM,OAAO,0BAA0B;IAQpB;IACA;IACA;IATlB;;;;;OAKG;IACH,YACkB,WAAyB,EACzB,MAAe,EACf,UAA0B,IAAI,qBAAqB,EAAE;QAFrD,gBAAW,GAAX,WAAW,CAAc;QACzB,WAAM,GAAN,MAAM,CAAS;QACf,YAAO,GAAP,OAAO,CAA8C;IACpE,CAAC;IAEJ;;;;OAIG;IACH,KAAK,CAAC,MAAM,CAAC,OAAgB;QAC5B,MAAM,OAAO,GAAG,MAAM,IAAI,CAAC,gBAAgB,CAAC,OAAO,CAAC,CAAC;QAErD,IAAI,I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UAAU,EAAE,OAAO,EAAE,EAAE,CAAC;YACxD,IAAI,CAAC;gBACJ,MAAM,QAAQ,GAAG,MAAM,cAAc,CAAC,MAAM,CAAC,WAAW,CAAC,KAAK,EAAE,CAAC,CAAC;gBAClE,MAAM,MAAM,GAAG,MAAM,QAAQ,CAAC,IAAI,EAAE,CAAC;gBAErC,IAAI,MAAM,CAAC,KAAK,EAAE,CAAC;oBAClB,MAAM,IAAI,KAAK,CAAC,MAAM,CAAC,KAAK,CAAC,OAAO,IAAI,WAAW,CAAC,CAAC;gBACtD,CAAC;gBAED,OAAO,MAAM,CAAC,MAAM,CAAC;YACtB,CAAC;YAAC,OAAO,KAAK,EAAE,CAAC;gBAChB,SAAS,GAAG,KAAK,YAAY,KAAK,CAAC,CAAC,CAAC,KAAK,CAAC,CAAC,CAAC,IAAI,KAAK,CAAC,MAAM,CAAC,KAAK,CAAC,CAAC,CAAC;gBACtE,IAAI,OAAO,GAAG,UAAU,EAAE,CAAC;oBAC1B,gDAAgD;oBAChD,MAAM,IAAI,OAAO,CAAC,CAAC,OAAO,EAAE,EAAE,CAAC,UAAU,CAAC,OAAO,EAAE,UAAU,GAAG,CAAC,IAAI,OAAO,CAAC,CAAC,CAAC;gBAChF,CAAC;YACF,CAAC;QACF,CAAC;QAED,MAAM,SAAS,CAAC;IACjB,CAAC,CAAC;IAEF,OAAO,MAAM,CAAC,EAAE,OAAO,EAAE,CAAC,CAAC;AAC5B,CAAC"}
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* A logger function interface that matches the console.log function signature
|
|
3
|
+
* Used for logging JSON-RPC requests and responses
|
|
4
|
+
* @param message The log message to display
|
|
5
|
+
* @param data Optional structured data to include in the log
|
|
6
|
+
*/
|
|
7
|
+
export type Logger = (message: string, data?: Record<string, unknown>) => void;
|
|
8
|
+
/**
|
|
9
|
+
* An interceptor function for modifying JSON-RPC HTTP requests and responses
|
|
10
|
+
* This allows for custom handling, validation, or manipulation of RPC calls
|
|
11
|
+
* @param reqBody The stringified JSON-RPC request body
|
|
12
|
+
* @param response The HTTP response from the JSON-RPC server
|
|
13
|
+
* @returns A potentially modified response or the original response
|
|
14
|
+
*/
|
|
15
|
+
export type Interceptor = (reqBody: string, response: Response) => Promise<Response>;
|
|
16
|
+
/**
|
|
17
|
+
* An interface for making HTTP requests and receiving responses
|
|
18
|
+
* Handles the actual HTTP round-trip for JSON-RPC requests
|
|
19
|
+
*/
|
|
20
|
+
export interface RequestHandler {
|
|
21
|
+
/**
|
|
22
|
+
* Sends an HTTP request and returns the response
|
|
23
|
+
* @param request The HTTP request to send
|
|
24
|
+
* @returns A Promise that resolves to the HTTP response
|
|
25
|
+
* @throws Error if the request fails
|
|
26
|
+
*/
|
|
27
|
+
handle(request: Request): Promise<Response>;
|
|
28
|
+
}
|
|
29
|
+
//# sourceMappingURL=types.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"types.d.ts","sourceRoot":"","sources":["../../transport/types.ts"],"names":[],"mappings":"AAAA;;;;;GAKG;AACH,MAAM,MAAM,MAAM,GAAG,CAAC,OAAO,EAAE,MAAM,EAAE,IAAI,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,KAAK,IAAI,CAAC;AAE/E;;;;;;GAMG;AACH,MAAM,MAAM,WAAW,GAAG,CAAC,OAAO,EAAE,MAAM,EAAE,QAAQ,EAAE,QAAQ,KAAK,OAAO,CAAC,QAAQ,CAAC,CAAC;AAErF;;;GAGG;AACH,MAAM,WAAW,cAAc;IAC9B;;;;;OAKG;IACH,MAAM,CAAC,OAAO,EAAE,OAAO,GAAG,OAAO,CAAC,QAAQ,CAAC,CAAC;CAC5C"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"types.js","sourceRoot":"","sources":["../../transport/types.ts"],"names":[],"mappings":""}
|