@naviprotocol/lending 2.2.0-preview.hbtc-usdc.09f9164 → 2.2.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.
package/dist/index.js CHANGED
@@ -1,96 +1,99 @@
1
- import { UserPositions as r, getCoins as o, getHealthFactor as a, getHealthFactorPTB as i, getLendingPositions as n, getLendingState as s, getSimulatedHealthFactor as l, getSimulatedHealthFactorPTB as d, getTransactions as c, mergeCoinsPTB as g, verifyHealthFactorPTB as P } from "./account.js";
2
- import { Address as T, FlashLoanAssetConfig as f, IncentiveAPYInfo as C, IncentivePoolInfo as B, IncentivePoolInfoByPhase as m, OracleInfo as h, ReserveDataInfo as A, UserStateInfo as I } from "./bcs.js";
1
+ import { UserPositions as r, getCoins as o, getHealthFactor as a, getHealthFactorPTB as i, getLendingPositions as n, getLendingState as s, getSimulatedHealthFactor as l, getSimulatedHealthFactorPTB as d, getTransactions as g, mergeCoinsPTB as c, verifyHealthFactorPTB as P } from "./account.js";
2
+ import { Address as f, FlashLoanAssetConfig as C, IncentiveAPYInfo as T, IncentivePoolInfo as B, IncentivePoolInfoByPhase as m, OracleInfo as h, ReserveDataInfo as A, UserStateInfo as I } from "./bcs.js";
3
3
  import { DEFAULT_CACHE_TIME as v, getConfig as F } from "./config.js";
4
- import { filterPriceFeeds as L, getPriceFeeds as S, getPythStalePriceFeedId as y, getPythStalePriceFeedIdV2 as E, updateOraclePriceBeforeUserOperationPTB as M, updateOraclePricesPTB as U, updatePythPriceFeeds as w } from "./oracle.js";
5
- import { flashloanPTB as R, getAllFlashLoanAssets as O, getFlashLoanAsset as H, repayFlashLoanPTB as _ } from "./flashloan.js";
4
+ import { filterPriceFeeds as L, getPriceFeeds as M, getPythStalePriceFeedId as S, getPythStalePriceFeedIdV2 as y, updateOraclePriceBeforeUserOperationPTB as E, updateOraclePricesPTB as U, updatePythPriceFeeds as w } from "./oracle.js";
5
+ import { flashloanPTB as R, getAllFlashLoanAssets as O, getFlashLoanAsset as k, repayFlashLoanPTB as H } from "./flashloan.js";
6
6
  import { liquidatePTB as D } from "./liquidate.js";
7
7
  import { PoolOperator as G, borrowCoinPTB as q, depositCoinPTB as z, getBorrowFee as K, getFees as Q, getPool as V, getPools as Y, getStats as j, repayCoinPTB as J, withdrawCoinPTB as W } from "./pool.js";
8
8
  import { claimLendingRewardsPTB as Z, getUserAvailableLendingRewards as $, getUserClaimedRewardHistory as ee, getUserTotalClaimedReward as te, summaryLendingRewards as re } from "./reward.js";
9
9
  import { devInspectTransaction as ae, normalizeCoinType as ie, parsePoolUID as ne, parseTxValue as se, withCache as le, withSingleton as de } from "./utils.js";
