@oydual31/more-vaults-sdk 0.2.3 → 0.2.5

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.
@@ -153,8 +153,46 @@ declare function isAsyncMode(publicClient: PublicClient, vault: Address): Promis
153
153
  declare function getAsyncRequestStatus(publicClient: PublicClient, vault: Address, guid: `0x${string}`): Promise<{
154
154
  fulfilled: boolean;
155
155
  finalized: boolean;
156
+ refunded: boolean;
156
157
  result: bigint;
157
158
  }>;
159
+ interface AsyncRequestFinalResult {
160
+ /** 'completed' = assets/shares received, 'refunded' = tokens returned to user */
161
+ status: 'completed' | 'refunded';
162
+ /** For deposit: shares minted. For redeem: assets returned. 0 if refunded. */
163
+ result: bigint;
164
+ }
165
+ /**
166
+ * Poll an async cross-chain request until it finalizes or times out.
167
+ *
168
+ * This is the correct way to wait for smartDeposit/smartRedeem results on
169
+ * async vaults. Do NOT poll balance — use this instead, which reads the
170
+ * on-chain request state by GUID.
171
+ *
172
+ * @param publicClient Public client on the hub chain
173
+ * @param vault Vault address
174
+ * @param guid GUID from smartDeposit/smartRedeem result
175
+ * @param pollInterval Milliseconds between polls (default: 30_000)
176
+ * @param timeout Max wait time in milliseconds (default: 900_000 = 15 min)
177
+ * @param onPoll Optional callback invoked after each poll with current status
178
+ * @returns Final result with status and amount
179
+ * @throws Error if timeout is reached
180
+ *
181
+ * @example
182
+ * const depositResult = await smartDeposit(walletClient, publicClient, { vault }, amount, receiver)
183
+ * if ('guid' in depositResult) {
184
+ * const final = await waitForAsyncRequest(publicClient, vault, depositResult.guid, 30_000, 900_000, (s) => {
185
+ * console.log(`Status: fulfilled=${s.fulfilled}, finalized=${s.finalized}`)
186
+ * })
187
+ * console.log(`Done: ${final.status}, result: ${final.result}`)
188
+ * }
189
+ */
190
+ declare function waitForAsyncRequest(publicClient: PublicClient, vault: Address, guid: `0x${string}`, pollInterval?: number, timeout?: number, onPoll?: (status: {
191
+ fulfilled: boolean;
192
+ finalized: boolean;
193
+ refunded: boolean;
194
+ result: bigint;
195
+ }) => void): Promise<AsyncRequestFinalResult>;
158
196
 
