@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.
@@ -2,6 +2,7 @@
2
2
 
3
3
  var transactions = require('@mysten/sui/transactions');
4
4
  var bcs = require('@mysten/sui/bcs');
5
+ var pythSuiJs = require('@pythnetwork/pyth-sui-js');
5
6
  var aggregatorSdk = require('@cetusprotocol/aggregator-sdk');
6
7
  var utils = require('@mysten/sui/utils');
7
8
 
@@ -259,6 +260,7 @@ var NAVI_BALANCE_DECIMALS = 9;
259
260
  var CONFIG_API = "https://open-api.naviprotocol.io/api/navi/config?env=prod";
260
261
  var POOLS_API = "https://open-api.naviprotocol.io/api/navi/pools?env=prod";
261
262
  var PACKAGE_API = "https://open-api.naviprotocol.io/api/package";
263
+ var PYTH_HERMES_URL = "https://hermes.pyth.network/";
262
264
  var packageCache = null;
263
265
  function toBigInt(v) {
264
266
  if (typeof v === "bigint") return v;
@@ -353,14 +355,33 @@ function addOracleUpdate(tx, config, pool) {
353
355
  ]
354
356
  });
355
357
  }
356
- function addOracleUpdatesForAllPools(tx, config, pools) {
357
- const updated = /* @__PURE__ */ new Set();
358
- for (const feed of config.oracle.feeds ?? []) {
359
- if (updated.has(feed.assetId)) continue;
360
- const pool = pools.find((p) => p.id === feed.assetId);
361
- if (!pool) continue;
358
+ async function refreshStableOracles(tx, client, config, pools) {
359
+ const stableTypes = STABLE_ASSETS.map((a) => SUPPORTED_ASSETS[a].type);
360
+ const stablePools = pools.filter((p) => {
361
+ const ct = p.suiCoinType || p.coinType || "";
362
+ return stableTypes.some((t) => matchesCoinType(ct, t));
363
+ });
364
+ const feeds = (config.oracle.feeds ?? []).filter(
365
+ (f2) => stablePools.some((p) => p.id === f2.assetId)
366
+ );
367
+ if (feeds.length === 0) return;
368
+ const pythFeedIds = feeds.map((f2) => f2.pythPriceFeedId).filter(Boolean);
369
+ if (pythFeedIds.length > 0 && config.oracle.pythStateId && config.oracle.wormholeStateId) {
370
+ try {
371
+ const connection = new pythSuiJs.SuiPriceServiceConnection(PYTH_HERMES_URL);
372
+ const priceUpdateData = await connection.getPriceFeedsUpdateData(pythFeedIds);
373
+ const pythClient = new pythSuiJs.SuiPythClient(
374
+ client,
375
+ config.oracle.pythStateId,
376
+ config.oracle.wormholeStateId
377
+ );
378
+ await pythClient.updatePriceFeeds(tx, priceUpdateData, pythFeedIds);
379
+ } catch (err) {
380
+ console.error("[t2000] Pyth oracle push failed, falling back to cached prices:", err.message ?? err);
381
+ }
382
+ }
383
+ for (const pool of stablePools) {
362
384
  addOracleUpdate(tx, config, pool);
363
- updated.add(feed.assetId);
364
385
  }
365
386
  }
366
387
  function rateToApy(rawRate) {
@@ -474,9 +495,13 @@ async function buildWithdrawTx(client, address, amount, options = {}) {
474
495
  const effectiveAmount = Math.min(amount, Math.max(0, deposited - WITHDRAW_DUST_BUFFER));
475
496
  if (effectiveAmount <= 0) throw new T2000Error("NO_COLLATERAL", `Nothing to withdraw for ${assetInfo.displayName} on NAVI`);
476
497
  const rawAmount = Number(stableToRaw(effectiveAmount, assetInfo.decimals));
498
+ if (rawAmount <= 0) {
499
+ throw new T2000Error("INVALID_AMOUNT", `Withdrawal amount too small to represent (effective=${effectiveAmount}, raw=${rawAmount}, decimals=${assetInfo.decimals})`);
500
+ }
501
+ console.error(`[t2000] withdraw: asset=${asset} poolId=${pool.id} amount=${amount} deposited=${deposited} effective=${effectiveAmount} raw=${rawAmount} supplyBal=${assetState?.supplyBalance} index=${pool.currentSupplyIndex}`);
477
502
  const tx = new transactions.Transaction();
478
503
  tx.setSender(address);
479
- addOracleUpdatesForAllPools(tx, config, pools);
504
+ await refreshStableOracles(tx, client, config, pools);
480
505
  const [balance] = tx.moveCall({
481
506
  target: `${config.package}::incentive_v3::withdraw_v2`,
482
507
  arguments: [
@@ -514,7 +539,11 @@ async function addWithdrawToTx(tx, client, address, amount, options = {}) {
514
539
  const effectiveAmount = Math.min(amount, Math.max(0, deposited - WITHDRAW_DUST_BUFFER));
515
540
  if (effectiveAmount <= 0) throw new T2000Error("NO_COLLATERAL", `Nothing to withdraw for ${assetInfo.displayName} on NAVI`);
516
541
  const rawAmount = Number(stableToRaw(effectiveAmount, assetInfo.decimals));
517
- addOracleUpdatesForAllPools(tx, config, pools);
542
+ if (rawAmount <= 0) {
543
+ throw new T2000Error("INVALID_AMOUNT", `Withdrawal amount too small to represent (effective=${effectiveAmount}, raw=${rawAmount}, decimals=${assetInfo.decimals})`);
544
+ }
545
+ console.error(`[t2000] withdraw: asset=${asset} poolId=${pool.id} amount=${amount} deposited=${deposited} effective=${effectiveAmount} raw=${rawAmount} supplyBal=${assetState?.supplyBalance} index=${pool.currentSupplyIndex}`);
546
+ await refreshStableOracles(tx, client, config, pools);
518
547
  const [balance] = tx.moveCall({
519
548
  target: `${config.package}::incentive_v3::withdraw_v2`,
520
549
  arguments: [
@@ -602,7 +631,7 @@ async function buildBorrowTx(client, address, amount, options = {}) {
602
631
  ]);
603
632
  const tx = new transactions.Transaction();
604
633
  tx.setSender(address);
605
- addOracleUpdatesForAllPools(tx, config, pools);
634
+ await refreshStableOracles(tx, client, config, pools);
606
635
  const [balance] = tx.moveCall({
607
636
  target: `${config.package}::incentive_v3::borrow_v2`,
608
637
  arguments: [