@xoxno/sdk-js 1.0.230 → 1.0.232

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.
@@ -2,10 +2,9 @@
2
2
  * Decoders for the Stellar lending `#[contractevent]`s (controller, pool, and
3
3
  * the price-aggregator's `config:asset_oracle`).
4
4
  *
5
- * Decodes core protocol topics plus pool `strategy:fee` and live controller
6
- * config topics used by the indexer. Intentional omissions still return
7
- * `null`: `config:spoke_asset` / `config:remove_spoke_asset` / `config:hub` /
8
- * `strategy:blend_migration` (handled by az-functions own decoders).
5
+ * Decodes every topic the controller, pool, price aggregator, governance and
6
+ * DeFindex strategy contracts emit, plus legacy keys for historical ledgers.
7
+ * Unknown topics (for example access-control events) return `null`.
9
8
  *
10
9
  * The public API takes base64-XDR strings (`decodeStellarLendingEvent(topicsB64,
11
10
  * dataB64)`) and parses them with this SDK's bundled `@stellar/stellar-sdk`, so
@@ -37,8 +36,7 @@ export declare const toBase64Xdr: (scv: {
37
36
  }) => string;
38
37
  /** Build the `"<domain>:<action>"` dispatch key from the event topic ScVals. */
39
38
  export declare const stellarLendingDispatchKey: (topicsB64: readonly string[]) => string;
40
- /** Topic keys this SDK can decode. See the module header for the intentional
41
- * omissions. */
39
+ /** Topic keys this SDK can decode. */
42
40
  export declare const STELLAR_LENDING_TOPICS: readonly string[];
43
41
  /**
44
42
  * Decode a Stellar lending controller event from its base64-XDR topics and
@@ -535,11 +535,11 @@ export type StellarMarketIndexByHub = {
535
535
  export type ReserveIrmCurveDto = {
536
536
  /** Base borrow rate (ray, 1e27). */
537
537
  "baseRateRay": string;
538
- /** Slope below the optimal utilization (ray). */
538
+ /** Rate added from zero to the mid utilization kink (ray). */
539
539
  "slope1Ray": string;
540
- /** Slope between optimal and mid utilization (ray). */
540
+ /** Rate added between the mid and optimal utilization kinks (ray). */
541
541
  "slope2Ray": string;
542
- /** Slope above the mid utilization (ray). */
542
+ /** Rate added from the optimal kink to full utilization (ray). */
543
543
  "slope3Ray": string;
544
544
  /** Optimal utilization kink (ray). */
545
545
  "optimalUtilizationRay": string;
@@ -37,11 +37,11 @@ export type StellarUserActivityItem = Api.StellarUserActivityItemDto;
37
37
  export type StellarLendingMarketSide = 'deposit' | 'borrow';
38
38
  /** Holder-list balance selector. Also used by holder distribution. */
39
39
  export type StellarLendingHoldersSide = 'deposits' | 'borrows';
40
- /** Time window for API charts. Dates are ISO-8601; bin examples: 1h, 1d. */
40
+ /** Time window for API charts. Dates are calendar days (yyyy-MM-dd, e.g. 2026-09-01); the API rejects ISO-8601 datetimes with HTTP 400. Bin examples: 1h, 1d. */
41
41
  export interface StellarMarketGraphQuery {
42
- /** Window start. */
42
+ /** Window start day, yyyy-MM-dd. */
43
43
  from: string;
44
- /** Window end. */
44
+ /** Window end day, yyyy-MM-dd. */
45
45
  to: string;
46
46
  /** Kusto timespan, for example 1h or 1d. */
47
47
  bin: string;
@@ -51,11 +51,16 @@ export type StellarActivityQuery = (typeof endpoints)['/stellar-lending/users/:o
51
51
  export type StellarActivityPageQuery = (typeof endpoints)['/stellar-lending/users/:owner/activity/page']['input'];
52
52
  export type StellarGovernanceQuery = (typeof endpoints)['/stellar-lending/governance/proposals']['input'];
53
53
  export type StellarPositionsQuery = (typeof endpoints)['/stellar-lending/positions']['input'];
54
+ /** from/to are calendar days (yyyy-MM-dd); the API rejects ISO-8601 datetimes with HTTP 400. */
54
55
  export type StellarRevenueQuery = (typeof endpoints)['/stellar-lending/revenue']['input'];
56
+ /** from/to are calendar days (yyyy-MM-dd); the API rejects ISO-8601 datetimes with HTTP 400. */
55
57
  export type StellarFeeRevenueQuery = (typeof endpoints)['/stellar-lending/revenue/fees']['input'];
56
58
  export type StellarParticipantsQuery = (typeof endpoints)['/stellar-lending/participants']['input'];
59
+ /** from/to are calendar days (yyyy-MM-dd); the API rejects ISO-8601 datetimes with HTTP 400. */
57
60
  export type StellarLiquidationsQuery = (typeof endpoints)['/stellar-lending/liquidations']['input'];
58
61
  export type StellarLiquidationsLeaderboardQuery = (typeof endpoints)['/stellar-lending/liquidations/leaderboard']['input'];
62
+ /** from/to are calendar days (yyyy-MM-dd); the API rejects ISO-8601 datetimes with HTTP 400. */
59
63
  export type StellarVolumeQuery = (typeof endpoints)['/stellar-lending/volume']['input'];
60
64
  export type StellarDistributionQuery = (typeof endpoints)['/stellar-lending/distribution']['input'];
65
+ /** from/to are calendar days (yyyy-MM-dd); the API rejects ISO-8601 datetimes with HTTP 400. */
61
66
  export type StellarRateSpreadQuery = (typeof endpoints)['/stellar-lending/rate-spread']['input'];
@@ -1,8 +1,9 @@
1
1
  /** Typed Stellar lending reads. Configure the API deployment with XOXNOClient.apiUrl.
2
2
  * These cached HTTP reads do not simulate transactions or authorize spending.
3
3
  */
4
- import type { OurRequestInit, XOXNOClient } from '../../utils/api.js';
4
+ import { type OurRequestInit, type XOXNOClient } from '../../utils/api.js';
5
5
  import type * as Api from './lending-api-types.js';
6
+ import type { UserTokenInventoryResponseDto } from '@xoxno/types';
6
7
  import type { StellarLendingMarketSide, StellarLendingHoldersSide, StellarMarketGraphQuery, StellarReservesParams, StellarActivityQuery, StellarActivityPageQuery, StellarGovernanceQuery, StellarPositionsQuery, StellarRevenueQuery, StellarFeeRevenueQuery, StellarParticipantsQuery, StellarLiquidationsQuery, StellarLiquidationsLeaderboardQuery, StellarVolumeQuery, StellarDistributionQuery, StellarRateSpreadQuery } from './lending-read-types.js';
7
8
  /** Load assets, hubs, spokes, reserves and keyed reserve details in one response.
8
9
  * @category Stellar lending reads
@@ -56,7 +57,18 @@ export declare const getStellarUserPositions: (client: XOXNOClient, owner: strin
56
57
  * @category Stellar lending reads
57
58
  */
58
59
  export declare const getStellarAccountPositions: (client: XOXNOClient, accountId: string, init?: OurRequestInit) => Promise<Api.AccountPositionsDto>;
59
- /** Read exact token base-unit holdings for a G or C wallet. Holdings may exceed spendable funds. Requires the API release exposing this route; older deployments return 404.
60
+ /** Read a G or C wallet's token holdings from `/user/{owner}/token-inventory`:
61
+ * Horizon balances (native XLM and classic assets, keyed by their SAC id) plus
62
+ * indexed Soroban tokens, cached for about 10 seconds. Each token's
63
+ * `identifier` is its contract id and `balance` is base units as a decimal
64
+ * string. Tokens without a balance or trustline are absent.
65
+ * @category Stellar lending reads
66
+ */
67
+ export declare const getStellarWalletTokens: (client: XOXNOClient, owner: string, init?: OurRequestInit) => Promise<UserTokenInventoryResponseDto>;
68
+ /** Read exact token base-unit holdings of one asset for a G or C wallet, from
69
+ * {@link getStellarWalletTokens}. An asset the wallet does not hold returns
70
+ * `amount: '0'`, with decimals read from the lending asset. Holdings may exceed
71
+ * spendable funds (reserves, liabilities).
60
72
  * @category Stellar lending reads
61
73
  */
62
74
  export declare const getStellarWalletBalance: (client: XOXNOClient, owner: string, asset: string, init?: OurRequestInit) => Promise<Api.StellarWalletBalanceDto>;
@@ -218,6 +230,8 @@ export declare const stellarLendingRead: (client: XOXNOClient) => {
218
230
  userPositions: (owner: string, init?: OurRequestInit) => Promise<Api.AccountPositionsDto>;
219
231
  /** {@inheritDoc getStellarAccountPositions} */
220
232
  accountPositions: (accountId: string, init?: OurRequestInit) => Promise<Api.AccountPositionsDto>;
233
+ /** {@inheritDoc getStellarWalletTokens} */
234
+ walletTokens: (owner: string, init?: OurRequestInit) => Promise<UserTokenInventoryResponseDto>;
221
235
  /** {@inheritDoc getStellarWalletBalance} */
222
236
  walletBalance: (owner: string, asset: string, init?: OurRequestInit) => Promise<Api.StellarWalletBalanceDto>;
223
237
  /** {@inheritDoc getStellarAssetPage} */
@@ -2,7 +2,7 @@ import type { LendingAsset, LendingAssetUsage, LendingPosition } from './types.j
2
2
  export interface StellarLendingReadClientOptions {
3
3
  /** API root, for example https://api.xoxno.com. May include a deployment prefix. */
4
4
  baseUrl: string;
5
- /** Inject the host's fetch implementation when unavailable globally; it must honor redirect: 'error'. */
5
+ /** Inject the host's fetch implementation when unavailable globally; it must honor redirect: 'manual'. */
6
6
  fetch?: typeof globalThis.fetch;
7
7
  }
8
8
  export interface StellarLendingReadOptions {
@@ -7,8 +7,9 @@ export interface MapSorobanErrorOptions {
7
7
  *
8
8
  * RPC simulation errors usually carry only the top-level code
9
9
  * (`HostError: Error(Contract, #N)`). With only the tag, a code raised by a
10
- * nested contract (for example the swap aggregator) cannot be told apart;
11
- * read the simulation's diagnostic events when that matters.
10
+ * nested contract (for example the swap aggregator or a token) cannot be told
11
+ * apart; read the simulation's diagnostic events when that matters.
12
+ * Tag-only matches on codes 1, 3, 6, 10, 11 and 12 return `null`.
12
13
  */
13
14
  contracts?: readonly string[];
14
15
  }
@@ -19,7 +20,11 @@ export interface MapSorobanErrorOptions {
19
20
  * strings can contain unrelated numbers (fees, ledgers, sequence numbers).
20
21
  *
21
22
  * Without `opts.contracts` it maps the first code in the string, whichever
22
- * contract emitted it.
23
+ * contract emitted it. A failed nested call (token, swap aggregator, oracle)
24
+ * reports its own code at the top level, so an unscoped or tag-only result can
25
+ * name the wrong lending error: a token's `#10` BalanceError reads as
26
+ * InvalidWasmHash. Pass `contracts` and, where available, the simulation's
27
+ * diagnostic event log to attribute the code to its emitter.
23
28
  */
24
29
  export declare function mapSorobanError(raw: string, opts?: MapSorobanErrorOptions): {
25
30
  code: number;
@@ -2,10 +2,9 @@
2
2
  * Decoders for the Stellar lending `#[contractevent]`s (controller, pool, and
3
3
  * the price-aggregator's `config:asset_oracle`).
4
4
  *
5
- * Decodes core protocol topics plus pool `strategy:fee` and live controller
6
- * config topics used by the indexer. Intentional omissions still return
7
- * `null`: `config:spoke_asset` / `config:remove_spoke_asset` / `config:hub` /
8
- * `strategy:blend_migration` (handled by az-functions own decoders).
5
+ * Decodes every topic the controller, pool, price aggregator, governance and
6
+ * DeFindex strategy contracts emit, plus legacy keys for historical ledgers.
7
+ * Unknown topics (for example access-control events) return `null`.
9
8
  *
10
9
  * The public API takes base64-XDR strings (`decodeStellarLendingEvent(topicsB64,
11
10
  * dataB64)`) and parses them with this SDK's bundled `@stellar/stellar-sdk`, so
@@ -196,12 +195,11 @@ const REGISTRY = {
196
195
  marketAddress: str(d.market_address)
197
196
  }
198
197
  }),
199
- // `hub_id` is required to tell same-token markets apart; the upstream DTO
200
- // does not declare it yet, hence the cast.
198
+ // `hub_id` tells same-token markets apart; absent on pre-hub ledgers.
201
199
  'market:params_update': (d)=>({
202
200
  topic: 'market:params_update',
203
201
  data: {
204
- hubId: num(d.hub_id),
202
+ hubId: d.hub_id === undefined ? undefined : num(d.hub_id),
205
203
  asset: str(d.asset),
206
204
  maxBorrowRateRay: dec(d.max_borrow_rate),
207
205
  baseBorrowRateRay: dec(d.base_borrow_rate),
@@ -307,13 +305,14 @@ const REGISTRY = {
307
305
  totalCollateralUsdWad: dec(d.total_collateral_usd_wad)
308
306
  }
309
307
  }),
310
- // The event carries `{ token, amount, account_id }` only. The upstream DTO
311
- // still declares `usdValueWad`; it is left absent rather than fabricated.
308
+ // Current contracts emit `{ token, amount, account_id }`; `usd_value_wad`
309
+ // exists only on older ledgers and is never fabricated.
312
310
  'strategy:initial_payment': (d)=>({
313
311
  topic: 'strategy:initial_payment',
314
312
  data: {
315
313
  token: str(d.token),
316
314
  amount: dec(d.amount),
315
+ usdValueWad: d.usd_value_wad === undefined ? undefined : dec(d.usd_value_wad),
317
316
  accountId: dec(d.account_id)
318
317
  }
319
318
  }),
@@ -402,10 +401,112 @@ const REGISTRY = {
402
401
  liquidationBonusFactorBps: num(s.liquidation_bonus_factor_bps)
403
402
  }
404
403
  };
405
- }
404
+ },
405
+ 'position:flash_position': (d)=>({
406
+ topic: 'position:flash_position',
407
+ data: {
408
+ accountId: dec(d.account_id),
409
+ hubId: num(d.hub_id),
410
+ asset: str(d.asset),
411
+ receiver: str(d.receiver),
412
+ caller: str(d.caller),
413
+ amount: dec(d.amount),
414
+ amountReceived: dec(d.amount_received),
415
+ fee: dec(d.fee)
416
+ }
417
+ }),
418
+ 'revenue:claim': (d)=>({
419
+ topic: 'revenue:claim',
420
+ data: {
421
+ hubId: num(d.hub_id),
422
+ asset: str(d.asset),
423
+ caller: str(d.caller),
424
+ accumulator: str(d.accumulator),
425
+ amount: dec(d.amount)
426
+ }
427
+ }),
428
+ 'account:delegate': (d)=>({
429
+ topic: 'account:delegate',
430
+ data: {
431
+ accountId: dec(d.account_id),
432
+ owner: str(d.owner),
433
+ delegate: str(d.delegate),
434
+ granted: Boolean(d.granted)
435
+ }
436
+ }),
437
+ 'config:hub': (d)=>({
438
+ topic: 'config:hub',
439
+ data: {
440
+ hubId: num(d.hub_id)
441
+ }
442
+ }),
443
+ 'config:spoke_asset': (d)=>{
444
+ const c = d.config;
445
+ return {
446
+ topic: 'config:spoke_asset',
447
+ data: {
448
+ spokeId: num(d.spoke_id),
449
+ hubId: num(d.hub_id),
450
+ asset: str(d.asset),
451
+ config: {
452
+ isCollateralizable: Boolean(c.is_collateralizable),
453
+ isBorrowable: Boolean(c.is_borrowable),
454
+ paused: Boolean(c.paused),
455
+ frozen: Boolean(c.frozen),
456
+ noSeize: Boolean(c.no_seize),
457
+ loanToValueBps: num(c.loan_to_value),
458
+ liquidationThresholdBps: num(c.liquidation_threshold),
459
+ liquidationBonusBps: num(c.liquidation_bonus),
460
+ liquidationFeesBps: num(c.liquidation_fees),
461
+ supplyCap: dec(c.supply_cap),
462
+ borrowCap: dec(c.borrow_cap)
463
+ }
464
+ }
465
+ };
466
+ },
467
+ 'config:remove_spoke_asset': (d)=>({
468
+ topic: 'config:remove_spoke_asset',
469
+ data: {
470
+ spokeId: num(d.spoke_id),
471
+ hubId: num(d.hub_id),
472
+ asset: str(d.asset)
473
+ }
474
+ }),
475
+ 'strategy:blend_migration': (d)=>({
476
+ topic: 'strategy:blend_migration',
477
+ data: {
478
+ accountId: dec(d.account_id),
479
+ blendPool: str(d.blend_pool),
480
+ collateralCount: num(d.collateral_count),
481
+ supplyCount: num(d.supply_count),
482
+ debtCount: num(d.debt_count)
483
+ }
484
+ }),
485
+ // Emitted by the DeFindex strategy adapter, not the controller.
486
+ 'strategy:harvest': (d)=>({
487
+ topic: 'strategy:harvest',
488
+ data: {
489
+ from: str(d.from),
490
+ amount: dec(d.amount),
491
+ pricePerShare: dec(d.price_per_share)
492
+ }
493
+ }),
494
+ 'governance:deploy_controller': (d)=>({
495
+ topic: 'governance:deploy_controller',
496
+ data: {
497
+ controller: str(d.controller),
498
+ wasmHash: hex(d.wasm_hash)
499
+ }
500
+ }),
501
+ 'governance:deploy_price_aggregator': (d)=>({
502
+ topic: 'governance:deploy_price_aggregator',
503
+ data: {
504
+ priceAggregator: str(d.price_aggregator),
505
+ wasmHash: hex(d.wasm_hash)
506
+ }
507
+ })
406
508
  };
407
- /** Topic keys this SDK can decode. See the module header for the intentional
408
- * omissions. */ export const STELLAR_LENDING_TOPICS = Object.freeze(Object.keys(REGISTRY));
509
+ /** Topic keys this SDK can decode. */ export const STELLAR_LENDING_TOPICS = Object.freeze(Object.keys(REGISTRY));
409
510
  /**
410
511
  * Decode a Stellar lending controller event from its base64-XDR topics and
411
512
  * data (exactly as Soroban RPC `getEvents` delivers them). Returns `null` for a
@@ -1,6 +1,7 @@
1
1
  /** Typed Stellar lending reads. Configure the API deployment with XOXNOClient.apiUrl.
2
2
  * These cached HTTP reads do not simulate transactions or authorize spending.
3
- */ const BASE = '/stellar-lending';
3
+ */ import { Chain } from "../../utils/api.js";
4
+ const BASE = '/stellar-lending';
4
5
  const enc = encodeURIComponent;
5
6
  /** Load assets, hubs, spokes, reserves and keyed reserve details in one response.
6
7
  * @category Stellar lending reads
@@ -46,9 +47,43 @@ const enc = encodeURIComponent;
46
47
  /** Read a single lending account, identified by its decimal ID; balances use RAY token quantities.
47
48
  * @category Stellar lending reads
48
49
  */ export const getStellarAccountPositions = (client, accountId, init)=>client.fetchWithTimeout(`${BASE}/accounts/${enc(accountId)}/positions`, init);
49
- /** Read exact token base-unit holdings for a G or C wallet. Holdings may exceed spendable funds. Requires the API release exposing this route; older deployments return 404.
50
- * @category Stellar lending reads
51
- */ export const getStellarWalletBalance = (client, owner, asset, init)=>client.fetchWithTimeout(`${BASE}/users/${enc(owner)}/assets/${enc(asset)}/balance`, init);
50
+ /** Read a G or C wallet's token holdings from `/user/{owner}/token-inventory`:
51
+ * Horizon balances (native XLM and classic assets, keyed by their SAC id) plus
52
+ * indexed Soroban tokens, cached for about 10 seconds. Each token's
53
+ * `identifier` is its contract id and `balance` is base units as a decimal
54
+ * string. Tokens without a balance or trustline are absent.
55
+ * @category Stellar lending reads
56
+ */ export const getStellarWalletTokens = (client, owner, init)=>client.fetchWithTimeout(`/user/${enc(owner)}/token-inventory`, init);
57
+ /** Read exact token base-unit holdings of one asset for a G or C wallet, from
58
+ * {@link getStellarWalletTokens}. An asset the wallet does not hold returns
59
+ * `amount: '0'`, with decimals read from the lending asset. Holdings may exceed
60
+ * spendable funds (reserves, liabilities).
61
+ * @category Stellar lending reads
62
+ */ export const getStellarWalletBalance = async (client, owner, asset, init)=>{
63
+ const inventory = await getStellarWalletTokens(client, owner, init);
64
+ const network = client.chain === Chain.MAINNET ? 'mainnet' : 'testnet';
65
+ const token = (inventory?.tokens ?? []).find((t)=>t.identifier === asset);
66
+ if (token) {
67
+ return {
68
+ owner,
69
+ network: token.stellarNetwork ?? network,
70
+ asset,
71
+ amount: String(token.balance),
72
+ decimals: token.decimals
73
+ };
74
+ }
75
+ const { decimals } = await getStellarAsset(client, asset, init);
76
+ if (typeof decimals !== 'number') {
77
+ throw new Error(`Stellar lending: no decimals for ${asset}; it is not a lending asset`);
78
+ }
79
+ return {
80
+ owner,
81
+ network,
82
+ asset,
83
+ amount: '0',
84
+ decimals
85
+ };
86
+ };
52
87
  /** Load overview, deposit/borrow markets and chart series for an asset page.
53
88
  * @category Stellar lending reads
54
89
  */ export const getStellarAssetPage = (client, asset, query, init)=>client.fetchWithTimeout(`${BASE}/assets/${enc(asset)}/page`, {
@@ -307,6 +342,7 @@ const enc = encodeURIComponent;
307
342
  /** {@inheritDoc getStellarSpoke} */ spoke: (spokeId, init)=>getStellarSpoke(client, spokeId, init),
308
343
  /** {@inheritDoc getStellarUserPositions} */ userPositions: (owner, init)=>getStellarUserPositions(client, owner, init),
309
344
  /** {@inheritDoc getStellarAccountPositions} */ accountPositions: (accountId, init)=>getStellarAccountPositions(client, accountId, init),
345
+ /** {@inheritDoc getStellarWalletTokens} */ walletTokens: (owner, init)=>getStellarWalletTokens(client, owner, init),
310
346
  /** {@inheritDoc getStellarWalletBalance} */ walletBalance: (owner, asset, init)=>getStellarWalletBalance(client, owner, asset, init),
311
347
  /** {@inheritDoc getStellarAssetPage} */ assetPage: (asset, query, init)=>getStellarAssetPage(client, asset, query, init),
312
348
  /** {@inheritDoc getStellarAssetMarkets} */ assetMarkets: (asset, side, init)=>getStellarAssetMarkets(client, asset, side, init),
@@ -45,12 +45,13 @@ export function createStellarLendingReadClient(options) {
45
45
  while(true){
46
46
  if (visited.has(url.href)) throw new Error('Pagination Link cycle');
47
47
  visited.add(url.href);
48
- // Reject redirects before a custom fetch could forward credentials to another host.
48
+ // Never follow redirects: a custom fetch could forward credentials to another host.
49
+ // 'manual' instead of 'error' because Cloudflare Workers reject redirect: 'error'.
49
50
  const response = await fetcher(url.href, {
50
51
  signal,
51
- redirect: 'error'
52
+ redirect: 'manual'
52
53
  });
53
- if (response.redirected || response.url && new URL(response.url).href !== url.href) {
54
+ if (response.type === 'opaqueredirect' || response.status >= 300 && response.status < 400 || response.redirected || response.url && new URL(response.url).href !== url.href) {
54
55
  throw new Error('Stellar lending redirects are not allowed');
55
56
  }
56
57
  if (!response.ok) throw new Error(`Stellar lending request failed: HTTP ${response.status}`);
@@ -504,6 +504,22 @@
504
504
  message: 'Flash_position ended without supply or positive debt in its debt market.'
505
505
  }
506
506
  };
507
+ /**
508
+ * Stellar Asset Contract (token) errors 1–13 share numbers with GenericError,
509
+ * and a failed nested token call surfaces as the caller's top-level code. The
510
+ * lending contracts never raise these codes on user paths: 1, 3, 11 and 12 are
511
+ * reserved, 6 and 10 are governance-only. A tag-only match on them is a nested
512
+ * token error (for example #10 BalanceError), not InvalidWasmHash.
513
+ * Other overlaps (such as #13 TrustlineMissingError vs AccountNotInMarket)
514
+ * stay ambiguous without diagnostic events.
515
+ */ const NESTED_ONLY_CODES = new Set([
516
+ 1,
517
+ 3,
518
+ 6,
519
+ 10,
520
+ 11,
521
+ 12
522
+ ]);
507
523
  const ERROR_CODE = /Error\(Contract,\s*#(\d+)\)/;
508
524
  const EMITTED_ERROR = /contract:(C[A-Z2-7]{55})[^\n]*?Error\(Contract,\s*#(\d+)\)/g;
509
525
  const INVOKED_TAG = /\[xoxno-invoked:(C[A-Z2-7]{55})\]/;
@@ -515,7 +531,9 @@ const scopedCode = (raw, allowed)=>{
515
531
  if (HAS_EVENT_LOG.test(raw)) return null;
516
532
  const tag = raw.match(INVOKED_TAG);
517
533
  const first = raw.match(ERROR_CODE);
518
- return tag && allowed.has(tag[1]) && first ? Number(first[1]) : null;
534
+ if (!tag || !allowed.has(tag[1]) || !first) return null;
535
+ const code = Number(first[1]);
536
+ return NESTED_ONLY_CODES.has(code) ? null : code;
519
537
  };
520
538
  /**
521
539
  * Extracts a `Error(Contract, #N)` code from a raw Soroban simulation/RPC error
@@ -524,7 +542,11 @@ const scopedCode = (raw, allowed)=>{
524
542
  * strings can contain unrelated numbers (fees, ledgers, sequence numbers).
525
543
  *
526
544
  * Without `opts.contracts` it maps the first code in the string, whichever
527
- * contract emitted it.
545
+ * contract emitted it. A failed nested call (token, swap aggregator, oracle)
546
+ * reports its own code at the top level, so an unscoped or tag-only result can
547
+ * name the wrong lending error: a token's `#10` BalanceError reads as
548
+ * InvalidWasmHash. Pass `contracts` and, where available, the simulation's
549
+ * diagnostic event log to attribute the code to its emitter.
528
550
  */ export function mapSorobanError(raw, opts) {
529
551
  let code;
530
552
  if (opts?.contracts?.length) {