@secondlayer/stacks 2.11.0 → 2.13.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.
Files changed (75) hide show
  1. package/README.md +47 -0
  2. package/dist/shared/{stacks-v150hffx.js → stacks-01pbr7gf.js} +13 -12
  3. package/dist/shared/stacks-01pbr7gf.js.map +11 -0
  4. package/dist/shared/stacks-0cdm20eh.js +225 -0
  5. package/dist/shared/stacks-0cdm20eh.js.map +11 -0
  6. package/dist/shared/{stacks-hxm7r5ay.cjs → stacks-44y18a8e.cjs} +3 -10
  7. package/dist/shared/stacks-44y18a8e.cjs.map +10 -0
  8. package/dist/shared/{stacks-7nvbv61d.js → stacks-59xy6t1p.js} +27 -25
  9. package/dist/shared/{stacks-7nvbv61d.js.map → stacks-59xy6t1p.js.map} +6 -6
  10. package/dist/shared/{stacks-63xbc6cj.js → stacks-6n2m9hyk.js} +3 -3
  11. package/dist/shared/stacks-bnbdqznq.cjs +211 -0
  12. package/dist/shared/stacks-bnbdqznq.cjs.map +11 -0
  13. package/dist/shared/stacks-esn3wcw5.cjs +416 -0
  14. package/dist/shared/stacks-esn3wcw5.cjs.map +18 -0
  15. package/dist/shared/{stacks-3kmj5yre.js → stacks-g67f4w1d.js} +5 -12
  16. package/dist/shared/stacks-g67f4w1d.js.map +10 -0
  17. package/dist/shared/stacks-kjg7fw4y.js +434 -0
  18. package/dist/shared/stacks-kjg7fw4y.js.map +18 -0
  19. package/dist/shared/{stacks-maamjqma.cjs → stacks-tbv5p6pn.cjs} +7 -9
  20. package/dist/shared/stacks-tbv5p6pn.cjs.map +11 -0
  21. package/dist/shared/{stacks-k5an9zcw.cjs → stacks-y5epf0sb.cjs} +19 -22
  22. package/dist/shared/{stacks-k5an9zcw.cjs.map → stacks-y5epf0sb.cjs.map} +6 -6
  23. package/dist/src/accounts/index.cjs +28 -1
  24. package/dist/src/accounts/index.cjs.map +4 -3
  25. package/dist/src/accounts/index.d.cts +23 -1
  26. package/dist/src/accounts/index.d.ts +23 -1
  27. package/dist/src/accounts/index.js +33 -1
  28. package/dist/src/accounts/index.js.map +4 -3
  29. package/dist/src/actions/index.cjs +8 -1
  30. package/dist/src/actions/index.cjs.map +1 -1
  31. package/dist/src/actions/index.d.cts +140 -6
  32. package/dist/src/actions/index.d.ts +140 -6
  33. package/dist/src/actions/index.js +22 -8
  34. package/dist/src/actions/index.js.map +1 -1
  35. package/dist/src/bitcoin/index.cjs +4 -175
  36. package/dist/src/bitcoin/index.cjs.map +4 -6
  37. package/dist/src/bitcoin/index.d.cts +18 -2
  38. package/dist/src/bitcoin/index.d.ts +18 -2
  39. package/dist/src/bitcoin/index.js +18 -181
  40. package/dist/src/bitcoin/index.js.map +4 -6
  41. package/dist/src/bns/index.js +3 -3
  42. package/dist/src/index.cjs +10 -1
  43. package/dist/src/index.cjs.map +3 -3
  44. package/dist/src/index.d.cts +167 -9
  45. package/dist/src/index.d.ts +167 -9
  46. package/dist/src/index.js +23 -7
  47. package/dist/src/index.js.map +3 -3
  48. package/dist/src/pox/index.js +3 -3
  49. package/dist/src/sbtc/index.cjs +49 -7
  50. package/dist/src/sbtc/index.cjs.map +6 -6
  51. package/dist/src/sbtc/index.d.cts +8 -2
  52. package/dist/src/sbtc/index.d.ts +8 -2
  53. package/dist/src/sbtc/index.js +56 -10
  54. package/dist/src/sbtc/index.js.map +6 -6
  55. package/dist/src/stackingdao/index.js +3 -3
  56. package/dist/src/tools/index.cjs +194 -13
  57. package/dist/src/tools/index.cjs.map +12 -9
  58. package/dist/src/tools/index.js +194 -13
  59. package/dist/src/tools/index.js.map +12 -9
  60. package/dist/src/x402/index.cjs +5 -2
  61. package/dist/src/x402/index.cjs.map +3 -3
  62. package/dist/src/x402/index.d.cts +36 -0
  63. package/dist/src/x402/index.d.ts +36 -0
  64. package/dist/src/x402/index.js +7 -4
  65. package/dist/src/x402/index.js.map +3 -3
  66. package/package.json +2 -4
  67. package/dist/shared/stacks-3kmj5yre.js.map +0 -10
  68. package/dist/shared/stacks-502j0efj.js +0 -210
  69. package/dist/shared/stacks-502j0efj.js.map +0 -15
  70. package/dist/shared/stacks-hxm7r5ay.cjs.map +0 -10
  71. package/dist/shared/stacks-maamjqma.cjs.map +0 -11
  72. package/dist/shared/stacks-v150hffx.js.map +0 -11
  73. package/dist/shared/stacks-xmsms921.cjs +0 -196
  74. package/dist/shared/stacks-xmsms921.cjs.map +0 -15
  75. /package/dist/shared/{stacks-63xbc6cj.js.map → stacks-6n2m9hyk.js.map} +0 -0
