@t2000/sdk 0.9.6 → 0.9.8

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,14 +606,33 @@ function addOracleUpdate(tx, config, pool) {
604
606
  ]
605
607
  });
606
608
  }
607
- function addOracleUpdatesForAllPools(tx, config, pools) {
608
- const updated = /* @__PURE__ */ new Set();
609
- for (const feed of config.oracle.feeds ?? []) {
610
- if (updated.has(feed.assetId)) continue;
611
- const pool = pools.find((p) => p.id === feed.assetId);
612
- if (!pool) 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 (err) {
631
+ console.error("[t2000] Pyth oracle push failed, falling back to cached prices:", err.message ?? err);
632
+ }
633
+ }
634
+ for (const pool of stablePools) {
613
635
  addOracleUpdate(tx, config, pool);
614
- updated.add(feed.assetId);
615
636
  }
616
637
  }
617
638
  function rateToApy(rawRate) {
@@ -725,9 +746,13 @@ async function buildWithdrawTx(client, address, amount, options = {}) {
725
746
  const effectiveAmount = Math.min(amount, Math.max(0, deposited - WITHDRAW_DUST_BUFFER));
726
747
  if (effectiveAmount <= 0) throw new T2000Error("NO_COLLATERAL", `Nothing to withdraw for ${assetInfo.displayName} on NAVI`);
727
748
  const rawAmount = Number(stableToRaw(effectiveAmount, assetInfo.decimals));
749
+ if (rawAmount <= 0) {
750
+ throw new T2000Error("INVALID_AMOUNT", `Withdrawal amount too small to represent (effective=${effectiveAmount}, raw=${rawAmount}, decimals=${assetInfo.decimals})`);
751
+ }
752
+ console.error(`[t2000] withdraw: asset=${asset} poolId=${pool.id} amount=${amount} deposited=${deposited} effective=${effectiveAmount} raw=${rawAmount} supplyBal=${assetState?.supplyBalance} index=${pool.currentSupplyIndex}`);
728
753
  const tx = new transactions.Transaction();
729
754
  tx.setSender(address);
730
- addOracleUpdatesForAllPools(tx, config, pools);
755
+ await refreshStableOracles(tx, client, config, pools);
731
756
  const [balance] = tx.moveCall({
732
757
  target: `${config.package}::incentive_v3::withdraw_v2`,
733
758
  arguments: [
@@ -765,7 +790,11 @@ async function addWithdrawToTx(tx, client, address, amount, options = {}) {
765
790
  const effectiveAmount = Math.min(amount, Math.max(0, deposited - WITHDRAW_DUST_BUFFER));
766
791
  if (effectiveAmount <= 0) throw new T2000Error("NO_COLLATERAL", `Nothing to withdraw for ${assetInfo.displayName} on NAVI`);
767
792
  const rawAmount = Number(stableToRaw(effectiveAmount, assetInfo.decimals));
768
- addOracleUpdatesForAllPools(tx, config, pools);
793
+ if (rawAmount <= 0) {
794
+ throw new T2000Error("INVALID_AMOUNT", `Withdrawal amount too small to represent (effective=${effectiveAmount}, raw=${rawAmount}, decimals=${assetInfo.decimals})`);
795
+ }
796
+ console.error(`[t2000] withdraw: asset=${asset} poolId=${pool.id} amount=${amount} deposited=${deposited} effective=${effectiveAmount} raw=${rawAmount} supplyBal=${assetState?.supplyBalance} index=${pool.currentSupplyIndex}`);
797
+ await refreshStableOracles(tx, client, config, pools);
769
798
  const [balance] = tx.moveCall({
770
799
  target: `${config.package}::incentive_v3::withdraw_v2`,
771
800
  arguments: [
@@ -853,7 +882,7 @@ async function buildBorrowTx(client, address, amount, options = {}) {
853
882
  ]);
854
883
  const tx = new transactions.Transaction();
855
884
  tx.setSender(address);
856
- addOracleUpdatesForAllPools(tx, config, pools);
885
+ await refreshStableOracles(tx, client, config, pools);
857
886
  const [balance] = tx.moveCall({
858
887
  target: `${config.package}::incentive_v3::borrow_v2`,
859
888
  arguments: [