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