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