stxer 0.2.6 → 0.3.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,30 @@
1
+ import { type ClarityValue, type ContractPrincipalCV, type OptionalCV } from '@stacks/transactions';
2
+ export interface BatchReads {
3
+ variables?: {
4
+ contract: ContractPrincipalCV;
5
+ variableName: string;
6
+ }[];
7
+ maps?: {
8
+ contract: ContractPrincipalCV;
9
+ mapName: string;
10
+ mapKey: ClarityValue;
11
+ }[];
12
+ index_block_hash?: string;
13
+ }
14
+ export interface BatchReadsResult {
15
+ variables: (ClarityValue | Error)[];
16
+ maps: (OptionalCV | Error)[];
17
+ }
18
+ export interface BatchApiOptions {
19
+ stxerApi?: string;
20
+ }
21
+ export declare function batchRead(reads: BatchReads, options?: BatchApiOptions): Promise<BatchReadsResult>;
22
+ export interface BatchReadonlyRequest {
23
+ readonly: {
24
+ contract: ContractPrincipalCV;
25
+ functionName: string;
26
+ functionArgs: ClarityValue[];
27
+ }[];
28
+ index_block_hash?: string;
29
+ }
30
+ export declare function batchReadonly(req: BatchReadonlyRequest, options?: BatchApiOptions): Promise<(ClarityValue | Error)[]>;
package/dist/index.d.ts CHANGED
@@ -1,52 +1,2 @@
1
- import { type StacksNetworkName } from '@stacks/network';
2
- import { type ClarityValue, ClarityVersion, type StacksTransactionWire } from '@stacks/transactions';
3
- export interface SimulationEval {
4
- contract_id: string;
5
- code: string;
6
- }
7
- export declare function runEval({ contract_id, code }: SimulationEval): import("@stacks/transactions").TupleCV<import("@stacks/transactions").TupleData<import("@stacks/transactions").BufferCV | import("@stacks/transactions").UIntCV>>;
8
- export declare function runSimulation(apiEndpoint: string, block_hash: string, block_height: number, txs: (StacksTransactionWire | SimulationEval)[]): Promise<string>;
9
- interface SimulationBuilderOptions {
10
- apiEndpoint?: string;
11
- stacksNodeAPI?: string;
12
- network?: StacksNetworkName | string;
13
- }
14
- export declare class SimulationBuilder {
15
- private apiEndpoint;
16
- private stacksNodeAPI;
17
- private network;
18
- private constructor();
19
- static new(options?: SimulationBuilderOptions): SimulationBuilder;
20
- private block;
21
- private sender;
22
- private steps;
23
- useBlockHeight(block: number): this;
24
- withSender(address: string): this;
25
- addSTXTransfer(params: {
26
- recipient: string;
27
- amount: number;
28
- sender?: string;
29
- fee?: number;
30
- }): this;
31
- addContractCall(params: {
32
- contract_id: string;
33
- function_name: string;
34
- function_args?: ClarityValue[];
35
- sender?: string;
36
- fee?: number;
37
- }): this;
38
- addContractDeploy(params: {
39
- contract_name: string;
40
- source_code: string;
41
- deployer?: string;
42
- fee?: number;
43
- clarity_version?: ClarityVersion;
44
- }): this;
45
- addEvalCode(inside_contract_id: string, code: string): this;
46
- addMapRead(contract_id: string, map: string, key: string): this;
47
- addVarRead(contract_id: string, variable: string): this;
48
- private getBlockInfo;
49
- run(): Promise<string>;
50
- pipe(transform: (builder: SimulationBuilder) => SimulationBuilder): SimulationBuilder;
51
- }
52
- export {};
1
+ export * from './batch-api';
2
+ export * from './simulation';
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1,53 @@
1
+ import { type StacksNetworkName } from '@stacks/network';
2
+ import { type ClarityValue, ClarityVersion, type StacksTransactionWire } from '@stacks/transactions';
3
+ export interface SimulationEval {
4
+ contract_id: string;
5
+ code: string;
6
+ }
7
+ export declare function runEval({ contract_id, code }: SimulationEval): import("@stacks/transactions").TupleCV<import("@stacks/transactions").TupleData<import("@stacks/transactions").BufferCV | import("@stacks/transactions").UIntCV>>;
8
+ export declare function runSimulation(apiEndpoint: string, block_hash: string, block_height: number, txs: (StacksTransactionWire | SimulationEval)[]): Promise<string>;
9
+ interface SimulationBuilderOptions {
10
+ apiEndpoint?: string;
11
+ stacksNodeAPI?: string;
12
+ network?: StacksNetworkName | string;
13
+ }
14
+ export declare class SimulationBuilder {
15
+ private apiEndpoint;
16
+ private stacksNodeAPI;
17
+ private network;
18
+ private constructor();
19
+ static new(options?: SimulationBuilderOptions): SimulationBuilder;
20
+ private block;
21
+ private sender;
22
+ private steps;
23
+ useBlockHeight(block: number): this;
24
+ withSender(address: string): this;
25
+ inlineSimulation(simulationId: string): this;
26
+ addSTXTransfer(params: {
27
+ recipient: string;
28
+ amount: number;
29
+ sender?: string;
30
+ fee?: number;
31
+ }): this;
32
+ addContractCall(params: {
33
+ contract_id: string;
34
+ function_name: string;
35
+ function_args?: ClarityValue[];
36
+ sender?: string;
37
+ fee?: number;
38
+ }): this;
39
+ addContractDeploy(params: {
40
+ contract_name: string;
41
+ source_code: string;
42
+ deployer?: string;
43
+ fee?: number;
44
+ clarity_version?: ClarityVersion;
45
+ }): this;
46
+ addEvalCode(inside_contract_id: string, code: string): this;
47
+ addMapRead(contract_id: string, map: string, key: string): this;
48
+ addVarRead(contract_id: string, variable: string): this;
49
+ private getBlockInfo;
50
+ run(): Promise<string>;
51
+ pipe(transform: (builder: SimulationBuilder) => SimulationBuilder): SimulationBuilder;
52
+ }
53
+ export {};
@@ -2,9 +2,9 @@
2
2
 
