@circle-fin/app-kit 1.9.0 → 1.10.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.
@@ -2868,7 +2868,10 @@ var EarnChain;
2868
2868
  contracts: {
2869
2869
  v1: {
2870
2870
  wallet: GATEWAY_WALLET_EVM_TESTNET,
2871
- minter: GATEWAY_MINTER_EVM_TESTNET
2871
+ minter: GATEWAY_MINTER_EVM_TESTNET,
2872
+ // DepositForHandler the GenericExecutor calls to run a fast cross-chain
2873
+ // deposit into the GatewayWallet above.
2874
+ depositForHandler: '0xD05E7D2E7d30b92c5F17d7d0fC575fce231F1A48'
2872
2875
  }
2873
2876
  },
2874
2877
  forwarderSupported: {
@@ -5952,7 +5955,10 @@ var Chains = /*#__PURE__*/Object.freeze({
5952
5955
  minter: z.string({
5953
5956
  required_error: 'Gateway minter address is required. Please provide a valid contract address.',
5954
5957
  invalid_type_error: 'Gateway minter address must be a string.'
5955
- }).min(1, 'Gateway minter address cannot be empty.')
5958
+ }).min(1, 'Gateway minter address cannot be empty.'),
5959
+ depositForHandler: z.string({
5960
+ invalid_type_error: 'Gateway depositForHandler address must be a string.'
5961
+ }).min(1, 'Gateway depositForHandler address cannot be empty.').optional()
5956
5962
  }).strict() // Reject any additional properties not defined in the schema
5957
5963
  ;
