@t2000/sdk 0.9.5 → 0.9.6

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.
@@ -353,18 +353,14 @@ function addOracleUpdate(tx, config, pool) {
353
353
  ]
354
354
  });
355
355
  }
356
- function addOracleUpdatesForPositions(tx, config, pools, states, primaryPool) {
356
+ function addOracleUpdatesForAllPools(tx, config, pools) {
357
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);
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);
363
361
  if (!pool) continue;
364
- const feed = config.oracle.feeds?.find((f2) => f2.assetId === pool.id);
365
- if (!feed) continue;
366
362
  addOracleUpdate(tx, config, pool);
367
- updated.add(pool.id);
363
+ updated.add(feed.assetId);
368
364
  }
369
365
  }
370
366
  function rateToApy(rawRate) {
@@ -392,7 +388,7 @@ function compoundBalance(rawBalance, currentIndex) {
392
388
  const result = (rawBalance * BigInt(currentIndex) + half) / scale;
393
389
  return Number(result) / 10 ** NAVI_BALANCE_DECIMALS;
394
390
  }
395
- async function getUserState(client, address, includeZero = false) {
391
+ async function getUserState(client, address) {
396
392
  const config = await getConfig();
397
393
  const tx = new transactions.Transaction();
398
394
  tx.moveCall({
@@ -410,7 +406,6 @@ async function getUserState(client, address, includeZero = false) {
410
406
  supplyBalance: toBigInt(s.supply_balance),
411
407
  borrowBalance: toBigInt(s.borrow_balance)
412
408
  }));
413
- if (includeZero) return mapped;
414
409
  return mapped.filter((s) => s.supplyBalance !== 0n || s.borrowBalance !== 0n);
415
410
  }
416
411
  async function fetchCoins(client, owner, coinType) {
@@ -468,20 +463,20 @@ async function buildSaveTx(client, address, amount, options = {}) {
468
463
  async function buildWithdrawTx(client, address, amount, options = {}) {
469
464
  const asset = options.asset ?? "USDC";
470
465
  const assetInfo = SUPPORTED_ASSETS[asset];
471
- const [config, pool, pools, allStates] = await Promise.all([
466
+ const [config, pool, pools, states] = await Promise.all([
472
467
  getConfig(),
473
468
  getPool(asset),
474
469
  getPools(),
475
- getUserState(client, address, true)
470
+ getUserState(client, address)
476
471
  ]);
477
- const assetState = allStates.find((s) => s.assetId === pool.id);
472
+ const assetState = states.find((s) => s.assetId === pool.id);
478
473
  const deposited = assetState ? compoundBalance(assetState.supplyBalance, pool.currentSupplyIndex) : 0;
479
474
  const effectiveAmount = Math.min(amount, Math.max(0, deposited - WITHDRAW_DUST_BUFFER));
480
475
  if (effectiveAmount <= 0) throw new T2000Error("NO_COLLATERAL", `Nothing to withdraw for ${assetInfo.displayName} on NAVI`);
481
476
  const rawAmount = Number(stableToRaw(effectiveAmount, assetInfo.decimals));
482
477
  const tx = new transactions.Transaction();
483
478
  tx.setSender(address);
484
- addOracleUpdatesForPositions(tx, config, pools, allStates, pool);
479
+ addOracleUpdatesForAllPools(tx, config, pools);
485
480
  const [balance] = tx.moveCall({
486
481
  target: `${config.package}::incentive_v3::withdraw_v2`,
487
482
  arguments: [
@@ -508,18 +503,18 @@ async function buildWithdrawTx(client, address, amount, options = {}) {
508
503
  async function addWithdrawToTx(tx, client, address, amount, options = {}) {
509
504
  const asset = options.asset ?? "USDC";
510
505
  const assetInfo = SUPPORTED_ASSETS[asset];
511
- const [config, pool, pools, allStates] = await Promise.all([
506
+ const [config, pool, pools, states] = await Promise.all([
512
507
  getConfig(),
513
508
  getPool(asset),
514
509
  getPools(),
515
- getUserState(client, address, true)
510
+ getUserState(client, address)
516
511
  ]);
517
- const assetState = allStates.find((s) => s.assetId === pool.id);
512
+ const assetState = states.find((s) => s.assetId === pool.id);
518
513
  const deposited = assetState ? compoundBalance(assetState.supplyBalance, pool.currentSupplyIndex) : 0;
519
514
  const effectiveAmount = Math.min(amount, Math.max(0, deposited - WITHDRAW_DUST_BUFFER));
520
515
  if (effectiveAmount <= 0) throw new T2000Error("NO_COLLATERAL", `Nothing to withdraw for ${assetInfo.displayName} on NAVI`);
521
516
  const rawAmount = Number(stableToRaw(effectiveAmount, assetInfo.decimals));
522
- addOracleUpdatesForPositions(tx, config, pools, allStates, pool);
517
+ addOracleUpdatesForAllPools(tx, config, pools);
523
518
  const [balance] = tx.moveCall({
524
519
  target: `${config.package}::incentive_v3::withdraw_v2`,
525
520
  arguments: [
@@ -600,15 +595,14 @@ async function buildBorrowTx(client, address, amount, options = {}) {
600
595
  const asset = options.asset ?? "USDC";
601
596
  const assetInfo = SUPPORTED_ASSETS[asset];
602
597
  const rawAmount = Number(stableToRaw(amount, assetInfo.decimals));
603
- const [config, pool, pools, allStates] = await Promise.all([
598
+ const [config, pool, pools] = await Promise.all([
604
599
  getConfig(),
605
600
  getPool(asset),
606
- getPools(),
607
- getUserState(client, address, true)
601
+ getPools()
608
602
  ]);
609
603
  const tx = new transactions.Transaction();
610
604
  tx.setSender(address);
611
- addOracleUpdatesForPositions(tx, config, pools, allStates, pool);
605
+ addOracleUpdatesForAllPools(tx, config, pools);
612
606
  const [balance] = tx.moveCall({
613
607
  target: `${config.package}::incentive_v3::borrow_v2`,
614
608
  arguments: [