@@ -1439,10 +1439,39 @@ class BroadcastError extends BaseError {
1439
1439
  name = "BroadcastError";
1440
1440
  txid;
1441
1441
  reason;
1442
+ reasonData;
1442
1443
  constructor(message, options) {
1443
1444
  super(message, options);
1444
1445
  this.txid = options?.txid;
1445
1446
  this.reason = options?.reason;
1447
+ this.reasonData = options?.reasonData;
1448
+ }
1449
+ }
1450
+
1451
+ class TransactionAbortedError extends BaseError {
1452
+ name = "TransactionAbortedError";
1453
+ receipt;
1454
+ constructor(message, options) {
1455
+ super(message, options);
1456
+ this.receipt = options.receipt;
1457
+ }
1458
+ }
1459
+
1460
+ class TransactionDroppedError extends BaseError {
1461
+ name = "TransactionDroppedError";
1462
+ txid;
1463
+ constructor(message, options) {
1464
+ super(message, options);
1465
+ this.txid = options.txid;
1466
+ }
1467
+ }
1468
+
1469
+ class WaitForTransactionTimeoutError extends BaseError {
1470
+ name = "WaitForTransactionTimeoutError";
1471
+ txid;
1472
+ constructor(message, options) {
1473
+ super(message, options);
1474
+ this.txid = options.txid;
1446
1475
  }
1447
1476
  }
1448
1477
 
@@ -1458,6 +1487,107 @@ async function getNonce(client, params) {
1458
1487
  return BigInt(nonce);
1459
1488
  }
1460
1489
 