5958
5964
  /**
@@ -8739,7 +8745,7 @@ function parseOrThrow(value, schema, context) {
8739
8745
  }
8740
8746
 
8741
8747
  var name = "@circle-fin/unified-balance-kit";
8742
- var version = "1.2.2";
8748
+ var version = "1.3.0";
8743
8749
  var pkg = {
8744
8750
  name: name,
8745
8751
  version: version};
@@ -16375,29 +16381,33 @@ const CIRCLE_BPS_DIVISOR = 10_000n;
16375
16381
  const DEFAULT_GAS_FEE = parseUnits('0.1', USDC_DECIMALS);
16376
16382
  /**
16377
16383
  * Return the estimated Gateway gas fee for a chain in USDC atomic units.
16378
- * Falls back to a conservative 0.1 USDC for unlisted chains.
16384
+ * Prefers an entry in `overrides` (the real per-chain fee derived from a
16385
+ * prior estimate), then the static {@link GAS_FEE_BY_CHAIN} constant, and
16386
+ * finally a conservative 0.1 USDC fallback for unlisted chains.
16379
16387
  *
16380
16388
  * @param chain - The source blockchain.
16389
+ * @param overrides - Optional real per-chain gas fees keyed by chain.
16381
16390
  * @returns Gas fee in USDC atomic units.
16382
- */ function getGasFee(chain) {
16383
- return GAS_FEE_BY_CHAIN.get(chain) ?? DEFAULT_GAS_FEE;
16391
+ */ function getGasFee(chain, overrides) {
16392
+ return overrides?.get(chain) ?? GAS_FEE_BY_CHAIN.get(chain) ?? DEFAULT_GAS_FEE;
16384
16393
  }
16385
16394
  /**
16386
16395
  * Return the estimated forwarder fee for the destination chain
16387
16396
  * (service fee + destination gas fee).
16388
16397
  *
16389
16398
  * @param destinationChain - The mint destination chain.
16399
+ * @param overrides - Optional real per-chain gas fees keyed by chain.
16390
16400
  * @returns Forwarder fee in USDC atomic units.
16391
- */ function getForwarderFee(destinationChain) {
16392
- const destGas = getGasFee(destinationChain);
16401
+ */ function getForwarderFee(destinationChain, overrides) {
16402
+ const destGas = getGasFee(destinationChain, overrides);
16393
16403
  return FORWARDER_SERVICE_FEE + destGas;
16394
16404
  }
16395
16405
  /**
16396
16406
  * Estimate the fixed fees (gas + forwarder) and compute the maximum
16397
16407
  * amount that can be drawn from this chain for a single intent,
16398
16408
  * accounting for the 0.5 bps transfer fee if cross-chain.
16399
- */ function computeMaxDrawable(slot, forwarderFeeRemaining) {
16400
- const gasFee = getGasFee(slot.chain);
16409
+ */ function computeMaxDrawable(slot, forwarderFeeRemaining, overrides) {
16410
+ const gasFee = getGasFee(slot.chain, overrides);
16401
16411
  let fixedFees = gasFee;
16402
16412
  let forwarderFeeUsed = 0n;
16403
16413
  if (forwarderFeeRemaining > 0n) {
@@ -16429,14 +16439,14 @@ const DEFAULT_GAS_FEE = parseUnits('0.1', USDC_DECIMALS);
16429
16439
  * buffer), and returns the allocations for this pass.
16430
16440
  *
16431
16441
  * Mutates `slot.remaining` so the next pass sees reduced balances.
16432
- */ function greedyAllocate(slots, amount, destinationChain, useForwarder) {
16442
+ */ function greedyAllocate(slots, amount, destinationChain, useForwarder, overrides) {
16433
16443
  const result = [];
16434
16444
  let remaining = amount;
16435
- let forwarderFeeRemaining = useForwarder ? getForwarderFee(destinationChain) : 0n;
16445
+ let forwarderFeeRemaining = useForwarder ? getForwarderFee(destinationChain, overrides) : 0n;
16436
16446
  if (remaining <= 0n) return result;
16437
16447
  for (const slot of slots){
16438
16448
  if (remaining <= 0n) break;
16439
- const { drawable, gasFee, forwarderFeeUsed } = computeMaxDrawable(slot, forwarderFeeRemaining);
16449
+ const { drawable, gasFee, forwarderFeeUsed } = computeMaxDrawable(slot, forwarderFeeRemaining, overrides);
16440
16450
  if (drawable <= 0n) continue;
16441
16451
  // Greedy: take as much as we can from this chain
16442
16452
  const take = remaining < drawable ? remaining : drawable // NOSONAR: This is a false positive — Math.min() only accepts number, not bigint, so the ternary is the correct pattern here.
@@ -16535,7 +16545,7 @@ const DEFAULT_GAS_FEE = parseUnits('0.1', USDC_DECIMALS);
16535
16545
  // After this pass, slot.remaining reflects consumed capacity.
16536
16546
  // -----------------------------------------------------------------------
16537
16547
  const transferAmount = parseUnits(ctx.amountIn, USDC_DECIMALS);
16538
- const allocations = greedyAllocate(slots, transferAmount, ctx.destinationChain, ctx.useForwarder);
16548
+ const allocations = greedyAllocate(slots, transferAmount, ctx.destinationChain, ctx.useForwarder, ctx.gasFeeOverrides);
16539
16549
  assertFullyAllocated(allocations, transferAmount, ctx.amountIn);
16540
16550
  // -----------------------------------------------------------------------
16541
16551
  // 4. Phase 2 — Allocate developer fee
@@ -16543,7 +16553,7 @@ const DEFAULT_GAS_FEE = parseUnits('0.1', USDC_DECIMALS);
16543
16553
  // Same-chain first here too — if the destination chain still has
16544
16554
  // capacity, use it (same-chain fee intent = cheapest gas).
16545
16555
  // -----------------------------------------------------------------------
16546
- const developerFeeAllocations = greedyAllocate(slots, devFeeAmount, ctx.destinationChain, false);
16556
+ const developerFeeAllocations = greedyAllocate(slots, devFeeAmount, ctx.destinationChain, false, ctx.gasFeeOverrides);
16547
16557
  if (devFeeAmount > 0n) {
16548
16558
  assertFullyAllocated(developerFeeAllocations, devFeeAmount, formatUnits(devFeeAmount.toString(), USDC_DECIMALS));
16549
16559
  }
@@ -16552,7 +16562,7 @@ const DEFAULT_GAS_FEE = parseUnits('0.1', USDC_DECIMALS);
16552
16562
  // Again same ordering, same shared reduced balances.
16553
16563
  // Same-chain first for the same reason.
16554
16564
  // -----------------------------------------------------------------------
16555
- const circleFeeAllocations = greedyAllocate(slots, circleFeeAmount, ctx.destinationChain, false);
16565
+ const circleFeeAllocations = greedyAllocate(slots, circleFeeAmount, ctx.destinationChain, false, ctx.gasFeeOverrides);
16556
16566
  if (circleFeeAmount > 0n) {
16557
16567
  assertFullyAllocated(circleFeeAllocations, circleFeeAmount, formatUnits(circleFeeAmount.toString(), USDC_DECIMALS));
16558
16568
  }
@@ -16651,10 +16661,12 @@ const BPS_DIVISOR = 100_000n;
16651
16661
  *
16652
16662
  * Unlike `findChainNameByDomain` (which returns the display `name`),
16653
16663
  * this returns `chain.chain` — the enum identifier expected by
16654
- * {@link FeeAllocation}.
16664
+ * {@link FeeAllocation}. Returns `undefined` when no allocation covers the
16665
+ * domain so callers skip the intent rather than bucketing it under a
16666
+ * fabricated sentinel.
16655
16667
  */ function findBlockchainByDomain(domain, allocations) {
16656
16668
  const alloc = allocations.find((a)=>a.chain.gateway.domain === domain);
16657
- return alloc?.chain.chain ?? 'Unknown';
16669
+ return alloc?.chain.chain;
16658
16670
  }
16659
16671
  /**
16660
16672
  * Normalize any address/salt format to lowercase bytes32 hex.
@@ -16778,6 +16790,33 @@ const BPS_DIVISOR = 100_000n;
16778
16790
  };
16779
16791
  });
16780
16792
  }
16793
+ /**
16794
+ * Read an intent's transfer value as a BigInt, tolerating the string form
16795
+ * that can appear on estimate-response specs.
16796
+ */ function intentValue(intent) {
16797
+ const { value } = intent.spec;
16798
+ return typeof value === 'bigint' ? value : safeBigInt(String(value), 'spec.value');
16799
+ }
16800
+ /**
16801
+ * Split a single intent's `maxFee` into its transfer-fee and gas-fee
16802
+ * components.
16803
+ *
16804
+ * `transferFee = value * GATEWAY_TRANSFER_FEE_SCALED_BPS / BPS_DIVISOR`
16805
+ * `gasFee = maxFee - transferFee`
16806
+ *
16807
+ * Same-chain transfers (withdrawals) do not incur a transfer fee, so the
16808
+ * whole `maxFee` is gas. See {@link aggregateFeesByIntent} for the caveats
16809
+ * on re-deriving the split locally.
16810
+ */ function splitIntentFee(intent) {
16811
+ const { maxFee, spec } = intent;
16812
+ const isSameChain = spec.sourceDomain === spec.destinationDomain;
16813
+ const transferFee = isSameChain ? 0n : intentValue(intent) * GATEWAY_TRANSFER_FEE_SCALED_BPS / BPS_DIVISOR;
16814
+ const gasFee = maxFee > transferFee ? maxFee - transferFee : 0n;
16815
+ return {
16816
+ transferFee,
16817
+ gasFee
16818
+ };
16819
+ }
16781
16820
  /**
16782
16821
  * Decompose each intent's `maxFee` into a transfer fee and a gas fee,
16783
16822
  * then aggregate both by source chain.
@@ -16802,7 +16841,6 @@ const BPS_DIVISOR = 100_000n;
16802
16841
  * names from source domains.
16803
16842
  * @returns Per-chain and total transfer/gas fee breakdowns.
16804
16843
  */ function aggregateFeesByIntent(estimatedIntents, allocations) {
16805
- const transferFeeBps = GATEWAY_TRANSFER_FEE_SCALED_BPS;
16806
16844
  const transferFeeByChain = new Map();
16807
16845
  const gasFeeByChain = new Map();
16808
16846
  let totalTransferFee = 0n;
@@ -16810,18 +16848,18 @@ const BPS_DIVISOR = 100_000n;
16810
16848
  for (const intent of estimatedIntents){
16811
16849
  const { maxFee, spec } = intent;
16812
16850
  if (maxFee === 0n) continue;
16813
- const chainName = findBlockchainByDomain(spec.sourceDomain, allocations);
16814
- const value = typeof spec.value === 'bigint' ? spec.value : safeBigInt(String(spec.value), 'spec.value');
16815
- const isSameChain = spec.sourceDomain === spec.destinationDomain;
16816
- const transferFee = isSameChain ? 0n : value * transferFeeBps / BPS_DIVISOR;
16817
- const gasFee = maxFee > transferFee ? maxFee - transferFee : 0n;
16851
+ const { transferFee, gasFee } = splitIntentFee(intent);
16852
+ totalTransferFee += transferFee;
16853
+ totalGasFee += gasFee;
16854
+ // Totals stay complete even if a domain can't be resolved; only the
16855
+ // per-chain breakdown skips it rather than inventing a placeholder chain.
16856
+ const chain = findBlockchainByDomain(spec.sourceDomain, allocations);
16857
+ if (chain === undefined) continue;
16818
16858
  if (transferFee > 0n) {
16819
- totalTransferFee += transferFee;
16820
- transferFeeByChain.set(chainName, (transferFeeByChain.get(chainName) ?? 0n) + transferFee);
16859
+ transferFeeByChain.set(chain, (transferFeeByChain.get(chain) ?? 0n) + transferFee);
16821
16860
  }
16822
16861
  if (gasFee > 0n) {
16823
- totalGasFee += gasFee;
16824
- gasFeeByChain.set(chainName, (gasFeeByChain.get(chainName) ?? 0n) + gasFee);
16862
+ gasFeeByChain.set(chain, (gasFeeByChain.get(chain) ?? 0n) + gasFee);
16825
16863
  }
16826
16864
  }
16827
16865
  return {
@@ -16885,6 +16923,91 @@ const BPS_DIVISOR = 100_000n;
16885
16923
  }
16886
16924
  return fees;
16887
16925
  }
16926
+ /**
16927
+ * Derive the real per-chain Gateway gas fee from estimated intents, keyed by
16928
+ * source {@link Blockchain}.
16929
+ *
16930
+ * The value is the maximum single-intent gas fee observed on each chain
16931
+ * (`maxFee − transferFee`) — the amount `computeAutoAllocation` must reserve
16932
+ * per burn intent on that chain. Gas is (near) amount-independent, so every
16933
+ * intent on a chain pays roughly the same; taking the max is a conservative
16934
+ * choice for the multi-intent-per-chain case.
16935
+ *
16936
+ * Intended for `AutoAllocationContext.gasFeeOverrides` so the corrective
16937
+ * re-allocation pass reserves the API's real fee instead of the static
16938
+ * {@link GAS_FEE_BY_CHAIN} constant.
16939
+ *
16940
+ * @param estimatedIntents - Intents with `maxFee` from {@link parseEstimateResponse}.
16941
+ * @param allocations - Normalised allocations used to resolve chain from source domain.
16942
+ * @returns Per-chain real gas fees in USDC atomic units.
16943
+ *
16944
+ * @example
16945
+ * ```typescript
16946
+ * import type { BurnIntent } from '../createIntent/types'
16947
+ * import type { NormalizedAllocation } from '../allocations'
16948
+ *
16949
+ * declare const estimatedIntents: BurnIntent[]
16950
+ * declare const allocations: NormalizedAllocation[]
16951
+ *
16952
+ * // Real per-chain gas, ready to pass as AutoAllocationContext.gasFeeOverrides
16953
+ * // to re-run computeAutoAllocation with the corrected reserve.
16954
+ * const overrides = deriveGasFeeOverrides(estimatedIntents, allocations)
16955
+ * ```
16956
+ */ function deriveGasFeeOverrides(estimatedIntents, allocations) {
16957
+ const overrides = new Map();
16958
+ for (const intent of estimatedIntents){
16959
+ if (intent.maxFee === 0n) continue;
16960
+ const chain = findBlockchainByDomain(intent.spec.sourceDomain, allocations);
16961
+ if (chain === undefined) continue;
16962
+ const { gasFee } = splitIntentFee(intent);
16963
+ const prev = overrides.get(chain) ?? 0n;
16964
+ if (gasFee > prev) overrides.set(chain, gasFee);
16965
+ }
16966
+ return overrides;
16967
+ }
16968
+ /**
16969
+ * Sum the total balance each source chain must cover, keyed by source
16970
+ * {@link Blockchain}.
16971
+ *
16972
+ * Approximates the Gateway API's balance validation, which rejects a transfer
16973
+ * (`BALANCE_INSUFFICIENT_TOKEN`) when a depositor's confirmed balance on a
16974
+ * source chain is below `sum(intent.value + intent.maxFee)` for that
16975
+ * depositor's intents. This aggregates by chain across all sources, so it is
16976
+ * exact for the common single-depositor-per-chain wallet. When several
16977
+ * depositors hold USDC on the same chain, the chain-level sum can mask a
16978
+ * per-depositor shortfall (or a surplus on one depositor can hide it); the
16979
+ * API's own per-depositor `9001` remains the backstop for that case. Scope the
16980
+ * comparison per (depositor, chain) if that multi-depositor case must be caught
16981
+ * pre-submit.
16982
+ *
16983
+ * @param estimatedIntents - Intents with `maxFee` from {@link parseEstimateResponse}.
16984
+ * @param allocations - Normalised allocations used to resolve chain from source domain.
16985
+ * @returns Per-chain required amount (transfer value + fees) in USDC atomic units.
16986
+ *
16987
+ * @example
16988
+ * ```typescript
16989
+ * import { Blockchain } from '@core/chains'
16990
+ * import type { BurnIntent } from '../createIntent/types'
16991
+ * import type { NormalizedAllocation } from '../allocations'
16992
+ *
16993
+ * declare const estimatedIntents: BurnIntent[]
16994
+ * declare const allocations: NormalizedAllocation[]
16995
+ * declare const confirmedBalanceAtomic: bigint
16996
+ *
16997
+ * const required = sumRequiredPerChain(estimatedIntents, allocations)
16998
+ * const overDrawn =
16999
+ * (required.get(Blockchain.Ethereum) ?? 0n) > confirmedBalanceAtomic
17000
+ * ```
17001
+ */ function sumRequiredPerChain(estimatedIntents, allocations) {
17002
+ const required = new Map();
17003
+ for (const intent of estimatedIntents){
17004
+ const chain = findBlockchainByDomain(intent.spec.sourceDomain, allocations);
17005
+ if (chain === undefined) continue;
17006
+ const amount = intentValue(intent) + intent.maxFee;
17007
+ required.set(chain, (required.get(chain) ?? 0n) + amount);
17008
+ }
17009
+ return required;
17010
+ }
16888
17011
 
16889
17012
  /**
16890
17013
  * Sign each adapter group: Solana one intent per signature, EVM batch per adapter.
@@ -17139,69 +17262,127 @@ const FORWARDER_POLL_TIMEOUT_MS = 300_000;
17139
17262
  * non-forwarder transfer response is missing attestation or signature.
17140
17263
  * @throws KitError Propagated from adapter signing if the user rejects
17141
17264
  * or the signer is unavailable.
17142
- */ async function resolveAllocationsAndIntents(params, destChain, recipientAddress, useForwarder) {
17143
- if (params.amountIn) {
17144
- const rawSources = Array.isArray(params.from) ? params.from : [
17145
- params.from
17146
- ];
17147
- const sourcesArray = rawSources.filter((s)=>s != null);
17148
- const networkType = destChain.isTestnet ? 'testnet' : 'mainnet';
17149
- const balanceResults = await Promise.all(sourcesArray.map(async (source)=>{
17150
- // When sourceAccount is set (delegate flow), scope the balance
17151
- // query to the Gateway depositor not the signer. Using the
17152
- // address-only path bypasses adapter address resolution, which
17153
- // would otherwise return the signer's balance (developer-
17154
- // controlled) or reject an explicit address (user-controlled).
17155
- let querySource;
17156
- if (source.sourceAccount) {
17157
- querySource = {
17158
- address: source.sourceAccount
17159
- };
17160
- } else {
17161
- querySource = {
17162
- adapter: source.adapter
17163
- };
17164
- if ('address' in source && source.address) {
17165
- querySource['address'] = source.address;
17166
- }
17167
- }
17168
- return getBalances$1({
17169
- token: params.token,
17170
- sources: querySource,
17171
- networkType
17172
- });
17173
- }));
17174
- const chainBalances = [];
17175
- for(let i = 0; i < balanceResults.length; i++){
17176
- const breakdowns = balanceResults[i]?.breakdown[0]?.breakdown ?? [];
17177
- for (const b of breakdowns){
17178
- chainBalances.push({
17179
- chain: b.chain,
17180
- confirmedBalance: b.confirmedBalance,
17181
- sourceIndex: i
17182
- });
17265
+ */ /**
17266
+ * Fetch confirmed per-chain USDC balances for every auto-allocation source.
17267
+ *
17268
+ * Used only on the `amountIn` (auto-allocation) path. Returns one
17269
+ * {@link ChainBalance} per (source, chain) pair so the greedy allocator — and
17270
+ * the corrective re-allocation pass — can reason about draw limits without a
17271
+ * second balance round-trip.
17272
+ *
17273
+ * @param params - Spend parameters (source(s) and token).
17274
+ * @param destChain - Resolved destination chain (used for network type).
17275
+ * @returns Confirmed balances tagged with their originating source index.
17276
+ */ async function fetchChainBalances(params, destChain) {
17277
+ const rawSources = Array.isArray(params.from) ? params.from : [
17278
+ params.from
17279
+ ];
17280
+ const sourcesArray = rawSources.filter((s)=>s != null);
17281
+ const networkType = destChain.isTestnet ? 'testnet' : 'mainnet';
17282
+ const balanceResults = await Promise.all(sourcesArray.map(async (source)=>{
17283
+ // When sourceAccount is set (delegate flow), scope the balance
17284
+ // query to the Gateway depositor — not the signer. Using the
17285
+ // address-only path bypasses adapter address resolution, which
17286
+ // would otherwise return the signer's balance (developer-
17287
+ // controlled) or reject an explicit address (user-controlled).
17288
+ let querySource;
17289
+ if (source.sourceAccount) {
17290
+ querySource = {
17291
+ address: source.sourceAccount
17292
+ };
17293
+ } else {
17294
+ querySource = {
17295
+ adapter: source.adapter
17296
+ };
17297
+ if ('address' in source && source.address) {
17298
+ querySource['address'] = source.address;
17183
17299
  }
17184
17300
  }
17185
- const customFeeConfig = params.config?.customFee;
17186
- const autoAllocResult = computeAutoAllocation({
17187
- amountIn: params.amountIn,
17188
- destinationChain: destChain.chain,
17189
- chainBalances,
17190
- useForwarder,
17191
- ...customFeeConfig ? {
17192
- customFee: customFeeConfig
17193
- } : {}
17301
+ return getBalances$1({
17302
+ token: params.token,
17303
+ sources: querySource,
17304
+ networkType
17194
17305
  });
17195
- const normalizedAutoAllocations = await normalizeAutoAllocations(autoAllocResult, sourcesArray);
17196
- const intents = buildAutoAllocatedBurnIntents(normalizedAutoAllocations, destChain, recipientAddress, params.token, params.config?.customFee);
17197
- const allocations = [
17198
- ...normalizedAutoAllocations.user,
17199
- ...normalizedAutoAllocations.devFee,
17200
- ...normalizedAutoAllocations.circleFee
17201
- ];
17306
+ }));
17307
+ const chainBalances = [];
17308
+ for(let i = 0; i < balanceResults.length; i++){
17309
+ const breakdowns = balanceResults[i]?.breakdown[0]?.breakdown ?? [];
17310
+ for (const b of breakdowns){
17311
+ chainBalances.push({
17312
+ chain: b.chain,
17313
+ confirmedBalance: b.confirmedBalance,
17314
+ sourceIndex: i
17315
+ });
17316
+ }
17317
+ }
17318
+ return chainBalances;
17319
+ }
17320
+ /**
17321
+ * Build auto-allocated normalised allocations and burn intents from
17322
+ * pre-fetched balances.
17323
+ *
17324
+ * Performs no balance API call, so it can be re-invoked with
17325
+ * `gasFeeOverrides` (the real per-chain gas from a prior estimate) to correct
17326
+ * an over-draw without re-querying balances.
17327
+ *
17328
+ * @param params - Spend parameters (source(s), token, optional custom fee).
17329
+ * @param destChain - Resolved destination chain with Gateway v1 config.
17330
+ * @param recipientAddress - Resolved recipient address on the destination chain.
17331
+ * @param useForwarder - Whether the Forwarding Service path is active.
17332
+ * @param amountIn - Human-readable USDC amount to allocate.
17333
+ * @param chainBalances - Confirmed balances from {@link fetchChainBalances}.
17334
+ * @param gasFeeOverrides - Optional real per-chain gas fees to reserve.
17335
+ * @returns Normalised allocations and burn intents for the estimate/transfer API.
17336
+ */ async function buildAutoAllocatedFromBalances(params, destChain, recipientAddress, useForwarder, amountIn, chainBalances, gasFeeOverrides) {
17337
+ const rawSources = Array.isArray(params.from) ? params.from : [
17338
+ params.from
17339
+ ];
17340
+ const sourcesArray = rawSources.filter((s)=>s != null);
17341
+ const customFeeConfig = params.config?.customFee;
17342
+ const autoAllocResult = computeAutoAllocation({
17343
+ amountIn,
17344
+ destinationChain: destChain.chain,
17345
+ chainBalances,
17346
+ useForwarder,
17347
+ ...customFeeConfig ? {
17348
+ customFee: customFeeConfig
17349
+ } : {},
17350
+ ...gasFeeOverrides ? {
17351
+ gasFeeOverrides
17352
+ } : {}
17353
+ });
17354
+ const normalizedAutoAllocations = await normalizeAutoAllocations(autoAllocResult, sourcesArray);
17355
+ const intents = buildAutoAllocatedBurnIntents(normalizedAutoAllocations, destChain, recipientAddress, params.token, params.config?.customFee);
17356
+ const allocations = [
17357
+ ...normalizedAutoAllocations.user,
17358
+ ...normalizedAutoAllocations.devFee,
17359
+ ...normalizedAutoAllocations.circleFee
17360
+ ];
17361
+ return {
17362
+ allocations,
17363
+ intents
17364
+ };
17365
+ }
17366
+ /**
17367
+ * Resolve allocations and burn intents for the spend.
17368
+ *
17369
+ * Auto-allocation (`amountIn`) fetches balances once and returns them so the
17370
+ * caller can detect and correct over-draw without re-querying. Explicit
17371
+ * allocations return no balances (they are user-authoritative).
17372
+ *
17373
+ * @param params - Spend parameters.
17374
+ * @param destChain - Resolved destination chain with Gateway v1 config.
17375
+ * @param recipientAddress - Resolved recipient address.
17376
+ * @param useForwarder - Whether the Forwarding Service path is active.
17377
+ * @returns Allocations, intents, and (auto-allocation only) confirmed balances.
17378
+ */ async function resolveAllocationsAndIntents(params, destChain, recipientAddress, useForwarder) {
17379
+ if (params.amountIn) {
17380
+ const chainBalances = await fetchChainBalances(params, destChain);
17381
+ const { allocations, intents } = await buildAutoAllocatedFromBalances(params, destChain, recipientAddress, useForwarder, params.amountIn, chainBalances);
17202
17382
  return {
17203
17383
  allocations,
17204
- intents
17384
+ intents,
17385
+ chainBalances
17205
17386
  };
17206
17387
  }
17207
17388
  const allocations = await normalizeAllocations(params);
@@ -17243,6 +17424,148 @@ const FORWARDER_POLL_TIMEOUT_MS = 300_000;
17243
17424
  forwardingFee: undefined
17244
17425
  };
17245
17426
  }
17427
+ /** Sum confirmed balances (atomic USDC) per source chain. */ function computeAvailablePerChain(chainBalances) {
17428
+ const available = new Map();
17429
+ for (const b of chainBalances){
17430
+ const atomic = parseUnits(b.confirmedBalance, USDC_DECIMALS$1);
17431
+ available.set(b.chain, (available.get(b.chain) ?? 0n) + atomic);
17432
+ }
17433
+ return available;
17434
+ }
17435
+ /**
17436
+ * Detect source chains whose required draw (value + maxFee across their
17437
+ * intents) exceeds the confirmed balance — the condition the Gateway API
17438
+ * rejects with `BALANCE_INSUFFICIENT_TOKEN` at `/v1/transfer`.
17439
+ *
17440
+ * Both sides are summed per chain (see {@link sumRequiredPerChain} and
17441
+ * {@link computeAvailablePerChain}), so detection is exact for the common
17442
+ * single-depositor-per-chain wallet. When multiple depositors hold USDC on the
17443
+ * same chain, a chain-level surplus can mask a per-depositor shortfall; the
17444
+ * API's own per-depositor `9001` remains the backstop in that case.
17445
+ */ function findOverdrawnChains(estimatedIntents, allocations, chainBalances) {
17446
+ const required = sumRequiredPerChain(estimatedIntents, allocations);
17447
+ const available = computeAvailablePerChain(chainBalances);
17448
+ const overdrawn = [];
17449
+ for (const [chain, req] of required){
17450
+ const avail = available.get(chain) ?? 0n;
17451
+ if (req > avail) {
17452
+ overdrawn.push({
17453
+ chain,
17454
+ required: req,
17455
+ available: avail
17456
+ });
17457
+ }
17458
+ }
17459
+ return overdrawn;
17460
+ }
17461
+ /**
17462
+ * Build a descriptive KitError for an auto-allocation gas shortfall that
17463
+ * survives the corrective re-allocation, naming the per-chain gap so the
17464
+ * caller sees the real cause instead of the opaque API 9001 rejection.
17465
+ */ function createAutoAllocationGasError(overdrawn, cause) {
17466
+ const detail = overdrawn.map((o)=>`${String(o.chain)} needs ${formatUnits(o.required.toString(), USDC_DECIMALS$1)} USDC ` + `(transfer + gas) but only ${formatUnits(o.available.toString(), USDC_DECIMALS$1)} USDC is available`).join('; ');
17467
+ return new KitError({
17468
+ ...BalanceError.INSUFFICIENT_GAS,
17469
+ recoverability: 'FATAL',
17470
+ message: `Insufficient USDC to cover the transfer amount plus Gateway gas fees: ${detail}. ` + `Reduce the amount or add USDC on the affected chain(s).`,
17471
+ ...cause === undefined ? {} : {
17472
+ cause: {
17473
+ trace: {
17474
+ cause
17475
+ }
17476
+ }
17477
+ }
17478
+ });
17479
+ }
17480
+ /**
17481
+ * Validate allocations against the network/forwarder rules, call the estimate
17482
+ * API, and return the estimated intents with any forwarding fee.
17483
+ *
17484
+ * @param allocations - Normalised allocations for the estimate.
17485
+ * @param intents - Burn intents to estimate.
17486
+ * @param destChain - Resolved destination chain with Gateway v1 config.
17487
+ * @param useForwarder - Whether the Forwarding Service path is active.
17488
+ * @returns Estimated intents (with real maxFee) and optional forwarding fee.
17489
+ */ async function validateAndEstimate(allocations, intents, destChain, useForwarder) {
17490
+ assertNetworkCompatibility(allocations, destChain);
17491
+ if (useForwarder) {
17492
+ assertForwarderRouteSupport(destChain, allocations);
17493
+ }
17494
+ const apiBaseUrl = getGatewayApiBaseUrl(destChain.isTestnet);
17495
+ const estimateBody = buildEstimateRequestBody(intents);
17496
+ const { entries, forwardingFee } = await fetchEstimate(apiBaseUrl, estimateBody, useForwarder, allocations);
17497
+ const estimatedIntents = parseEstimateResponse(entries, intents);
17498
+ return {
17499
+ estimatedIntents,
17500
+ forwardingFee
17501
+ };
17502
+ }
17503
+ /**
17504
+ * Fold newly-observed per-chain gas into the accumulated overrides, keeping the
17505
+ * higher fee per chain so a chain a later pass reveals is never under-reserved.
17506
+ */ function mergeGasFeeOverrides(base, next) {
17507
+ const merged = new Map(base);
17508
+ for (const [chain, fee] of next){
17509
+ const prev = merged.get(chain);
17510
+ if (prev === undefined || fee > prev) {
17511
+ merged.set(chain, fee);
17512
+ }
17513
+ }
17514
+ return merged;
17515
+ }
17516
+ /**
17517
+ * Maximum corrective re-allocation passes before failing fast. One pass fixes
17518
+ * the common case; a second/third covers a chain that a spill only introduces
17519
+ * after gas is reserved. Bounds the worst case at this many extra estimate
17520
+ * round-trips (only ever reached when the balance genuinely falls short).
17521
+ */ const MAX_CORRECTION_PASSES = 3;
17522
+ /**
17523
+ * Correct an auto-allocation over-draw: reserve the estimate's real per-chain
17524
+ * gas, re-allocate from the same balances, and re-estimate — repeating up to
17525
+ * {@link MAX_CORRECTION_PASSES} times, accumulating the real gas each pass
17526
+ * reveals.
17527
+ *
17528
+ * One pass fixes the common case, where the over-drawn chain was already in the
17529
+ * first estimate. A further pass covers a chain that a spill only introduces
17530
+ * once gas is reserved on the destination: that chain isn't in the first
17531
+ * estimate, so its real gas is unknown until it appears, and its first
17532
+ * re-allocation falls back to the static reserve. Each pass folds the newly
17533
+ * revealed gas into the overrides (see {@link mergeGasFeeOverrides}) so the
17534
+ * next pass reserves it too. Per-chain gas is ~amount-independent, so once
17535
+ * every drawn chain's real gas is known the allocation converges.
17536
+ *
17537
+ * When the shortfall is genuine — the re-allocation can't cover the amount, or
17538
+ * the passes are exhausted while still over-drawn — throws a gas-specific
17539
+ * {@link KitError} instead of submitting a doomed transfer.
17540
+ */ async function correctOverdraw(opts) {
17541
+ let overrides = deriveGasFeeOverrides(opts.estimatedIntents, opts.allocations);
17542
+ let overdrawn = opts.overdrawn;
17543
+ for(let pass = 0; pass < MAX_CORRECTION_PASSES; pass++){
17544
+ let corrected;
17545
+ try {
17546
+ corrected = await buildAutoAllocatedFromBalances(opts.params, opts.destChain, opts.recipientAddress, opts.useForwarder, opts.amountIn, opts.chainBalances, overrides);
17547
+ } catch (err) {
17548
+ // Re-allocating with the real gas reserved can't cover the amount →
17549
+ // surface a gas-specific error instead of the opaque API rejection.
17550
+ if (err instanceof KitError && err.code === BalanceError.INSUFFICIENT_TOKEN.code) {
17551
+ throw createAutoAllocationGasError(overdrawn, err);
17552
+ }
17553
+ throw err;
17554
+ }
17555
+ const { estimatedIntents, forwardingFee } = await validateAndEstimate(corrected.allocations, corrected.intents, opts.destChain, opts.useForwarder);
17556
+ const stillOverdrawn = findOverdrawnChains(estimatedIntents, corrected.allocations, opts.chainBalances);
17557
+ if (stillOverdrawn.length === 0) {
17558
+ return {
17559
+ allocations: corrected.allocations,
17560
+ estimatedIntents,
17561
+ forwardingFee
17562
+ };
17563
+ }
17564
+ overdrawn = stillOverdrawn;
17565
+ overrides = mergeGasFeeOverrides(overrides, deriveGasFeeOverrides(estimatedIntents, corrected.allocations));
17566
+ }
17567
+ throw createAutoAllocationGasError(overdrawn);
17568
+ }
17246
17569
  /**
17247
17570
  * Validate allocations, call the estimate API, and return estimated intents.
17248
17571
  *
@@ -17250,6 +17573,14 @@ const FORWARDER_POLL_TIMEOUT_MS = 300_000;
17250
17573
  * path. Handles forwarder route validation, network compatibility, and the
17251
17574
  * estimate API call.
17252
17575
  *
17576
+ * For auto-allocation (`amountIn`), the greedy allocator reserves a static
17577
+ * per-chain gas fee that can undershoot the API's real fee, draining a source
17578
+ * (typically the destination chain) below `value + maxFee` and triggering a
17579
+ * `BALANCE_INSUFFICIENT_TOKEN` rejection. When the first estimate reveals such
17580
+ * an over-draw, a bounded corrective re-allocation reserves the real gas and
17581
+ * re-estimates until it converges or fails fast (see {@link correctOverdraw}).
17582
+ * Explicit allocations are user-authoritative and never re-allocated.
17583
+ *
17253
17584
  * @param params - Spend parameters.
17254
17585
  * @param destChain - Resolved destination chain with Gateway v1 config.
17255
17586
  * @param recipientAddress - Resolved recipient address.
@@ -17259,15 +17590,24 @@ const FORWARDER_POLL_TIMEOUT_MS = 300_000;
17259
17590
  if (useForwarder) {
17260
17591
  assertForwarderRouteSupport(destChain);
17261
17592
  }
17262
- const { allocations, intents } = await resolveAllocationsAndIntents(params, destChain, recipientAddress, useForwarder);
17263
- assertNetworkCompatibility(allocations, destChain);
17264
- if (useForwarder) {
17265
- assertForwarderRouteSupport(destChain, allocations);
17593
+ const { allocations, intents, chainBalances } = await resolveAllocationsAndIntents(params, destChain, recipientAddress, useForwarder);
17594
+ const { estimatedIntents, forwardingFee } = await validateAndEstimate(allocations, intents, destChain, useForwarder);
17595
+ if (params.amountIn && chainBalances) {
17596
+ const overdrawn = findOverdrawnChains(estimatedIntents, allocations, chainBalances);
17597
+ if (overdrawn.length > 0) {
17598
+ return correctOverdraw({
17599
+ params,
17600
+ destChain,
17601
+ recipientAddress,
17602
+ useForwarder,
17603
+ amountIn: params.amountIn,
17604
+ chainBalances,
17605
+ estimatedIntents,
17606
+ allocations,
17607
+ overdrawn
17608
+ });
17609
+ }
17266
17610
  }
17267
- const apiBaseUrl = getGatewayApiBaseUrl(destChain.isTestnet);
17268
- const estimateBody = buildEstimateRequestBody(intents);
17269
- const { entries, forwardingFee } = await fetchEstimate(apiBaseUrl, estimateBody, useForwarder, allocations);
17270
- const estimatedIntents = parseEstimateResponse(entries, intents);
17271
17611
  return {
17272
17612
  allocations,
17273
17613
  estimatedIntents,
@@ -18175,8 +18515,10 @@ const assertCustomFeePolicySymbol = Symbol('assertCustomFeePolicy');
18175
18515
  *
18176
18516
  * - `computeFee` — required function that receives resolved spend params
18177
18517
  * and returns a fee as a string (or `Promise<string>`).
18178
- * - `resolveFeeRecipientAddress` — required function that returns a
18179
- * recipient address as a string (or `Promise<string>`).
18518
+ * - `resolveFeeRecipientAddress` — optional function that returns a
18519
+ * recipient address as a string (or `Promise<string>`). Omit it when
18520
+ * using `setFeeRecipients()`'s declarative map instead — a policy
18521
+ * with neither throws at spend time.
18180
18522
  *
18181
18523
  * @example
18182
18524
  * ```ts
@@ -18189,7 +18531,7 @@ const assertCustomFeePolicySymbol = Symbol('assertCustomFeePolicy');
18189
18531
  * ```
18190
18532
  */ const customFeePolicySchema = z.object({
18191
18533
  computeFee: z.function().returns(z.string().or(z.promise(z.string()))),
18192
- resolveFeeRecipientAddress: z.function().returns(z.string().or(z.promise(z.string())))
18534
+ resolveFeeRecipientAddress: z.function().returns(z.string().or(z.promise(z.string()))).optional()
18193
18535
  }).strict();
18194
18536
  /**
18195
18537
  * Assert that the provided value conforms to {@link CustomFeePolicy}.
@@ -18211,6 +18553,71 @@ const assertCustomFeePolicySymbol = Symbol('assertCustomFeePolicy');
18211
18553
  validateWithStateTracking(config, customFeePolicySchema, 'UnifiedBalanceKit custom fee policy', assertCustomFeePolicySymbol);
18212
18554
  }
18213
18555
 
18556
+ const assertFeeRecipientsConfigSymbol = Symbol('assertFeeRecipientsConfig');
18557
+ /**
18558
+ * Schema for validating {@link FeeRecipientsConfig}.
18559
+ *
18560
+ * Requires at least one of `evm`/`solana`, non-empty string values for
18561
+ * whichever keys are present, and — mirroring the `depositAccount`
18562
+ * validation in `deposit/validate/assertions` — an address format that
18563
+ * matches the given chain type (EVM hex vs Solana base58).
18564
+ *
18565
+ * @example
18566
+ * ```ts
18567
+ * const config = {
18568
+ * evm: '0x1234567890123456789012345678901234567890',
18569
+ * solana: '9WzDXwBbmkg8ZTbNMqUxvQRAyrZzDsGYdLVL9zYtAWWM',
18570
+ * }
18571
+ * const result = feeRecipientsConfigSchema.safeParse(config)
18572
+ * // result.success === true
18573
+ * ```
18574
+ */ const feeRecipientsConfigSchema = z.object({
18575
+ evm: z.string().min(1, 'Fee recipient address is required.').optional(),
18576
+ solana: z.string().min(1, 'Fee recipient address is required.').optional()
18577
+ }).strict().refine((config)=>Object.keys(config).length > 0, {
18578
+ message: 'At least one fee recipient (evm or solana) is required.'
18579
+ }).superRefine((config, ctx)=>{
18580
+ for (const type of Object.keys(config)){
18581
+ const address = config[type];
18582
+ if (address == null) continue;
18583
+ // `{ name: type, type }` is a placeholder chain identifier — only
18584
+ // `.type` is checked by these two helpers today, `.name` is unused.
18585
+ // No real ChainDefinition exists here, since validation runs before
18586
+ // a destination chain is resolved.
18587
+ if (!isValidAddressForChain(address, {
18588
+ name: type,
18589
+ type
18590
+ })) {
18591
+ const { expectedAddressFormat } = extractChainInfo({
18592
+ name: type,
18593
+ type
18594
+ });
18595
+ ctx.addIssue({
18596
+ code: z.ZodIssueCode.custom,
18597
+ path: [
18598
+ type
18599
+ ],
18600
+ message: `Invalid ${type} address "${address}". Expected ${expectedAddressFormat}.`
18601
+ });
18602
+ }
18603
+ }
18604
+ });
18605
+ /**
18606
+ * Assert that the provided value conforms to {@link FeeRecipientsConfig}.
18607
+ *
18608
+ * Throws a validation error with annotated paths if the configuration is
18609
+ * malformed.
18610
+ *
18611
+ * @param config - The fee recipients map to validate.
18612
+ *
18613
+ * @example
18614
+ * ```ts
18615
+ * assertFeeRecipientsConfig({ evm: '0x1234567890123456789012345678901234567890' })
18616
+ * ```
18617
+ */ function assertFeeRecipientsConfig(config) {
18618
+ validateWithStateTracking(config, feeRecipientsConfigSchema, 'UnifiedBalanceKit fee recipients config', assertFeeRecipientsConfigSymbol);
18619
+ }
18620
+
18214
18621
  function sameChain(a, b) {
18215
18622
  return a.chain !== undefined && a.chain === b.chain;
18216
18623
  }
@@ -19191,6 +19598,105 @@ function assertSourceAccountAddresses(from) {
19191
19598
  config: params.config
19192
19599
  };
19193
19600
  }
19601
+ /**
19602
+ * Tracks, per {@link CustomFeePolicy} instance, which chain types have
19603
+ * already triggered the "falling back to resolveFeeRecipientAddress"
19604
+ * warning, so repeated `spend()`/`estimateSpend()` calls (e.g. live
19605
+ * quoting) warn once per (policy, chain type) pair rather than on every
19606
+ * call.
19607
+ */ const warnedFeeRecipientFallbacks = new WeakMap();
19608
+ /**
19609
+ * Invoke `resolveFeeRecipientAddress` and validate its return value has a
19610
+ * plausible address format for `destChain`, the same check
19611
+ * `setFeeRecipients()` already applies at config time. Unlike the map,
19612
+ * the callback's return value can't be validated ahead of time, so it's
19613
+ * checked here instead — a malformed value throws immediately rather
19614
+ * than silently becoming the fee recipient.
19615
+ *
19616
+ * @internal
19617
+ */ async function resolveFeeRecipientFromCallback(callback, destChain, params) {
19618
+ const address = await callback(destChain, params);
19619
+ if (!isValidAddressForChain(address, destChain)) {
19620
+ throw new KitError({
19621
+ ...InputError.VALIDATION_FAILED,
19622
+ recoverability: 'FATAL',
19623
+ message: `resolveFeeRecipientAddress returned an invalid address ` + `"${address}" for chain type "${destChain.type}" ` + `(resolved destination: ${destChain.name}).`
19624
+ });
19625
+ }
19626
+ return address;
19627
+ }
19628
+ /**
19629
+ * Resolve the single fee recipient address for a spend.
19630
+ *
19631
+ * Every fee burn intent in a spend mints to the same destination
19632
+ * chain regardless of which source chain(s) funded it, so exactly one
19633
+ * recipient address — valid on `destChain` — is ever needed.
19634
+ *
19635
+ * `feeRecipients` (set via `setFeeRecipients`) takes priority over the
19636
+ * policy's `resolveFeeRecipientAddress` callback for any chain type it
19637
+ * has an entry for, since it's a direct lookup and doesn't require
19638
+ * invoking developer code. For a chain type `feeRecipients` doesn't
19639
+ * cover, it falls back to `resolveFeeRecipientAddress` if one is
19640
+ * configured — a warning is logged once per (policy, chain type) pair
19641
+ * so the fallback isn't a silent surprise, without spamming repeated
19642
+ * `estimateSpend()` calls used for live quoting. Throws if neither
19643
+ * resolves `destChain`'s type, or if `resolveFeeRecipientAddress`
19644
+ * resolves it to a malformed address (see
19645
+ * {@link resolveFeeRecipientFromCallback}).
19646
+ *
19647
+ * @internal
19648
+ */ async function resolveFeeRecipient(destChain, policy, feeRecipients, params) {
19649
+ if (feeRecipients) {
19650
+ // `destChain.type` is `@core/chains`' broader `ChainType` union;
19651
+ // `FeeRecipientChainType` is the narrower subset this map supports
19652
+ // today. A type not present as a key simply has no configured
19653
+ // recipient, which is handled below.
19654
+ const type = destChain.type;
19655
+ const recipientAddress = feeRecipients[type];
19656
+ if (recipientAddress) {
19657
+ return recipientAddress;
19658
+ }
19659
+ if (policy.resolveFeeRecipientAddress) {
19660
+ const warnedTypes = warnedFeeRecipientFallbacks.get(policy);
19661
+ if (!warnedTypes?.has(type)) {
19662
+ warnedFeeRecipientFallbacks.set(policy, (warnedTypes ?? new Set()).add(type));
19663
+ console.warn(`setFeeRecipients() is configured but has no entry for chain ` + `type "${type}" — falling back to customFeePolicy.` + `resolveFeeRecipientAddress for this chain type. Add a ` + `"${type}" entry to setFeeRecipients() to avoid this fallback.`);
19664
+ }
19665
+ return resolveFeeRecipientFromCallback(policy.resolveFeeRecipientAddress, destChain, params);
19666
+ }
19667
+ throw new KitError({
19668
+ ...InputError.VALIDATION_FAILED,
19669
+ recoverability: 'FATAL',
19670
+ message: `No fee recipient configured for chain type "${type}" ` + `(resolved destination: ${destChain.name}). Call setFeeRecipients() ` + `with an entry for "${type}", or provide resolveFeeRecipientAddress ` + `on the custom fee policy.`
19671
+ });
19672
+ }
19673
+ if (!policy.resolveFeeRecipientAddress) {
19674
+ throw new KitError({
19675
+ ...InputError.VALIDATION_FAILED,
19676
+ recoverability: 'FATAL',
19677
+ message: 'No fee recipient configured — call setFeeRecipients() or provide ' + 'resolveFeeRecipientAddress on the custom fee policy.'
19678
+ });
19679
+ }
19680
+ return resolveFeeRecipientFromCallback(policy.resolveFeeRecipientAddress, destChain, params);
19681
+ }
19682
+ /**
19683
+ * Guard against a common misconfiguration: a developer sets the
19684
+ * declarative `feeRecipients` map expecting it alone to drive fee
19685
+ * collection, but no fee is ever charged without a `computeFee` from
19686
+ * `customFeePolicy` to determine the amount. Without this check that
19687
+ * misconfiguration fails silently — no fee is charged and no error is
19688
+ * raised.
19689
+ *
19690
+ * @internal
19691
+ */ function assertFeeRecipientsHasPolicy(feeRecipients) {
19692
+ if (feeRecipients) {
19693
+ throw new KitError({
19694
+ ...InputError.VALIDATION_FAILED,
19695
+ recoverability: 'FATAL',
19696
+ message: 'setFeeRecipients() is configured but no developer fee will be ' + 'charged: setCustomFeePolicy() must also be set to provide ' + 'computeFee, which determines the fee amount. Call ' + 'setCustomFeePolicy(), or remove setFeeRecipients() if no ' + 'developer fee is intended.'
19697
+ });
19698
+ }
19699
+ }
19194
19700
  /**
19195
19701
  * Apply a {@link CustomFeePolicy} to an adapter-only spend.
19196
19702
  *
@@ -19199,15 +19705,20 @@ function assertSourceAccountAddresses(from) {
19199
19705
  * `config.customFee` so the provider sees it.
19200
19706
  *
19201
19707
  * @internal
19202
- */ async function mergeCustomFeePolicyForAdapterOnly(params, policy) {
19203
- if (params.config?.customFee || !policy) {
19708
+ */ async function mergeCustomFeePolicyForAdapterOnly(params, policy, feeRecipients) {
19709
+ if (params.config?.customFee) {
19710
+ return params;
19711
+ }
19712
+ if (!policy) {
19713
+ assertFeeRecipientsHasPolicy(feeRecipients);
19204
19714
  return params;
19205
19715
  }
19206
19716
  const destChain = resolveChainIdentifier(params.to.chain);
19207
- const [feeValue, recipientAddress] = await Promise.all([
19208
- policy.computeFee(params),
19209
- policy.resolveFeeRecipientAddress(destChain, params)
19210
- ]);
19717
+ // Resolve the recipient before computing the fee: a KitError here
19718
+ // (missing/unresolvable recipient) shouldn't be preceded by an
19719
+ // otherwise-wasted computeFee call, which may be a network request.
19720
+ const recipientAddress = await resolveFeeRecipient(destChain, policy, feeRecipients, params);
19721
+ const feeValue = await policy.computeFee(params);
19211
19722
  return {
19212
19723
  ...params,
19213
19724
  config: {
@@ -19237,18 +19748,35 @@ function assertSourceAccountAddresses(from) {
19237
19748
  });
19238
19749
  }
19239
19750
  }
19240
- async function mergeCustomFeeConfig(resolved, policy) {
19241
- if (resolved.config?.customFee || !policy) {
19751
+ async function mergeCustomFeeConfig(resolved, policy, feeRecipients) {
19752
+ if (resolved.config?.customFee) {
19242
19753
  return resolved;
19243
19754
  }
19244
- const firstSourceChain = resolved.from[0]?.allocations[0]?.chain;
19245
- if (!firstSourceChain) {
19755
+ if (!policy) {
19756
+ assertFeeRecipientsHasPolicy(feeRecipients);
19246
19757
  return resolved;
19247
19758
  }
19248
- const [feeValue, recipientAddress] = await Promise.all([
19249
- policy.computeFee(resolved),
19250
- policy.resolveFeeRecipientAddress(firstSourceChain, resolved)
19251
- ]);
19759
+ // Skip fee resolution when there's no source chain to spend from at
19760
+ // all. This state can't arise from validated input today — the
19761
+ // caller re-checks and throws "No source chain found" right after
19762
+ // this returns — but skipping here isn't dead code: verified that
19763
+ // removing it lets a degenerate zero-allocation resolved value reach
19764
+ // computeFee/assertDeveloperFeeWithinBounds first, which throws a
19765
+ // misleading "Developer fee must be less than the total spend
19766
+ // amount" (0 >= 0 total allocation) instead of the correct "No
19767
+ // source chain found" error — or, for a real developer computeFee
19768
+ // that assumes a non-empty allocation, an uncaught raw exception
19769
+ // instead of any KitError at all. This guard exists to guarantee the
19770
+ // caller's clear error is what actually surfaces, not for
19771
+ // correctness.
19772
+ if (collectSourceChains(resolved).length === 0) {
19773
+ return resolved;
19774
+ }
19775
+ // Resolve the recipient before computing the fee: a KitError here
19776
+ // (missing/unresolvable recipient) shouldn't be preceded by an
19777
+ // otherwise-wasted computeFee call, which may be a network request.
19778
+ const recipientAddress = await resolveFeeRecipient(resolved.to.chain, policy, feeRecipients, resolved);
19779
+ const feeValue = await policy.computeFee(resolved);
19252
19780
  return {
19253
19781
  ...resolved,
19254
19782
  config: {
@@ -19304,14 +19832,14 @@ async function mergeCustomFeeConfig(resolved, policy) {
19304
19832
  }
19305
19833
  const destChain = resolveChainIdentifier(params.to.chain);
19306
19834
  if (!hasExplicitAllocations(params.from)) {
19307
- const merged = await mergeCustomFeePolicyForAdapterOnly(params, context.customFeePolicy);
19835
+ const merged = await mergeCustomFeePolicyForAdapterOnly(params, context.customFeePolicy, context.feeRecipients);
19308
19836
  assertDeveloperFeeWithinAmount(merged);
19309
19837
  const provider = findProviderForChain(context, normalizeToken(merged.token), destChain);
19310
19838
  return callSpend(provider, toProviderAdapterOnlyParams(merged));
19311
19839
  }
19312
19840
  const resolved = await resolveSpendParams(params);
19313
19841
  assertSpendNetworkCompatibility(resolved);
19314
- const withFee = await mergeCustomFeeConfig(resolved, context.customFeePolicy);
19842
+ const withFee = await mergeCustomFeeConfig(resolved, context.customFeePolicy, context.feeRecipients);
19315
19843
  assertDeveloperFeeWithinBounds(withFee);
19316
19844
  const sourceChains = collectSourceChains(withFee);
19317
19845
  if (sourceChains.length === 0) {
@@ -19351,14 +19879,14 @@ async function mergeCustomFeeConfig(resolved, policy) {
19351
19879
  assertSpendParams(params);
19352
19880
  const destChain = resolveChainIdentifier(params.to.chain);
19353
19881
  if (!hasExplicitAllocations(params.from)) {
19354
- const merged = await mergeCustomFeePolicyForAdapterOnly(params, context.customFeePolicy);
19882
+ const merged = await mergeCustomFeePolicyForAdapterOnly(params, context.customFeePolicy, context.feeRecipients);
19355
19883
  assertDeveloperFeeWithinAmount(merged);
19356
19884
  const provider = findProviderForChain(context, normalizeToken(merged.token), destChain);
19357
19885
  return provider.estimateSpend(toProviderAdapterOnlyParams(merged));
19358
19886
  }
19359
19887
  const resolved = await resolveSpendParams(params);
19360
19888
  assertSpendNetworkCompatibility(resolved);
19361
- const withFee = await mergeCustomFeeConfig(resolved, context.customFeePolicy);
19889
+ const withFee = await mergeCustomFeeConfig(resolved, context.customFeePolicy, context.feeRecipients);
19362
19890
  assertDeveloperFeeWithinBounds(withFee);
19363
19891
  const sourceChains = collectSourceChains(withFee);
19364
19892
  if (sourceChains.length === 0) {
@@ -20333,6 +20861,46 @@ const removeFundParamsSchema = z.object({
20333
20861
  */ removeCustomFeePolicy() {
20334
20862
  delete this.context.customFeePolicy;
20335
20863
  }
20864
+ /**
20865
+ * Set a declarative fee recipient map, keyed by chain type. Once set,
20866
+ * `spend()`/`estimateSpend()` resolve the fee recipient by looking up
20867
+ * the spend's destination chain type in this map — taking priority
20868
+ * over `customFeePolicy`'s `resolveFeeRecipientAddress` callback.
20869
+ *
20870
+ * @remarks
20871
+ * This only controls which address a fee is sent to — it does not by
20872
+ * itself cause any fee to be charged. You still need
20873
+ * {@link UnifiedBalanceKit.setCustomFeePolicy}'s `computeFee` to
20874
+ * determine the fee amount; calling `setFeeRecipients` without ever
20875
+ * calling `setCustomFeePolicy` throws at spend time (there is no
20876
+ * `computeFee` to determine an amount).
20877
+ *
20878
+ * @param config - Fee recipient addresses keyed by chain type (e.g.
20879
+ * `{ evm: '0x...', solana: 'Sol...' }`). Provide entries for every
20880
+ * chain type you expect to spend to; spending to a chain type with
20881
+ * no matching entry throws before any fee collection is attempted.
20882
+ *
20883
+ * @example
20884
+ * ```typescript
20885
+ * kit.setFeeRecipients({
20886
+ * evm: '0x1234567890123456789012345678901234567890',
20887
+ * solana: '9WzDXwBbmkg8ZTbNMqUxvQRAyrZzDsGYdLVL9zYtAWWM',
20888
+ * })
20889
+ * ```
20890
+ */ setFeeRecipients(config) {
20891
+ assertFeeRecipientsConfig(config);
20892
+ this.context.feeRecipients = config;
20893
+ }
20894
+ /**
20895
+ * Remove the declarative fee recipient map for the kit.
20896
+ *
20897
+ * @example
20898
+ * ```typescript
20899
+ * kit.removeFeeRecipients()
20900
+ * ```
20901
+ */ removeFeeRecipients() {
20902
+ delete this.context.feeRecipients;
20903
+ }
20336
20904
  }
20337
20905
 
20338
20906
  // Auto-register this kit for user agent tracking
@@ -20659,6 +21227,45 @@ registerKit(`${pkg.name}/${pkg.version}`);
20659
21227
  */ removeCustomFeePolicy() {
20660
21228
  this.kit.removeCustomFeePolicy();
20661
21229
  }
21230
+ /**
21231
+ * Set a declarative fee recipient map, keyed by chain type.
21232
+ *
21233
+ * Once set, `spend()`/`estimateSpend()` resolve the fee recipient by
21234
+ * looking up the spend's destination chain type in this map — taking
21235
+ * priority over `customFeePolicy`'s `resolveFeeRecipientAddress`
21236
+ * callback.
21237
+ *
21238
+ * @remarks
21239
+ * This only controls which address a fee is sent to — it does not by
21240
+ * itself cause any fee to be charged. You still need
21241
+ * `setCustomFeePolicy`'s `computeFee` to determine the fee amount;
21242
+ * calling `setFeeRecipients` without ever calling `setCustomFeePolicy`
21243
+ * throws at spend time (there is no `computeFee` to determine an
21244
+ * amount).
21245
+ *
21246
+ * @param config - Fee recipient addresses keyed by chain type (e.g.
21247
+ * `{ evm: '0x...', solana: 'Sol...' }`).
21248
+ *
21249
+ * @example
21250
+ * ```typescript
21251
+ * kit.unifiedBalance.setFeeRecipients({
21252
+ * evm: '0x1234567890123456789012345678901234567890',
21253
+ * solana: '9WzDXwBbmkg8ZTbNMqUxvQRAyrZzDsGYdLVL9zYtAWWM',
21254
+ * })
21255
+ * ```
21256
+ */ setFeeRecipients(config) {
21257
+ this.kit.setFeeRecipients(config);
21258
+ }
21259
+ /**
21260
+ * Remove the declarative fee recipient map.
21261
+ *
21262
+ * @example
21263
+ * ```typescript
21264
+ * kit.unifiedBalance.removeFeeRecipients()
21265
+ * ```
21266
+ */ removeFeeRecipients() {
21267
+ this.kit.removeFeeRecipients();
21268
+ }
20662
21269
  }
20663
21270
 
20664
21271
  export { AppKitUnifiedBalance };