@gauntlet-xyz/sdk 3.0.1 → 3.1.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.
Files changed (117) hide show
  1. package/README.md +34 -3
  2. package/dist/api/activity.d.ts +3 -1
  3. package/dist/api/activity.d.ts.map +1 -1
  4. package/dist/api/activityRows.d.ts +11 -0
  5. package/dist/api/activityRows.d.ts.map +1 -0
  6. package/dist/api/caip.d.ts.map +1 -1
  7. package/dist/api/client.d.ts.map +1 -1
  8. package/dist/api/decimal.d.ts.map +1 -1
  9. package/dist/api/http.d.ts.map +1 -1
  10. package/dist/api/index.js +7 -8
  11. package/dist/api/positionHistory.d.ts.map +1 -1
  12. package/dist/attribution/index.d.ts +5 -2
  13. package/dist/attribution/index.d.ts.map +1 -1
  14. package/dist/{chunk-HC2BS7MK.js → chunk-JJOLVL5V.js} +113 -6
  15. package/dist/chunk-JJOLVL5V.js.map +7 -0
  16. package/dist/{chunk-S2TTA45N.js → chunk-JRSNLXFY.js} +2 -2
  17. package/dist/{chunk-SU6JAKIA.js → chunk-MEKISE2T.js} +131 -47
  18. package/dist/chunk-MEKISE2T.js.map +7 -0
  19. package/dist/{chunk-7TA2OJUK.js → chunk-YQXTLIJA.js} +209 -96
  20. package/dist/chunk-YQXTLIJA.js.map +7 -0
  21. package/dist/cjs/api/activity.d.ts +3 -1
  22. package/dist/cjs/api/activity.d.ts.map +1 -1
  23. package/dist/cjs/api/activity.js +14 -12
  24. package/dist/cjs/api/activity.js.map +2 -2
  25. package/dist/cjs/api/activityRows.d.ts +11 -0
  26. package/dist/cjs/api/activityRows.d.ts.map +1 -0
  27. package/dist/cjs/api/activityRows.js +139 -0
  28. package/dist/cjs/api/activityRows.js.map +7 -0
  29. package/dist/cjs/api/caip.d.ts.map +1 -1
  30. package/dist/cjs/api/caip.js +3 -1
  31. package/dist/cjs/api/caip.js.map +2 -2
  32. package/dist/cjs/api/client.d.ts.map +1 -1
  33. package/dist/cjs/api/client.js +29 -1
  34. package/dist/cjs/api/client.js.map +2 -2
  35. package/dist/cjs/api/decimal.d.ts.map +1 -1
  36. package/dist/cjs/api/decimal.js +1 -0
  37. package/dist/cjs/api/decimal.js.map +2 -2
  38. package/dist/cjs/api/http.d.ts.map +1 -1
  39. package/dist/cjs/api/http.js +1 -0
  40. package/dist/cjs/api/http.js.map +2 -2
  41. package/dist/cjs/api/positionHistory.d.ts.map +1 -1
  42. package/dist/cjs/api/positionHistory.js +17 -4
  43. package/dist/cjs/api/positionHistory.js.map +2 -2
  44. package/dist/cjs/attribution/index.d.ts +5 -2
  45. package/dist/cjs/attribution/index.d.ts.map +1 -1
  46. package/dist/cjs/attribution/index.js +3 -1
  47. package/dist/cjs/attribution/index.js.map +2 -2
  48. package/dist/cjs/evm/adapters/aera.d.ts.map +1 -1
  49. package/dist/cjs/evm/adapters/aera.js +16 -7
  50. package/dist/cjs/evm/adapters/aera.js.map +2 -2
  51. package/dist/cjs/evm/adapters/types.d.ts +1 -0
  52. package/dist/cjs/evm/adapters/types.d.ts.map +1 -1
  53. package/dist/cjs/evm/adapters/types.js.map +1 -1
  54. package/dist/cjs/evm/aeraContracts.d.ts.map +1 -1
  55. package/dist/cjs/evm/aeraContracts.js +21 -11
  56. package/dist/cjs/evm/aeraContracts.js.map +2 -2
  57. package/dist/cjs/evm/deposit.d.ts +3 -2
  58. package/dist/cjs/evm/deposit.d.ts.map +1 -1
  59. package/dist/cjs/evm/deposit.js +40 -10
  60. package/dist/cjs/evm/deposit.js.map +2 -2
  61. package/dist/cjs/evm/depositQuote.d.ts.map +1 -1
  62. package/dist/cjs/evm/depositQuote.js +1 -0
  63. package/dist/cjs/evm/depositQuote.js.map +2 -2
  64. package/dist/cjs/evm/depositReceiverApproval.d.ts.map +1 -1
  65. package/dist/cjs/evm/depositReceiverApproval.js +14 -9
  66. package/dist/cjs/evm/depositReceiverApproval.js.map +2 -2
  67. package/dist/cjs/evm/userCurrentBalance.d.ts +15 -1
  68. package/dist/cjs/evm/userCurrentBalance.d.ts.map +1 -1
  69. package/dist/cjs/evm/userCurrentBalance.js +94 -51
  70. package/dist/cjs/evm/userCurrentBalance.js.map +2 -2
  71. package/dist/cjs/evm/withdraw.d.ts +2 -0
  72. package/dist/cjs/evm/withdraw.d.ts.map +1 -1
  73. package/dist/cjs/evm/withdraw.js +14 -1
  74. package/dist/cjs/evm/withdraw.js.map +2 -2
  75. package/dist/cjs/evm/withdrawQuote.d.ts.map +1 -1
  76. package/dist/cjs/evm/withdrawQuote.js +1 -0
  77. package/dist/cjs/evm/withdrawQuote.js.map +2 -2
  78. package/dist/evm/adapters/aera.d.ts.map +1 -1
  79. package/dist/evm/adapters/types.d.ts +1 -0
  80. package/dist/evm/adapters/types.d.ts.map +1 -1
  81. package/dist/evm/aeraContracts.d.ts.map +1 -1
  82. package/dist/evm/deposit.d.ts +3 -2
  83. package/dist/evm/deposit.d.ts.map +1 -1
  84. package/dist/evm/depositQuote.d.ts.map +1 -1
  85. package/dist/evm/depositReceiverApproval.d.ts.map +1 -1
  86. package/dist/evm/index.js +6 -8
  87. package/dist/evm/userCurrentBalance.d.ts +15 -1
  88. package/dist/evm/userCurrentBalance.d.ts.map +1 -1
  89. package/dist/evm/withdraw.d.ts +2 -0
  90. package/dist/evm/withdraw.d.ts.map +1 -1
  91. package/dist/evm/withdrawQuote.d.ts.map +1 -1
  92. package/dist/index.js +11 -13
  93. package/dist/privy/index.js +2 -2
  94. package/package.json +1 -1
  95. package/src/api/activity.ts +18 -15
  96. package/src/api/activityRows.ts +143 -0
  97. package/src/api/caip.ts +3 -1
  98. package/src/api/client.ts +36 -1
  99. package/src/api/decimal.ts +1 -0
  100. package/src/api/http.ts +1 -0
  101. package/src/api/positionHistory.ts +20 -5
  102. package/src/attribution/index.ts +6 -2
  103. package/src/evm/adapters/aera.ts +26 -8
  104. package/src/evm/adapters/types.ts +1 -0
  105. package/src/evm/aeraContracts.ts +29 -14
  106. package/src/evm/deposit.ts +43 -12
  107. package/src/evm/depositQuote.ts +1 -0
  108. package/src/evm/depositReceiverApproval.ts +14 -9
  109. package/src/evm/userCurrentBalance.ts +144 -60
  110. package/src/evm/withdraw.ts +20 -1
  111. package/src/evm/withdrawQuote.ts +1 -0
  112. package/dist/chunk-7TA2OJUK.js.map +0 -7
  113. package/dist/chunk-HC2BS7MK.js.map +0 -7
  114. package/dist/chunk-STIV2CC4.js +0 -57
  115. package/dist/chunk-STIV2CC4.js.map +0 -7
  116. package/dist/chunk-SU6JAKIA.js.map +0 -7
  117. /package/dist/{chunk-S2TTA45N.js.map → chunk-JRSNLXFY.js.map} +0 -0
