@secretkeylabs/stacks-tools 0.3.0 → 0.4.0-47d10ad

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/dist/index.js CHANGED
@@ -16,6 +16,17 @@ async function safePromise(promise) {
16
16
  });
17
17
  }
18
18
  }
19
+ function safeCall(fn) {
20
+ try {
21
+ return success(fn());
22
+ } catch (e) {
23
+ return error({
24
+ name: "SafeCallError",
25
+ message: "Safe call failed.",
26
+ data: e
27
+ });
28
+ }
29
+ }
19
30
 
20
31
  // src/stacks-api/accounts/balances.ts
21
32
  import * as v from "valibot";
@@ -91,11 +102,6 @@ async function balances(opts) {
91
102
  return success(validationResult.output);
92
103
  }
93
104
 
94
- // src/stacks-api/accounts/index.ts
95
- var accounts = {
96
- balances
97
- };
98
-
99
105
  // src/stacks-api/types.ts
100
106
  import * as v2 from "valibot";
101
107
  var baseListResponseSchema = v2.object({
@@ -169,11 +175,6 @@ async function getBlock(opts) {
169
175
  return success(validationResult.output);
170
176
  }
171
177
 
172
- // src/stacks-api/blocks/index.ts
173
- var blocks = {
174
- getBlock
175
- };
176
-
177
178
  // src/stacks-api/info/core-api.ts
178
179
  import * as v4 from "valibot";
179
180
  var CoreApiResponseSchema = v4.object({
@@ -325,12 +326,6 @@ async function poxDetails(args) {
325
326
  return success(validationResult.output);
326
327
  }
327
328
 
328
- // src/stacks-api/info/index.ts
329
- var info = {
330
- coreApi,
331
- poxDetails
332
- };
333
-
334
329
  // src/stacks-api/proof-of-transfer/cycle.ts
335
330
  import * as v6 from "valibot";
336
331
  var responseSchema3 = v6.object({
@@ -618,15 +613,6 @@ async function stackersForSignerInCycle(opts) {
618
613
  return success(validationResult.output);
619
614
  }
620
615
 
621
- // src/stacks-api/proof-of-transfer/index.ts
622
- var proofOfTransfer = {
623
- cycle,
624
- cycles,
625
- signerInCycle,
626
- signersInCycle,
627
- stackersForSignerInCycle
628
- };
629
-
630
616
  // src/stacks-api/smart-contracts/read-only.ts
631
617
  import * as v11 from "valibot";
632
618
  var readOnlyResponseSchema = v11.variant("okay", [
@@ -639,15 +625,15 @@ var readOnlyResponseSchema = v11.variant("okay", [
639
625
  cause: v11.unknown()
640
626
  })
641
627
  ]);
642
- async function readOnly(opts, apiOpts) {
643
- const init = {};
644
- if (apiOpts.apiKeyConfig) {
628
+ async function readOnly(args) {
629
+ const init = { method: "POST" };
630
+ if (args.apiKeyConfig) {
645
631
  init.headers = {
646
- [apiOpts.apiKeyConfig.header]: apiOpts.apiKeyConfig.key
632
+ [args.apiKeyConfig.header]: args.apiKeyConfig.key
647
633
  };
648
634
  }
649
635
  const res = await fetch(
650
- `${apiOpts.baseUrl}/v2/contracts/call-read/${opts.contractAddress}/${opts.contractName}/${opts.functionName}`,
636
+ `${args.baseUrl}/v2/contracts/call-read/${args.contractAddress}/${args.contractName}/${args.functionName}`,
651
637
  init
652
638
  );
653
639
  if (!res.ok) {
@@ -680,11 +666,6 @@ async function readOnly(opts, apiOpts) {
680
666
  return success(validationResult.output);
681
667
  }
682
668
 
683
- // src/stacks-api/smart-contracts/index.ts
684
- var smartContracts = {
685
- readOnly
686
- };
687
-
688
669
  // src/stacks-api/stacking-pool/members.ts
689
670
  import * as v12 from "valibot";
690
671
  var memberSchema = v12.object({
@@ -747,11 +728,6 @@ async function members(opts, apiOpts) {
747
728
  return success(validationResult.output);
748
729
  }
749
730
 
750
- // src/stacks-api/stacking-pool/index.ts
751
- var stackingPool = {
752
- members
753
- };
754
-
755
731
  // src/stacks-api/transactions/schemas.ts
756
732
  import * as v13 from "valibot";
757
733
  var baseTransactionSchema = v13.object({
@@ -776,7 +752,11 @@ var baseTransactionSchema = v13.object({
776
752
  parent_burn_block_time_iso: v13.string(),
777
753
  canonical: v13.boolean(),
778
754
  tx_index: v13.number(),
779
- tx_status: v13.string(),
755
+ tx_status: v13.union([
756
+ v13.literal("success"),
757
+ v13.literal("abort_by_response"),
758
+ v13.literal("abort_by_post_condition")
759
+ ]),
780
760
  tx_result: v13.object({
781
761
  hex: v13.string(),
782
762
  repr: v13.string()
@@ -951,25 +931,31 @@ async function getTransaction(args) {
951
931
  return success(validationResult.output);
952
932
  }
953
933
 
954
- // src/stacks-api/transactions/index.ts
955
- var transactions = {
956
- addressTransactions,
957
- getTransaction
958
- };
959
-
960
934
  // src/stacks-api/index.ts
961
- var stacksApi = {
962
- accounts,
963
- blocks,
964
- info,
965
- proofOfTransfer,
966
- smartContracts,
967
- stackingPool,
968
- transactions
935
+ var accounts = { balances };
936
+ var blocks = { getBlock };
937
+ var info = { coreApi, poxDetails };
938
+ var proofOfTransfer = {
939
+ cycle,
940
+ cycles,
941
+ signerInCycle,
942
+ signersInCycle,
943
+ stackersForSignerInCycle
969
944
  };
945
+ var smartContracts = { readOnly };
946
+ var stackingPool = { members };
947
+ var transactions = { addressTransactions, getTransaction };
970
948
 
971
949
  // src/utils/call-rate-limited-api.ts
972
950
  import { backOff } from "exponential-backoff";
951
+ var defaultStartingDelay = 15e3;
952
+ var defaultNumOfAttempts = 5;
953
+ function callRateLimitedApi(fn, options) {
954
+ return backOff(fn, {
955
+ startingDelay: options?.startingDelay ?? defaultStartingDelay,
956
+ numOfAttempts: options?.numOfAttempts ?? defaultNumOfAttempts
957
+ });
958
+ }
973
959
  async function safeCallRateLimitedApi(fn, options) {
974
960
  try {
975
961
  return await backOff(() => fn(), {
@@ -987,66 +973,22 @@ async function safeCallRateLimitedApi(fn, options) {
987
973
  }
988
974
  }
989
975
 
990
- // src/queries/get-signer-stacked-amount.ts
991
- async function getSignerStackedAmount(args) {
992
- let totalLocked = 0n;
993
- const { identifier, ...rest } = args;
994
- let hasMore = true;
995
- let offset = 0;
996
- let found = false;
997
- const limit = 200;
998
- while (hasMore && !found) {
999
- const [error2, data] = await safeCallRateLimitedApi(
1000
- () => signersInCycle({
1001
- ...rest,
1002
- limit
1003
- })
1004
- );
1005
- if (error2) {
1006
- return error({
1007
- name: "GetSignerTotalLockedError",
1008
- message: "Failed to get signer total locked.",
1009
- data: {
1010
- error: error2
1011
- }
1012
- });
1013
- }
1014
- for (const signer of data.results) {
1015
- if (identifier.type === "address") {
1016
- if (signer.signer_address === identifier.signerAddress) {
1017
- totalLocked = BigInt(signer.stacked_amount);
1018
- found = true;
1019
- break;
1020
- }
1021
- } else {
1022
- if (signer.signing_key === identifier.signerPublicKey) {
1023
- totalLocked = BigInt(signer.stacked_amount);
1024
- found = true;
1025
- break;
1026
- }
1027
- }
1028
- }
1029
- offset += limit + data.results.length;
1030
- hasMore = offset < data.total;
1031
- }
1032
- if (!found) {
1033
- return error({
1034
- name: "SignerNotFound",
1035
- message: "Signer not found.",
1036
- data: {
1037
- identifier,
1038
- cycle: args.cycleNumber
1039
- }
1040
- });
1041
- }
1042
- return success(totalLocked);
1043
- }
1044
-
1045
- // src/queries/index.ts
1046
- var queries = {
1047
- getSignerStackedAmount
976
+ // src/index.ts
977
+ var stacksApi = {
978
+ accounts,
979
+ blocks,
980
+ info,
981
+ proofOfTransfer,
982
+ smartContracts,
983
+ stackingPool,
984
+ transactions
1048
985
  };
1049
986
  export {
1050
- queries,
1051
- stacksApi
987
+ callRateLimitedApi,
988
+ error,
989
+ safeCall,
990
+ safeCallRateLimitedApi,
991
+ safePromise,
992
+ stacksApi,
993
+ success
1052
994
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@secretkeylabs/stacks-tools",
3
- "version": "0.3.0",
3
+ "version": "0.4.0-47d10ad",
4
4
  "type": "module",
5
5
  "files": [
6
6
  "dist"