@t2000/sdk 0.9.4 → 0.9.5

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
@@ -125,6 +125,7 @@ function mapMoveAbortCode(code) {
125
125
  10: "Already at current version",
126
126
  // NAVI Protocol abort codes
127
127
  1502: "Oracle price is stale \u2014 try again in a moment",
128
+ 1503: "Oracle validation failed during withdrawal \u2014 try again in a moment",
128
129
  1600: "Health factor too low \u2014 withdrawal would risk liquidation",
129
130
  1605: "Asset borrowing is disabled or at capacity on this protocol",
130
131
  // Cetus DEX abort codes
@@ -640,7 +641,7 @@ function compoundBalance(rawBalance, currentIndex) {
640
641
  const result = (rawBalance * BigInt(currentIndex) + half) / scale;
641
642
  return Number(result) / 10 ** NAVI_BALANCE_DECIMALS;
642
643
  }
643
- async function getUserState(client, address) {
644
+ async function getUserState(client, address, includeZero = false) {
644
645
  const config = await getConfig();
645
646
  const tx = new Transaction();
646
647
  tx.moveCall({
@@ -653,11 +654,13 @@ async function getUserState(client, address) {
653
654
  });
654
655
  const decoded = decodeDevInspect(result, bcs.vector(UserStateInfo));
655
656
  if (!decoded) return [];
656
- return decoded.map((s) => ({
657
+ const mapped = decoded.map((s) => ({
657
658
  assetId: s.asset_id,
658
659
  supplyBalance: toBigInt(s.supply_balance),
659
660
  borrowBalance: toBigInt(s.borrow_balance)
660
- })).filter((s) => s.supplyBalance !== 0n || s.borrowBalance !== 0n);
661
+ }));
662
+ if (includeZero) return mapped;
663
+ return mapped.filter((s) => s.supplyBalance !== 0n || s.borrowBalance !== 0n);
661
664
  }
662
665
  async function fetchCoins(client, owner, coinType) {
663
666
  const all = [];
@@ -714,20 +717,20 @@ async function buildSaveTx(client, address, amount, options = {}) {
714
717
  async function buildWithdrawTx(client, address, amount, options = {}) {
715
718
  const asset = options.asset ?? "USDC";
716
719
  const assetInfo = SUPPORTED_ASSETS[asset];
717
- const [config, pool, pools, states] = await Promise.all([
720
+ const [config, pool, pools, allStates] = await Promise.all([
718
721
  getConfig(),
719
722
  getPool(asset),
720
723
  getPools(),
721
- getUserState(client, address)
724
+ getUserState(client, address, true)
722
725
  ]);
723
- const assetState = states.find((s) => s.assetId === pool.id);
726
+ const assetState = allStates.find((s) => s.assetId === pool.id);
724
727
  const deposited = assetState ? compoundBalance(assetState.supplyBalance, pool.currentSupplyIndex) : 0;
725
728
  const effectiveAmount = Math.min(amount, Math.max(0, deposited - WITHDRAW_DUST_BUFFER));
726
729
  if (effectiveAmount <= 0) throw new T2000Error("NO_COLLATERAL", `Nothing to withdraw for ${assetInfo.displayName} on NAVI`);
727
730
  const rawAmount = Number(stableToRaw(effectiveAmount, assetInfo.decimals));
728
731
  const tx = new Transaction();
729
732
  tx.setSender(address);
730
- addOracleUpdatesForPositions(tx, config, pools, states, pool);
733
+ addOracleUpdatesForPositions(tx, config, pools, allStates, pool);
731
734
  const [balance] = tx.moveCall({
732
735
  target: `${config.package}::incentive_v3::withdraw_v2`,
733
736
  arguments: [
@@ -754,18 +757,18 @@ async function buildWithdrawTx(client, address, amount, options = {}) {
754
757
  async function addWithdrawToTx(tx, client, address, amount, options = {}) {
755
758
  const asset = options.asset ?? "USDC";
756
759
  const assetInfo = SUPPORTED_ASSETS[asset];
757
- const [config, pool, pools, states] = await Promise.all([
760
+ const [config, pool, pools, allStates] = await Promise.all([
758
761
  getConfig(),
759
762
  getPool(asset),
760
763
  getPools(),
761
- getUserState(client, address)
764
+ getUserState(client, address, true)
762
765
  ]);
763
- const assetState = states.find((s) => s.assetId === pool.id);
766
+ const assetState = allStates.find((s) => s.assetId === pool.id);
764
767
  const deposited = assetState ? compoundBalance(assetState.supplyBalance, pool.currentSupplyIndex) : 0;
765
768
  const effectiveAmount = Math.min(amount, Math.max(0, deposited - WITHDRAW_DUST_BUFFER));
766
769
  if (effectiveAmount <= 0) throw new T2000Error("NO_COLLATERAL", `Nothing to withdraw for ${assetInfo.displayName} on NAVI`);
767
770
  const rawAmount = Number(stableToRaw(effectiveAmount, assetInfo.decimals));
768
- addOracleUpdatesForPositions(tx, config, pools, states, pool);
771
+ addOracleUpdatesForPositions(tx, config, pools, allStates, pool);
769
772
  const [balance] = tx.moveCall({
770
773
  target: `${config.package}::incentive_v3::withdraw_v2`,
771
774
  arguments: [
@@ -846,15 +849,15 @@ async function buildBorrowTx(client, address, amount, options = {}) {
846
849
  const asset = options.asset ?? "USDC";
847
850
  const assetInfo = SUPPORTED_ASSETS[asset];
848
851
  const rawAmount = Number(stableToRaw(amount, assetInfo.decimals));
849
- const [config, pool, pools, states] = await Promise.all([
852
+ const [config, pool, pools, allStates] = await Promise.all([
850
853
  getConfig(),
851
854
  getPool(asset),
852
855
  getPools(),
853
- getUserState(client, address)
856
+ getUserState(client, address, true)
854
857
  ]);
855
858
  const tx = new Transaction();
856
859
  tx.setSender(address);
857
- addOracleUpdatesForPositions(tx, config, pools, states, pool);
860
+ addOracleUpdatesForPositions(tx, config, pools, allStates, pool);
858
861
  const [balance] = tx.moveCall({
859
862
  target: `${config.package}::incentive_v3::borrow_v2`,
860
863
  arguments: [