@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.
package/dist/index.cjs CHANGED
@@ -11,6 +11,7 @@ var promises = require('fs/promises');
11
11
  var path = require('path');
12
12
  var os = require('os');
13
13
  var bcs = require('@mysten/sui/bcs');
14
+ var pythSuiJs = require('@pythnetwork/pyth-sui-js');
14
15
  var aggregatorSdk = require('@cetusprotocol/aggregator-sdk');
15
16
 
16
17
  // src/t2000.ts
@@ -510,6 +511,7 @@ var NAVI_BALANCE_DECIMALS = 9;
510
511
  var CONFIG_API = "https://open-api.naviprotocol.io/api/navi/config?env=prod";
511
512
  var POOLS_API = "https://open-api.naviprotocol.io/api/navi/pools?env=prod";
512
513
  var PACKAGE_API = "https://open-api.naviprotocol.io/api/package";
514
+ var PYTH_HERMES_URL = "https://hermes.pyth.network/";
513
515
  var packageCache = null;
514
516
  function toBigInt(v) {
515
517
  if (typeof v === "bigint") return v;
@@ -604,18 +606,32 @@ function addOracleUpdate(tx, config, pool) {
604
606
  ]
605
607
  });
606
608
  }
607
- function addOracleUpdatesForPositions(tx, config, pools, states, primaryPool) {
608
- const updated = /* @__PURE__ */ new Set();
609
- addOracleUpdate(tx, config, primaryPool);
610
- updated.add(primaryPool.id);
611
- for (const state of states) {
612
- if (updated.has(state.assetId)) continue;
613
- const pool = pools.find((p) => p.id === state.assetId);
614
- if (!pool) continue;
615
- const feed = config.oracle.feeds?.find((f2) => f2.assetId === pool.id);
616
- if (!feed) continue;
609
+ async function refreshStableOracles(tx, client, config, pools) {
610
+ const stableTypes = STABLE_ASSETS.map((a) => SUPPORTED_ASSETS[a].type);
611
+ const stablePools = pools.filter((p) => {
612
+ const ct = p.suiCoinType || p.coinType || "";
613
+ return stableTypes.some((t) => matchesCoinType(ct, t));
614
+ });
615
+ const feeds = (config.oracle.feeds ?? []).filter(
616
+ (f2) => stablePools.some((p) => p.id === f2.assetId)
617
+ );
618
+ if (feeds.length === 0) return;
619
+ const pythFeedIds = feeds.map((f2) => f2.pythPriceFeedId).filter(Boolean);
620
+ if (pythFeedIds.length > 0 && config.oracle.pythStateId && config.oracle.wormholeStateId) {
621
+ try {
622
+ const connection = new pythSuiJs.SuiPriceServiceConnection(PYTH_HERMES_URL);
623
+ const priceUpdateData = await connection.getPriceFeedsUpdateData(pythFeedIds);
624
+ const pythClient = new pythSuiJs.SuiPythClient(
625
+ client,
626
+ config.oracle.pythStateId,
627
+ config.oracle.wormholeStateId
628
+ );
629
+ await pythClient.updatePriceFeeds(tx, priceUpdateData, pythFeedIds);
630
+ } catch {
631
+ }
632
+ }
633
+ for (const pool of stablePools) {
617
634
  addOracleUpdate(tx, config, pool);
618
- updated.add(pool.id);
619
635
  }
620
636
  }
621
637
  function rateToApy(rawRate) {
@@ -643,7 +659,7 @@ function compoundBalance(rawBalance, currentIndex) {
643
659
  const result = (rawBalance * BigInt(currentIndex) + half) / scale;
644
660
  return Number(result) / 10 ** NAVI_BALANCE_DECIMALS;
645
661
  }
646
- async function getUserState(client, address, includeZero = false) {
662
+ async function getUserState(client, address) {
647
663
  const config = await getConfig();
648
664
  const tx = new transactions.Transaction();
649
665
  tx.moveCall({
@@ -661,7 +677,6 @@ async function getUserState(client, address, includeZero = false) {
661
677
  supplyBalance: toBigInt(s.supply_balance),
662
678
  borrowBalance: toBigInt(s.borrow_balance)
663
679
  }));
664
- if (includeZero) return mapped;
665
680
  return mapped.filter((s) => s.supplyBalance !== 0n || s.borrowBalance !== 0n);
666
681
  }
667
682
  async function fetchCoins(client, owner, coinType) {
@@ -719,20 +734,20 @@ async function buildSaveTx(client, address, amount, options = {}) {
719
734
  async function buildWithdrawTx(client, address, amount, options = {}) {
720
735
  const asset = options.asset ?? "USDC";
721
736
  const assetInfo = SUPPORTED_ASSETS[asset];
722
- const [config, pool, pools, allStates] = await Promise.all([
737
+ const [config, pool, pools, states] = await Promise.all([
723
738
  getConfig(),
724
739
  getPool(asset),
725
740
  getPools(),
726
- getUserState(client, address, true)
741
+ getUserState(client, address)
727
742
  ]);
728
- const assetState = allStates.find((s) => s.assetId === pool.id);
743
+ const assetState = states.find((s) => s.assetId === pool.id);
729
744
  const deposited = assetState ? compoundBalance(assetState.supplyBalance, pool.currentSupplyIndex) : 0;
730
745
  const effectiveAmount = Math.min(amount, Math.max(0, deposited - WITHDRAW_DUST_BUFFER));
731
746
  if (effectiveAmount <= 0) throw new T2000Error("NO_COLLATERAL", `Nothing to withdraw for ${assetInfo.displayName} on NAVI`);
732
747
  const rawAmount = Number(stableToRaw(effectiveAmount, assetInfo.decimals));
733
748
  const tx = new transactions.Transaction();
734
749
  tx.setSender(address);
735
- addOracleUpdatesForPositions(tx, config, pools, allStates, pool);
750
+ await refreshStableOracles(tx, client, config, pools);
736
751
  const [balance] = tx.moveCall({
737
752
  target: `${config.package}::incentive_v3::withdraw_v2`,
738
753
  arguments: [
@@ -759,18 +774,18 @@ async function buildWithdrawTx(client, address, amount, options = {}) {
759
774
  async function addWithdrawToTx(tx, client, address, amount, options = {}) {
760
775
  const asset = options.asset ?? "USDC";
761
776
  const assetInfo = SUPPORTED_ASSETS[asset];
762
- const [config, pool, pools, allStates] = await Promise.all([
777
+ const [config, pool, pools, states] = await Promise.all([
763
778
  getConfig(),
764
779
  getPool(asset),
765
780
  getPools(),
766
- getUserState(client, address, true)
781
+ getUserState(client, address)
767
782
  ]);
768
- const assetState = allStates.find((s) => s.assetId === pool.id);
783
+ const assetState = states.find((s) => s.assetId === pool.id);
769
784
  const deposited = assetState ? compoundBalance(assetState.supplyBalance, pool.currentSupplyIndex) : 0;
770
785
  const effectiveAmount = Math.min(amount, Math.max(0, deposited - WITHDRAW_DUST_BUFFER));
771
786
  if (effectiveAmount <= 0) throw new T2000Error("NO_COLLATERAL", `Nothing to withdraw for ${assetInfo.displayName} on NAVI`);
772
787
  const rawAmount = Number(stableToRaw(effectiveAmount, assetInfo.decimals));
773
- addOracleUpdatesForPositions(tx, config, pools, allStates, pool);
788
+ await refreshStableOracles(tx, client, config, pools);
774
789
  const [balance] = tx.moveCall({
775
790
  target: `${config.package}::incentive_v3::withdraw_v2`,
776
791
  arguments: [
@@ -851,15 +866,14 @@ async function buildBorrowTx(client, address, amount, options = {}) {
851
866
  const asset = options.asset ?? "USDC";
852
867
  const assetInfo = SUPPORTED_ASSETS[asset];
853
868
  const rawAmount = Number(stableToRaw(amount, assetInfo.decimals));
854
- const [config, pool, pools, allStates] = await Promise.all([
869
+ const [config, pool, pools] = await Promise.all([
855
870
  getConfig(),
856
871
  getPool(asset),
857
- getPools(),
858
- getUserState(client, address, true)
872
+ getPools()
859
873
  ]);
860
874
  const tx = new transactions.Transaction();
861
875
  tx.setSender(address);
862
- addOracleUpdatesForPositions(tx, config, pools, allStates, pool);
876
+ await refreshStableOracles(tx, client, config, pools);
863
877
  const [balance] = tx.moveCall({
864
878
  target: `${config.package}::incentive_v3::borrow_v2`,
865
879
  arguments: [