@t2000/sdk 0.9.5 → 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,18 +353,32 @@ function addOracleUpdate(tx, config, pool) {
351
353
  ]
352
354
  });
353
355
  }
354
- function addOracleUpdatesForPositions(tx, config, pools, states, primaryPool) {
355
- const updated = /* @__PURE__ */ new Set();
356
- addOracleUpdate(tx, config, primaryPool);
357
- updated.add(primaryPool.id);
358
- for (const state of states) {
359
- if (updated.has(state.assetId)) continue;
360
- const pool = pools.find((p) => p.id === state.assetId);
361
- if (!pool) continue;
362
- const feed = config.oracle.feeds?.find((f2) => f2.assetId === pool.id);
363
- if (!feed) 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) {
364
381
  addOracleUpdate(tx, config, pool);
365
- updated.add(pool.id);
366
382
  }
367
383
  }
368
384
  function rateToApy(rawRate) {
@@ -390,7 +406,7 @@ function compoundBalance(rawBalance, currentIndex) {
390
406
  const result = (rawBalance * BigInt(currentIndex) + half) / scale;
391
407
  return Number(result) / 10 ** NAVI_BALANCE_DECIMALS;
392
408
  }
393
- async function getUserState(client, address, includeZero = false) {
409
+ async function getUserState(client, address) {
394
410
  const config = await getConfig();
395
411
  const tx = new Transaction();
396
412
  tx.moveCall({
@@ -408,7 +424,6 @@ async function getUserState(client, address, includeZero = false) {
408
424
  supplyBalance: toBigInt(s.supply_balance),
409
425
  borrowBalance: toBigInt(s.borrow_balance)
410
426
  }));
411
- if (includeZero) return mapped;
412
427
  return mapped.filter((s) => s.supplyBalance !== 0n || s.borrowBalance !== 0n);
413
428
  }
414
429
  async function fetchCoins(client, owner, coinType) {
@@ -466,20 +481,20 @@ async function buildSaveTx(client, address, amount, options = {}) {
466
481
  async function buildWithdrawTx(client, address, amount, options = {}) {
467
482
  const asset = options.asset ?? "USDC";
468
483
  const assetInfo = SUPPORTED_ASSETS[asset];
469
- const [config, pool, pools, allStates] = await Promise.all([
484
+ const [config, pool, pools, states] = await Promise.all([
470
485
  getConfig(),
471
486
  getPool(asset),
472
487
  getPools(),
473
- getUserState(client, address, true)
488
+ getUserState(client, address)
474
489
  ]);
475
- const assetState = allStates.find((s) => s.assetId === pool.id);
490
+ const assetState = states.find((s) => s.assetId === pool.id);
476
491
  const deposited = assetState ? compoundBalance(assetState.supplyBalance, pool.currentSupplyIndex) : 0;
477
492
  const effectiveAmount = Math.min(amount, Math.max(0, deposited - WITHDRAW_DUST_BUFFER));
478
493
  if (effectiveAmount <= 0) throw new T2000Error("NO_COLLATERAL", `Nothing to withdraw for ${assetInfo.displayName} on NAVI`);
479
494
  const rawAmount = Number(stableToRaw(effectiveAmount, assetInfo.decimals));
480
495
  const tx = new Transaction();
481
496
  tx.setSender(address);
482
- addOracleUpdatesForPositions(tx, config, pools, allStates, pool);
497
+ await refreshStableOracles(tx, client, config, pools);
483
498
  const [balance] = tx.moveCall({
484
499
  target: `${config.package}::incentive_v3::withdraw_v2`,
485
500
  arguments: [
@@ -506,18 +521,18 @@ async function buildWithdrawTx(client, address, amount, options = {}) {
506
521
  async function addWithdrawToTx(tx, client, address, amount, options = {}) {
507
522
  const asset = options.asset ?? "USDC";
508
523
  const assetInfo = SUPPORTED_ASSETS[asset];
509
- const [config, pool, pools, allStates] = await Promise.all([
524
+ const [config, pool, pools, states] = await Promise.all([
510
525
  getConfig(),
511
526
  getPool(asset),
512
527
  getPools(),
513
- getUserState(client, address, true)
528
+ getUserState(client, address)
514
529
  ]);
515
- const assetState = allStates.find((s) => s.assetId === pool.id);
530
+ const assetState = states.find((s) => s.assetId === pool.id);
516
531
  const deposited = assetState ? compoundBalance(assetState.supplyBalance, pool.currentSupplyIndex) : 0;
517
532
  const effectiveAmount = Math.min(amount, Math.max(0, deposited - WITHDRAW_DUST_BUFFER));
518
533
  if (effectiveAmount <= 0) throw new T2000Error("NO_COLLATERAL", `Nothing to withdraw for ${assetInfo.displayName} on NAVI`);
519
534
  const rawAmount = Number(stableToRaw(effectiveAmount, assetInfo.decimals));
520
- addOracleUpdatesForPositions(tx, config, pools, allStates, pool);
535
+ await refreshStableOracles(tx, client, config, pools);
521
536
  const [balance] = tx.moveCall({
522
537
  target: `${config.package}::incentive_v3::withdraw_v2`,
523
538
  arguments: [
@@ -598,15 +613,14 @@ async function buildBorrowTx(client, address, amount, options = {}) {
598
613
  const asset = options.asset ?? "USDC";
599
614
  const assetInfo = SUPPORTED_ASSETS[asset];
600
615
  const rawAmount = Number(stableToRaw(amount, assetInfo.decimals));
601
- const [config, pool, pools, allStates] = await Promise.all([
616
+ const [config, pool, pools] = await Promise.all([
602
617
  getConfig(),
603
618
  getPool(asset),
604
- getPools(),
605
- getUserState(client, address, true)
619
+ getPools()
606
620
  ]);
607
621
  const tx = new Transaction();
608
622
  tx.setSender(address);
609
- addOracleUpdatesForPositions(tx, config, pools, allStates, pool);
623
+ await refreshStableOracles(tx, client, config, pools);
610
624
  const [balance] = tx.moveCall({
611
625
  target: `${config.package}::incentive_v3::borrow_v2`,
612
626
  arguments: [