stxer 0.4.5 → 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,327 @@
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
+ export interface TenureCost {
6
+ read_count: number;
7
+ read_length: number;
8
+ write_count: number;
9
+ write_length: number;
10
+ runtime: number;
11
+ }
12
+ export interface SidecarTip {
13
+ bitcoin_height: number;
14
+ block_hash: string;
15
+ block_height: number;
16
+ block_time: number;
17
+ burn_block_height: number;
18
+ burn_block_time: number;
19
+ consensus_hash: string;
20
+ index_block_hash: string;
21
+ is_nakamoto: boolean;
22
+ tenure_cost: TenureCost;
23
+ tenure_height: number;
24
+ sortition_id: string;
25
+ epoch_id: string;
26
+ }
27
+ export interface ClarityAbiType {
28
+ readonly?: {
29
+ type: ClarityAbiTypeName;
30
+ length?: number;
31
+ } | string;
32
+ tuple?: {
33
+ name: string;
34
+ type: ClarityAbiType;
35
+ }[];
36
+ list?: {
37
+ type: ClarityAbiType;
38
+ length: number;
39
+ };
40
+ buffer?: {
41
+ length: number;
42
+ };
43
+ string_ascii?: {
44
+ length: number;
45
+ };
46
+ string_utf8?: {
47
+ length: number;
48
+ };
49
+ principal?: [];
50
+ bool?: [];
51
+ int?: {
52
+ length: number;
53
+ };
54
+ uint?: {
55
+ length: number;
56
+ };
57
+ response?: {
58
+ ok: ClarityAbiType;
59
+ error: ClarityAbiType;
60
+ };
61
+ optional?: ClarityAbiType;
62
+ contract?: {
63
+ name: string;
64
+ address: string;
65
+ };
66
+ }
67
+ export type ClarityAbiTypeName = 'optional' | 'response' | 'bool' | 'int' | 'uint' | 'string-ascii' | 'string-utf8' | 'buff' | 'list' | 'tuple' | 'principal' | 'trait-reference' | 'contract';
68
+ export interface ClarityAbiFunction {
69
+ name: string;
70
+ access: 'private' | 'public' | 'read_only';
71
+ args: Array<{
72
+ name: string;
73
+ type: ClarityAbiType;
74
+ }>;
75
+ outputs: {
76
+ type: ClarityAbiType;
77
+ };
78
+ }
79
+ export interface ClarityAbiVariable {
80
+ name: string;
81
+ access: 'variable' | 'constant';
82
+ type: ClarityAbiType;
83
+ }
84
+ export interface ClarityAbiMap {
85
+ name: string;
86
+ key: ClarityAbiType;
87
+ value: ClarityAbiType;
88
+ }
89
+ export interface ClarityAbiFungibleToken {
90
+ name: string;
91
+ }
92
+ export interface ClarityAbiNonFungibleToken {
93
+ name: string;
94
+ type: ClarityAbiType;
95
+ }
96
+ export type ClarityEpoch = 'Epoch10' | 'Epoch20' | 'Epoch2_05' | 'Epoch21' | 'Epoch22' | 'Epoch23' | 'Epoch24' | 'Epoch25' | 'Epoch30' | 'Epoch31' | 'Epoch32' | 'Epoch33' | 'Epoch34' | 'Epoch35';
97
+ export type ClarityVersion = 'Clarity1' | 'Clarity2' | 'Clarity3' | 'Clarity4';
98
+ export interface ClarityAbi {
99
+ functions: ClarityAbiFunction[];
100
+ variables: ClarityAbiVariable[];
101
+ maps: ClarityAbiMap[];
102
+ fungible_tokens: ClarityAbiFungibleToken[];
103
+ non_fungible_tokens: ClarityAbiNonFungibleToken[];
104
+ epoch: ClarityEpoch;
105
+ clarity_version: ClarityVersion;
106
+ }
107
+ export interface SymbolicExpressionField {
108
+ field: string;
109
+ }
110
+ export interface SymbolicExpressionTraitReference {
111
+ trait_reference: {
112
+ clarity_name: string;
113
+ trait_definition: string;
114
+ trait_definition_type: 'Defined' | 'Imported';
115
+ };
116
+ }
117
+ export interface SymbolicExpressionAtom {
118
+ atom: string;
119
+ }
120
+ export interface SymbolicExpressionAtomValue {
121
+ atom_value: string;
122
+ }
123
+ export interface SymbolicExpressionLiteralValue {
124
+ literal_value: string;
125
+ }
126
+ export interface SymbolicExpressionList {
127
+ list: SymbolicExpression[];
128
+ }
129
+ export type SymbolicExpression = {
130
+ id: number;
131
+ span: string;
132
+ expr: SymbolicExpressionAtom | SymbolicExpressionAtomValue | SymbolicExpressionLiteralValue | SymbolicExpressionList | SymbolicExpressionField | SymbolicExpressionTraitReference;
133
+ pre_comments?: Array<{
134
+ comment: string;
135
+ span: string;
136
+ }>;
137
+ post_comments?: Array<{
138
+ comment: string;
139
+ span: string;
140
+ }>;
141
+ end_line_comment?: string;
142
+ };
143
+ export interface ContractAST {
144
+ contract_identifier: string;
145
+ expressions: SymbolicExpression[];
146
+ top_level_expression_sorting: number[];
147
+ referenced_traits: Record<string, {
148
+ trait_definition: string;
149
+ trait_definition_type: 'Defined' | 'Imported';
150
+ }>;
151
+ implemented_traits: string[];
152
+ tx_id?: string;
153
+ canonical?: boolean;
154
+ contract_id?: string;
155
+ block_height?: number;
156
+ source_code?: string;
157
+ abi?: ClarityAbi;
158
+ }
159
+ export interface ExecutionCost {
160
+ read_count: number;
161
+ read_length: number;
162
+ write_count: number;
163
+ write_length: number;
164
+ runtime: number;
165
+ }
166
+ export interface TransactionReceipt {
167
+ result: string;
168
+ stx_burned: number;
169
+ tx_index: number;
170
+ vm_error: string | null;
171
+ post_condition_aborted: boolean;
172
+ costs: number;
173
+ execution_cost: ExecutionCost;
174
+ events: string[];
175
+ }
176
+ export interface TransactionOkResult {
177
+ Ok: TransactionReceipt;
178
+ }
179
+ export interface TransactionErrResult {
180
+ Err: string;
181
+ }
182
+ export interface MapEntryStep {
183
+ MapEntry: [string, string, string];
184
+ }
185
+ export interface DataVarStep {
186
+ DataVar: [string, string];
187
+ }
188
+ export interface EvalReadonlyStep {
189
+ EvalReadonly: [string, string, string, string];
190
+ }
191
+ export interface StxBalanceStep {
192
+ StxBalance: string;
193
+ }
194
+ export interface FtBalanceStep {
195
+ FtBalance: [string, string, string];
196
+ }
197
+ export interface FtSupplyStep {
198
+ FtSupply: [string, string];
199
+ }
200
+ export interface NonceStep {
201
+ Nonce: string;
202
+ }
203
+ export type ReadStep = MapEntryStep | DataVarStep | EvalReadonlyStep | StxBalanceStep | FtBalanceStep | FtSupplyStep | NonceStep;
204
+ export interface ReadOkResult {
205
+ Ok: string;
206
+ }
207
+ export interface ReadErrResult {
208
+ Err: string;
209
+ }
210
+ export type ReadResult = ReadOkResult | ReadErrResult;
211
+ export interface TransactionStepSummary {
212
+ Transaction: string;
213
+ TxId: string;
214
+ Result: {
215
+ Transaction: TransactionOkResult | TransactionErrResult;
216
+ };
217
+ ExecutionCost: ExecutionCost;
218
+ }
219
+ export interface ReadsStepSummary {
220
+ Reads: ReadStep[];
221
+ Result: {
222
+ Reads: ReadResult[];
223
+ };
224
+ }
225
+ export interface SetContractCodeStepSummary {
226
+ SetContractCode: [string, string, number];
227
+ Result: {
228
+ SetContractCode: {
229
+ Ok: null;
230
+ } | {
231
+ Err: string;
232
+ };
233
+ };
234
+ }
235
+ export interface EvalStepSummary {
236
+ Eval: [string, string, string, string];
237
+ Result: {
238
+ Eval: {
239
+ Ok: string;
240
+ } | {
241
+ Err: string;
242
+ };
243
+ };
244
+ }
245
+ export interface TenureExtendStepSummary {
246
+ Result: {
247
+ TenureExtend: ExecutionCost;
248
+ };
249
+ }
250
+ export type SimulationStepSummary = TransactionStepSummary | ReadsStepSummary | SetContractCodeStepSummary | EvalStepSummary | TenureExtendStepSummary;
251
+ export interface SimulationMetadata {
252
+ ast_rules: 0 | 1;
253
+ block_height: number;
254
+ block_hash: string;
255
+ burn_block_height: number;
256
+ burn_block_hash: string;
257
+ consensus_hash: string;
258
+ epoch: string;
259
+ index_block_hash: string;
260
+ skip_tracing: boolean;
261
+ sortition_id: string;
262
+ }
263
+ export interface SimulationResult {
264
+ metadata: SimulationMetadata;
265
+ steps: SimulationStepSummary[];
266
+ }
267
+ export interface CreateSimulationRequest {
268
+ block_height?: number;
269
+ block_hash?: string;
270
+ skip_tracing?: boolean;
271
+ }
272
+ export interface CreateSimulationResponse {
273
+ id: string;
274
+ }
275
+ export type SimulationStepInput = {
276
+ Transaction: string;
277
+ } | {
278
+ Eval: [string, string, string, string];
279
+ } | {
280
+ SetContractCode: [string, string, number];
281
+ } | {
282
+ Reads: ReadStep[];
283
+ } | {
284
+ TenureExtend: [];
285
+ };
286
+ export interface SubmitSimulationStepsRequest {
287
+ steps: SimulationStepInput[];
288
+ }
289
+ export interface SubmitSimulationStepsResponse {
290
+ steps: SimulationStepSummary[];
291
+ }
292
+ export interface SimulationBatchReadsRequest {
293
+ vars?: [string, string][];
294
+ maps?: [string, string, string][];
295
+ readonly?: Array<string | [string, string, string, string]>;
296
+ readonly_with_sender?: Array<[
297
+ string,
298
+ string,
299
+ string,
300
+ string,
301
+ ...string[]
302
+ ]>;
303
+ stx?: string[];
304
+ nonces?: string[];
305
+ ft_balance?: [string, string][];
306
+ ft_supply?: [string, string][];
307
+ }
308
+ export interface SimulationBatchReadsResponse {
309
+ vars: ReadResult[];
310
+ maps: ReadResult[];
311
+ readonly: ReadResult[];
312
+ readonly_with_sender: ReadResult[];
313
+ stx: ReadResult[];
314
+ nonces: ReadResult[];
315
+ ft_balance: ReadResult[];
316
+ ft_supply: ReadResult[];
317
+ }
318
+ export interface InstantSimulationRequest {
319
+ transaction: string;
320
+ block_height?: number;
321
+ block_hash?: string;
322
+ reads?: ReadStep[];
323
+ }
324
+ export interface InstantSimulationResponse {
325
+ reads?: ReadResult[];
326
+ receipt: TransactionReceipt;
327
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "stxer",
3
- "version": "0.4.5",
3
+ "version": "0.6.1",
4
4
  "license": "MIT",
5
5
  "author": "Kyle Fang",
6
6
  "repository": {
@@ -12,7 +12,9 @@
12
12
  "typings": "dist/index.d.ts",
13
13
  "files": [
14
14
  "dist",
15
- "src"
15
+ "src",
16
+ "!src/sample",
17
+ "!dist/sample"
16
18
  ],
17
19
  "husky": {
18
20
  "hooks": {
@@ -42,21 +44,21 @@
42
44
  }
43
45
  ],
44
46
  "devDependencies": {
45
- "@biomejs/biome": "^2.3.6",
46
- "@size-limit/preset-small-lib": "^11.2.0",
47
+ "@biomejs/biome": "^2.3.11",
48
+ "@size-limit/preset-small-lib": "^12.0.0",
47
49
  "@tsconfig/recommended": "^1.0.13",
48
- "@types/node": "^24.10.1",
50
+ "@types/node": "^25.0.9",
49
51
  "dts-cli": "^2.0.5",
50
52
  "husky": "^9.1.7",
51
- "size-limit": "^11.2.0",
53
+ "size-limit": "^12.0.0",
52
54
  "tslib": "^2.8.1",
53
- "tsx": "^4.20.6",
55
+ "tsx": "^4.21.0",
54
56
  "typescript": "^5.9.3"
55
57
  },
56
58
  "dependencies": {
57
- "@stacks/network": "^7.2.0",
59
+ "@stacks/network": "^7.3.1",
58
60
  "@stacks/stacks-blockchain-api-types": "^7.14.1",
59
- "@stacks/transactions": "^7.3.0",
61
+ "@stacks/transactions": "^7.3.1",
60
62
  "c32check": "^2.0.0",
61
63
  "clarity-abi": "^0.1.0",
62
64
  "ts-clarity": "^0.1.1"
package/src/ast.ts ADDED
@@ -0,0 +1,120 @@
1
+ /**
2
+ * WARNING:
3
+ *
4
+ * this file will be used in cross-runtime environments (browser, cloudflare workers, XLinkSDK, etc.),
5
+ * so please be careful when adding `import`s to it.
6
+ */
7
+
8
+ import { DEFAULT_STXER_API } from './constants';
9
+ import type { ClarityEpoch, ContractAST } from './types';
10
+
11
+ export interface AstOptions {
12
+ stxerApi?: string;
13
+ }
14
+
15
+ export interface GetContractAstOptions extends AstOptions {
16
+ contractId: string;
17
+ }
18
+
19
+ /**
20
+ * Fetch the AST for an on-chain contract.
21
+ * @param options - Contract ID and optional API endpoint
22
+ * @returns The contract AST with metadata
23
+ */
24
+ export async function getContractAST(
25
+ options: GetContractAstOptions,
26
+ ): Promise<ContractAST> {
27
+ const url = `${options.stxerApi ?? DEFAULT_STXER_API}/contracts/${options.contractId}`;
28
+
29
+ const response = await fetch(url, {
30
+ method: 'GET',
31
+ headers: {
32
+ Accept: 'application/json',
33
+ },
34
+ });
35
+
36
+ if (!response.ok) {
37
+ throw new Error(
38
+ `Failed to fetch contract AST: ${response.status} ${response.statusText}`,
39
+ );
40
+ }
41
+
42
+ const text = await response.text();
43
+ if (!text.startsWith('{')) {
44
+ throw new Error(`Invalid response from contracts endpoint: ${text}`);
45
+ }
46
+
47
+ return JSON.parse(text) as ContractAST;
48
+ }
49
+
50
+ export interface ParseContractOptions extends AstOptions {
51
+ sourceCode: string;
52
+ contractId: string;
53
+ clarityVersion?: '1' | '2' | '3' | '4';
54
+ epoch?: ClarityEpoch;
55
+ }
56
+
57
+ /**
58
+ * Parse contract source code into an AST.
59
+ * @param options - Source code, contract ID, and optional configuration
60
+ * @returns The parsed contract AST
61
+ */
62
+ export async function parseContract(
63
+ options: ParseContractOptions,
64
+ ): Promise<ContractAST> {
65
+ const url = `${options.stxerApi ?? DEFAULT_STXER_API}/contracts:parse`;
66
+
67
+ const payload: {
68
+ contract_id: string;
69
+ source_code: string;
70
+ clarity_version?: string;
71
+ epoch?: string;
72
+ } = {
73
+ contract_id: options.contractId,
74
+ source_code: options.sourceCode,
75
+ };
76
+
77
+ if (options.clarityVersion !== undefined) {
78
+ payload.clarity_version = options.clarityVersion;
79
+ }
80
+ if (options.epoch !== undefined) {
81
+ payload.epoch = options.epoch;
82
+ }
83
+
84
+ const response = await fetch(url, {
85
+ method: 'POST',
86
+ body: JSON.stringify(payload),
87
+ headers: {
88
+ 'Content-Type': 'application/json',
89
+ Accept: 'application/json',
90
+ },
91
+ });
92
+
93
+ if (!response.ok) {
94
+ throw new Error(
95
+ `Failed to parse contract: ${response.status} ${response.statusText}`,
96
+ );
97
+ }
98
+
99
+ const text = await response.text();
100
+ if (!text.startsWith('{')) {
101
+ throw new Error(`Invalid response from contract parse endpoint: ${text}`);
102
+ }
103
+
104
+ return JSON.parse(text) as ContractAST;
105
+ }
106
+
107
+ // Re-export AST-related types for convenience
108
+ export type {
109
+ ClarityAbi,
110
+ ClarityAbiFunction,
111
+ ClarityAbiFungibleToken,
112
+ ClarityAbiMap,
113
+ ClarityAbiNonFungibleToken,
114
+ ClarityAbiType,
115
+ ClarityAbiVariable,
116
+ ClarityEpoch,
117
+ ClarityVersion,
118
+ ContractAST,
119
+ SymbolicExpression,
120
+ } from './types';
@@ -10,7 +10,7 @@ import {
10
10
  contractPrincipalCV,
11
11
  type OptionalCV,
12
12
  } from '@stacks/transactions';
13
- import { type BatchReads, batchRead } from './BatchAPI';
13
+ import { type BatchReads, batchRead } from './batch-api';
14
14
 
15
15
  export interface ReadOnlyRequest {
16
16
  mode: 'readonly';
@@ -19,7 +19,7 @@ import type {
19
19
  InferVariableType,
20
20
  } from 'ts-clarity';
21
21
  import { decodeAbi, encodeAbi } from 'ts-clarity';
22
- import { BatchProcessor } from './BatchProcessor';
22
+ import { BatchProcessor } from './batch-processor';
23
23
 
24
24
  // Shared processor instance with default settings
25
25
  const defaultProcessor = new BatchProcessor({
@@ -0,0 +1,12 @@
1
+ /**
2
+ * API endpoint constants
3
+ */
4
+
5
+ /** Default stxer API endpoint for mainnet */
6
+ export const STXER_API_MAINNET = 'https://api.stxer.xyz';
7
+
8
+ /** stxer API endpoint for testnet */
9
+ export const STXER_API_TESTNET = 'https://testnet-api.stxer.xyz';
10
+
11
+ /** Default stxer API endpoint (mainnet) */
12
+ export const DEFAULT_STXER_API = STXER_API_MAINNET;
package/src/index.ts CHANGED
@@ -1,3 +1,17 @@
1
- export * from './BatchAPI';
1
+ export * from './ast';
2
+ export * from './batch-api';
2
3
  export * from './clarity-api';
4
+ export * from './constants';
3
5
  export * from './simulation';
6
+ // Export simulation-api functions explicitly to avoid type conflicts
7
+ export {
8
+ type CreateSessionOptions,
9
+ createSimulationSession,
10
+ getSimulationResult,
11
+ instantSimulation,
12
+ type SimulationApiOptions,
13
+ simulationBatchReads,
14
+ submitSimulationSteps,
15
+ } from './simulation-api';
16
+ export * from './tip';
17
+ export * from './types';