1490
+ // src/actions/public/txSources.ts
1491
+ function normalizeTxid(txid2) {
1492
+ return txid2.startsWith("0x") ? txid2 : `0x${txid2}`;
1493
+ }
1494
+ function decodeResultHex(hex) {
1495
+ if (!hex)
1496
+ return {};
1497
+ try {
1498
+ return { result: deserializeCVBytes(hex), resultHex: hex };
1499
+ } catch {
1500
+ return { resultHex: hex };
1501
+ }
1502
+ }
1503
+ function normalizeStatus(txStatus) {
1504
+ switch (txStatus) {
1505
+ case "pending":
1506
+ case "success":
1507
+ case "abort_by_response":
1508
+ case "abort_by_post_condition":
1509
+ return txStatus;
1510
+ default:
1511
+ return txStatus.startsWith("dropped") ? "dropped" : undefined;
1512
+ }
1513
+ }
1514
+ function extendedApiSource() {
1515
+ return {
1516
+ async get({ client, txid: txid2 }) {
1517
+ const data = await client.request(`/extended/v1/tx/${normalizeTxid(txid2)}`, { method: "GET" });
1518
+ const txStatus = typeof data?.tx_status === "string" ? data.tx_status : undefined;
1519
+ if (!txStatus)
1520
+ return { receipt: null };
1521
+ const status = normalizeStatus(txStatus);
1522
+ if (!status)
1523
+ return { receipt: null };
1524
+ return {
1525
+ receipt: {
1526
+ txid: normalizeTxid(txid2),
1527
+ status,
1528
+ blockHeight: typeof data.block_height === "number" && data.block_height > 0 ? data.block_height : undefined,
1529
+ blockHash: data.block_hash ?? undefined,
1530
+ ...decodeResultHex(data.tx_result?.hex),
1531
+ events: Array.isArray(data.events) ? data.events : [],
1532
+ raw: data
1533
+ }
1534
+ };
1535
+ }
1536
+ };
1537
+ }
1538
+
1539
+ // src/actions/public/waitForTransactionReceipt.ts
1540
+ var sleep = (ms) => new Promise((r) => setTimeout(r, ms));
1541
+ async function waitForTransactionReceipt(client, params) {
1542
+ const {
1543
+ txid: txid2,
1544
+ confirmations = 1,
1545
+ timeout = 180000,
1546
+ pollingInterval = 3000,
1547
+ droppedGracePeriod = 30000
1548
+ } = params;
1549
+ const source = params.source ?? extendedApiSource();
1550
+ const startedAt = Date.now();
1551
+ let unknownSince = null;
1552
+ let everPending = false;
1553
+ while (true) {
1554
+ const snapshot = await source.get({ client, txid: txid2 });
1555
+ const receipt = snapshot.receipt;
1556
+ if (receipt === null) {
1557
+ unknownSince ??= Date.now();
1558
+ if (Date.now() - unknownSince >= droppedGracePeriod) {
1559
+ throw new TransactionDroppedError(everPending ? `Transaction ${txid2} left the mempool without being mined` : `Transaction ${txid2} was never observed by the status source`, { txid: txid2 });
1560
+ }
1561
+ } else {
1562
+ unknownSince = null;
1563
+ switch (receipt.status) {
1564
+ case "dropped":
1565
+ throw new TransactionDroppedError(`Transaction ${txid2} was dropped from the mempool`, { txid: txid2 });
1566
+ case "abort_by_response":
1567
+ case "abort_by_post_condition":
1568
+ throw new TransactionAbortedError(`Transaction ${txid2} aborted (${receipt.status})`, { receipt });
1569
+ case "pending":
1570
+ everPending = true;
1571
+ break;
1572
+ case "success": {
1573
+ if (confirmations <= 1)
1574
+ return receipt;
1575
+ if (receipt.blockHeight !== undefined) {
1576
+ const tip = snapshot.tip ?? await getBlockHeight(client);
1577
+ if (tip - receipt.blockHeight + 1 >= confirmations)
1578
+ return receipt;
1579
+ }
1580
+ break;
1581
+ }
1582
+ }
1583
+ }
1584
+ if (Date.now() - startedAt >= timeout) {
1585
+ throw new WaitForTransactionTimeoutError(`Timed out after ${timeout}ms waiting for transaction ${txid2}`, { txid: txid2 });
1586
+ }
1587
+ await sleep(pollingInterval);
1588
+ }
1589
+ }
1590
+
1461
1591
  // src/actions/wallet/sendTransaction.ts
1462
1592
  async function sendTransaction(client, params) {
1463
1593
  const hex = bytesToHex(serializeTransaction(params.transaction));
@@ -1471,10 +1601,20 @@ async function sendTransaction(client, params) {
1471
1601
  });
