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