@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.cjs CHANGED
@@ -30,8 +30,13 @@ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: tru
30
30
  // src/index.ts
31
31
  var src_exports = {};
32
32
  __export(src_exports, {
33
- queries: () => queries,
34
- stacksApi: () => stacksApi
33
+ callRateLimitedApi: () => callRateLimitedApi,
34
+ error: () => error,
35
+ safeCall: () => safeCall,
36
+ safeCallRateLimitedApi: () => safeCallRateLimitedApi,
37
+ safePromise: () => safePromise,
38
+ stacksApi: () => stacksApi,
39
+ success: () => success
35
40
  });
36
41
  module.exports = __toCommonJS(src_exports);
37
42
 
@@ -53,6 +58,17 @@ async function safePromise(promise) {
53
58
  });
54
59
  }
55
60
  }
61
+ function safeCall(fn) {
62
+ try {
63
+ return success(fn());
64
+ } catch (e) {
65
+ return error({
66
+ name: "SafeCallError",
67
+ message: "Safe call failed.",
68
+ data: e
69
+ });
70
+ }
71
+ }
56
72
 
57
73
  // src/stacks-api/accounts/balances.ts
58
74
  var v = __toESM(require("valibot"), 1);
@@ -128,11 +144,6 @@ async function balances(opts) {
128
144
  return success(validationResult.output);
129
145
  }
130
146
 
131
- // src/stacks-api/accounts/index.ts
132
- var accounts = {
133
- balances
134
- };
135
-
136
147
  // src/stacks-api/types.ts
137
148
  var v2 = __toESM(require("valibot"), 1);