159
197
  interface UserPosition {
160
198
  /** Vault share balance */
@@ -290,6 +328,40 @@ type VaultSummary = VaultStatus & VaultMetadata;
290
328
  * @returns Merged VaultStatus and VaultMetadata
291
329
  */
292
330
  declare function getVaultSummary(publicClient: PublicClient, vault: Address): Promise<VaultSummary>;
331
+ interface MultiChainUserPosition {
332
+ /** Shares held directly on the hub vault (vault.balanceOf) */
333
+ hubShares: bigint;
334
+ /** Per-spoke SHARE_OFT balances: { [chainId]: bigint } */
335
+ spokeShares: Record<number, bigint>;
336
+ /** hubShares + sum of all spokeShares */
337
+ totalShares: bigint;
338
+ /** convertToAssets(totalShares) on the hub */
339
+ estimatedAssets: bigint;
340
+ /** Share price: convertToAssets(10^decimals) */
341
+ sharePrice: bigint;
342
+ /** Vault decimals */
343
+ decimals: number;
344
+ /** Pending async withdrawal request on hub, or null */
345
+ pendingWithdrawal: {
346
+ shares: bigint;
347
+ timelockEndsAt: bigint;
348
+ canRedeemNow: boolean;
349
+ } | null;
350
+ }
351
+ /**
352
+ * Read the user's position across all chains of an omni vault.
353
+ *
354
+ * Discovers topology automatically, reads hub shares + pending withdrawal,
355
+ * then reads SHARE_OFT balances on each spoke chain in parallel.
356
+ *
357
+ * For local (single-chain) vaults, spokeShares will be empty and this
358
+ * behaves identically to getUserPosition.
359
+ *
360
+ * @param vault Vault address (same on all chains via CREATE3)
361
+ * @param user User wallet address
362
+ * @returns Aggregated position across all chains
363
+ */
364
+ declare function getUserPositionMultiChain(vault: Address, user: Address): Promise<MultiChainUserPosition>;
293
365
 
294
366
  declare const OMNI_FACTORY_ADDRESS: Address;
295
367
  interface VaultTopology {
@@ -539,4 +611,4 @@ declare function getOutboundRoutes(hubChainId: number, vault: Address): Promise<
539
611
  */
540
612
  declare function quoteRouteDepositFee(route: InboundRoute, hubChainId: number, amount: bigint, userAddress: Address): Promise<bigint>;
541
613
 
542
- export { type AsyncRequestStatus as A, getVaultStatus as B, getVaultSummary as C, type DepositBlockReason as D, getVaultTopology as E, isAsyncMode as F, isOnHubChain as G, previewDeposit as H, type InboundRoute as I, previewRedeem as J, quoteLzFee as K, quoteRouteDepositFee as L, type MaxWithdrawable as M, NATIVE_SYMBOL as N, OMNI_FACTORY_ADDRESS as O, waitForTx as P, type SpokeBalance as S, type UserBalances as U, type VaultDistribution as V, type AsyncRequestStatusInfo as a, type DepositEligibility as b, type InboundRouteWithBalance as c, type OutboundRoute as d, type UserPosition as e, type VaultMetadata as f, type VaultMode as g, type VaultStatus as h, type VaultSummary as i, type VaultTopology as j, canDeposit as k, discoverVaultTopology as l, ensureAllowance as m, getAllVaultChainIds as n, getAsyncRequestStatus as o, getAsyncRequestStatusLabel as p, getFullVaultTopology as q, getInboundRoutes as r, getMaxWithdrawable as s, getOutboundRoutes as t, getUserBalances as u, getUserBalancesForRoutes as v, getUserPosition as w, getVaultDistribution as x, getVaultDistributionWithTopology as y, getVaultMetadata as z };
614
+ export { type AsyncRequestFinalResult as A, getVaultDistribution as B, getVaultDistributionWithTopology as C, type DepositBlockReason as D, getVaultMetadata as E, getVaultStatus as F, getVaultSummary as G, getVaultTopology as H, type InboundRoute as I, isAsyncMode as J, isOnHubChain as K, previewDeposit as L, type MaxWithdrawable as M, NATIVE_SYMBOL as N, OMNI_FACTORY_ADDRESS as O, previewRedeem as P, quoteLzFee as Q, quoteRouteDepositFee as R, type SpokeBalance as S, waitForAsyncRequest as T, type UserBalances as U, type VaultDistribution as V, waitForTx as W, type AsyncRequestStatus as a, type AsyncRequestStatusInfo as b, type DepositEligibility as c, type InboundRouteWithBalance as d, type MultiChainUserPosition as e, type OutboundRoute as f, type UserPosition as g, type VaultMetadata as h, type VaultMode as i, type VaultStatus as j, type VaultSummary as k, type VaultTopology as l, canDeposit as m, discoverVaultTopology as n, ensureAllowance as o, getAllVaultChainIds as p, getAsyncRequestStatus as q, getAsyncRequestStatusLabel as r, getFullVaultTopology as s, getInboundRoutes as t, getMaxWithdrawable as u, getOutboundRoutes as v, getUserBalances as w, getUserBalancesForRoutes as x, getUserPosition as y, getUserPositionMultiChain as z };
@@ -153,8 +153,46 @@ declare function isAsyncMode(publicClient: PublicClient, vault: Address): Promis
153
153
  declare function getAsyncRequestStatus(publicClient: PublicClient, vault: Address, guid: `0x${string}`): Promise<{
154
154
  fulfilled: boolean;
155
155
  finalized: boolean;
156
+ refunded: boolean;
156
157
  result: bigint;
157
158
  }>;
159
+ interface AsyncRequestFinalResult {
160
+ /** 'completed' = assets/shares received, 'refunded' = tokens returned to user */
161
+ status: 'completed' | 'refunded';
162
+ /** For deposit: shares minted. For redeem: assets returned. 0 if refunded. */
163
+ result: bigint;
164
+ }
165
+ /**
166
+ * Poll an async cross-chain request until it finalizes or times out.
167
+ *
168
+ * This is the correct way to wait for smartDeposit/smartRedeem results on
169
+ * async vaults. Do NOT poll balance — use this instead, which reads the
170
+ * on-chain request state by GUID.
171
+ *
172
+ * @param publicClient Public client on the hub chain
173
+ * @param vault Vault address
174
+ * @param guid GUID from smartDeposit/smartRedeem result
175
+ * @param pollInterval Milliseconds between polls (default: 30_000)
176
+ * @param timeout Max wait time in milliseconds (default: 900_000 = 15 min)
177
+ * @param onPoll Optional callback invoked after each poll with current status
178
+ * @returns Final result with status and amount
179
+ * @throws Error if timeout is reached
180
+ *
181
+ * @example
182
+ * const depositResult = await smartDeposit(walletClient, publicClient, { vault }, amount, receiver)
183
+ * if ('guid' in depositResult) {
184
+ * const final = await waitForAsyncRequest(publicClient, vault, depositResult.guid, 30_000, 900_000, (s) => {
185
+ * console.log(`Status: fulfilled=${s.fulfilled}, finalized=${s.finalized}`)
186
+ * })
187
+ * console.log(`Done: ${final.status}, result: ${final.result}`)
188
+ * }
189
+ */
190
+ declare function waitForAsyncRequest(publicClient: PublicClient, vault: Address, guid: `0x${string}`, pollInterval?: number, timeout?: number, onPoll?: (status: {
191
+ fulfilled: boolean;
192
+ finalized: boolean;
193
+ refunded: boolean;
194
+ result: bigint;
195
+ }) => void): Promise<AsyncRequestFinalResult>;
158
196
 
159
197
  interface UserPosition {
160
198
  /** Vault share balance */
@@ -290,6 +328,40 @@ type VaultSummary = VaultStatus & VaultMetadata;
290
328
  * @returns Merged VaultStatus and VaultMetadata
291
329
  */
292
330
  declare function getVaultSummary(publicClient: PublicClient, vault: Address): Promise<VaultSummary>;
331
+ interface MultiChainUserPosition {
332
+ /** Shares held directly on the hub vault (vault.balanceOf) */
333
+ hubShares: bigint;
334
+ /** Per-spoke SHARE_OFT balances: { [chainId]: bigint } */
335
+ spokeShares: Record<number, bigint>;
336
+ /** hubShares + sum of all spokeShares */
337
+ totalShares: bigint;
338
+ /** convertToAssets(totalShares) on the hub */
339
+ estimatedAssets: bigint;
340
+ /** Share price: convertToAssets(10^decimals) */
341
+ sharePrice: bigint;
342
+ /** Vault decimals */
343
+ decimals: number;
344
+ /** Pending async withdrawal request on hub, or null */
345
+ pendingWithdrawal: {
346
+ shares: bigint;
347
+ timelockEndsAt: bigint;
348
+ canRedeemNow: boolean;
349
+ } | null;
350
+ }
351
+ /**
352
+ * Read the user's position across all chains of an omni vault.
353
+ *
354
+ * Discovers topology automatically, reads hub shares + pending withdrawal,
355
+ * then reads SHARE_OFT balances on each spoke chain in parallel.
356
+ *
357
+ * For local (single-chain) vaults, spokeShares will be empty and this
358
+ * behaves identically to getUserPosition.
359
+ *
360
+ * @param vault Vault address (same on all chains via CREATE3)
361
+ * @param user User wallet address
362
+ * @returns Aggregated position across all chains
363
+ */
364
+ declare function getUserPositionMultiChain(vault: Address, user: Address): Promise<MultiChainUserPosition>;
293
365
 
294
366
  declare const OMNI_FACTORY_ADDRESS: Address;
295
367
  interface VaultTopology {
@@ -539,4 +611,4 @@ declare function getOutboundRoutes(hubChainId: number, vault: Address): Promise<
539
611
  */
540
612
  declare function quoteRouteDepositFee(route: InboundRoute, hubChainId: number, amount: bigint, userAddress: Address): Promise<bigint>;
541
613
 
542
- export { type AsyncRequestStatus as A, getVaultStatus as B, getVaultSummary as C, type DepositBlockReason as D, getVaultTopology as E, isAsyncMode as F, isOnHubChain as G, previewDeposit as H, type InboundRoute as I, previewRedeem as J, quoteLzFee as K, quoteRouteDepositFee as L, type MaxWithdrawable as M, NATIVE_SYMBOL as N, OMNI_FACTORY_ADDRESS as O, waitForTx as P, type SpokeBalance as S, type UserBalances as U, type VaultDistribution as V, type AsyncRequestStatusInfo as a, type DepositEligibility as b, type InboundRouteWithBalance as c, type OutboundRoute as d, type UserPosition as e, type VaultMetadata as f, type VaultMode as g, type VaultStatus as h, type VaultSummary as i, type VaultTopology as j, canDeposit as k, discoverVaultTopology as l, ensureAllowance as m, getAllVaultChainIds as n, getAsyncRequestStatus as o, getAsyncRequestStatusLabel as p, getFullVaultTopology as q, getInboundRoutes as r, getMaxWithdrawable as s, getOutboundRoutes as t, getUserBalances as u, getUserBalancesForRoutes as v, getUserPosition as w, getVaultDistribution as x, getVaultDistributionWithTopology as y, getVaultMetadata as z };
614
+ export { type AsyncRequestFinalResult as A, getVaultDistribution as B, getVaultDistributionWithTopology as C, type DepositBlockReason as D, getVaultMetadata as E, getVaultStatus as F, getVaultSummary as G, getVaultTopology as H, type InboundRoute as I, isAsyncMode as J, isOnHubChain as K, previewDeposit as L, type MaxWithdrawable as M, NATIVE_SYMBOL as N, OMNI_FACTORY_ADDRESS as O, previewRedeem as P, quoteLzFee as Q, quoteRouteDepositFee as R, type SpokeBalance as S, waitForAsyncRequest as T, type UserBalances as U, type VaultDistribution as V, waitForTx as W, type AsyncRequestStatus as a, type AsyncRequestStatusInfo as b, type DepositEligibility as c, type InboundRouteWithBalance as d, type MultiChainUserPosition as e, type OutboundRoute as f, type UserPosition as g, type VaultMetadata as h, type VaultMode as i, type VaultStatus as j, type VaultSummary as k, type VaultTopology as l, canDeposit as m, discoverVaultTopology as n, ensureAllowance as o, getAllVaultChainIds as p, getAsyncRequestStatus as q, getAsyncRequestStatusLabel as r, getFullVaultTopology as s, getInboundRoutes as t, getMaxWithdrawable as u, getOutboundRoutes as v, getUserBalances as w, getUserBalancesForRoutes as x, getUserPosition as y, getUserPositionMultiChain as z };
@@ -1131,9 +1131,27 @@ async function getAsyncRequestStatus(publicClient, vault, guid) {
1131
1131
  return {
1132
1132
  fulfilled: info.fulfilled,
1133
1133
  finalized: info.finalized,
1134
+ refunded: info.refunded,
1134
1135
  result: finalizationResult
1135
1136
  };
1136
1137
  }
1138
+ async function waitForAsyncRequest(publicClient, vault, guid, pollInterval = 3e4, timeout = 9e5, onPoll) {
1139
+ const deadline = Date.now() + timeout;
1140
+ while (Date.now() < deadline) {
1141
+ const status = await getAsyncRequestStatus(publicClient, vault, guid);
1142
+ if (onPoll) onPoll(status);
1143
+ if (status.finalized) {
1144
+ return { status: "completed", result: status.result };
1145
+ }
1146
+ if (status.refunded) {
1147
+ return { status: "refunded", result: 0n };
1148
+ }
1149
+ await new Promise((r) => setTimeout(r, pollInterval));
1150
+ }
1151
+ throw new Error(
1152
+ `[MoreVaults] Async request ${guid} did not finalize within ${timeout / 1e3}s. The request may still complete \u2014 check https://layerzeroscan.com/tx/${guid}`
1153
+ );
1154
+ }
1137
1155
  var PUBLIC_RPCS = {
1138
1156
  1: [
1139
1157
  "https://ethereum-rpc.publicnode.com",
@@ -2956,6 +2974,115 @@ async function getVaultSummary(publicClient, vault) {
2956
2974
  ]);
2957
2975
  return { ...status, ...metadata };
2958
2976
  }
2977
+ var FACTORY_COMPOSER_ABI3 = [
2978
+ {
2979
+ type: "function",
2980
+ name: "vaultComposer",
2981
+ inputs: [{ name: "_vault", type: "address" }],
2982
+ outputs: [{ name: "", type: "address" }],
2983
+ stateMutability: "view"
2984
+ }
2985
+ ];
2986
+ var COMPOSER_SHARE_OFT_ABI = [
2987
+ {
2988
+ type: "function",
2989
+ name: "SHARE_OFT",
2990
+ inputs: [],
2991
+ outputs: [{ name: "", type: "address" }],
2992
+ stateMutability: "view"
2993
+ }
2994
+ ];
2995
+ async function getUserPositionMultiChain(vault, user) {
2996
+ const v = viem.getAddress(vault);
2997
+ const u = viem.getAddress(user);
2998
+ const topo = await discoverVaultTopology(vault);
2999
+ const hubClient = createChainClient(topo.hubChainId);
3000
+ if (!hubClient) throw new Error(`No public RPC for hub chainId ${topo.hubChainId}`);
3001
+ const [hubShares, decimals, withdrawalRequest] = await hubClient.multicall({
3002
+ contracts: [
3003
+ { address: v, abi: VAULT_ABI, functionName: "balanceOf", args: [u] },
3004
+ { address: v, abi: METADATA_ABI, functionName: "decimals" },
3005
+ { address: v, abi: VAULT_ABI, functionName: "getWithdrawalRequest", args: [u] }
3006
+ ],
3007
+ allowFailure: false
3008
+ });
3009
+ const [withdrawShares, timelockEndsAt] = withdrawalRequest;
3010
+ const spokeShares = {};
3011
+ if (topo.spokeChainIds.length > 0) {
3012
+ let hubShareOft = null;
3013
+ try {
3014
+ const composerAddress = await hubClient.readContract({
3015
+ address: OMNI_FACTORY_ADDRESS,
3016
+ abi: FACTORY_COMPOSER_ABI3,
3017
+ functionName: "vaultComposer",
3018
+ args: [v]
3019
+ });
3020
+ if (composerAddress !== "0x0000000000000000000000000000000000000000") {
3021
+ hubShareOft = await hubClient.readContract({
3022
+ address: composerAddress,
3023
+ abi: COMPOSER_SHARE_OFT_ABI,
3024
+ functionName: "SHARE_OFT"
3025
+ });
3026
+ }
3027
+ } catch {
3028
+ }
3029
+ if (hubShareOft) {
3030
+ const spokePromises = topo.spokeChainIds.map(async (spokeChainId) => {
3031
+ try {
3032
+ const spokeEid = CHAIN_ID_TO_EID[spokeChainId];
3033
+ if (!spokeEid) return { chainId: spokeChainId, balance: 0n };
3034
+ const spokeOftBytes32 = await hubClient.readContract({
3035
+ address: hubShareOft,
3036
+ abi: OFT_ABI,
3037
+ functionName: "peers",
3038
+ args: [spokeEid]
3039
+ });
3040
+ const spokeOft = viem.getAddress(`0x${spokeOftBytes32.slice(-40)}`);
3041
+ if (spokeOft === "0x0000000000000000000000000000000000000000") {
3042
+ return { chainId: spokeChainId, balance: 0n };
3043
+ }
3044
+ const spokeClient = createChainClient(spokeChainId);
3045
+ if (!spokeClient) return { chainId: spokeChainId, balance: 0n };
3046
+ const balance = await spokeClient.readContract({
3047
+ address: spokeOft,
3048
+ abi: ERC20_ABI,
3049
+ functionName: "balanceOf",
3050
+ args: [u]
3051
+ });
3052
+ return { chainId: spokeChainId, balance };
3053
+ } catch {
3054
+ return { chainId: spokeChainId, balance: 0n };
3055
+ }
3056
+ });
3057
+ const results = await Promise.all(spokePromises);
3058
+ for (const { chainId, balance } of results) {
3059
+ spokeShares[chainId] = balance;
3060
+ }
3061
+ }
3062
+ }
3063
+ const totalSpokeShares = Object.values(spokeShares).reduce((sum, b) => sum + b, 0n);
3064
+ const totalShares = hubShares + totalSpokeShares;
3065
+ const oneShare = 10n ** BigInt(decimals);
3066
+ const [estimatedAssets, sharePrice] = await Promise.all([
3067
+ totalShares === 0n ? Promise.resolve(0n) : hubClient.readContract({ address: v, abi: VAULT_ABI, functionName: "convertToAssets", args: [totalShares] }),
3068
+ hubClient.readContract({ address: v, abi: VAULT_ABI, functionName: "convertToAssets", args: [oneShare] })
3069
+ ]);
3070
+ const block = await hubClient.getBlock();
3071
+ const pendingWithdrawal = withdrawShares === 0n ? null : {
3072
+ shares: withdrawShares,
3073
+ timelockEndsAt,
3074
+ canRedeemNow: timelockEndsAt === 0n || block.timestamp >= timelockEndsAt
3075
+ };
3076
+ return {
3077
+ hubShares,
3078
+ spokeShares,
3079
+ totalShares,
3080
+ estimatedAssets,
3081
+ sharePrice,
3082
+ decimals,
3083
+ pendingWithdrawal
3084
+ };
3085
+ }
2959
3086
 
2960
3087
  // src/viem/distribution.ts
2961
3088
  async function getVaultDistribution(hubClient, vault, spokeClients) {
@@ -3072,6 +3199,7 @@ exports.getOutboundRoutes = getOutboundRoutes;
3072
3199
  exports.getUserBalances = getUserBalances;
3073
3200
  exports.getUserBalancesForRoutes = getUserBalancesForRoutes;
3074
3201
  exports.getUserPosition = getUserPosition;
3202
+ exports.getUserPositionMultiChain = getUserPositionMultiChain;
3075
3203
  exports.getVaultDistribution = getVaultDistribution;
3076
3204
  exports.getVaultDistributionWithTopology = getVaultDistributionWithTopology;
3077
3205
  exports.getVaultMetadata = getVaultMetadata;
@@ -3099,6 +3227,7 @@ exports.requestRedeem = requestRedeem;
3099
3227
  exports.resolveRedeemAddresses = resolveRedeemAddresses;
3100
3228
  exports.smartDeposit = smartDeposit;
3101
3229
  exports.smartRedeem = smartRedeem;
3230
+ exports.waitForAsyncRequest = waitForAsyncRequest;
3102
3231
  exports.waitForCompose = waitForCompose;
3103
3232
  exports.waitForTx = waitForTx;
3104
3233
  exports.withdrawAssets = withdrawAssets;