stxer 0.4.3 → 0.6.1

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.
@@ -0,0 +1,273 @@
1
+ /**
2
+ * Low-level programmatic simulation APIs
3
+ *
4
+ * These functions provide direct access to the stxer V2 simulation endpoints
5
+ * for programmatic use cases where you need more control than SimulationBuilder provides.
6
+ */
7
+
8
+ import { DEFAULT_STXER_API } from './constants';
9
+ import type {
10
+ InstantSimulationRequest,
11
+ InstantSimulationResponse,
12
+ SimulationBatchReadsRequest,
13
+ SimulationBatchReadsResponse,
14
+ SimulationResult,
15
+ SubmitSimulationStepsRequest,
16
+ SubmitSimulationStepsResponse,
17
+ } from './types';
18
+
19
+ /**
20
+ * Options for API calls
21
+ */
22
+ export interface SimulationApiOptions {
23
+ /** stxer API endpoint (default: https://api.stxer.xyz) */
24
+ stxerApi?: string;
25
+ }
26
+
27
+ /**
28
+ * Create simulation session options
29
+ */
30
+ export interface CreateSessionOptions {
31
+ /** Block height for simulation (optional, uses tip if not provided) */
32
+ block_height?: number;
33
+ /** Block hash corresponding to block_height */
34
+ block_hash?: string;
35
+ /** Skip debug tracing for faster simulations */
36
+ skip_tracing?: boolean;
37
+ }
38
+
39
+ /**
40
+ * Instantly simulate a transaction
41
+ *
42
+ * This is useful for apps/wallets to get the result of a transaction before sending it.
43
+ * Lightweight - no debug tracing information.
44
+ *
45
+ * @param request - Instant simulation request
46
+ * @param options - API options
47
+ * @returns Instant simulation response with receipt and optional reads
48
+ *
49
+ * @example
50
+ * ```typescript
51
+ * import { instantSimulation } from 'stxer';
52
+ *
53
+ * const result = await instantSimulation({
54
+ * transaction: '0x...',
55
+ * block_height: 130818,
56
+ * block_hash: '0x...',
57
+ * reads: [
58
+ * { DataVar: ['SP...contract', 'my-var'] },
59
+ * { StxBalance: 'SP...' }
60
+ * ]
61
+ * });
62
+ * console.log(result.receipt.result);
63
+ * ```
64
+ */
65
+ export async function instantSimulation(
66
+ request: InstantSimulationRequest,
67
+ options: SimulationApiOptions = {},
68
+ ): Promise<InstantSimulationResponse> {
69
+ const apiEndpoint = options.stxerApi ?? DEFAULT_STXER_API;
70
+
71
+ const response = await fetch(
72
+ `${apiEndpoint}/devtools/v2/simulation:instant`,
73
+ {
74
+ method: 'POST',
75
+ body: JSON.stringify(request),
76
+ headers: {
77
+ 'Content-Type': 'application/json',
78
+ Accept: 'application/json',
79
+ },
80
+ },
81
+ );
82
+
83
+ if (!response.ok) {
84
+ const text = await response.text();
85
+ throw new Error(`Instant simulation failed: ${text}`);
86
+ }
87
+
88
+ return response.json() as Promise<InstantSimulationResponse>;
89
+ }
90
+
91
+ /**
92
+ * Create a new simulation session
93
+ *
94
+ * A session allows you to run multiple steps in a forked chain state.
95
+ *
96
+ * @param options - Create session options
97
+ * @param apiOptions - API options
98
+ * @returns Session ID
99
+ *
100
+ * @example
101
+ * ```typescript
102
+ * import { createSimulationSession } from 'stxer';
103
+ *
104
+ * const sessionId = await createSimulationSession({
105
+ * block_height: 130818,
106
+ * skip_tracing: false
107
+ * });
108
+ * ```
109
+ */
110
+ export async function createSimulationSession(
111
+ options: CreateSessionOptions = {},
112
+ apiOptions: SimulationApiOptions = {},
113
+ ): Promise<string> {
114
+ const apiEndpoint = apiOptions.stxerApi ?? DEFAULT_STXER_API;
115
+
116
+ const response = await fetch(`${apiEndpoint}/devtools/v2/simulations`, {
117
+ method: 'POST',
118
+ body: JSON.stringify(options),
119
+ headers: {
120
+ 'Content-Type': 'application/json',
121
+ Accept: 'application/json',
122
+ },
123
+ });
124
+
125
+ if (!response.ok) {
126
+ const text = await response.text();
127
+ throw new Error(`Failed to create simulation session: ${text}`);
128
+ }
129
+
130
+ const result = (await response.json()) as { id: string };
131
+ return result.id;
132
+ }
133
+
134
+ /**
135
+ * Submit steps to a simulation session
136
+ *
137
+ * Steps are executed sequentially in the session's forked chain state.
138
+ *
139
+ * @param sessionId - Simulation session ID
140
+ * @param request - Steps to submit
141
+ * @param options - API options
142
+ * @returns Array of step results
143
+ *
144
+ * @example
145
+ * ```typescript
146
+ * import { submitSimulationSteps } from 'stxer';
147
+ *
148
+ * const results = await submitSimulationSteps(sessionId, {
149
+ * steps: [
150
+ * { Transaction: '0x...' },
151
+ * { Eval: ['SP...', '', 'SP...contract', '(var-get my-var)'] },
152
+ * { Reads: [{ DataVar: ['SP...contract', 'my-var'] }] }
153
+ * ]
154
+ * });
155
+ * ```
156
+ */
157
+ export async function submitSimulationSteps(
158
+ sessionId: string,
159
+ request: SubmitSimulationStepsRequest,
160
+ options: SimulationApiOptions = {},
161
+ ): Promise<SubmitSimulationStepsResponse> {
162
+ const apiEndpoint = options.stxerApi ?? DEFAULT_STXER_API;
163
+
164
+ const response = await fetch(
165
+ `${apiEndpoint}/devtools/v2/simulations/${sessionId}`,
166
+ {
167
+ method: 'POST',
168
+ body: JSON.stringify(request),
169
+ headers: {
170
+ 'Content-Type': 'application/json',
171
+ Accept: 'application/json',
172
+ },
173
+ },
174
+ );
175
+
176
+ if (!response.ok) {
177
+ const text = await response.text();
178
+ throw new Error(`Failed to submit simulation steps: ${text}`);
179
+ }
180
+
181
+ return response.json() as Promise<SubmitSimulationStepsResponse>;
182
+ }
183
+
184
+ /**
185
+ * Get simulation session results
186
+ *
187
+ * Returns the full simulation result including metadata and all step results.
188
+ *
189
+ * @param sessionId - Simulation session ID
190
+ * @param options - API options
191
+ * @returns Complete simulation result
192
+ *
193
+ * @example
194
+ * ```typescript
195
+ * import { getSimulationResult } from 'stxer';
196
+ *
197
+ * const result = await getSimulationResult(sessionId);
198
+ * console.log(result.metadata);
199
+ * console.log(result.steps);
200
+ * ```
201
+ */
202
+ export async function getSimulationResult(
203
+ sessionId: string,
204
+ options: SimulationApiOptions = {},
205
+ ): Promise<SimulationResult> {
206
+ const apiEndpoint = options.stxerApi ?? DEFAULT_STXER_API;
207
+
208
+ const response = await fetch(
209
+ `${apiEndpoint}/devtools/v2/simulations/${sessionId}`,
210
+ {
211
+ method: 'GET',
212
+ headers: {
213
+ Accept: 'application/json',
214
+ },
215
+ },
216
+ );
217
+
218
+ if (!response.ok) {
219
+ const text = await response.text();
220
+ throw new Error(`Failed to get simulation result: ${text}`);
221
+ }
222
+
223
+ return response.json() as Promise<SimulationResult>;
224
+ }
225
+
226
+ /**
227
+ * Batch reads from a simulation session
228
+ *
229
+ * Similar to sidecar batch reads, but reads from the simulation's forked state.
230
+ *
231
+ * @param sessionId - Simulation session ID
232
+ * @param request - Batch reads request
233
+ * @param options - API options
234
+ * @returns Batch read results
235
+ *
236
+ * @example
237
+ * ```typescript
238
+ * import { simulationBatchReads } from 'stxer';
239
+ *
240
+ * const results = await simulationBatchReads(sessionId, {
241
+ * vars: [['SP...contract', 'my-var']],
242
+ * maps: [['SP...contract', 'my-map', '0x...']],
243
+ * stx: ['SP...']
244
+ * });
245
+ * console.log(results.vars);
246
+ * ```
247
+ */
248
+ export async function simulationBatchReads(
249
+ sessionId: string,
250
+ request: SimulationBatchReadsRequest,
251
+ options: SimulationApiOptions = {},
252
+ ): Promise<SimulationBatchReadsResponse> {
253
+ const apiEndpoint = options.stxerApi ?? DEFAULT_STXER_API;
254
+
255
+ const response = await fetch(
256
+ `${apiEndpoint}/devtools/v2/simulations/${sessionId}/reads`,
257
+ {
258
+ method: 'POST',
259
+ body: JSON.stringify(request),
260
+ headers: {
261
+ 'Content-Type': 'application/json',
262
+ Accept: 'application/json',
263
+ },
264
+ },
265
+ );
266
+
267
+ if (!response.ok) {
268
+ const text = await response.text();
269
+ throw new Error(`Failed to batch reads from simulation: ${text}`);
270
+ }
271
+
272
+ return response.json() as Promise<SimulationBatchReadsResponse>;
273
+ }