@@ -1,6 +1,3 @@
1
- import {
2
- resolveVault
3
- } from "./chunk-STIV2CC4.js";
4
1
  import {
5
2
  AccountMismatchError,
6
3
  AccountRequiredError,
@@ -13,6 +10,7 @@ import {
13
10
  DEFAULT_MAX_PRICE_AGE,
14
11
  DEFAULT_REQUEST_DEADLINE_BUFFER,
15
12
  DEFAULT_SOLVER_TIP,
13
+ GauntletSDKError,
16
14
  InvalidSlippageBPSError,
17
15
  InvalidSolverTipError,
18
16
  InvalidSyncDepositBoundError,
@@ -26,8 +24,9 @@ import {
26
24
  UnsupportedFeatureError,
27
25
  UnsupportedProtocolError,
28
26
  VaultNotFoundError,
29
- requireNonZeroSyncWithdrawBound
30
- } from "./chunk-HC2BS7MK.js";
27
+ requireNonZeroSyncWithdrawBound,
28
+ resolveVault
29
+ } from "./chunk-JJOLVL5V.js";
31
30
 
32
31
  // src/evm/types.ts
33
32
  var ContractVersion = {
@@ -1507,7 +1506,6 @@ function readAtBlock(options = {}) {
1507
1506
  }
1508
1507
 
1509
1508
  // src/evm/aeraContracts.ts
1510
- var contractVersionCache = /* @__PURE__ */ new Map();
1511
1509
  var contractVersionAbi = [
1512
1510
  {
1513
1511
  type: "function",
@@ -1543,9 +1541,6 @@ function applySlippageDown(amount, slippageBps) {
1543
1541
  function applySlippageUp(amount, slippageBps) {
1544
1542
  return (amount * (MAX_BPS + BigInt(slippageBps)) + MAX_BPS - 1n) / MAX_BPS;
1545
1543
  }
1546
- function contractVersionCacheKey(client, address) {
1547
- return `${client.chain?.id ?? "unknown"}:${address.toLowerCase()}`;
1548
- }
1549
1544
  function isVersionUnavailableError(error) {
1550
1545
  if (!(error instanceof ContractFunctionExecutionError) || error.functionName !== "version") {
1551
1546
  return false;
@@ -1563,10 +1558,22 @@ function getMultiDepositorVault(client, vaultAddress) {
1563
1558
  client
1564
1559
  });
1565
1560
  }
1561
+ var latestContractVersions = /* @__PURE__ */ new WeakMap();
1566
1562
  async function resolveContractVersion(client, address, options = {}) {
1567
- const cacheKey = contractVersionCacheKey(client, address);
1568
- const cached = contractVersionCache.get(cacheKey);
1569
- if (cached) return cached;
1563
+ if (options.blockNumber !== void 0) return readContractVersion(client, address, options);
1564
+ let versions = latestContractVersions.get(client);
1565
+ if (versions === void 0) {
1566
+ versions = /* @__PURE__ */ new Map();
1567
+ latestContractVersions.set(client, versions);
1568
+ }
1569
+ const key = address.toLowerCase();
1570
+ const cached = versions.get(key);
1571
+ if (cached !== void 0) return cached;
1572
+ const version = await readContractVersion(client, address, options);
1573
+ versions.set(key, version);
1574
+ return version;
1575
+ }
1576
+ async function readContractVersion(client, address, options) {
1570
1577
  try {
1571
1578
  const versionString = await client.readContract({
1572
1579
  address,
@@ -1574,12 +1581,14 @@ async function resolveContractVersion(client, address, options = {}) {
1574
1581
  functionName: "version",
1575
1582
  ...readAtBlock(options)
1576
1583
  });
1577
- const version = versionString.startsWith("2.") ? ContractVersion.V2 : ContractVersion.V1;
1578
- contractVersionCache.set(cacheKey, version);
1579
- return version;
1584
+ if (!/^[12]\./.test(versionString)) {
1585
+ throw new UnsupportedFeatureError(`Aera: unsupported contract version ${versionString}`);
1586
+ }
1587
+ return versionString.startsWith("2.") ? ContractVersion.V2 : ContractVersion.V1;
1580
1588
  } catch (error) {
1581
1589
  if (!isVersionUnavailableError(error)) throw error;
1582
- contractVersionCache.set(cacheKey, ContractVersion.V1);
1590
+ const code = await client.getCode({ address, ...readAtBlock(options) });
1591
+ if (code === void 0 || code === "0x") throw error;
1583
1592
  return ContractVersion.V1;
1584
1593
  }
1585
1594
  }
@@ -3262,16 +3271,25 @@ var aeraAdapter = {
3262
3271
  } else {
3263
3272
  throw new InvalidWithdrawParamsError();
3264
3273
  }
3265
- const tokenOutBeforeTip = await getAsyncRedeemTokenOut3(
3266
- publicClient,
3267
- runtime,
3268
- vault.vaultAddress,
3269
- asset.address,
3270
- shares
3274
+ const tokensOut = "amount" in params && params.amount != null ? params.amount : subtractSolverTip(
3275
+ await getAsyncRedeemTokenOut3(
3276
+ publicClient,
3277
+ runtime,
3278
+ vault.vaultAddress,
3279
+ asset.address,
3280
+ shares
3281
+ ),
3282
+ solverTip
3271
3283
  );
3272
- minTokenOut = applySlippageDown(subtractSolverTip(tokenOutBeforeTip, solverTip), slippageBps);
3284
+ minTokenOut = applySlippageDown(tokensOut, slippageBps);
3273
3285
  maxUnitsIn = shares;
3274
3286
  }
3287
+ if (params.maxShares !== void 0) {
3288
+ if (isAsync || typeof params.maxShares !== "bigint" || params.maxShares <= 0n || !isV2ExactTokenWithdraw && shares !== void 0 && shares > params.maxShares) {
3289
+ throw new InvalidWithdrawParamsError();
3290
+ }
3291
+ if (maxUnitsIn > params.maxShares) maxUnitsIn = params.maxShares;
3292
+ }
3275
3293
  const steps = [];
3276
3294
  if (isAsync) {
3277
3295
  if (shares === void 0) {
@@ -3562,7 +3580,7 @@ async function buildAttribution(client) {
3562
3580
  throw new UnimplementedFeatureError("AttributionMode.PRIVATE");
3563
3581
  }
3564
3582
  }
3565
- async function encodeTransactionWithAttribution(client, transaction) {
3583
+ async function encodeTransactionWithAttribution(client, transaction, chainId) {
3566
3584
  const customAttribution = await buildAttribution(client);
3567
3585
  const encodedTx = encodeFunctionData({
3568
3586
  abi: transaction.abi,
@@ -3575,10 +3593,12 @@ async function encodeTransactionWithAttribution(client, transaction) {
3575
3593
  type: transaction.type,
3576
3594
  account: transaction.account,
3577
3595
  to: transaction.address,
3596
+ chainId,
3578
3597
  data: encodedTxWithAttribution
3579
3598
  },
3580
3599
  tx: {
3581
3600
  ...transaction,
3601
+ chainId,
3582
3602
  attribution: customAttribution
3583
3603
  }
3584
3604
  };
@@ -3620,6 +3640,7 @@ function resolveSyncOnlyOperationMode(vaultId, requested) {
3620
3640
 
3621
3641
  // src/evm/deposit.ts
3622
3642
  async function getDepositTx(client, params) {
3643
+ params = structuredClone(params);
3623
3644
  const slippageBps = params.slippageBps ?? DEFAULT_BPS;
3624
3645
  if (!Number.isInteger(slippageBps) || slippageBps < 0 || slippageBps > Number(MAX_BPS)) {
3625
3646
  throw new InvalidSlippageBPSError(slippageBps);
@@ -3661,7 +3682,7 @@ async function getDepositTx(client, params) {
3661
3682
  modifiedDepositMode = resolveSyncOnlyOperationMode(params.vaultId, requestedDepositMode);
3662
3683
  }
3663
3684
  const spender = protocol === "aera" && modifiedDepositMode === "async" ? aeraRuntime.provisioner.address : vault.vaultAddress;
3664
- const { sufficient } = await adapter.checkAllowance({
3685
+ const { sufficient, current } = await adapter.checkAllowance({
3665
3686
  publicClient,
3666
3687
  token: token.address,
3667
3688
  owner: account,
@@ -3670,14 +3691,43 @@ async function getDepositTx(client, params) {
3670
3691
  });
3671
3692
  const steps = [];
3672
3693
  if (!sufficient) {
3673
- steps.push(
3674
- adapter.buildApproval({
3675
- token: token.address,
3676
- spender,
3677
- amount: params.amount,
3678
- account
3679
- })
3680
- );
3694
+ const approval = adapter.buildApproval({
3695
+ token: token.address,
3696
+ spender,
3697
+ amount: params.amount,
3698
+ account
3699
+ });
3700
+ if (current > 0n && params.amount > 0n) {
3701
+ try {
3702
+ await publicClient.estimateContractGas({
3703
+ address: approval.address,
3704
+ abi: approval.abi,
3705
+ functionName: "approve",
3706
+ args: approval.args,
3707
+ account
3708
+ });
3709
+ } catch (approvalError) {
3710
+ const reset = adapter.buildApproval({
3711
+ token: token.address,
3712
+ spender,
3713
+ amount: 0n,
3714
+ account
3715
+ });
3716
+ try {
3717
+ await publicClient.estimateContractGas({
3718
+ address: reset.address,
3719
+ abi: reset.abi,
3720
+ functionName: "approve",
3721
+ args: reset.args,
3722
+ account
3723
+ });
3724
+ } catch {
3725
+ throw approvalError;
3726
+ }
3727
+ steps.push(reset);
3728
+ }
3729
+ }
3730
+ steps.push(approval);
3681
3731
  }
3682
3732
  const depositSteps = await adapter.buildDeposit({
3683
3733
  vault,
@@ -3694,11 +3744,12 @@ async function getDepositTx(client, params) {
3694
3744
  minUnitsOut: params.minUnitsOut
3695
3745
  });
3696
3746
  steps.push(...depositSteps);
3697
- return Promise.all(steps.map((step) => encodeTransactionWithAttribution(client, step)));
3747
+ return Promise.all(steps.map((step) => encodeTransactionWithAttribution(client, step, chainId)));
3698
3748
  }
3699
3749
 
3700
3750
  // src/evm/depositReceiverApproval.ts
3701
3751
  async function getDepositReceiverApprovalTx(client, params) {
3752
+ params = { ...params };
3702
3753
  const resolved = await resolveVault(client, params.vaultId, params.chainId);
3703
3754
  if (!resolved) throw new VaultNotFoundError(params.vaultId);
3704
3755
  const chainId = params.chainId ?? resolved.vault.chainId;
@@ -3710,15 +3761,19 @@ async function getDepositReceiverApprovalTx(client, params) {
3710
3761
  if (runtime.provisioner.version !== ContractVersion.V2) {
3711
3762
  throw new UnsupportedFeatureError("Aera: deposit receiver approval on V1");
3712
3763
  }
3713
- return encodeTransactionWithAttribution(client, {
3714
- type: "setDepositReceiverApproval",
3715
- ...setDepositReceiverApprovalTxRequest(
3716
- runtime.provisioner.address,
3717
- params.depositor,
3718
- params.approved ?? true,
3719
- receiver
3720
- )
3721
- });
3764
+ return encodeTransactionWithAttribution(
3765
+ client,
3766
+ {
3767
+ type: "setDepositReceiverApproval",
3768
+ ...setDepositReceiverApprovalTxRequest(
3769
+ runtime.provisioner.address,
3770
+ params.depositor,
3771
+ params.approved ?? true,
3772
+ receiver
3773
+ )
3774
+ },
3775
+ chainId
3776
+ );
3722
3777
  }
3723
3778
 
3724
3779
  // src/evm/depositQuote.ts
@@ -3750,6 +3805,7 @@ async function resolveSyncDepositContext(client, params, readKind) {
3750
3805
  return { publicClient, vault, token, runtime };
3751
3806
  }
3752
3807
  async function getSyncDepositQuote2(client, params) {
3808
+ params = structuredClone(params);
3753
3809
  const slippageBps = params.slippageBps ?? DEFAULT_BPS;
3754
3810
  if (!Number.isInteger(slippageBps) || slippageBps < 0 || slippageBps > Number(MAX_BPS)) {
3755
3811
  throw new InvalidSlippageBPSError(slippageBps);
@@ -3784,6 +3840,12 @@ async function getSyncDepositRate(client, params) {
3784
3840
 
3785
3841
  // src/evm/withdraw.ts
3786
3842
  function validateWithdrawParams(params) {
3843
+ if (params.entireAmount !== void 0 && typeof params.entireAmount !== "boolean") {
3844
+ throw new InvalidWithdrawParamsError();
3845
+ }
3846
+ if (params.amount !== void 0 && params.amount < 0n || params.shares !== void 0 && params.shares < 0n) {
3847
+ throw new InvalidWithdrawParamsError();
3848
+ }
3787
3849
  const modes = [
3788
3850
  "amount" in params && params.amount != null,
3789
3851
  "shares" in params && params.shares != null,
@@ -3829,6 +3891,7 @@ function validateSyncWithdrawQuote({
3829
3891
  }
3830
3892
  }
3831
3893
  async function getWithdrawTx(client, params) {
3894
+ params = structuredClone(params);
3832
3895
  validateWithdrawParams(params);
3833
3896
  if (params.slippageBps !== void 0 && (!Number.isInteger(params.slippageBps) || params.slippageBps < 0 || params.slippageBps > Number(MAX_BPS))) {
3834
3897
  throw new InvalidSlippageBPSError(params.slippageBps);
@@ -3844,6 +3907,9 @@ async function getWithdrawTx(client, params) {
3844
3907
  }
3845
3908
  }
3846
3909
  const { vault, protocol } = resolved;
3910
+ if (params.maxShares !== void 0 && protocol !== "aera") {
3911
+ throw new UnsupportedFeatureError("maxShares requires an Aera sync withdrawal");
3912
+ }
3847
3913
  const token = vault.supplyToken.length > 1 ? vault.supplyToken.find((tInfo) => tInfo.symbol === params.assetSymbol) : vault.supplyToken[0];
3848
3914
  if (token === void 0) {
3849
3915
  throw new UnsupportedAssetError(params.assetSymbol ?? "unknown", params.vaultId);
@@ -3901,12 +3967,15 @@ async function getWithdrawTx(client, params) {
3901
3967
  maxPriceAge: params.maxPriceAge,
3902
3968
  aeraRuntime,
3903
3969
  syncWithdrawQuote: params.syncWithdrawQuote,
3970
+ maxShares: params.maxShares,
3904
3971
  ..."shares" in params && params.shares != null ? { shares: params.shares } : {},
3905
3972
  ..."amount" in params && params.amount != null ? { amount: params.amount } : {},
3906
3973
  ..."entireAmount" in params && params.entireAmount ? { entireAmount: true } : {}
3907
3974
  };
3908
3975
  const steps = await adapter.buildWithdraw(withdrawParams);
3909
- return await Promise.all(steps.map((step) => encodeTransactionWithAttribution(client, step)));
3976
+ return await Promise.all(
3977
+ steps.map((step) => encodeTransactionWithAttribution(client, step, chainId))
3978
+ );
3910
3979
  }
3911
3980
 
3912
3981
  // src/evm/withdrawQuote.ts
@@ -3976,6 +4045,7 @@ async function getSyncWithdrawRate(client, params) {
3976
4045
  );
3977
4046
  }
3978
4047
  async function getSyncWithdrawQuote(client, params) {
4048
+ params = structuredClone(params);
3979
4049
  validateSyncWithdrawQuoteParams(params);
3980
4050
  if (params.slippageBps !== void 0 && (!Number.isInteger(params.slippageBps) || params.slippageBps < 0 || params.slippageBps > Number(MAX_BPS))) {
3981
4051
  throw new InvalidSlippageBPSError(params.slippageBps);
@@ -4149,6 +4219,7 @@ async function getAeraTokenModeSupport(client, params) {
4149
4219
 
4150
4220
  // src/evm/userCurrentBalance.ts
4151
4221
  import { parseAbi } from "viem";
4222
+ var LOG_CHUNK_CONCURRENCY = 16;
4152
4223
  var DEPOSIT_REQUESTED_V1_ABI = [
4153
4224
  {
4154
4225
  type: "event",
@@ -4196,71 +4267,105 @@ function dedupeByRequestHash(logs, hashKey) {
4196
4267
  return true;
4197
4268
  });
4198
4269
  }
4199
- async function queryDeploymentBalance(client, deployment, address) {
4270
+ async function requestScanStart(publicClient, chainId, blockNumber, days, cutoff) {
4271
+ let span = (BLOCKS_3_DAYS[chainId] ?? DEFAULT_BLOCKS_3_DAYS) * BigInt(days) / 3n;
4272
+ for (; ; ) {
4273
+ if (span >= blockNumber) return 0n;
4274
+ const start = blockNumber - span;
4275
+ const block = await publicClient.getBlock({ blockNumber: start });
4276
+ if (block.timestamp <= cutoff) return start;
4277
+ span *= 2n;
4278
+ }
4279
+ }
4280
+ async function mapInBatches(items, size, fn) {
4281
+ const results = [];
4282
+ for (let i = 0; i < items.length; i += size) {
4283
+ results.push(...await Promise.all(items.slice(i, i + size).map(fn)));
4284
+ }
4285
+ return results;
4286
+ }
4287
+ async function queryDeploymentBalance(client, deployment, params) {
4200
4288
  const publicClient = client.getPublicClient(deployment.chainId);
4201
- const token = deployment.supplyToken[0];
4289
+ const { address } = params;
4290
+ const token = deployment.supplyToken.length > 1 ? deployment.supplyToken.find((candidate) => candidate.symbol === params.assetSymbol) : deployment.supplyToken[0];
4291
+ if (token === void 0)
4292
+ throw new UnsupportedAssetError(params.assetSymbol ?? "unknown", params.vaultId);
4293
+ const snapshot = await publicClient.getBlock({ blockTag: "latest" });
4294
+ if (snapshot.number === null) {
4295
+ throw new GauntletSDKError("The balance snapshot has no block number");
4296
+ }
4297
+ const blockNumber = snapshot.number;
4298
+ const readOptions = { blockNumber };
4299
+ const expirationDays = deployment.expirationDays ?? 3;
4300
+ if (!Number.isSafeInteger(expirationDays) || expirationDays <= 0) {
4301
+ throw new GauntletSDKError("The request expiration must be a positive integer number of days");
4302
+ }
4303
+ const scanDays = Math.max(3, expirationDays);
4304
+ const cutoff = snapshot.timestamp - BigInt(DAY) * BigInt(scanDays);
4305
+ const fromBlock = params.fromBlock ?? await requestScanStart(publicClient, deployment.chainId, blockNumber, scanDays, cutoff);
4306
+ if (fromBlock < 0n || fromBlock > blockNumber) {
4307
+ throw new GauntletSDKError("The request scan range is invalid");
4308
+ }
4202
4309
  const vaultContract = getMultiDepositorVault(publicClient, deployment.vaultAddress);
4203
4310
  const [runtime, units] = await Promise.all([
4204
- resolveAeraRuntimeContracts(publicClient, deployment),
4205
- vaultContract.read.balanceOf([address])
4311
+ resolveAeraRuntimeContracts(publicClient, deployment, readOptions),
4312
+ vaultContract.read.balanceOf([address], readOptions)
4206
4313
  ]);
4207
4314
  const provisionerAddress = runtime.provisioner.address;
4208
4315
  const isV2 = runtime.provisioner.version === ContractVersion.V2;
4209
- const unitsLockedUntilPromise = isV2 ? getUserUnitsRefundableUntil(publicClient, provisionerAddress, address) : Promise.resolve(void 0);
4316
+ const unitsLockedUntilPromise = isV2 ? getUserUnitsRefundableUntil(publicClient, provisionerAddress, address, readOptions) : Promise.resolve(void 0);
4210
4317
  const balance = await convertUnitsToToken(
4211
4318
  publicClient,
4212
4319
  runtime.feeCalculator.address,
4213
4320
  runtime.feeCalculator.version,
4214
4321
  deployment.vaultAddress,
4215
4322
  token.address,
4216
- units
4323
+ units,
4324
+ readOptions
4217
4325
  );
4218
- const currentBlock = await publicClient.getBlockNumber();
4219
- const lookback = BLOCKS_3_DAYS[deployment.chainId] ?? DEFAULT_BLOCKS_3_DAYS;
4220
- const fromBlock = currentBlock > lookback ? currentBlock - lookback : 0n;
4221
4326
  const chunks = [];
4222
- for (let cursor = fromBlock; cursor <= currentBlock; cursor += LOG_PAGE_SIZE + 1n) {
4223
- const end = cursor + LOG_PAGE_SIZE <= currentBlock ? cursor + LOG_PAGE_SIZE : currentBlock;
4327
+ for (let cursor = fromBlock; cursor <= blockNumber; cursor += LOG_PAGE_SIZE + 1n) {
4328
+ const end = cursor + LOG_PAGE_SIZE <= blockNumber ? cursor + LOG_PAGE_SIZE : blockNumber;
4224
4329
  chunks.push([cursor, end]);
4225
4330
  }
4226
4331
  const [depositPages, redeemPages] = await Promise.all([
4227
- Promise.all(
4228
- chunks.map(
4229
- ([from, to]) => isV2 ? publicClient.getContractEvents({
4230
- address: provisionerAddress,
4231
- abi: provisionerV2Abi,
4232
- eventName: "DepositRequested",
4233
- args: { receiver: address },
4234
- fromBlock: from,
4235
- toBlock: to
4236
- }) : publicClient.getContractEvents({
4237
- address: provisionerAddress,
4238
- abi: DEPOSIT_REQUESTED_V1_ABI,
4239
- eventName: "DepositRequested",
4240
- args: { user: address },
4241
- fromBlock: from,
4242
- toBlock: to
4243
- })
4244
- )
4332
+ mapInBatches(
4333
+ chunks,
4334
+ LOG_CHUNK_CONCURRENCY,
4335
+ ([from, to]) => isV2 ? publicClient.getContractEvents({
4336
+ address: provisionerAddress,
4337
+ abi: provisionerV2Abi,
4338
+ eventName: "DepositRequested",
4339
+ args: { receiver: address, token: token.address },
4340
+ fromBlock: from,
4341
+ toBlock: to
4342
+ }) : publicClient.getContractEvents({
4343
+ address: provisionerAddress,
4344
+ abi: DEPOSIT_REQUESTED_V1_ABI,
4345
+ eventName: "DepositRequested",
4346
+ args: { user: address, token: token.address },
4347
+ fromBlock: from,
4348
+ toBlock: to
4349
+ })
4245
4350
  ),
4246
- Promise.all(
4247
- chunks.map(
4248
- ([from, to]) => isV2 ? publicClient.getContractEvents({
4249
- address: provisionerAddress,
4250
- abi: provisionerV2Abi,
4251
- eventName: "RedeemRequested",
4252
- args: { user: address },
4253
- fromBlock: from,
4254
- toBlock: to
4255
- }) : publicClient.getContractEvents({
4256
- address: provisionerAddress,
4257
- abi: REDEEM_REQUESTED_V1_ABI,
4258
- eventName: "RedeemRequested",
4259
- args: { user: address },
4260
- fromBlock: from,
4261
- toBlock: to
4262
- })
4263
- )
4351
+ mapInBatches(
4352
+ chunks,
4353
+ LOG_CHUNK_CONCURRENCY,
4354
+ ([from, to]) => isV2 ? publicClient.getContractEvents({
4355
+ address: provisionerAddress,
4356
+ abi: provisionerV2Abi,
4357
+ eventName: "RedeemRequested",
4358
+ args: { user: address, token: token.address },
4359
+ fromBlock: from,
4360
+ toBlock: to
4361
+ }) : publicClient.getContractEvents({
4362
+ address: provisionerAddress,
4363
+ abi: REDEEM_REQUESTED_V1_ABI,
4364
+ eventName: "RedeemRequested",
4365
+ args: { user: address, token: token.address },
4366
+ fromBlock: from,
4367
+ toBlock: to
4368
+ })
4264
4369
  )
4265
4370
  ]);
4266
4371
  const depositLogs = dedupeByRequestHash(depositPages.flat(), "depositRequestHash");
@@ -4280,7 +4385,8 @@ async function queryDeploymentBalance(client, deployment, address) {
4280
4385
  args: [log.args.redeemRequestHash]
4281
4386
  }))
4282
4387
  ],
4283
- allowFailure: false
4388
+ allowFailure: false,
4389
+ blockNumber
4284
4390
  });
4285
4391
  const depositChecks = pendingChecks.slice(0, depositLogs.length);
4286
4392
  const redeemChecks = pendingChecks.slice(depositLogs.length);
@@ -4302,10 +4408,13 @@ async function queryDeploymentBalance(client, deployment, address) {
4302
4408
  runtime.feeCalculator.version,
4303
4409
  deployment.vaultAddress,
4304
4410
  token.address,
4305
- pendingWithdrawUnits
4411
+ pendingWithdrawUnits,
4412
+ readOptions
4306
4413
  ) : 0n;
4307
4414
  const unitsLockedUntil = await unitsLockedUntilPromise;
4308
4415
  return {
4416
+ blockNumber,
4417
+ fromBlock,
4309
4418
  chain: CHAIN_NAMES[deployment.chainId] ?? `chain-${deployment.chainId}`,
4310
4419
  chainId: deployment.chainId,
4311
4420
  token: token.address,
@@ -4317,6 +4426,7 @@ async function queryDeploymentBalance(client, deployment, address) {
4317
4426
  };
4318
4427
  }
4319
4428
  async function getUserCurrentBalance(client, params) {
4429
+ params = { ...params };
4320
4430
  const manifest = await client.manifest;
4321
4431
  const vaultInfo = manifest.vaults.find((v) => v.vaultId === params.vaultId);
4322
4432
  if (!vaultInfo) throw new VaultNotFoundError(params.vaultId);
@@ -4334,9 +4444,12 @@ async function getUserCurrentBalance(client, params) {
4334
4444
  evmDeployments.map((d) => `${d.chainId}`).join(", ")
4335
4445
  );
4336
4446
  }
4337
- return [await queryDeploymentBalance(client, deployment, params.address)];
4447
+ return [await queryDeploymentBalance(client, deployment, params)];
4448
+ }
4449
+ if (params.fromBlock !== void 0 && evmDeployments.length > 1) {
4450
+ throw new GauntletSDKError("fromBlock requires chainId for a vault with several deployments");
4338
4451
  }
4339
- return Promise.all(evmDeployments.map((d) => queryDeploymentBalance(client, d, params.address)));
4452
+ return Promise.all(evmDeployments.map((d) => queryDeploymentBalance(client, d, params)));
4340
4453
  }
4341
4454
 
4342
4455
  export {
@@ -4361,4 +4474,4 @@ export {
4361
4474
  getAeraTokenModeSupport,
4362
4475
  getUserCurrentBalance
4363
4476
  };
4364
- //# sourceMappingURL=chunk-7TA2OJUK.js.map
4477
+ //# sourceMappingURL=chunk-YQXTLIJA.js.map