@t2000/sdk 0.9.6 → 0.9.7

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.
@@ -1,5 +1,6 @@
1
1
  import { Transaction } from '@mysten/sui/transactions';
2
2
  import { bcs } from '@mysten/sui/bcs';
3
+ import { SuiPriceServiceConnection, SuiPythClient } from '@pythnetwork/pyth-sui-js';
3
4
  import { AggregatorClient, Env } from '@cetusprotocol/aggregator-sdk';
4
5
  import { normalizeStructTag } from '@mysten/sui/utils';
5
6
 
@@ -257,6 +258,7 @@ var NAVI_BALANCE_DECIMALS = 9;
257
258
  var CONFIG_API = "https://open-api.naviprotocol.io/api/navi/config?env=prod";
258
259
  var POOLS_API = "https://open-api.naviprotocol.io/api/navi/pools?env=prod";
259
260
  var PACKAGE_API = "https://open-api.naviprotocol.io/api/package";
261
+ var PYTH_HERMES_URL = "https://hermes.pyth.network/";
260
262
  var packageCache = null;
261
263
  function toBigInt(v) {
262
264
  if (typeof v === "bigint") return v;
@@ -351,14 +353,32 @@ function addOracleUpdate(tx, config, pool) {
351
353
  ]
352
354
  });
353
355
  }
354
- function addOracleUpdatesForAllPools(tx, config, pools) {
355
- const updated = /* @__PURE__ */ new Set();
356
- for (const feed of config.oracle.feeds ?? []) {
357
- if (updated.has(feed.assetId)) continue;
358
- const pool = pools.find((p) => p.id === feed.assetId);
359
- if (!pool) continue;
356
+ async function refreshStableOracles(tx, client, config, pools) {
357
+ const stableTypes = STABLE_ASSETS.map((a) => SUPPORTED_ASSETS[a].type);
358
+ const stablePools = pools.filter((p) => {
359
+ const ct = p.suiCoinType || p.coinType || "";
360
+ return stableTypes.some((t) => matchesCoinType(ct, t));
361
+ });
362
+ const feeds = (config.oracle.feeds ?? []).filter(
363
+ (f2) => stablePools.some((p) => p.id === f2.assetId)
364
+ );
365
+ if (feeds.length === 0) return;
366
+ const pythFeedIds = feeds.map((f2) => f2.pythPriceFeedId).filter(Boolean);
367
+ if (pythFeedIds.length > 0 && config.oracle.pythStateId && config.oracle.wormholeStateId) {
368
+ try {
369
+ const connection = new SuiPriceServiceConnection(PYTH_HERMES_URL);
370
+ const priceUpdateData = await connection.getPriceFeedsUpdateData(pythFeedIds);
371
+ const pythClient = new SuiPythClient(
372
+ client,
373
+ config.oracle.pythStateId,
374
+ config.oracle.wormholeStateId
375
+ );
376
+ await pythClient.updatePriceFeeds(tx, priceUpdateData, pythFeedIds);
377
+ } catch {
378
+ }
379
+ }
380
+ for (const pool of stablePools) {
360
381
  addOracleUpdate(tx, config, pool);
361
- updated.add(feed.assetId);
362
382
  }
363
383
  }
364
384
  function rateToApy(rawRate) {
@@ -474,7 +494,7 @@ async function buildWithdrawTx(client, address, amount, options = {}) {
474
494
  const rawAmount = Number(stableToRaw(effectiveAmount, assetInfo.decimals));
475
495
  const tx = new Transaction();
476
496
  tx.setSender(address);
477
- addOracleUpdatesForAllPools(tx, config, pools);
497
+ await refreshStableOracles(tx, client, config, pools);
478
498
  const [balance] = tx.moveCall({
479
499
  target: `${config.package}::incentive_v3::withdraw_v2`,
480
500
  arguments: [
@@ -512,7 +532,7 @@ async function addWithdrawToTx(tx, client, address, amount, options = {}) {
512
532
  const effectiveAmount = Math.min(amount, Math.max(0, deposited - WITHDRAW_DUST_BUFFER));
513
533
  if (effectiveAmount <= 0) throw new T2000Error("NO_COLLATERAL", `Nothing to withdraw for ${assetInfo.displayName} on NAVI`);
514
534
  const rawAmount = Number(stableToRaw(effectiveAmount, assetInfo.decimals));
515
- addOracleUpdatesForAllPools(tx, config, pools);
535
+ await refreshStableOracles(tx, client, config, pools);
516
536
  const [balance] = tx.moveCall({
517
537
  target: `${config.package}::incentive_v3::withdraw_v2`,
518
538
  arguments: [
@@ -600,7 +620,7 @@ async function buildBorrowTx(client, address, amount, options = {}) {
600
620
  ]);
601
621
  const tx = new Transaction();
602
622
  tx.setSender(address);
603
- addOracleUpdatesForAllPools(tx, config, pools);
623
+ await refreshStableOracles(tx, client, config, pools);
604
624
  const [balance] = tx.moveCall({
605
625
  target: `${config.package}::incentive_v3::borrow_v2`,
606
626
  arguments: [