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.
package/src/types.ts ADDED
@@ -0,0 +1,392 @@
1
+ /**
2
+ * Type definitions for stxer-api V2 endpoints
3
+ * Hand-written types based on OpenAPI spec at https://api.stxer.xyz/openapi.json
4
+ */
5
+
6
+ // =============================================================================
7
+ // Sidecar Tip Types
8
+ // =============================================================================
9
+
10
+ export interface TenureCost {
11
+ read_count: number;
12
+ read_length: number;
13
+ write_count: number;
14
+ write_length: number;
15
+ runtime: number;
16
+ }
17
+
18
+ export interface SidecarTip {
19
+ bitcoin_height: number;
20
+ block_hash: string;
21
+ block_height: number;
22
+ block_time: number;
23
+ burn_block_height: number;
24
+ burn_block_time: number;
25
+ consensus_hash: string;
26
+ index_block_hash: string;
27
+ is_nakamoto: boolean;
28
+ tenure_cost: TenureCost;
29
+ tenure_height: number;
30
+ sortition_id: string;
31
+ epoch_id: string;
32
+ }
33
+
34
+ // =============================================================================
35
+ // AST Types
36
+ // =============================================================================
37
+
38
+ export interface ClarityAbiType {
39
+ readonly?: { type: ClarityAbiTypeName; length?: number } | string;
40
+ tuple?: { name: string; type: ClarityAbiType }[];
41
+ list?: { type: ClarityAbiType; length: number };
42
+ buffer?: { length: number };
43
+ string_ascii?: { length: number };
44
+ string_utf8?: { length: number };
45
+ principal?: [];
46
+ bool?: [];
47
+ int?: { length: number };
48
+ uint?: { length: number };
49
+ response?: { ok: ClarityAbiType; error: ClarityAbiType };
50
+ optional?: ClarityAbiType;
51
+ contract?: { name: string; address: string };
52
+ }
53
+
54
+ export type ClarityAbiTypeName =
55
+ | 'optional'
56
+ | 'response'
57
+ | 'bool'
58
+ | 'int'
59
+ | 'uint'
60
+ | 'string-ascii'
61
+ | 'string-utf8'
62
+ | 'buff'
63
+ | 'list'
64
+ | 'tuple'
65
+ | 'principal'
66
+ | 'trait-reference'
67
+ | 'contract';
68
+
69
+ export interface ClarityAbiFunction {
70
+ name: string;
71
+ access: 'private' | 'public' | 'read_only';
72
+ args: Array<{ name: string; type: ClarityAbiType }>;
73
+ outputs: { type: ClarityAbiType };
74
+ }
75
+
76
+ export interface ClarityAbiVariable {
77
+ name: string;
78
+ access: 'variable' | 'constant';
79
+ type: ClarityAbiType;
80
+ }
81
+
82
+ export interface ClarityAbiMap {
83
+ name: string;
84
+ key: ClarityAbiType;
85
+ value: ClarityAbiType;
86
+ }
87
+
88
+ export interface ClarityAbiFungibleToken {
89
+ name: string;
90
+ }
91
+
92
+ export interface ClarityAbiNonFungibleToken {
93
+ name: string;
94
+ type: ClarityAbiType;
95
+ }
96
+
97
+ export type ClarityEpoch =
98
+ | 'Epoch10'
99
+ | 'Epoch20'
100
+ | 'Epoch2_05'
101
+ | 'Epoch21'
102
+ | 'Epoch22'
103
+ | 'Epoch23'
104
+ | 'Epoch24'
105
+ | 'Epoch25'
106
+ | 'Epoch30'
107
+ | 'Epoch31'
108
+ | 'Epoch32'
109
+ | 'Epoch33'
110
+ | 'Epoch34'
111
+ | 'Epoch35';
112
+
113
+ export type ClarityVersion = 'Clarity1' | 'Clarity2' | 'Clarity3' | 'Clarity4';
114
+
115
+ export interface ClarityAbi {
116
+ functions: ClarityAbiFunction[];
117
+ variables: ClarityAbiVariable[];
118
+ maps: ClarityAbiMap[];
119
+ fungible_tokens: ClarityAbiFungibleToken[];
120
+ non_fungible_tokens: ClarityAbiNonFungibleToken[];
121
+ epoch: ClarityEpoch;
122
+ clarity_version: ClarityVersion;
123
+ }
124
+
125
+ export interface SymbolicExpressionField {
126
+ field: string;
127
+ }
128
+
129
+ export interface SymbolicExpressionTraitReference {
130
+ trait_reference: {
131
+ clarity_name: string;
132
+ trait_definition: string;
133
+ trait_definition_type: 'Defined' | 'Imported';
134
+ };
135
+ }
136
+
137
+ export interface SymbolicExpressionAtom {
138
+ atom: string;
139
+ }
140
+
141
+ export interface SymbolicExpressionAtomValue {
142
+ atom_value: string;
143
+ }
144
+
145
+ export interface SymbolicExpressionLiteralValue {
146
+ literal_value: string;
147
+ }
148
+
149
+ export interface SymbolicExpressionList {
150
+ list: SymbolicExpression[];
151
+ }
152
+
153
+ export type SymbolicExpression = {
154
+ id: number;
155
+ span: string;
156
+ expr:
157
+ | SymbolicExpressionAtom
158
+ | SymbolicExpressionAtomValue
159
+ | SymbolicExpressionLiteralValue
160
+ | SymbolicExpressionList
161
+ | SymbolicExpressionField
162
+ | SymbolicExpressionTraitReference;
163
+ pre_comments?: Array<{ comment: string; span: string }>;
164
+ post_comments?: Array<{ comment: string; span: string }>;
165
+ end_line_comment?: string;
166
+ };
167
+
168
+ export interface ContractAST {
169
+ contract_identifier: string;
170
+ expressions: SymbolicExpression[];
171
+ top_level_expression_sorting: number[];
172
+ referenced_traits: Record<
173
+ string,
174
+ { trait_definition: string; trait_definition_type: 'Defined' | 'Imported' }
175
+ >;
176
+ implemented_traits: string[];
177
+ tx_id?: string;
178
+ canonical?: boolean;
179
+ contract_id?: string;
180
+ block_height?: number;
181
+ source_code?: string;
182
+ abi?: ClarityAbi;
183
+ }
184
+
185
+ // =============================================================================
186
+ // V2 Simulation Types
187
+ // =============================================================================
188
+
189
+ export interface ExecutionCost {
190
+ read_count: number;
191
+ read_length: number;
192
+ write_count: number;
193
+ write_length: number;
194
+ runtime: number;
195
+ }
196
+
197
+ export interface TransactionReceipt {
198
+ result: string;
199
+ stx_burned: number;
200
+ tx_index: number;
201
+ vm_error: string | null;
202
+ post_condition_aborted: boolean;
203
+ costs: number;
204
+ execution_cost: ExecutionCost;
205
+ events: string[];
206
+ }
207
+
208
+ export interface TransactionOkResult {
209
+ Ok: TransactionReceipt;
210
+ }
211
+
212
+ export interface TransactionErrResult {
213
+ Err: string;
214
+ }
215
+
216
+ // Read step types
217
+ export interface MapEntryStep {
218
+ MapEntry: [string, string, string]; // [contract_id, map_name, key_hex]
219
+ }
220
+
221
+ export interface DataVarStep {
222
+ DataVar: [string, string]; // [contract_id, variable_name]
223
+ }
224
+
225
+ export interface EvalReadonlyStep {
226
+ EvalReadonly: [string, string, string, string]; // [sender, sponsor, contract_id, code]
227
+ }
228
+
229
+ export interface StxBalanceStep {
230
+ StxBalance: string; // principal
231
+ }
232
+
233
+ export interface FtBalanceStep {
234
+ FtBalance: [string, string, string]; // [contract_id, token_name, principal]
235
+ }
236
+
237
+ export interface FtSupplyStep {
238
+ FtSupply: [string, string]; // [contract_id, token_name]
239
+ }
240
+
241
+ export interface NonceStep {
242
+ Nonce: string; // principal
243
+ }
244
+
245
+ export type ReadStep =
246
+ | MapEntryStep
247
+ | DataVarStep
248
+ | EvalReadonlyStep
249
+ | StxBalanceStep
250
+ | FtBalanceStep
251
+ | FtSupplyStep
252
+ | NonceStep;
253
+
254
+ export interface ReadOkResult {
255
+ Ok: string; // clarity value hex OR number string for balances/nonces
256
+ }
257
+
258
+ export interface ReadErrResult {
259
+ Err: string;
260
+ }
261
+
262
+ export type ReadResult = ReadOkResult | ReadErrResult;
263
+
264
+ // Simulation step types (summary format from GET /devtools/v2/simulations/{id})
265
+ export interface TransactionStepSummary {
266
+ Transaction: string; // tx hex
267
+ TxId: string;
268
+ Result: {
269
+ Transaction: TransactionOkResult | TransactionErrResult;
270
+ };
271
+ ExecutionCost: ExecutionCost;
272
+ }
273
+
274
+ export interface ReadsStepSummary {
275
+ Reads: ReadStep[];
276
+ Result: {
277
+ Reads: ReadResult[];
278
+ };
279
+ }
280
+
281
+ export interface SetContractCodeStepSummary {
282
+ SetContractCode: [string, string, number]; // [contract_id, code, clarity_version]
283
+ Result: {
284
+ SetContractCode: { Ok: null } | { Err: string };
285
+ };
286
+ }
287
+
288
+ export interface EvalStepSummary {
289
+ Eval: [string, string, string, string]; // [sender, sponsor, contract_id, code]
290
+ Result: {
291
+ Eval: { Ok: string } | { Err: string };
292
+ };
293
+ }
294
+
295
+ export interface TenureExtendStepSummary {
296
+ Result: {
297
+ TenureExtend: ExecutionCost;
298
+ };
299
+ }
300
+
301
+ export type SimulationStepSummary =
302
+ | TransactionStepSummary
303
+ | ReadsStepSummary
304
+ | SetContractCodeStepSummary
305
+ | EvalStepSummary
306
+ | TenureExtendStepSummary;
307
+
308
+ // Simulation metadata
309
+ export interface SimulationMetadata {
310
+ ast_rules: 0 | 1;
311
+ block_height: number;
312
+ block_hash: string;
313
+ burn_block_height: number;
314
+ burn_block_hash: string;
315
+ consensus_hash: string;
316
+ epoch: string;
317
+ index_block_hash: string;
318
+ skip_tracing: boolean;
319
+ sortition_id: string;
320
+ }
321
+
322
+ // Full simulation result
323
+ export interface SimulationResult {
324
+ metadata: SimulationMetadata;
325
+ steps: SimulationStepSummary[];
326
+ }
327
+
328
+ // Create session request
329
+ export interface CreateSimulationRequest {
330
+ block_height?: number;
331
+ block_hash?: string;
332
+ skip_tracing?: boolean;
333
+ }
334
+
335
+ export interface CreateSimulationResponse {
336
+ id: string;
337
+ }
338
+
339
+ // Submit steps request
340
+ export type SimulationStepInput =
341
+ | { Transaction: string } // tx hex
342
+ | { Eval: [string, string, string, string] } // [sender, sponsor, contract_id, code]
343
+ | { SetContractCode: [string, string, number] } // [contract_id, code, clarity_version]
344
+ | { Reads: ReadStep[] }
345
+ | { TenureExtend: [] };
346
+
347
+ export interface SubmitSimulationStepsRequest {
348
+ steps: SimulationStepInput[];
349
+ }
350
+
351
+ export interface SubmitSimulationStepsResponse {
352
+ steps: SimulationStepSummary[];
353
+ }
354
+
355
+ // Batch reads from simulation
356
+ export interface SimulationBatchReadsRequest {
357
+ vars?: [string, string][]; // [contract_id, variable_name]
358
+ maps?: [string, string, string][]; // [contract_id, map_name, key_hex]
359
+ readonly?: Array<string | [string, string, string, string]>; // [contract_id, function_name, ...args]
360
+ readonly_with_sender?: Array<
361
+ // [sender, sponsor, contract_id, function_name, ...args]
362
+ [string, string, string, string, ...string[]]
363
+ >;
364
+ stx?: string[]; // principals
365
+ nonces?: string[]; // principals
366
+ ft_balance?: [string, string][]; // [token_identifier, principal]
367
+ ft_supply?: [string, string][]; // [contract_id, ft_token_name]
368
+ }
369
+
370
+ export interface SimulationBatchReadsResponse {
371
+ vars: ReadResult[];
372
+ maps: ReadResult[];
373
+ readonly: ReadResult[];
374
+ readonly_with_sender: ReadResult[];
375
+ stx: ReadResult[];
376
+ nonces: ReadResult[];
377
+ ft_balance: ReadResult[];
378
+ ft_supply: ReadResult[];
379
+ }
380
+
381
+ // Instant simulation
382
+ export interface InstantSimulationRequest {
383
+ transaction: string; // tx hex
384
+ block_height?: number;
385
+ block_hash?: string;
386
+ reads?: ReadStep[];
387
+ }
388
+
389
+ export interface InstantSimulationResponse {
390
+ reads?: ReadResult[];
391
+ receipt: TransactionReceipt;
392
+ }
@@ -1 +0,0 @@
1
- export {};
@@ -1 +0,0 @@
1
- export {};
@@ -1,42 +0,0 @@
1
- import { uintCV } from '@stacks/transactions';
2
- import { SimulationBuilder } from '..';
3
-
4
- const sender = 'SP212Y5JKN59YP3GYG07K3S8W5SSGE4KH6B5STXER';
5
-
6
- const test = () =>
7
- SimulationBuilder.new()
8
- .withSender(sender)
9
- .addContractDeploy({
10
- contract_name: 'test-simulation',
11
- source_code: `
12
- ;; counter example
13
- (define-data-var counter uint u0)
14
-
15
- (define-public (increment (delta uint))
16
- (begin
17
- (var-set counter (+ (var-get counter) delta))
18
- (ok (var-get counter))))
19
-
20
- (define-public (decrement)
21
- (begin
22
- (var-set counter (- (var-get counter) u1))
23
- (ok (var-get counter))))
24
-
25
- (define-read-only (get-counter)
26
- (ok (var-get counter)))
27
- `,
28
- })
29
- .addEvalCode(`${sender}.test-simulation`, '(get-counter)')
30
- .addContractCall({
31
- contract_id: `${sender}.test-simulation`,
32
- function_name: 'increment',
33
- function_args: [uintCV(10)],
34
- })
35
- .addEvalCode(`${sender}.test-simulation`, '(get-counter)')
36
- .run();
37
-
38
- if (require.main === module) {
39
- (async () => {
40
- await test();
41
- })().catch(console.error);
42
- }
@@ -1,139 +0,0 @@
1
- import {
2
- contractPrincipalCV,
3
- principalCV,
4
- tupleCV,
5
- uintCV,
6
- } from '@stacks/transactions';
7
- import { SIP010TraitABI } from 'clarity-abi/abis';
8
- import { unwrapResponse } from 'ts-clarity';
9
- import { batchRead } from '../BatchAPI';
10
- import { BatchProcessor } from '../BatchProcessor';
11
- import { callReadonly, readMap, readVariable } from '../clarity-api';
12
-
13
- async function batchReadsExample() {
14
- const rs = await batchRead({
15
- // index_block_hash:
16
- // 'ce04817b9c6d90814ff9c06228d3a07d64335b1d9b01a233456fc304e34f7c0e', // block 373499
17
- variables: [
18
- {
19
- contract: contractPrincipalCV(
20
- 'SP1Z92MPDQEWZXW36VX71Q25HKF5K2EPCJ304F275',
21
- 'liquidity-token-v5kbe3oqvac',
22
- ),
23
- variableName: 'balance-x',
24
- },
25
- {
26
- contract: contractPrincipalCV(
27
- 'SP1Z92MPDQEWZXW36VX71Q25HKF5K2EPCJ304F275',
28
- 'liquidity-token-v5kbe3oqvac',
29
- ),
30
- variableName: 'balance-y',
31
- },
32
- {
33
- contract: contractPrincipalCV(
34
- 'SP1Z92MPDQEWZXW36VX71Q25HKF5K2EPCJ304F275',
35
- 'liquidity-token-v5kbe3oqvac',
36
- ),
37
- variableName: 'something-not-exists',
38
- },
39
- ],
40
- maps: [
41
- {
42
- contract: contractPrincipalCV(
43
- 'SP102V8P0F7JX67ARQ77WEA3D3CFB5XW39REDT0AM',
44
- 'amm-registry-v2-01',
45
- ),
46
- mapName: 'pools-data-map',
47
- mapKey: tupleCV({
48
- 'token-x': principalCV(
49
- 'SP102V8P0F7JX67ARQ77WEA3D3CFB5XW39REDT0AM.token-wstx-v2',
50
- ),
51
- 'token-y': principalCV(
52
- 'SP102V8P0F7JX67ARQ77WEA3D3CFB5XW39REDT0AM.token-alex',
53
- ),
54
- factor: uintCV(1e8),
55
- }),
56
- },
57
- {
58
- contract: contractPrincipalCV(
59
- 'SP1Y5YSTAHZ88XYK1VPDH24GY0HPX5J4JECTMY4A1',
60
- 'univ2-core',
61
- ),
62
- mapName: 'pools',
63
- mapKey: uintCV(1),
64
- },
65
- {
66
- contract: contractPrincipalCV(
67
- 'SP1Y5YSTAHZ88XYK1VPDH24GY0HPX5J4JECTMY4A1',
68
- 'contract-not-exists',
69
- ),
70
- mapName: 'pools',
71
- mapKey: uintCV(1),
72
- },
73
- ],
74
- });
75
- console.log(rs);
76
- }
77
-
78
- async function batchQueueProcessorExample() {
79
- const processor = new BatchProcessor({
80
- stxerAPIEndpoint: 'https://api.stxer.xyz',
81
- batchDelayMs: 1000,
82
- });
83
-
84
- const promiseA = processor.read({
85
- mode: 'variable',
86
- contractAddress: 'SP1Z92MPDQEWZXW36VX71Q25HKF5K2EPCJ304F275',
87
- contractName: 'liquidity-token-v5kbe3oqvac',
88
- variableName: 'balance-x',
89
- });
90
-
91
- const promiseB = processor.read({
92
- mode: 'variable',
93
- contractAddress: 'SP1Z92MPDQEWZXW36VX71Q25HKF5K2EPCJ304F275',
94
- contractName: 'liquidity-token-v5kbe3oqvac',
95
- variableName: 'balance-y',
96
- });
97
-
98
- const result = await Promise.all([promiseA, promiseB]);
99
- console.log(result);
100
- }
101
-
102
- async function batchSip010Example() {
103
- const supply = callReadonly({
104
- abi: SIP010TraitABI.functions,
105
- functionName: 'get-total-supply',
106
- contract: 'SP102V8P0F7JX67ARQ77WEA3D3CFB5XW39REDT0AM.token-alex',
107
- }).then(unwrapResponse);
108
- const balance = callReadonly({
109
- abi: SIP010TraitABI.functions,
110
- functionName: 'get-balance',
111
- contract: 'SP102V8P0F7JX67ARQ77WEA3D3CFB5XW39REDT0AM.token-alex',
112
- args: {
113
- who: 'SP102V8P0F7JX67ARQ77WEA3D3CFB5XW39REDT0AM.amm-vault-v2-01',
114
- },
115
- }).then(unwrapResponse);
116
- const paused = readVariable({
117
- abi: [{ name: 'paused', type: 'bool', access: 'variable' }],
118
- variableName: 'paused',
119
- contract: 'SP102V8P0F7JX67ARQ77WEA3D3CFB5XW39REDT0AM.amm-vault-v2-01',
120
- });
121
- const approved = readMap({
122
- abi: [{ key: 'principal', name: 'approved-tokens', value: 'bool' }],
123
- mapName: 'approved-tokens',
124
- key: 'SP102V8P0F7JX67ARQ77WEA3D3CFB5XW39REDT0AM.token-alex',
125
- contract: 'SP102V8P0F7JX67ARQ77WEA3D3CFB5XW39REDT0AM.amm-vault-v2-01',
126
- });
127
- const result = await Promise.all([supply, balance, paused, approved]);
128
- console.log(result);
129
- }
130
-
131
- async function main() {
132
- await batchReadsExample();
133
- await batchQueueProcessorExample();
134
- await batchSip010Example();
135
- }
136
-
137
- if (require.main === module) {
138
- main().catch(console.error);
139
- }
File without changes
File without changes