3
3
  Object.defineProperty(exports, '__esModule', { value: true });
4
4
 
5
+ var transactions = require('@stacks/transactions');
5
6
  var tsClarity = require('ts-clarity');
6
7
  var network = require('@stacks/network');
7
- var transactions = require('@stacks/transactions');
8
8
  var c32check = require('c32check');
9
9
 
10
10
  function _arrayLikeToArray(r, a) {
@@ -372,6 +372,145 @@ function _unsupportedIterableToArray(r, a) {
372
372
  }
373
373
  }
374
374
 
375
+ var DEFAULT_STXER_API = 'https://api.stxer.xyz';
376
+ function batchRead(_x, _x2) {
377
+ return _batchRead.apply(this, arguments);
378
+ }
379
+ function _batchRead() {
380
+ _batchRead = _asyncToGenerator(/*#__PURE__*/_regeneratorRuntime().mark(function _callee(reads, options) {
381
+ var _options$stxerApi;
382
+ var payload, _iterator, _step, variable, _iterator2, _step2, map, ibh, url, data, text, results, rs, variablesLength, i, result, _i, _result;
383
+ return _regeneratorRuntime().wrap(function _callee$(_context) {
384
+ while (1) switch (_context.prev = _context.next) {
385
+ case 0:
386
+ if (options === void 0) {
387
+ options = {};
388
+ }
389
+ payload = [];
390
+ if (reads.variables != null) {
391
+ for (_iterator = _createForOfIteratorHelperLoose(reads.variables); !(_step = _iterator()).done;) {
392
+ variable = _step.value;
393
+ payload.push([transactions.serializeCV(variable.contract), variable.variableName]);
394
+ }
395
+ }
396
+ if (reads.maps != null) {
397
+ for (_iterator2 = _createForOfIteratorHelperLoose(reads.maps); !(_step2 = _iterator2()).done;) {
398
+ map = _step2.value;
399
+ payload.push([transactions.serializeCV(map.contract), map.mapName, transactions.serializeCV(map.mapKey)]);
400
+ }
401
+ }
402
+ ibh = reads.index_block_hash == null ? null : reads.index_block_hash.startsWith('0x') ? reads.index_block_hash.substring(2) : reads.index_block_hash;
403
+ url = ((_options$stxerApi = options.stxerApi) != null ? _options$stxerApi : DEFAULT_STXER_API) + "/sidecar/v2/batch-reads" + (ibh == null ? '' : "?tip=" + ibh);
404
+ _context.next = 8;
405
+ return fetch(url, {
406
+ method: 'POST',
407
+ body: JSON.stringify(payload)
408
+ });
409
+ case 8:
410
+ data = _context.sent;
411
+ _context.next = 11;
412
+ return data.text();
413
+ case 11:
414
+ text = _context.sent;
415
+ if (!(!text.includes('Ok') && !text.includes('Err'))) {
416
+ _context.next = 14;
417
+ break;
418
+ }
419
+ throw new Error("Requesting batch reads failed: " + text + ", url: " + url + ", payload: " + JSON.stringify(payload));
420
+ case 14:
421
+ results = JSON.parse(text);
422
+ rs = {
423
+ variables: [],
424
+ maps: []
425
+ };
426
+ variablesLength = 0;
427
+ if (reads.variables != null) {
428
+ variablesLength = reads.variables.length;
429
+ for (i = 0; i < variablesLength; i++) {
430
+ result = results[i];
431
+ if ('Ok' in result) {
432
+ rs.variables.push(transactions.deserializeCV(result.Ok));
433
+ } else {
434
+ rs.variables.push(new Error(result.Err));
435
+ }
436
+ }
437
+ }
438
+ if (reads.maps != null) {
439
+ for (_i = 0; _i < reads.maps.length; _i++) {
440
+ _result = results[_i + variablesLength];
441
+ if ('Ok' in _result) {
442
+ rs.maps.push(transactions.deserializeCV(_result.Ok));
443
+ } else {
444
+ rs.maps.push(new Error(_result.Err));
445
+ }
446
+ }
447
+ }
448
+ return _context.abrupt("return", rs);
449
+ case 20:
450
+ case "end":
451
+ return _context.stop();
452
+ }
453
+ }, _callee);
454
+ }));
455
+ return _batchRead.apply(this, arguments);
456
+ }
457
+ function batchReadonly(_x3, _x4) {
458
+ return _batchReadonly.apply(this, arguments);
459
+ }
460
+ function _batchReadonly() {
461
+ _batchReadonly = _asyncToGenerator(/*#__PURE__*/_regeneratorRuntime().mark(function _callee2(req, options) {
462
+ var _options$stxerApi2;
463
+ var payload, ibh, url, data, text, results, rs, _iterator3, _step3, result;
464
+ return _regeneratorRuntime().wrap(function _callee2$(_context2) {
465
+ while (1) switch (_context2.prev = _context2.next) {
466
+ case 0:
467
+ if (options === void 0) {
468
+ options = {};
469
+ }
470
+ payload = req.readonly.map(function (r) {
471
+ return [transactions.serializeCV(r.contract), r.functionName].concat(r.functionArgs.map(function (arg) {
472
+ return transactions.serializeCV(arg);
473
+ }));
474
+ });
475
+ ibh = req.index_block_hash == null ? null : req.index_block_hash.startsWith('0x') ? req.index_block_hash.substring(2) : req.index_block_hash;
476
+ url = ((_options$stxerApi2 = options.stxerApi) != null ? _options$stxerApi2 : DEFAULT_STXER_API) + "/sidecar/v2/batch-readonly" + (ibh == null ? '' : "?tip=" + ibh);
477
+ _context2.next = 6;
478
+ return fetch(url, {
479
+ method: 'POST',
480
+ body: JSON.stringify(payload)
481
+ });
482
+ case 6:
483
+ data = _context2.sent;
484
+ _context2.next = 9;
485
+ return data.text();
486
+ case 9:
487
+ text = _context2.sent;
488
+ if (!(!text.includes('Ok') && !text.includes('Err'))) {
489
+ _context2.next = 12;
490
+ break;
491
+ }
492
+ throw new Error("Requesting batch readonly failed: " + text + ", url: " + url + ", payload: " + JSON.stringify(payload));
493
+ case 12:
494
+ results = JSON.parse(text);
495
+ rs = [];
496
+ for (_iterator3 = _createForOfIteratorHelperLoose(results); !(_step3 = _iterator3()).done;) {
497
+ result = _step3.value;
498
+ if ('Ok' in result) {
499
+ rs.push(transactions.deserializeCV(result.Ok));
500
+ } else {
501
+ rs.push(new Error(result.Err));
502
+ }
503
+ }
504
+ return _context2.abrupt("return", rs);
505
+ case 16:
506
+ case "end":
507
+ return _context2.stop();
508
+ }
509
+ }, _callee2);
510
+ }));
511
+ return _batchReadonly.apply(this, arguments);
512
+ }
513
+
375
514
  function runTx(tx) {
376
515
  // type 0: run transaction
377
516
  return transactions.tupleCV({
@@ -399,7 +538,7 @@ function runSimulation(_x, _x2, _x3, _x4) {
399
538
  }
400
539
  function _runSimulation() {
401
540
  _runSimulation = _asyncToGenerator(/*#__PURE__*/_regeneratorRuntime().mark(function _callee5(apiEndpoint, block_hash, block_height, txs) {
402
- var header, heightBytes, view, hashHex, matches, hashBytes, txBytes, totalLength, body, offset, _iterator2, _step2, tx, rs;
541
+ var header, heightBytes, view, hashHex, matches, hashBytes, txBytes, totalLength, body, offset, _iterator3, _step4, tx, rs;
403
542
  return _regeneratorRuntime().wrap(function _callee5$(_context5) {
404
543
  while (1) switch (_context5.prev = _context5.next) {
405
544
  case 0:
@@ -436,8 +575,8 @@ function _runSimulation() {
436
575
  offset += heightBytes.length;
437
576
  body.set(hashBytes, offset);
438
577
  offset += hashBytes.length;
439
- for (_iterator2 = _createForOfIteratorHelperLoose(txBytes); !(_step2 = _iterator2()).done;) {
440
- tx = _step2.value;
578
+ for (_iterator3 = _createForOfIteratorHelperLoose(txBytes); !(_step4 = _iterator3()).done;) {
579
+ tx = _step4.value;
441
580
  body.set(tx, offset);
442
581
  offset += tx.length;
443
582
  }
@@ -512,6 +651,12 @@ var SimulationBuilder = /*#__PURE__*/function () {
512
651
  this.sender = address;
513
652
  return this;
514
653
  };
654
+ _proto.inlineSimulation = function inlineSimulation(simulationId) {
655
+ this.steps.push({
656
+ simulationId: simulationId
657
+ });
658
+ return this;
659
+ };
515
660
  _proto.addSTXTransfer = function addSTXTransfer(params) {
516
661
  var _params$sender, _params$fee;
517
662
  if (params.sender == null && this.sender === '') {
@@ -617,7 +762,7 @@ var SimulationBuilder = /*#__PURE__*/function () {
617
762
  _proto.run = /*#__PURE__*/function () {
618
763
  var _run = /*#__PURE__*/_asyncToGenerator(/*#__PURE__*/_regeneratorRuntime().mark(function _callee3() {
619
764
  var _this = this;
620
- var block, txs, nonce_by_address, nextNonce, _iterator, _step, step, network$1, _step$function_args, nonce, _step$contract_id$spl, contractAddress, contractName, tx, _nonce, _tx, _nonce2, _tx2, id;
765
+ var block, txs, nonce_by_address, nextNonce, network$1, _iterator, _step, step, previousSimulation, _iterator2, _step2, _step3, _step$function_args, nonce, _step$contract_id$spl, contractAddress, contractName, tx, _nonce, _tx, _nonce2, _tx2, id;
621
766
  return _regeneratorRuntime().wrap(function _callee3$(_context3) {
622
767
  while (1) switch (_context3.prev = _context3.next) {
623
768
  case 0:
@@ -662,13 +807,6 @@ var SimulationBuilder = /*#__PURE__*/function () {
662
807
  return _ref2.apply(this, arguments);
663
808
  };
664
809
  }();
665
- _iterator = _createForOfIteratorHelperLoose(this.steps);
666
- case 9:
667
- if ((_step = _iterator()).done) {
668
- _context3.next = 50;
669
- break;
670
- }
671
- step = _step.value;
672
810
  network$1 = this.network === 'mainnet' ? network.STACKS_MAINNET : network.STACKS_TESTNET;
673
811
  if (this.stacksNodeAPI) {
674
812
  network$1 = _extends({}, network$1, {
@@ -677,16 +815,47 @@ var SimulationBuilder = /*#__PURE__*/function () {
677
815
  })
678
816
  });
679
817
  }
680
- if (!('sender' in step && 'function_name' in step)) {
681
- _context3.next = 25;
818
+ _iterator = _createForOfIteratorHelperLoose(this.steps);
819
+ case 11:
820
+ if ((_step = _iterator()).done) {
821
+ _context3.next = 57;
822
+ break;
823
+ }
824
+ step = _step.value;
825
+ if (!('simulationId' in step)) {
826
+ _context3.next = 20;
682
827
  break;
683
828
  }
684
829
  _context3.next = 16;
685
- return nextNonce(step.sender);
830
+ return fetch("https://api.stxer.xyz/simulations/" + step.simulationId + "/request").then(function (x) {
831
+ return x.json();
832
+ });
686
833
  case 16:
834
+ previousSimulation = _context3.sent;
835
+ for (_iterator2 = _createForOfIteratorHelperLoose(previousSimulation.steps); !(_step2 = _iterator2()).done;) {
836
+ _step3 = _step2.value;
837
+ if ('tx' in _step3) {
838
+ txs.push(transactions.deserializeTransaction(_step3.tx));
839
+ } else if ('code' in _step3 && 'contract' in _step3) {
840
+ txs.push({
841
+ contract_id: _step3.contract,
842
+ code: _step3.code
843
+ });
844
+ }
845
+ }
846
+ _context3.next = 55;
847
+ break;
848
+ case 20:
849
+ if (!('sender' in step && 'function_name' in step)) {
850
+ _context3.next = 32;
851
+ break;
852
+ }
853
+ _context3.next = 23;
854
+ return nextNonce(step.sender);
855
+ case 23:
687
856
  nonce = _context3.sent;
688
857
  _step$contract_id$spl = step.contract_id.split('.'), contractAddress = _step$contract_id$spl[0], contractName = _step$contract_id$spl[1];
689
- _context3.next = 20;
858
+ _context3.next = 27;
690
859
  return transactions.makeUnsignedContractCall({
691
860
  contractAddress: contractAddress,
692
861
  contractName: contractName,
@@ -698,22 +867,22 @@ var SimulationBuilder = /*#__PURE__*/function () {
698
867
  postConditionMode: transactions.PostConditionMode.Allow,
699
868
  fee: step.fee
700
869
  });
701
- case 20:
870
+ case 27:
702
871
  tx = _context3.sent;
703
872
  tx.auth.spendingCondition.signer = c32check.c32addressDecode(step.sender)[1];
704
873
  txs.push(tx);
705
- _context3.next = 48;
874
+ _context3.next = 55;
706
875
  break;
707
- case 25:
876
+ case 32:
708
877
  if (!('sender' in step && 'recipient' in step)) {
709
- _context3.next = 36;
878
+ _context3.next = 43;
710
879
  break;
711
880
  }
712
- _context3.next = 28;
881
+ _context3.next = 35;
713
882
  return nextNonce(step.sender);
714
- case 28:
883
+ case 35:
715
884
  _nonce = _context3.sent;
716
- _context3.next = 31;
885
+ _context3.next = 38;
717
886
  return transactions.makeUnsignedSTXTokenTransfer({
718
887
  recipient: step.recipient,
719
888
  amount: step.amount,
@@ -722,22 +891,22 @@ var SimulationBuilder = /*#__PURE__*/function () {
722
891
  publicKey: '',
723
892
  fee: step.fee
724
893
  });
725
- case 31:
894
+ case 38:
726
895
  _tx = _context3.sent;
727
896
  _tx.auth.spendingCondition.signer = c32check.c32addressDecode(step.sender)[1];
728
897
  txs.push(_tx);
729
- _context3.next = 48;
898
+ _context3.next = 55;
730
899
  break;
731
- case 36:
900
+ case 43:
732
901
  if (!('deployer' in step)) {
733
- _context3.next = 47;
902
+ _context3.next = 54;
734
903
  break;
735
904
  }
736
- _context3.next = 39;
905
+ _context3.next = 46;
737
906
  return nextNonce(step.deployer);
738
- case 39:
907
+ case 46:
739
908
  _nonce2 = _context3.sent;
740
- _context3.next = 42;
909
+ _context3.next = 49;
741
910
  return transactions.makeUnsignedContractDeploy({
742
911
  contractName: step.contract_name,
743
912
  codeBody: step.source_code,
@@ -747,30 +916,30 @@ var SimulationBuilder = /*#__PURE__*/function () {
747
916
  postConditionMode: transactions.PostConditionMode.Allow,
748
917
  fee: step.fee
749
918
  });
750
- case 42:
919
+ case 49:
751
920
  _tx2 = _context3.sent;
752
921
  _tx2.auth.spendingCondition.signer = c32check.c32addressDecode(step.deployer)[1];
753
922
  txs.push(_tx2);
754
- _context3.next = 48;
923
+ _context3.next = 55;
755
924
  break;
756
- case 47:
925
+ case 54:
757
926
  if ('code' in step) {
758
927
  txs.push(step);
759
928
  } else {
760
929
  // biome-ignore lint/style/noUnusedTemplateLiteral: <explanation>
761
930
  console.log("Invalid simulation step:", step);
762
931
  }
763
- case 48:
764
- _context3.next = 9;
932
+ case 55:
933
+ _context3.next = 11;
765
934
  break;
766
- case 50:
767
- _context3.next = 52;
935
+ case 57:
936
+ _context3.next = 59;
768
937
  return runSimulation(this.apiEndpoint + "/simulations", block.block_hash, block.block_height, txs);
769
- case 52:
938
+ case 59:
770
939
  id = _context3.sent;
771
940
  console.log("Simulation will be available at: https://stxer.xyz/simulations/" + this.network + "/" + id);
772
941
  return _context3.abrupt("return", id);
773
- case 55:
942
+ case 62:
774
943
  case "end":
775
944
  return _context3.stop();
776
945
  }
@@ -788,6 +957,8 @@ var SimulationBuilder = /*#__PURE__*/function () {
788
957
  }();
789
958
 
790
959
  exports.SimulationBuilder = SimulationBuilder;
960
+ exports.batchRead = batchRead;
961
+ exports.batchReadonly = batchReadonly;
791
962
  exports.runEval = runEval;
792
963
  exports.runSimulation = runSimulation;
793
964
  //# sourceMappingURL=stxer.cjs.development.js.map