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