10
- import { createAccountCapPTB as ge, getAccountCapOwnerPTB as Pe } from "./account-cap.js";
11
- import { createEModeCapPTB as Te, emodeIdentityId as fe, enterEModePTB as Ce, exitEModePTB as Be, getUserEModeCaps as me } from "./emode.js";
12
- import { DEFAULT_MARKET_IDENTITY as Ae, MARKETS as Ie, Market as ue, getMarket as ve, getMarketConfig as Fe, getMarkets as xe } from "./market.js";
13
- import { NaviMissingGraphQLClientError as Se, PUBLIC_MAINNET_GRPC_URL as ye, createNaviLegacyJsonRpcClient as Ee, createNaviSuiClient as Me, createNaviSuiClientBundle as Ue, getAddressBalance as we, getCoinObjectOnlyBalance as Ne, listAddressBalances as Re, normalizeAddressBalance as Oe, requireNaviGraphQLClient as He } from "./sui.js";
14
- import { SuiPriceServiceConnection as ke, SuiPythClient as De } from "./pyth.js";
15
- import { buildNaviOpenApiUrl as Ge, configureNaviSdk as qe, mergeServiceHeaders as ze, resolveNaviOpenApiEndpoint as Ke } from "./services.js";
10
+ import { createAccountCapPTB as ce, getAccountCapOwnerPTB as Pe } from "./account-cap.js";
11
+ import { createEModeCapPTB as fe, emodeIdentityId as Ce, enterEModePTB as Te, exitEModePTB as Be, getUserEModeCaps as me } from "./emode.js";
12
+ import { DEFAULT_MARKET_IDENTITY as Ae, MARKETS as Ie, Market as ue, getMarket as ve, getMarketConfig as Fe, getMarketConfigs as xe, getMarkets as Le, loadMarketConfigs as Me, registerMarketConfigs as Se } from "./market.js";
13
+ import { NaviMissingGraphQLClientError as Ee, PUBLIC_MAINNET_GRPC_URL as Ue, createNaviLegacyJsonRpcClient as we, createNaviSuiClient as Ne, createNaviSuiClientBundle as Re, getAddressBalance as Oe, getCoinObjectOnlyBalance as ke, listAddressBalances as He, normalizeAddressBalance as _e, requireNaviGraphQLClient as De } from "./sui.js";
14
+ import { SuiPriceServiceConnection as Ge, SuiPythClient as qe } from "./pyth.js";
15
+ import { buildNaviOpenApiUrl as Ke, configureNaviSdk as Qe, mergeServiceHeaders as Ve, resolveNaviOpenApiEndpoint as Ye } from "./services.js";
16
16
  export {
17
- T as Address,
17
+ f as Address,
18
18
  v as DEFAULT_CACHE_TIME,
19
19
  Ae as DEFAULT_MARKET_IDENTITY,
20
- f as FlashLoanAssetConfig,
21
- C as IncentiveAPYInfo,
20
+ C as FlashLoanAssetConfig,
21
+ T as IncentiveAPYInfo,
22
22
  B as IncentivePoolInfo,
23
23
  m as IncentivePoolInfoByPhase,
24
24
  Ie as MARKETS,
25
25
  ue as Market,
26
- Se as NaviMissingGraphQLClientError,
26
+ Ee as NaviMissingGraphQLClientError,
27
27
  h as OracleInfo,
28
- ye as PUBLIC_MAINNET_GRPC_URL,
28
+ Ue as PUBLIC_MAINNET_GRPC_URL,
29
29
  G as PoolOperator,
30
30
  A as ReserveDataInfo,
31
- ke as SuiPriceServiceConnection,
32
- De as SuiPythClient,
31
+ Ge as SuiPriceServiceConnection,
32
+ qe as SuiPythClient,
33
33
  r as UserPositions,
34
34
  I as UserStateInfo,
35
35
  q as borrowCoinPTB,
36
- Ge as buildNaviOpenApiUrl,
36
+ Ke as buildNaviOpenApiUrl,
37
37
  Z as claimLendingRewardsPTB,
38
- qe as configureNaviSdk,
39
- ge as createAccountCapPTB,
40
- Te as createEModeCapPTB,
41
- Ee as createNaviLegacyJsonRpcClient,
42
- Me as createNaviSuiClient,
43
- Ue as createNaviSuiClientBundle,
38
+ Qe as configureNaviSdk,
39
+ ce as createAccountCapPTB,
40
+ fe as createEModeCapPTB,
41
+ we as createNaviLegacyJsonRpcClient,
42
+ Ne as createNaviSuiClient,
43
+ Re as createNaviSuiClientBundle,
44
44
  z as depositCoinPTB,
45
45
  ae as devInspectTransaction,
46
- fe as emodeIdentityId,
47
- Ce as enterEModePTB,
46
+ Ce as emodeIdentityId,
47
+ Te as enterEModePTB,
48
48
  Be as exitEModePTB,
49
49
  L as filterPriceFeeds,
50
50
  R as flashloanPTB,
51
51
  Pe as getAccountCapOwnerPTB,
52
- we as getAddressBalance,
52
+ Oe as getAddressBalance,
53
53
  O as getAllFlashLoanAssets,
54
54
  K as getBorrowFee,
55
- Ne as getCoinObjectOnlyBalance,
55
+ ke as getCoinObjectOnlyBalance,
56
56
  o as getCoins,
57
57
  F as getConfig,
58
58
  Q as getFees,
59
- H as getFlashLoanAsset,
59
+ k as getFlashLoanAsset,
60
60
  a as getHealthFactor,
61
61
  i as getHealthFactorPTB,
62
62
  n as getLendingPositions,
63
63
  s as getLendingState,
64
64
  ve as getMarket,
65
65
  Fe as getMarketConfig,
66
- xe as getMarkets,
66
+ xe as getMarketConfigs,
67
+ Le as getMarkets,
67
68
  V as getPool,
68
69
  Y as getPools,
69
- S as getPriceFeeds,
70
- y as getPythStalePriceFeedId,
71
- E as getPythStalePriceFeedIdV2,
70
+ M as getPriceFeeds,
71
+ S as getPythStalePriceFeedId,
72
+ y as getPythStalePriceFeedIdV2,
72
73
  l as getSimulatedHealthFactor,
73
74
  d as getSimulatedHealthFactorPTB,
74
75
  j as getStats,
75
- c as getTransactions,
76
+ g as getTransactions,
76
77
  $ as getUserAvailableLendingRewards,
77
78
  ee as getUserClaimedRewardHistory,
78
79
  me as getUserEModeCaps,
79
80
  te as getUserTotalClaimedReward,
80
81
  D as liquidatePTB,
81
- Re as listAddressBalances,
82
- g as mergeCoinsPTB,
83
- ze as mergeServiceHeaders,
84
- Oe as normalizeAddressBalance,
82
+ He as listAddressBalances,
83
+ Me as loadMarketConfigs,
84
+ c as mergeCoinsPTB,
85
+ Ve as mergeServiceHeaders,
86
+ _e as normalizeAddressBalance,
85
87
  ie as normalizeCoinType,
86
88
  ne as parsePoolUID,
87
89
  se as parseTxValue,
90
+ Se as registerMarketConfigs,
88
91
  J as repayCoinPTB,
89
- _ as repayFlashLoanPTB,
90
- He as requireNaviGraphQLClient,
91
- Ke as resolveNaviOpenApiEndpoint,
92
+ H as repayFlashLoanPTB,
93
+ De as requireNaviGraphQLClient,
94
+ Ye as resolveNaviOpenApiEndpoint,
92
95
  re as summaryLendingRewards,
93
- M as updateOraclePriceBeforeUserOperationPTB,
96
+ E as updateOraclePriceBeforeUserOperationPTB,
94
97
  U as updateOraclePricesPTB,
95
98
  w as updatePythPriceFeeds,
96
99
  P as verifyHealthFactorPTB,
package/dist/market.d.ts CHANGED
@@ -1,4 +1,4 @@
1
- import { MarketIdentity, EnvOption, CacheOption, EMode, MarketConfig, Pool, EModePool } from './types.js';
1
+ import { MarketIdentity, EnvOption, CacheOption, EMode, MarketConfig, Pool, EModePool, ServiceOption } from './types.js';
2
2
 
3
3
  export declare const DEFAULT_MARKET_IDENTITY = "main";
4
4
  export declare const MARKETS: {
@@ -22,47 +22,47 @@ export declare const MARKETS: {
22
22
  key: string;
23
23
  name: string;
24
24
  };
25
- "sui-usdc": {
25
+ 'sui-usdc': {
26
26
  id: number;
27
27
  key: string;
28
28
  name: string;
29
29
  };
30
- "wbtc-usdc": {
30
+ 'wbtc-usdc': {
31
31
  id: number;
32
32
  key: string;
33
33
  name: string;
34
34
  };
35
- "xbtc-usdc": {
35
+ 'xbtc-usdc': {
36
36
  id: number;
37
37
  key: string;
38
38
  name: string;
39
39
  };
40
- "vsui-usdc": {
40
+ 'vsui-usdc': {
41
41
  id: number;
42
42
  key: string;
43
43
  name: string;
44
44
  };
45
- "vsui-sui": {
45
+ 'vsui-sui': {
46
46
  id: number;
47
47
  key: string;
48
48
  name: string;
49
49
  };
50
- "hasui-sui": {
50
+ 'hasui-sui': {
51
51
  id: number;
52
52
  key: string;
53
53
  name: string;
54
54
  };
55
- "high-usdc": {
55
+ 'high-usdc': {
56
56
  id: number;
57
57
  key: string;
58
58
  name: string;
59
59
  };
60
- "eearn-usdc": {
60
+ 'eearn-usdc': {
61
61
  id: number;
62
62
  key: string;
63
63
  name: string;
64
64
  };
65
- "hbtc-usdc": {
65
+ 'hbtc-usdc': {
66
66
  id: number;
67
67
  key: string;
68
68
  name: string;
@@ -93,59 +93,48 @@ export declare class Market {
93
93
  getEModePools(emodeId: number): EModePool[];
94
94
  private checkMarket;
95
95
  }
96
- export declare const getMarketConfig: (marketIdentity: MarketIdentity) => {
97
- id: number;
98
- key: string;
99
- name: string;
100
- } | {
101
- id: number;
102
- key: string;
103
- name: string;
104
- } | {
105
- id: number;
106
- key: string;
107
- name: string;
108
- } | {
109
- id: number;
110
- key: string;
111
- name: string;
112
- } | {
113
- id: number;
114
- key: string;
115
- name: string;
116
- } | {
117
- id: number;
118
- key: string;
119
- name: string;
120
- } | {
121
- id: number;
122
- key: string;
123
- name: string;
124
- } | {
125
- id: number;
126
- key: string;
127
- name: string;
128
- } | {
129
- id: number;
130
- key: string;
131
- name: string;
132
- } | {
133
- id: number;
134
- key: string;
135
- name: string;
136
- } | {
137
- id: number;
138
- key: string;
139
- name: string;
140
- } | {
141
- id: number;
142
- key: string;
143
- name: string;
144
- } | {
145
- id: number;
146
- key: string;
147
- name: string;
96
+ export type MarketEnv = EnvOption['env'];
97
+ export type MarketConfigsOptions = Partial<EnvOption & CacheOption & ServiceOption>;
98
+ export type RegisterMarketConfigsOptions = {
99
+ env?: MarketEnv;
100
+ /**
101
+ * The caller is itself the source of truth for this env's market list (e.g.
102
+ * open-api registering its own config). `loadMarketConfigs` then returns the
103
+ * registered list without an HTTP round trip.
104
+ */
105
+ authoritative?: boolean;
148
106
  };
107
+ /**
108
+ * Replaces the market list of an env with the given configs.
109
+ *
110
+ * Registration only seeds the list: unless `authoritative` is set, a later
111
+ * `loadMarketConfigs` still fetches from open-api and replaces it.
112
+ */
113
+ export declare const registerMarketConfigs: (configs: MarketConfig[], options?: RegisterMarketConfigsOptions) => MarketConfig[];
114
+ /**
115
+ * Returns the market list of an env: the loaded/registered list when present,
116
+ * otherwise the built-in MARKETS.
117
+ */
118
+ export declare const getMarketConfigs: (options?: {
119
+ env?: MarketEnv;
120
+ }) => MarketConfig[];
121
+ /**
122
+ * Loads the market list of an env from open-api (`/navi/markets`) and stores
123
+ * it for `getMarketConfigs` / `getMarketConfig`.
124
+ *
125
+ * Never throws: on any failure it keeps whatever the env already has (the
126
+ * built-in MARKETS by default) and logs a warning.
127
+ */
128
+ export declare const loadMarketConfigs: (options?: MarketConfigsOptions) => Promise<MarketConfig[]>;
129
+ /**
130
+ * Resolves a market by id, key or config. Searches the built-in MARKETS first
131
+ * (unchanged behavior for every existing market), then the loaded or
132
+ * registered list of `options.env`, so a market that only exists in open-api
133
+ * resolves too. Without an env it searches every env's list.
134
+ */
135
+ export declare const getMarketConfig: (marketIdentity: MarketIdentity, options?: {
136
+ env?: MarketEnv;
137
+ }) => MarketConfig;
149
138
  export declare const getMarkets: (markets: MarketIdentity[], options?: Partial<EnvOption & CacheOption>) => Promise<Market[]>;
150
139
  export declare const getMarket: (market: MarketIdentity, options?: Partial<EnvOption & CacheOption>) => Promise<Market>;
151
140
  //# sourceMappingURL=market.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"market.d.ts","sourceRoot":"","sources":["../src/market.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EACV,cAAc,EACd,SAAS,EACT,WAAW,EACX,KAAK,EACL,YAAY,EACZ,IAAI,EACJ,SAAS,EACV,MAAM,SAAS,CAAA;AAIhB,eAAO,MAAM,uBAAuB,SAAS,CAAA;AAE7C,eAAO,MAAM,OAAO;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;CAkE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,MAAM,oBAAoB,GAAG,OAAO,CAAC,SAAS,GAAG,WAAW,GAAG,aAAa,CAAC,CAAA;AAEnF,MAAM,MAAM,4BAA4B,GAAG;IACzC,GAAG,CAAC,EAAE,SAAS,CAAA;IACf;;;;OAIG;IACH,aAAa,CAAC,EAAE,OAAO,CAAA;CACxB,CAAA;AAoFD;;;;;GAKG;AACH,eAAO,MAAM,qBAAqB,GAChC,SAAS,YAAY,EAAE,EACvB,UAAU,4BAA4B,KACrC,YAAY,EAYd,CAAA;AAED;;;GAGG;AACH,eAAO,MAAM,gBAAgB,GAAI,UAAU;IAAE,GAAG,CAAC,EAAE,SAAS,CAAA;CAAE,KAAG,YAAY,EAG5E,CAAA;AA0CD;;;;;;GAMG;AACH,eAAO,MAAM,iBAAiB,GAC5B,UAAU,oBAAoB,KAC7B,OAAO,CAAC,YAAY,EAAE,CA2BxB,CAAA;AAED;;;;;GAKG;AACH,eAAO,MAAM,eAAe,GAAI,gBAAgB,cAAc,EAAE,UAAU;IAAE,GAAG,CAAC,EAAE,SAAS,CAAA;CAAE,iBAmB5F,CAAA;AAED,eAAO,MAAM,UAAU,YAGR,cAAc,EAAE,YACf,OAAO,CAAC,SAAS,GAAG,WAAW,CAAC,KACzC,OAAO,CAAC,MAAM,EAAE,CAiBtB,CAAA;AAED,eAAO,MAAM,SAAS,WAEH,cAAc,YAAY,OAAO,CAAC,SAAS,GAAG,WAAW,CAAC,KAAG,OAAO,CAAC,MAAM,CAK7F,CAAA"}
package/dist/market.js CHANGED
@@ -1,7 +1,9 @@
1
- import { withCache as M, getPoolsMap as m, getEmodesMap as p, withSingleton as k } from "./utils.js";
2
- import { getPools as S } from "./pool.js";
3
- import f from "bignumber.js";
4
- const C = "main", y = {
1
+ import { withCache as y, getPoolsMap as w, getEmodesMap as b, withSingleton as g, requestHeaders as A } from "./utils.js";
2
+ import { getPools as U } from "./pool.js";
3
+ import E from "bignumber.js";
4
+ import $ from "./package.json.js";
5
+ import { resolveNaviOpenApiEndpoint as _, buildNaviOpenApiUrl as D, mergeServiceHeaders as B } from "./services.js";
6
+ const L = "main", N = {
5
7
  main: {
6
8
  id: 0,
7
9
  key: "main",
@@ -68,99 +70,190 @@ const C = "main", y = {
68
70
  name: "hBTC / USDC Market"
69
71
  }
70
72
  };
71
- class w {
72
- constructor(s, t) {
73
+ class O {
74
+ constructor(e, s) {
73
75
  this.poolMap = {}, this.emodeMap = {}, this.pools = [], this.emodes = [], this.emodePools = [], this.emodeBorrowablePools = [], this.emodeSupplyablePools = [], this._overview = {
74
76
  marketTotalSupplyValue: "0",
75
77
  marketTotalBorrowValue: "0"
76
- }, this.config = b(s), this.initPools(t);
78
+ }, this.config = C(e), this.initPools(s);
77
79
  }
78
80
  get overview() {
79
81
  return this._overview;
80
82
  }
81
- initPools(s) {
82
- const t = m(this.pools), i = p(this.emodes), o = /* @__PURE__ */ new Set(), n = /* @__PURE__ */ new Set();
83
- let a = f(0), d = f(0);
84
- s.forEach((e) => {
85
- if (!this.checkMarket(e.market)) {
86
- console.warn(`Pool is not in market ${this.config.name}`, e);
83
+ initPools(e) {
84
+ const s = w(this.pools), n = b(this.emodes), t = /* @__PURE__ */ new Set(), i = /* @__PURE__ */ new Set();
85
+ let a = E(0), d = E(0);
86
+ e.forEach((r) => {
87
+ if (!this.checkMarket(r.market)) {
88
+ console.warn(`Pool is not in market ${this.config.name}`, r);
87
89
  return;
88
90
  }
89
- t[e.uniqueId] || this.pools.push(e), e?.emodes?.forEach((l) => {
90
- i[l.uniqueId] || this.emodes.push(l), l.assets.forEach((h) => {
91
- h.isDebt && l.assets.find(
92
- (u) => u.isCollateral && u.ltv > 0 && u.assetId !== e.id
93
- ) && o.add(e.uniqueId), h.isCollateral && l.assets.find((u) => u.isDebt && u.assetId !== e.id) && n.add(e.uniqueId);
91
+ s[r.uniqueId] || this.pools.push(r), r?.emodes?.forEach((c) => {
92
+ n[c.uniqueId] || this.emodes.push(c), c.assets.forEach((m) => {
93
+ m.isDebt && c.assets.find(
94
+ (u) => u.isCollateral && u.ltv > 0 && u.assetId !== r.id
95
+ ) && t.add(r.uniqueId), m.isCollateral && c.assets.find((u) => u.isDebt && u.assetId !== r.id) && i.add(r.uniqueId);
94
96
  });
95
- }), d = d.plus(e.poolBorrowValue), a = a.plus(e.poolSupplyValue);
96
- }), this.poolMap = m(this.pools, "id"), this.emodeMap = p(this.emodes, "emodeId"), this.emodes.forEach((e) => {
97
- const c = this.getEModePools(e.emodeId);
98
- this.emodePools.push(...c);
97
+ }), d = d.plus(r.poolBorrowValue), a = a.plus(r.poolSupplyValue);
98
+ }), this.poolMap = w(this.pools, "id"), this.emodeMap = b(this.emodes, "emodeId"), this.emodes.forEach((r) => {
99
+ const l = this.getEModePools(r.emodeId);
100
+ this.emodePools.push(...l);
99
101
  }), this._overview = {
100
102
  marketTotalSupplyValue: a.toString(),
101
103
  marketTotalBorrowValue: d.toString()
102
- }, this.emodeBorrowablePools = this.pools.filter((e) => o.has(e.uniqueId)), this.emodeSupplyablePools = this.pools.filter((e) => n.has(e.uniqueId));
104
+ }, this.emodeBorrowablePools = this.pools.filter((r) => t.has(r.uniqueId)), this.emodeSupplyablePools = this.pools.filter((r) => i.has(r.uniqueId));
103
105
  }
104
- getEMode(s) {
105
- return this.emodeMap[s] || null;
106
+ getEMode(e) {
107
+ return this.emodeMap[e] || null;
106
108
  }
107
- getEModeRelatePools(s, t) {
108
- const { collateral: i, debt: o, emodeId: n } = t || {}, a = [];
109
- return s.emodes.forEach((d) => {
110
- typeof n == "number" && n !== d.emodeId || d.assets.forEach((e) => {
111
- typeof i == "boolean" && i && e.isCollateral && e.assetId === s.id && a.push(this.poolMap[e.assetId]), typeof o == "boolean" && o && e.isDebt && e.assetId === s.id && a.push(this.poolMap[e.assetId]);
109
+ getEModeRelatePools(e, s) {
110
+ const { collateral: n, debt: t, emodeId: i } = s || {}, a = [];
111
+ return e.emodes.forEach((d) => {
112
+ typeof i == "number" && i !== d.emodeId || d.assets.forEach((r) => {
113
+ typeof n == "boolean" && n && r.isCollateral && r.assetId === e.id && a.push(this.poolMap[r.assetId]), typeof t == "boolean" && t && r.isDebt && r.assetId === e.id && a.push(this.poolMap[r.assetId]);
112
114
  });
113
115
  }), a;
114
116
  }
115
- getEModePools(s) {
116
- const t = this.getEMode(s);
117
- return t ? t.assets.map((o) => o.assetId).map((o) => this.poolMap[o]).filter((o) => !!o).map((o) => {
118
- const n = t.assets.find((a) => a.assetId === o.id);
117
+ getEModePools(e) {
118
+ const s = this.getEMode(e);
119
+ return s ? s.assets.map((t) => t.assetId).map((t) => this.poolMap[t]).filter((t) => !!t).map((t) => {
120
+ const i = s.assets.find((a) => a.assetId === t.id);
119
121
  return {
120
- ...o,
122
+ ...t,
121
123
  emode: {
122
- ...n,
123
- emodeId: t.emodeId
124
+ ...i,
125
+ emodeId: s.emodeId
124
126
  },
125
127
  isEMode: !0
126
128
  };
127
129
  }) : [];
128
130
  }
129
- checkMarket(s) {
130
- let t = !1;
131
- return typeof s == "number" && s === this.config.id && (t = !0), typeof s == "string" && s === this.config.key && (t = !0), typeof s == "object" && s.id === this.config.id && (t = !0), t;
131
+ checkMarket(e) {
132
+ let s = !1;
133
+ return typeof e == "number" && e === this.config.id && (s = !0), typeof e == "string" && e === this.config.key && (s = !0), typeof e == "object" && e.id === this.config.id && (s = !0), s;
132
134
  }
133
135
  }
134
- const b = (r) => {
135
- const t = Object.values(y).find((i) => typeof r == "number" ? i.id === r : typeof r == "string" ? i.key === r : i.id === r.id);
136
- if (!t)
136
+ const v = "prod", R = 1e3 * 60 * 5, S = 5e3, h = "[@naviprotocol/lending]", p = Object.values(N), f = {}, M = /* @__PURE__ */ new Set(), I = (o, e) => typeof e == "number" ? o.id === e : typeof e == "string" ? o.key === e : o.id === e.id && o.key === e.key, V = (o) => typeof o?.id == "number" && typeof o?.key == "string" && !!o.key && typeof o?.name == "string", T = (o) => {
137
+ const e = [], s = /* @__PURE__ */ new Set(), n = /* @__PURE__ */ new Set();
138
+ return o.forEach((t) => {
139
+ if (!V(t)) {
140
+ console.warn(`${h} Ignoring invalid market config`, t);
141
+ return;
142
+ }
143
+ if (s.has(t.key))
144
+ return;
145
+ const i = p.find((d) => d.key === t.key);
146
+ if (i) {
147
+ i.id !== t.id && console.warn(
148
+ `${h} Market "${t.key}" id ${t.id} conflicts with built-in id ${i.id}; using built-in`
149
+ ), s.add(i.key), n.add(i.id), e.push({ id: i.id, key: i.key, name: i.name });
150
+ return;
151
+ }
152
+ const a = p.find((d) => d.id === t.id) || (n.has(t.id) ? e.find((d) => d.id === t.id) : void 0);
153
+ if (a) {
154
+ console.warn(
155
+ `${h} Ignoring market "${t.key}": id ${t.id} already belongs to "${a.key}"`
156
+ );
157
+ return;
158
+ }
159
+ s.add(t.key), n.add(t.id), e.push({ id: t.id, key: t.key, name: t.name });
160
+ }), e;
161
+ }, z = (o, e) => {
162
+ const s = e?.env || v, n = T(o || []);
163
+ return n.length === 0 ? (console.warn(`${h} Ignoring empty market list for env "${s}"`), k({ env: s })) : (f[s] = n, e?.authoritative && M.add(s), n);
164
+ }, k = (o) => {
165
+ const e = o?.env || v;
166
+ return f[e] || p;
167
+ }, q = y(
168
+ g(async (o) => {
169
+ const e = _(o), s = D(
170
+ `/navi/markets?env=${o.env}&sdk=${$.version}`,
171
+ o
172
+ ), n = typeof AbortController < "u" ? new AbortController() : void 0;
173
+ let t;
174
+ const i = new Promise((c, m) => {
175
+ t = setTimeout(() => {
176
+ n?.abort(), m(new Error(`Timed out after ${S}ms`));
177
+ }, S);
178
+ }), a = (async () => {
179
+ const c = await fetch(s, {
180
+ headers: B(A, e),
181
+ signal: n?.signal
182
+ });
183
+ if (!c.ok)
184
+ throw new Error(`HTTP ${c.status}`);
185
+ return c.json();
186
+ })();
187
+ a.catch(() => {
188
+ });
189
+ const r = (await Promise.race([a, i]).finally(() => clearTimeout(t)))?.data?.markets;
190
+ if (!Array.isArray(r))
191
+ throw new Error("Invalid /navi/markets response");
192
+ const l = T(r);
193
+ if (l.length === 0)
194
+ throw new Error("Empty /navi/markets response");
195
+ return l;
196
+ })
197
+ ), J = async (o) => {
198
+ const e = o?.env || v;
199
+ if (M.has(e))
200
+ return k({ env: e });
201
+ try {
202
+ const s = await q({
203
+ cacheTime: R,
204
+ ...o?.services ? { services: o.services } : {},
205
+ ...o?.disableCache ? { disableCache: !0 } : {},
206
+ ...o?.cacheTime !== void 0 ? { cacheTime: o.cacheTime } : {},
207
+ env: e
208
+ });
209
+ return M.has(e) ? k({ env: e }) : (f[e] = s, s);
210
+ } catch (s) {
211
+ return console.warn(
212
+ `${h} Failed to load markets for env "${e}", using the current list`,
213
+ s
214
+ ), k({ env: e });
215
+ }
216
+ }, C = (o, e) => {
217
+ let s = p.find(
218
+ (n) => I(n, o)
219
+ );
220
+ if (!s) {
221
+ const n = e?.env ? [f[e.env]] : Object.values(f);
222
+ for (const t of n)
223
+ if (s = t?.find((i) => I(i, o)), s)
224
+ break;
225
+ }
226
+ if (!s)
137
227
  throw new Error("Market not found");
138
- return t;
139
- }, I = M(
140
- k(
141
- async (r, s) => {
142
- const t = await S({
228
+ return s;
229
+ }, F = y(
230
+ g(
231
+ async (o, e) => {
232
+ const s = await U({
143
233
  cacheTime: 6e4,
144
- ...s,
145
- markets: r
234
+ ...e,
235
+ markets: o
146
236
  });
147
- return r.map((i) => {
148
- const o = b(i), n = t.filter((a) => a.market === o.key);
149
- return new w(i, n);
237
+ return o.map((n) => {
238
+ const t = C(n, { env: e?.env || "prod" }), i = s.filter((a) => a.market === t.key);
239
+ return new O(t, i);
150
240
  });
151
241
  }
152
242
  )
153
- ), T = M(
154
- k(
155
- async (r, s) => (await I([r], s))[0]
243
+ ), W = y(
244
+ g(
245
+ async (o, e) => (await F([o], e))[0]
156
246
  )
157
247
  );
158
248
  export {
159
- C as DEFAULT_MARKET_IDENTITY,
160
- y as MARKETS,
161
- w as Market,
162
- T as getMarket,
163
- b as getMarketConfig,
164
- I as getMarkets
249
+ L as DEFAULT_MARKET_IDENTITY,
250
+ N as MARKETS,
251
+ O as Market,
252
+ W as getMarket,
253
+ C as getMarketConfig,
254
+ k as getMarketConfigs,
255
+ F as getMarkets,
256
+ J as loadMarketConfigs,
257
+ z as registerMarketConfigs
165
258
  };
166
259
  //# sourceMappingURL=market.js.map