1472
1602
  if (data.error) {
1473
1603
  throw new BroadcastError(data.reason ?? data.error, {
1474
- reason: data.reason
1604
+ reason: data.reason,
1605
+ reasonData: data.reason_data,
1606
+ txid: data.txid ?? getTransactionId(params.transaction)
1475
1607
  });
1476
1608
  }
1477
1609
  const txid2 = typeof data === "string" ? data.replace(/"/g, "") : data.txid ?? getTransactionId(params.transaction);
1610
+ if (params.wait) {
1611
+ const confirmations = params.wait === true ? 1 : params.wait;
1612
+ const receipt = await waitForTransactionReceipt(client, {
1613
+ txid: txid2,
1614
+ confirmations
1615
+ });
1616
+ return { txid: txid2, receipt };
1617
+ }
1478
1618
  return { txid: txid2 };
1479
1619
  }
1480
1620
 
@@ -1487,8 +1627,12 @@ async function resolveNonce(client, address) {
1487
1627
  function isNonceConflictError(error) {
1488
1628
  if (!(error instanceof BroadcastError))
1489
1629
  return false;
1490
- const haystack = `${error.reason ?? ""} ${error.message}`.toLowerCase();
1491
- return haystack.includes("nonce");
1630
+ if (error.reason === "ConflictingNonceInMempool" || error.reason === "BadNonce") {
1631
+ return true;
1632
+ }
1633
+ if (error.reason !== undefined)
1634
+ return false;
1635
+ return error.message.toLowerCase().includes("nonce");
1492
1636
  }
1493
1637
  async function broadcastWithNonceReset(client, params) {
1494
1638
  try {
@@ -1508,6 +1652,36 @@ async function broadcastWithNonceReset(client, params) {
1508
1652
  function isProviderAccount(account) {
1509
1653
  return account.type === "provider";
1510
1654
  }
1655
+ var FEE_TIERS = ["min", "low", "mid", "high"];
1656
+ var TIER_INDEX = { low: 0, mid: 1, high: 2 };
1657
+ function isFeeTier(fee) {
1658
+ return typeof fee === "string" && FEE_TIERS.includes(fee);
1659
+ }
1660
+ function minimumFee(transaction) {
1661
+ return BigInt(serializeTransaction(transaction).length);
1662
+ }
1663
+ async function resolveFee(client, transaction, fee) {
1664
+ if (fee !== undefined && !isFeeTier(fee))
1665
+ return intToBigInt(fee);
1666
+ const tier = fee ?? "mid";
1667
+ if (tier === "min")
1668
+ return minimumFee(transaction);
1669
+ try {
1670
+ const estimates = await estimateFee(client, { transaction });
1671
+ const pick = estimates[TIER_INDEX[tier]] ?? estimates[estimates.length - 1];
1672
+ if (pick)
1673
+ return BigInt(pick.fee);
1674
+ } catch {}
1675
+ return minimumFee(transaction);
1676
+ }
1677
+ function setUnsignedFee(transaction, fee) {
1678
+ transaction.auth.spendingCondition.fee = fee;
1679
+ }
1680
+ function assertNoFeeTierForProvider(fee) {
1681
+ if (isFeeTier(fee)) {
1682
+ throw new Error(`Fee tier '${fee}' requires local signing; provider (wallet) accounts set their own fee`);
1683
+ }
1684
+ }
1511
1685
 
1512
1686
  // src/actions/wallet/callContract.ts
1513
1687
  async function callContract(client, params) {
@@ -1516,6 +1690,7 @@ async function callContract(client, params) {
1516
1690
  throw new Error("Account required");
1517
1691
  const [contractAddress, contractName] = parseContractId(params.contract);
1518
1692
  if (isProviderAccount(account)) {
1693
+ assertNoFeeTierForProvider(params.fee);
1519
1694
  const result = await account.provider.request("stx_callContract", {
1520
1695
  contract: params.contract,
1521
1696
  functionName: params.functionName,
@@ -1524,24 +1699,21 @@ async function callContract(client, params) {
1524
1699
  return result.txid;
1525
1700
  }
1526
1701
  const nonce = params.nonce ?? await resolveNonce(client, account.address);
1702
+ const needsFeeResolution = params.fee === undefined || isFeeTier(params.fee);
1527
1703
  const unsigned = buildContractCall({
1528
1704
  contractAddress,
1529
1705
  contractName,
1530
1706
  functionName: params.functionName,
1531
1707
  functionArgs: params.functionArgs ?? [],
1532
- fee: params.fee ?? 0n,
1708
+ fee: needsFeeResolution ? 0n : params.fee,
1533
1709
  nonce,
1534
1710
  publicKey: account.publicKey,
1535
1711
  chain: client.chain,
1536
1712
  postConditionMode: params.postConditionMode,
1537
1713
  postConditions: params.postConditions
1538
1714
  });
1539
- if (params.fee === undefined) {
1540
- const estimates = await estimateFee(client, { transaction: unsigned });
1541
- const mid = estimates[1] ?? estimates[0];
1542
- if (mid) {
1543
- unsigned.auth.spendingCondition.fee = BigInt(mid.fee);
1544
- }
1715
+ if (needsFeeResolution) {
1716
+ setUnsignedFee(unsigned, await resolveFee(client, unsigned, params.fee));
1545
1717
  }
1546
1718
  const signed = await signTransactionWithAccount(unsigned, account);
1547
1719
  return broadcastWithNonceReset(client, {
@@ -2361,6 +2533,13 @@ async function getContractAbi(client, params) {
2361
2533
  });
2362
2534
  }
2363
2535
 
2536
+ // src/actions/public/getTransaction.ts
2537
+ async function getTransaction(client, params) {
2538
+ const source = params.source ?? extendedApiSource();
2539
+ const { receipt } = await source.get({ client, txid: params.txid });
2540
+ return receipt;
2541
+ }
2542
+
2364
2543
  // src/actions/public/multicall.ts
2365
2544
  async function multicall(client, params) {
2366
2545
  const { calls, allowFailure = true } = params;
@@ -2511,6 +2690,8 @@ function publicActions(client) {
2511
2690
  multicall: (params) => multicall(client, params),
2512
2691
  simulateCall: (params) => simulateCall(client, params),
2513
2692
  simulateTransaction: (params) => simulateTransaction(client, params),
2693
+ getTransaction: (params) => getTransaction(client, params),
2694
+ waitForTransactionReceipt: (params) => waitForTransactionReceipt(client, params),
2514
2695
  watchBlocks: (params) => watchBlocks(client, params),
2515
2696
  watchMempool: (params) => watchMempool(client, params),
2516
2697
  watchTransaction: (params) => watchTransaction(client, params),
@@ -2819,7 +3000,7 @@ var bnsReverse = tool({
2819
3000
  inputSchema: z.object({ principal: PRINCIPAL }),
2820
3001
  execute: ({ principal }) => bnsReverseImpl(getDefaultPublicClient(), principal)
2821
3002
  });
2822
- var getTransaction = tool({
3003
+ var getTransaction2 = tool({
2823
3004
  description: "Fetch a confirmed Stacks transaction by txId (Hiro extended API).",
2824
3005
  inputSchema: z.object({ txId: z.string() }),
2825
3006
  execute: ({ txId }) => transactionByTxId(getDefaultPublicClient(), txId)
@@ -2847,7 +3028,7 @@ var getNftHoldings = tool({
2847
3028
  });
2848
3029
  export {
2849
3030
  readContract2 as readContract,
2850
- getTransaction,
3031
+ getTransaction2 as getTransaction,
2851
3032
  getStxBalance,
2852
3033
  getNftHoldings,
2853
3034
  getMempoolStats,
@@ -2861,5 +3042,5 @@ export {
2861
3042
  bnsResolve
2862
3043
  };
2863
3044
 
2864
- //# debugId=4D62EAB4ED52294064756E2164756E21
3045
+ //# debugId=B4C309A1C0AA64D664756E2164756E21
2865
3046
  //# sourceMappingURL=index.js.map