@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.
package/dist/index.js CHANGED
@@ -602,18 +602,14 @@ function addOracleUpdate(tx, config, pool) {
602
602
  ]
603
603
  });
604
604
  }
605
- function addOracleUpdatesForPositions(tx, config, pools, states, primaryPool) {
605
+ function addOracleUpdatesForAllPools(tx, config, pools) {
606
606
  const updated = /* @__PURE__ */ new Set();
607
- addOracleUpdate(tx, config, primaryPool);
608
- updated.add(primaryPool.id);
609
- for (const state of states) {
610
- if (updated.has(state.assetId)) continue;
611
- const pool = pools.find((p) => p.id === state.assetId);
607
+ for (const feed of config.oracle.feeds ?? []) {
608
+ if (updated.has(feed.assetId)) continue;
609
+ const pool = pools.find((p) => p.id === feed.assetId);
612
610
  if (!pool) continue;
613
- const feed = config.oracle.feeds?.find((f2) => f2.assetId === pool.id);
614
- if (!feed) continue;
615
611
  addOracleUpdate(tx, config, pool);
616
- updated.add(pool.id);
612
+ updated.add(feed.assetId);
617
613
  }
618
614
  }
619
615
  function rateToApy(rawRate) {
@@ -641,7 +637,7 @@ function compoundBalance(rawBalance, currentIndex) {
641
637
  const result = (rawBalance * BigInt(currentIndex) + half) / scale;
642
638
  return Number(result) / 10 ** NAVI_BALANCE_DECIMALS;
643
639
  }
644
- async function getUserState(client, address, includeZero = false) {
640
+ async function getUserState(client, address) {
645
641
  const config = await getConfig();
646
642
  const tx = new Transaction();
647
643
  tx.moveCall({
@@ -659,7 +655,6 @@ async function getUserState(client, address, includeZero = false) {
659
655
  supplyBalance: toBigInt(s.supply_balance),
660
656
  borrowBalance: toBigInt(s.borrow_balance)
661
657
  }));
662
- if (includeZero) return mapped;
663
658
  return mapped.filter((s) => s.supplyBalance !== 0n || s.borrowBalance !== 0n);
664
659
  }
665
660
  async function fetchCoins(client, owner, coinType) {
@@ -717,20 +712,20 @@ async function buildSaveTx(client, address, amount, options = {}) {
717
712
  async function buildWithdrawTx(client, address, amount, options = {}) {
718
713
  const asset = options.asset ?? "USDC";
719
714
  const assetInfo = SUPPORTED_ASSETS[asset];
720
- const [config, pool, pools, allStates] = await Promise.all([
715
+ const [config, pool, pools, states] = await Promise.all([
721
716
  getConfig(),
722
717
  getPool(asset),
723
718
  getPools(),
724
- getUserState(client, address, true)
719
+ getUserState(client, address)
725
720
  ]);
726
- const assetState = allStates.find((s) => s.assetId === pool.id);
721
+ const assetState = states.find((s) => s.assetId === pool.id);
727
722
  const deposited = assetState ? compoundBalance(assetState.supplyBalance, pool.currentSupplyIndex) : 0;
728
723
  const effectiveAmount = Math.min(amount, Math.max(0, deposited - WITHDRAW_DUST_BUFFER));
729
724
  if (effectiveAmount <= 0) throw new T2000Error("NO_COLLATERAL", `Nothing to withdraw for ${assetInfo.displayName} on NAVI`);
730
725
  const rawAmount = Number(stableToRaw(effectiveAmount, assetInfo.decimals));
731
726
  const tx = new Transaction();
732
727
  tx.setSender(address);
733
- addOracleUpdatesForPositions(tx, config, pools, allStates, pool);
728
+ addOracleUpdatesForAllPools(tx, config, pools);
734
729
  const [balance] = tx.moveCall({
735
730
  target: `${config.package}::incentive_v3::withdraw_v2`,
736
731
  arguments: [
@@ -757,18 +752,18 @@ async function buildWithdrawTx(client, address, amount, options = {}) {
757
752
  async function addWithdrawToTx(tx, client, address, amount, options = {}) {
758
753
  const asset = options.asset ?? "USDC";
759
754
  const assetInfo = SUPPORTED_ASSETS[asset];
760
- const [config, pool, pools, allStates] = await Promise.all([
755
+ const [config, pool, pools, states] = await Promise.all([
761
756
  getConfig(),
762
757
  getPool(asset),
763
758
  getPools(),
764
- getUserState(client, address, true)
759
+ getUserState(client, address)
765
760
  ]);
766
- const assetState = allStates.find((s) => s.assetId === pool.id);
761
+ const assetState = states.find((s) => s.assetId === pool.id);
767
762
  const deposited = assetState ? compoundBalance(assetState.supplyBalance, pool.currentSupplyIndex) : 0;
768
763
  const effectiveAmount = Math.min(amount, Math.max(0, deposited - WITHDRAW_DUST_BUFFER));
769
764
  if (effectiveAmount <= 0) throw new T2000Error("NO_COLLATERAL", `Nothing to withdraw for ${assetInfo.displayName} on NAVI`);
770
765
  const rawAmount = Number(stableToRaw(effectiveAmount, assetInfo.decimals));
771
- addOracleUpdatesForPositions(tx, config, pools, allStates, pool);
766
+ addOracleUpdatesForAllPools(tx, config, pools);
772
767
  const [balance] = tx.moveCall({
773
768
  target: `${config.package}::incentive_v3::withdraw_v2`,
774
769
  arguments: [
@@ -849,15 +844,14 @@ async function buildBorrowTx(client, address, amount, options = {}) {
849
844
  const asset = options.asset ?? "USDC";
850
845
  const assetInfo = SUPPORTED_ASSETS[asset];
851
846
  const rawAmount = Number(stableToRaw(amount, assetInfo.decimals));
852
- const [config, pool, pools, allStates] = await Promise.all([
847
+ const [config, pool, pools] = await Promise.all([
853
848
  getConfig(),
854
849
  getPool(asset),
855
- getPools(),
856
- getUserState(client, address, true)
850
+ getPools()
857
851
  ]);
858
852
  const tx = new Transaction();
859
853
  tx.setSender(address);
860
- addOracleUpdatesForPositions(tx, config, pools, allStates, pool);
854
+ addOracleUpdatesForAllPools(tx, config, pools);
861
855
  const [balance] = tx.moveCall({
862
856
  target: `${config.package}::incentive_v3::borrow_v2`,
863
857
  arguments: [