138
149
  var baseListResponseSchema = v2.object({
@@ -206,11 +217,6 @@ async function getBlock(opts) {
206
217
  return success(validationResult.output);
207
218
  }
208
219
 
209
- // src/stacks-api/blocks/index.ts
210
- var blocks = {
211
- getBlock
212
- };
213
-
214
220
  // src/stacks-api/info/core-api.ts
215
221
  var v4 = __toESM(require("valibot"), 1);
216
222
  var CoreApiResponseSchema = v4.object({
@@ -362,12 +368,6 @@ async function poxDetails(args) {
362
368
  return success(validationResult.output);
363
369
  }
364
370
 
365
- // src/stacks-api/info/index.ts
366
- var info = {
367
- coreApi,
368
- poxDetails
369
- };
370
-
371
371
  // src/stacks-api/proof-of-transfer/cycle.ts
372
372
  var v6 = __toESM(require("valibot"), 1);
373
373
  var responseSchema3 = v6.object({
@@ -655,15 +655,6 @@ async function stackersForSignerInCycle(opts) {
655
655
  return success(validationResult.output);
656
656
  }
657
657
 
658
- // src/stacks-api/proof-of-transfer/index.ts
659
- var proofOfTransfer = {
660
- cycle,
661
- cycles,
662
- signerInCycle,
663
- signersInCycle,
664
- stackersForSignerInCycle
665
- };
666
-
667
658
  // src/stacks-api/smart-contracts/read-only.ts
668
659
  var v11 = __toESM(require("valibot"), 1);
669
660
  var readOnlyResponseSchema = v11.variant("okay", [
@@ -676,15 +667,15 @@ var readOnlyResponseSchema = v11.variant("okay", [
676
667
  cause: v11.unknown()
677
668
  })
678
669
  ]);
679
- async function readOnly(opts, apiOpts) {
680
- const init = {};
681
- if (apiOpts.apiKeyConfig) {
670
+ async function readOnly(args) {
671
+ const init = { method: "POST" };
672
+ if (args.apiKeyConfig) {
682
673
  init.headers = {
683
- [apiOpts.apiKeyConfig.header]: apiOpts.apiKeyConfig.key
674
+ [args.apiKeyConfig.header]: args.apiKeyConfig.key
684
675
  };
685
676
  }
686
677
  const res = await fetch(
687
- `${apiOpts.baseUrl}/v2/contracts/call-read/${opts.contractAddress}/${opts.contractName}/${opts.functionName}`,
678
+ `${args.baseUrl}/v2/contracts/call-read/${args.contractAddress}/${args.contractName}/${args.functionName}`,
688
679
  init
689
680
  );
690
681
  if (!res.ok) {
@@ -717,11 +708,6 @@ async function readOnly(opts, apiOpts) {
717
708
  return success(validationResult.output);
718
709
  }
719
710
 
720
- // src/stacks-api/smart-contracts/index.ts
721
- var smartContracts = {
722
- readOnly
723
- };
724
-
725
711
  // src/stacks-api/stacking-pool/members.ts
726
712
  var v12 = __toESM(require("valibot"), 1);
727
713
  var memberSchema = v12.object({
@@ -784,11 +770,6 @@ async function members(opts, apiOpts) {
784
770
  return success(validationResult.output);
785
771
  }
786
772
 
787
- // src/stacks-api/stacking-pool/index.ts
788
- var stackingPool = {
789
- members
790
- };
791
-
792
773
  // src/stacks-api/transactions/schemas.ts
793
774
  var v13 = __toESM(require("valibot"), 1);
794
775
  var baseTransactionSchema = v13.object({
@@ -813,7 +794,11 @@ var baseTransactionSchema = v13.object({
813
794
  parent_burn_block_time_iso: v13.string(),
814
795
  canonical: v13.boolean(),
815
796
  tx_index: v13.number(),
816
- tx_status: v13.string(),
797
+ tx_status: v13.union([
798
+ v13.literal("success"),
799
+ v13.literal("abort_by_response"),
800
+ v13.literal("abort_by_post_condition")
801
+ ]),
817
802
  tx_result: v13.object({
818
803
  hex: v13.string(),
819
804
  repr: v13.string()
@@ -988,25 +973,31 @@ async function getTransaction(args) {
988
973
  return success(validationResult.output);
989
974
  }
990
975
 
991
- // src/stacks-api/transactions/index.ts
992
- var transactions = {
993
- addressTransactions,
994
- getTransaction
995
- };
996
-
997
976
  // src/stacks-api/index.ts
998
- var stacksApi = {
999
- accounts,
1000
- blocks,
1001
- info,
1002
- proofOfTransfer,
1003
- smartContracts,
1004
- stackingPool,
1005
- transactions
977
+ var accounts = { balances };
978
+ var blocks = { getBlock };
979
+ var info = { coreApi, poxDetails };
980
+ var proofOfTransfer = {
981
+ cycle,
982
+ cycles,
983
+ signerInCycle,
984
+ signersInCycle,
985
+ stackersForSignerInCycle
1006
986
  };
987
+ var smartContracts = { readOnly };
988
+ var stackingPool = { members };
989
+ var transactions = { addressTransactions, getTransaction };
1007
990
 
1008
991
  // src/utils/call-rate-limited-api.ts
1009
992
  var import_exponential_backoff = require("exponential-backoff");
993
+ var defaultStartingDelay = 15e3;
994
+ var defaultNumOfAttempts = 5;
995
+ function callRateLimitedApi(fn, options) {
996
+ return (0, import_exponential_backoff.backOff)(fn, {
997
+ startingDelay: options?.startingDelay ?? defaultStartingDelay,
998
+ numOfAttempts: options?.numOfAttempts ?? defaultNumOfAttempts
999
+ });
1000
+ }
1010
1001
  async function safeCallRateLimitedApi(fn, options) {
1011
1002
  try {
1012
1003
  return await (0, import_exponential_backoff.backOff)(() => fn(), {
@@ -1024,67 +1015,23 @@ async function safeCallRateLimitedApi(fn, options) {
1024
1015
  }
1025
1016
  }
1026
1017
 
1027
- // src/queries/get-signer-stacked-amount.ts
1028
- async function getSignerStackedAmount(args) {
1029
- let totalLocked = 0n;
1030
- const { identifier, ...rest } = args;
1031
- let hasMore = true;
1032
- let offset = 0;
1033
- let found = false;
1034
- const limit = 200;
1035
- while (hasMore && !found) {
1036
- const [error2, data] = await safeCallRateLimitedApi(
1037
- () => signersInCycle({
1038
- ...rest,
1039
- limit
1040
- })
1041
- );
1042
- if (error2) {
1043
- return error({
1044
- name: "GetSignerTotalLockedError",
1045
- message: "Failed to get signer total locked.",
1046
- data: {
1047
- error: error2
1048
- }
1049
- });
1050
- }
1051
- for (const signer of data.results) {
1052
- if (identifier.type === "address") {
1053
- if (signer.signer_address === identifier.signerAddress) {
1054
- totalLocked = BigInt(signer.stacked_amount);
1055
- found = true;
1056
- break;
1057
- }
1058
- } else {
1059
- if (signer.signing_key === identifier.signerPublicKey) {
1060
- totalLocked = BigInt(signer.stacked_amount);
1061
- found = true;
1062
- break;
1063
- }
1064
- }
1065
- }
1066
- offset += limit + data.results.length;
1067
- hasMore = offset < data.total;
1068
- }
1069
- if (!found) {
1070
- return error({
1071
- name: "SignerNotFound",
1072
- message: "Signer not found.",
1073
- data: {
1074
- identifier,
1075
- cycle: args.cycleNumber
1076
- }
1077
- });
1078
- }
1079
- return success(totalLocked);
1080
- }
1081
-
1082
- // src/queries/index.ts
1083
- var queries = {
1084
- getSignerStackedAmount
1018
+ // src/index.ts
1019
+ var stacksApi = {
1020
+ accounts,
1021
+ blocks,
1022
+ info,
1023
+ proofOfTransfer,
1024
+ smartContracts,
1025
+ stackingPool,
1026
+ transactions
1085
1027
  };
1086
1028
  // Annotate the CommonJS export names for ESM import in node:
1087
1029
  0 && (module.exports = {
1088
- queries,
1089
- stacksApi
1030
+ callRateLimitedApi,
1031
+ error,
1032
+ safeCall,
1033
+ safeCallRateLimitedApi,
1034
+ safePromise,
1035
+ stacksApi,
1036
+ success
1090
1037
  });