stxer 0.6.1 → 0.7.0
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 +66 -2
- package/dist/batch-api.d.ts +2 -1
- package/dist/index.d.ts +1 -1
- package/dist/stxer.cjs.development.js +13 -3
- package/dist/stxer.cjs.development.js.map +1 -1
- package/dist/stxer.cjs.production.min.js +1 -1
- package/dist/stxer.cjs.production.min.js.map +1 -1
- package/dist/stxer.esm.js +13 -4
- package/dist/stxer.esm.js.map +1 -1
- package/dist/types.d.ts +303 -32
- package/package.json +14 -9
- package/src/batch-api.ts +9 -8
- package/src/index.ts +5 -10
- package/src/simulation-api.ts +1 -1
- package/src/types.ts +344 -36
package/README.md
CHANGED
|
@@ -220,6 +220,40 @@ const sessionId = await createSimulationSession(
|
|
|
220
220
|
);
|
|
221
221
|
```
|
|
222
222
|
|
|
223
|
+
#### TransactionReceipt: failure signals & events
|
|
224
|
+
|
|
225
|
+
When a step lands as `{ Transaction: { Ok: receipt } }`, the engine ran the transaction to completion — but that does **not** mean the contract logic succeeded. There are four failure signals on or around a receipt; check them in this order:
|
|
226
|
+
|
|
227
|
+
| Signal | Where | Meaning |
|
|
228
|
+
|---|---|---|
|
|
229
|
+
| Outer `Err` | `Result.Transaction = { Err: string }` | Engine refused the tx (deserialization, etc.). No receipt is produced. |
|
|
230
|
+
| `post_condition_aborted: true` | on the receipt | Execution ran, a post-condition tripped, state was rolled back. |
|
|
231
|
+
| `vm_error: string` (without PC abort) | on the receipt | Clarity VM raised a runtime error (overflow, unwrap on `none`, etc.) or static analysis failed. |
|
|
232
|
+
| `(err uX)` inside `result` | on the receipt | Contract returned a Clarity error response. Not signalled by any flag — decode `result` to detect (response::err prefix is `08`). |
|
|
233
|
+
|
|
234
|
+
**`post_condition_aborted` and `vm_error` are not independent.** When a post-condition trips, the upstream sets `post_condition_aborted: true` *and* writes the PC abort reason into `vm_error` as a side effect — so `vm_error` is also non-null in that case. The reverse is not true: `vm_error` alone (with `post_condition_aborted: false`) means a VM/runtime/analysis failure that is *not* a PC abort. A clean way to narrow:
|
|
235
|
+
|
|
236
|
+
```typescript
|
|
237
|
+
if (receipt.post_condition_aborted) {
|
|
238
|
+
// PC abort. receipt.vm_error holds the abort reason.
|
|
239
|
+
} else if (receipt.vm_error != null) {
|
|
240
|
+
// Runtime / analysis failure (not a PC abort).
|
|
241
|
+
} else if (receipt.result.startsWith('08')) {
|
|
242
|
+
// Contract returned (err uX). Decode receipt.result for the value.
|
|
243
|
+
} else {
|
|
244
|
+
// Clean success.
|
|
245
|
+
}
|
|
246
|
+
```
|
|
247
|
+
|
|
248
|
+
**Events are JSON-encoded strings, not objects.** `receipt.events` is `string[]`; each entry is a JSON-encoded event payload. Call `JSON.parse(receipt.events[i])` to inspect — iterating without parsing yields opaque strings.
|
|
249
|
+
|
|
250
|
+
```typescript
|
|
251
|
+
for (const ev of receipt.events) {
|
|
252
|
+
const event = JSON.parse(ev) as { type: string; contract_event?: { ... } };
|
|
253
|
+
if (event.type === 'contract_event') { /* handle print/log */ }
|
|
254
|
+
}
|
|
255
|
+
```
|
|
256
|
+
|
|
223
257
|
### 3. Get Chain Tip
|
|
224
258
|
|
|
225
259
|
Fetch the current chain tip information:
|
|
@@ -264,10 +298,10 @@ The SDK provides two approaches for efficient batch reading from the Stacks bloc
|
|
|
264
298
|
#### Direct Batch Reading
|
|
265
299
|
|
|
266
300
|
```typescript
|
|
267
|
-
import { batchRead } from 'stxer';
|
|
301
|
+
import { batchRead, type BatchReadsResult } from 'stxer';
|
|
268
302
|
|
|
269
303
|
// Batch read variables and maps
|
|
270
|
-
const result = await batchRead({
|
|
304
|
+
const result: BatchReadsResult = await batchRead({
|
|
271
305
|
variables: [{
|
|
272
306
|
contract: contractPrincipalCV(...),
|
|
273
307
|
variableName: 'my-var'
|
|
@@ -283,6 +317,14 @@ const result = await batchRead({
|
|
|
283
317
|
functionArgs: [/* clarity values */]
|
|
284
318
|
}]
|
|
285
319
|
});
|
|
320
|
+
|
|
321
|
+
// Result shape — `index_block_hash` matches the upstream wire field
|
|
322
|
+
// (the SDK previously aliased this as `tip`; renamed to remove the
|
|
323
|
+
// indirection).
|
|
324
|
+
result.index_block_hash; // string — the block the batch ran against
|
|
325
|
+
result.vars; // (ClarityValue | Error)[]
|
|
326
|
+
result.maps; // (ClarityValue | Error)[]
|
|
327
|
+
result.readonly; // (ClarityValue | Error)[]
|
|
286
328
|
```
|
|
287
329
|
|
|
288
330
|
#### BatchProcessor for Queue-based Operations
|
|
@@ -416,6 +458,28 @@ const ast = await getContractAST({
|
|
|
416
458
|
});
|
|
417
459
|
```
|
|
418
460
|
|
|
461
|
+
## Samples
|
|
462
|
+
|
|
463
|
+
Runnable end-to-end examples live in [`src/sample/`](https://github.com/stxer/stxer-sdk/tree/master/src/sample) on GitHub. Samples track `master` and may evolve faster than published SDK versions; if you pin a specific SDK version, browse the matching git tag.
|
|
464
|
+
|
|
465
|
+
| Sample | What it demonstrates |
|
|
466
|
+
|---|---|
|
|
467
|
+
| [`counter.ts`](https://github.com/stxer/stxer-sdk/blob/master/src/sample/counter.ts) | Full Simulation v2 lifecycle without `SimulationBuilder` — every `SimulationStepInput` variant (Transaction / Eval / SetContractCode / Reads / TenureExtend), narrowing on `SimulationStepResult`, and `SimulationStepSummary`. |
|
|
468
|
+
| [`instant.ts`](https://github.com/stxer/stxer-sdk/blob/master/src/sample/instant.ts) | `instantSimulation` with all 7 `ReadStep` variants. |
|
|
469
|
+
| [`failure-modes.ts`](https://github.com/stxer/stxer-sdk/blob/master/src/sample/failure-modes.ts) | The four failure signals on a `TransactionReceipt`, with bug-zoo triggers for each. |
|
|
470
|
+
| [`batch-categories.ts`](https://github.com/stxer/stxer-sdk/blob/master/src/sample/batch-categories.ts) | Every `simulationBatchReads` category and the sidecar `batchRead`. |
|
|
471
|
+
| [`contract-vitest.test.ts`](https://github.com/stxer/stxer-sdk/blob/master/src/sample/contract-vitest.test.ts) | Vitest suite that drives a Clarity contract through Simulation v2 — copy-pasteable CI test template. Pinned fork point so assertions stay deterministic. |
|
|
472
|
+
| [`verify-types.ts`](https://github.com/stxer/stxer-sdk/blob/master/src/sample/verify-types.ts) | Runtime type-drift detector. Hits every endpoint, dereferences every documented field, asserts each value's runtime type matches the declared SDK type. Run it before publishing your client after upstream changes. |
|
|
473
|
+
| [`read.ts`](https://github.com/stxer/stxer-sdk/blob/master/src/sample/read.ts) | `batchRead`, `BatchProcessor`, and the high-level `clarity-api` helpers (`callReadonly`, `readVariable`, `readMap`). |
|
|
474
|
+
|
|
475
|
+
Run any of them locally by cloning the SDK repo and:
|
|
476
|
+
|
|
477
|
+
```bash
|
|
478
|
+
pnpm install
|
|
479
|
+
pnpm sample:counter # or sample:instant / failure-modes / batch-categories / verify-types
|
|
480
|
+
pnpm sample:vitest # run the Vitest contract-test sample
|
|
481
|
+
```
|
|
482
|
+
|
|
419
483
|
## API Reference
|
|
420
484
|
|
|
421
485
|
### Constants
|
package/dist/batch-api.d.ts
CHANGED
|
@@ -23,7 +23,8 @@ export interface BatchReads {
|
|
|
23
23
|
index_block_hash?: string;
|
|
24
24
|
}
|
|
25
25
|
export interface BatchReadsResult {
|
|
26
|
-
|
|
26
|
+
/** Index block hash the batch ran against. Matches the wire field name. */
|
|
27
|
+
index_block_hash: string;
|
|
27
28
|
vars: (ClarityValue | Error)[];
|
|
28
29
|
maps: (ClarityValue | Error)[];
|
|
29
30
|
readonly: (ClarityValue | Error)[];
|
package/dist/index.d.ts
CHANGED
|
@@ -3,6 +3,6 @@ export * from './batch-api';
|
|
|
3
3
|
export * from './clarity-api';
|
|
4
4
|
export * from './constants';
|
|
5
5
|
export * from './simulation';
|
|
6
|
-
export
|
|
6
|
+
export * from './simulation-api';
|
|
7
7
|
export * from './tip';
|
|
8
8
|
export * from './types';
|
|
@@ -299,7 +299,7 @@ function _parseContract() {
|
|
|
299
299
|
var DEFAULT_STXER_API = 'https://api.stxer.xyz';
|
|
300
300
|
function convertResults(rs) {
|
|
301
301
|
var results = [];
|
|
302
|
-
for (var _iterator = _createForOfIteratorHelperLoose(rs), _step; !(_step = _iterator()).done;) {
|
|
302
|
+
for (var _iterator = _createForOfIteratorHelperLoose(rs != null ? rs : []), _step; !(_step = _iterator()).done;) {
|
|
303
303
|
var v = _step.value;
|
|
304
304
|
if ('Ok' in v) {
|
|
305
305
|
results.push(transactions.deserializeCV(v.Ok));
|
|
@@ -372,7 +372,7 @@ function _batchRead() {
|
|
|
372
372
|
case 3:
|
|
373
373
|
rs = JSON.parse(text);
|
|
374
374
|
return _context.a(2, {
|
|
375
|
-
|
|
375
|
+
index_block_hash: rs.index_block_hash,
|
|
376
376
|
vars: convertResults(rs.vars),
|
|
377
377
|
maps: convertResults(rs.maps),
|
|
378
378
|
readonly: convertResults(rs.readonly)
|
|
@@ -756,7 +756,7 @@ function _instantSimulation() {
|
|
|
756
756
|
}
|
|
757
757
|
apiEndpoint = (_options$stxerApi = options.stxerApi) != null ? _options$stxerApi : DEFAULT_STXER_API$1;
|
|
758
758
|
_context.n = 1;
|
|
759
|
-
return fetch(apiEndpoint + "/devtools/v2/
|
|
759
|
+
return fetch(apiEndpoint + "/devtools/v2/simulations:instant", {
|
|
760
760
|
method: 'POST',
|
|
761
761
|
body: JSON.stringify(request),
|
|
762
762
|
headers: {
|
|
@@ -1485,6 +1485,15 @@ function _getTip() {
|
|
|
1485
1485
|
return _getTip.apply(this, arguments);
|
|
1486
1486
|
}
|
|
1487
1487
|
|
|
1488
|
+
/**
|
|
1489
|
+
* Type definitions for stxer-api V2 endpoints
|
|
1490
|
+
* Hand-written types based on OpenAPI spec at https://api.stxer.xyz/openapi.json
|
|
1491
|
+
*/
|
|
1492
|
+
/** Convenience parser for `TransactionReceipt.events[i]`. */
|
|
1493
|
+
function parseSimulationEvent(raw) {
|
|
1494
|
+
return JSON.parse(raw);
|
|
1495
|
+
}
|
|
1496
|
+
|
|
1488
1497
|
exports.DEFAULT_STXER_API = DEFAULT_STXER_API$1;
|
|
1489
1498
|
exports.STXER_API_MAINNET = STXER_API_MAINNET;
|
|
1490
1499
|
exports.STXER_API_TESTNET = STXER_API_TESTNET;
|
|
@@ -1497,6 +1506,7 @@ exports.getSimulationResult = getSimulationResult;
|
|
|
1497
1506
|
exports.getTip = getTip;
|
|
1498
1507
|
exports.instantSimulation = instantSimulation;
|
|
1499
1508
|
exports.parseContract = parseContract;
|
|
1509
|
+
exports.parseSimulationEvent = parseSimulationEvent;
|
|
1500
1510
|
exports.readMap = readMap;
|
|
1501
1511
|
exports.readVariable = readVariable;
|
|
1502
1512
|
exports.simulationBatchReads = simulationBatchReads;
|