@secretkeylabs/stacks-tools 0.4.0-98de979 → 0.4.0-da5eb9d

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
@@ -151,6 +151,12 @@ async function latestNonce(opts) {
151
151
  return success(validationResult.output);
152
152
  }
153
153
 
154
+ // src/stacks-api/accounts/index.ts
155
+ var accounts = {
156
+ balances,
157
+ latestNonce
158
+ };
159
+
154
160
  // src/stacks-api/types.ts
155
161
  import * as v3 from "valibot";
156
162
  var baseListResponseSchema = v3.object({
@@ -224,6 +230,52 @@ async function getBlock(opts) {
224
230
  return success(validationResult.output);
225
231
  }
226
232
 
233
+ // src/stacks-api/blocks/index.ts
234
+ var blocks = {
235
+ getBlock
236
+ };
237
+
238
+ // src/stacks-api/faucets/stx.ts
239
+ async function stx(opts) {
240
+ const search = new URLSearchParams();
241
+ search.append("address", opts.address);
242
+ if (opts.stacking) search.append("stacking", "true");
243
+ const init = {};
244
+ if (opts.apiKeyConfig) {
245
+ init.headers = {
246
+ [opts.apiKeyConfig.header]: opts.apiKeyConfig.key
247
+ };
248
+ }
249
+ init.method = "POST";
250
+ const endpoint = `${opts.baseUrl}/extended/v1/faucets/stx?${search}`;
251
+ const res = await fetch(endpoint, init);
252
+ if (!res.ok) {
253
+ return error({
254
+ name: "FetchStxError",
255
+ message: "Failed to fetch STX.",
256
+ data: {
257
+ status: res.status,
258
+ statusText: res.statusText,
259
+ bodyParseResult: await safePromise(res.json())
260
+ }
261
+ });
262
+ }
263
+ const [jsonError, data] = await safePromise(res.json());
264
+ if (jsonError) {
265
+ return error({
266
+ name: "ParseBodyError",
267
+ message: "Failed to parse response body as JSON.",
268
+ data: jsonError
269
+ });
270
+ }
271
+ return success(data);
272
+ }
273
+
274
+ // src/stacks-api/faucets/index.ts
275
+ var faucets = {
276
+ stx
277
+ };
278
+
227
279
  // src/stacks-api/info/core-api.ts
228
280
  import * as v5 from "valibot";
229
281
  var CoreApiResponseSchema = v5.object({
@@ -376,6 +428,12 @@ async function poxDetails(args) {
376
428
  return success(validationResult.output);
377
429
  }
378
430
 
431
+ // src/stacks-api/info/index.ts
432
+ var info = {
433
+ coreApi,
434
+ poxDetails
435
+ };
436
+
379
437
  // src/stacks-api/proof-of-transfer/cycle.ts
380
438
  import * as v7 from "valibot";
381
439
  var responseSchema4 = v7.object({
@@ -664,6 +722,15 @@ async function stackersForSignerInCycle(opts) {
664
722
  return success(validationResult.output);
665
723
  }
666
724
 
725
+ // src/stacks-api/proof-of-transfer/index.ts
726
+ var proofOfTransfer = {
727
+ cycle,
728
+ cycles,
729
+ signerInCycle,
730
+ signersInCycle,
731
+ stackersForSignerInCycle
732
+ };
733
+
667
734
  // src/stacks-api/smart-contracts/read-only.ts
668
735
  import * as v12 from "valibot";
669
736
  var readOnlyResponseSchema = v12.variant("okay", [
@@ -726,6 +793,11 @@ async function readOnly(args) {
726
793
  return success(validationResult.output);
727
794
  }
728
795
 
796
+ // src/stacks-api/smart-contracts/index.ts
797
+ var smartContracts = {
798
+ readOnly
799
+ };
800
+
729
801
  // src/stacks-api/stacking-pool/members.ts
730
802
  import * as v13 from "valibot";
731
803
  var memberSchema = v13.object({
@@ -742,22 +814,20 @@ var membersResponseSchema = v13.object({
742
814
  total: v13.number(),
743
815
  results: v13.array(memberSchema)
744
816
  });
745
- async function members(opts, apiOpts) {
817
+ async function members(args) {
746
818
  const search = new URLSearchParams();
747
- if (opts.afterBlock) search.append("after_block", opts.afterBlock.toString());
748
- if (opts.unanchored) search.append("unanchored", "true");
749
- if (opts.limit) search.append("limit", opts.limit.toString());
750
- if (opts.offset) search.append("offset", opts.offset.toString());
819
+ if (args.afterBlock) search.append("after_block", args.afterBlock.toString());
820
+ if (args.unanchored) search.append("unanchored", "true");
821
+ if (args.limit) search.append("limit", args.limit.toString());
822
+ if (args.offset) search.append("offset", args.offset.toString());
751
823
  const init = {};
752
- if (apiOpts.apiKeyConfig) {
824
+ if (args.apiKeyConfig) {
753
825
  init.headers = {
754
- [apiOpts.apiKeyConfig.header]: apiOpts.apiKeyConfig.key
826
+ [args.apiKeyConfig.header]: args.apiKeyConfig.key
755
827
  };
756
828
  }
757
- const res = await fetch(
758
- `${apiOpts.baseUrl}/extended/beta/stacking/${opts.poolPrincipal}/delegations?${search}`,
759
- init
760
- );
829
+ const endpoint = `${args.baseUrl}/extended/v1/pox4/${args.poolPrincipal}/delegations?${search}`;
830
+ const res = await fetch(endpoint, init);
761
831
  if (!res.ok) {
762
832
  return error({
763
833
  name: "FetchMembersError",
@@ -788,6 +858,11 @@ async function members(opts, apiOpts) {
788
858
  return success(validationResult.output);
789
859
  }
790
860
 
861
+ // src/stacks-api/stacking-pool/index.ts
862
+ var stackingPool = {
863
+ members
864
+ };
865
+
791
866
  // src/stacks-api/transactions/schemas.ts
792
867
  import * as v14 from "valibot";
793
868
  var baseTransactionSchema = v14.object({
@@ -991,20 +1066,89 @@ async function getTransaction(args) {
991
1066
  return success(validationResult.output);
992
1067
  }
993
1068
 
1069
+ // src/stacks-api/transactions/index.ts
1070
+ var transactions = {
1071
+ addressTransactions,
1072
+ getTransaction
1073
+ };
1074
+
994
1075
  // src/stacks-api/index.ts
995
- var accounts = { balances, latestNonce };
996
- var blocks = { getBlock };
997
- var info = { coreApi, poxDetails };
998
- var proofOfTransfer = {
999
- cycle,
1000
- cycles,
1001
- signerInCycle,
1002
- signersInCycle,
1003
- stackersForSignerInCycle
1076
+ var stacksApi = {
1077
+ accounts,
1078
+ blocks,
1079
+ faucets,
1080
+ info,
1081
+ proofOfTransfer,
1082
+ smartContracts,
1083
+ stackingPool,
1084
+ transactions
1085
+ };
1086
+
1087
+ // src/stacks-rpc-api/smart-contracts/map-entry.ts
1088
+ import * as v17 from "valibot";
1089
+ var mapEntryResponseSchema = v17.object({
1090
+ /**
1091
+ * Hex-encoded string of clarity value. It is always an optional tuple.
1092
+ */
1093
+ data: v17.string(),
1094
+ /**
1095
+ * Hex-encoded string of the MARF proof for the data
1096
+ */
1097
+ proof: v17.optional(v17.string())
1098
+ });
1099
+ async function mapEntry(args) {
1100
+ const search = new URLSearchParams();
1101
+ if (args.proof === 0) search.append("proof", "0");
1102
+ if (args.tip) search.append("tip", args.tip);
1103
+ const init = {};
1104
+ if (args.apiKeyConfig) {
1105
+ init.headers = {
1106
+ [args.apiKeyConfig.header]: args.apiKeyConfig.key
1107
+ };
1108
+ }
1109
+ init.method = "POST";
1110
+ init.body = args.mapKey;
1111
+ const endpoint = `${args.baseUrl}/v2/map_entry/${args.contractAddress}/${args.contractName}/${args.mapName}?${search}`;
1112
+ const res = await fetch(endpoint, init);
1113
+ if (!res.ok) {
1114
+ return error({
1115
+ name: "FetchMapEntryError",
1116
+ message: "Failed to fetch map entry.",
1117
+ data: {
1118
+ status: res.status,
1119
+ statusText: res.statusText,
1120
+ bodyParseResult: await safePromise(res.json())
1121
+ }
1122
+ });
1123
+ }
1124
+ const [jsonError, data] = await safePromise(res.json());
1125
+ if (jsonError) {
1126
+ return error({
1127
+ name: "ParseBodyError",
1128
+ message: "Failed to parse response body as JSON.",
1129
+ data: jsonError
1130
+ });
1131
+ }
1132
+ const validationResult = v17.safeParse(mapEntryResponseSchema, data);
1133
+ if (!validationResult.success) {
1134
+ return error({
1135
+ name: "ValidateDataError",
1136
+ message: "Failed to validate response data.",
1137
+ data: validationResult
1138
+ });
1139
+ }
1140
+ return success(validationResult.output);
1141
+ }
1142
+
1143
+ // src/stacks-rpc-api/smart-contracts/index.ts
1144
+ var smartContracts2 = {
1145
+ mapEntry
1146
+ };
1147
+
1148
+ // src/stacks-rpc-api/index.ts
1149
+ var stacksRpcApi = {
1150
+ smartContracts: smartContracts2
1004
1151
  };
1005
- var smartContracts = { readOnly };
1006
- var stackingPool = { members };
1007
- var transactions = { addressTransactions, getTransaction };
1008
1152
 
1009
1153
  // src/utils/call-rate-limited-api.ts
1010
1154
  import { backOff } from "exponential-backoff";
@@ -1042,22 +1186,73 @@ async function safeCallRateLimitedApi(fn, options) {
1042
1186
  }
1043
1187
  }
1044
1188
 
1045
- // src/index.ts
1046
- var stacksApi = {
1047
- accounts,
1048
- blocks,
1049
- info,
1050
- proofOfTransfer,
1051
- smartContracts,
1052
- stackingPool,
1053
- transactions
1189
+ // src/queries/get-signer-stacked-amount.ts
1190
+ async function getSignerStackedAmount(args) {
1191
+ let totalLocked = 0n;
1192
+ const { identifier, ...rest } = args;
1193
+ let hasMore = true;
1194
+ let offset = 0;
1195
+ let found = false;
1196
+ const limit = 200;
1197
+ while (hasMore && !found) {
1198
+ const [error2, data] = await safeCallRateLimitedApi(
1199
+ () => signersInCycle({
1200
+ ...rest,
1201
+ limit
1202
+ })
1203
+ );
1204
+ if (error2) {
1205
+ return error({
1206
+ name: "GetSignerTotalLockedError",
1207
+ message: "Failed to get signer total locked.",
1208
+ data: {
1209
+ error: error2
1210
+ }
1211
+ });
1212
+ }
1213
+ for (const signer of data.results) {
1214
+ if (identifier.type === "address") {
1215
+ if (signer.signer_address === identifier.signerAddress) {
1216
+ totalLocked = BigInt(signer.stacked_amount);
1217
+ found = true;
1218
+ break;
1219
+ }
1220
+ } else {
1221
+ if (signer.signing_key === identifier.signerPublicKey) {
1222
+ totalLocked = BigInt(signer.stacked_amount);
1223
+ found = true;
1224
+ break;
1225
+ }
1226
+ }
1227
+ }
1228
+ offset += data.results.length;
1229
+ hasMore = offset < data.total;
1230
+ }
1231
+ if (!found) {
1232
+ return error({
1233
+ name: "SignerNotFound",
1234
+ message: "Signer not found.",
1235
+ data: {
1236
+ identifier,
1237
+ cycle: args.cycleNumber
1238
+ }
1239
+ });
1240
+ }
1241
+ return success(totalLocked);
1242
+ }
1243
+
1244
+ // src/queries/index.ts
1245
+ var queries = {
1246
+ getSignerStackedAmount
1054
1247
  };
1055
1248
  export {
1056
1249
  callRateLimitedApi,
1057
1250
  error,
1251
+ queries,
1058
1252
  safeCall,
1059
1253
  safeCallRateLimitedApi,
1060
1254
  safePromise,
1061
1255
  stacksApi,
1256
+ stacksRpcApi,
1062
1257
  success
1063
1258
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@secretkeylabs/stacks-tools",
3
- "version": "0.4.0-98de979",
3
+ "version": "0.4.0-da5eb9d",
4
4
  "type": "module",
5
5
  "files": [
6
6
  "dist"