@t2000/sdk 0.17.0 → 0.17.2
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/adapters/index.cjs +21 -9
- package/dist/adapters/index.cjs.map +1 -1
- package/dist/adapters/index.js +21 -9
- package/dist/adapters/index.js.map +1 -1
- package/dist/index.cjs +25 -13
- package/dist/index.cjs.map +1 -1
- package/dist/index.js +25 -13
- package/dist/index.js.map +1 -1
- package/package.json +1 -1
package/dist/index.js
CHANGED
|
@@ -589,7 +589,9 @@ SUPPORTED_ASSETS.USDC.type;
|
|
|
589
589
|
var RATE_DECIMALS = 27;
|
|
590
590
|
var LTV_DECIMALS = 27;
|
|
591
591
|
var MIN_HEALTH_FACTOR = 1.5;
|
|
592
|
-
|
|
592
|
+
function withdrawDustBuffer(decimals) {
|
|
593
|
+
return 1e3 / 10 ** decimals;
|
|
594
|
+
}
|
|
593
595
|
var CLOCK = "0x06";
|
|
594
596
|
var SUI_SYSTEM_STATE = "0x05";
|
|
595
597
|
var NAVI_BALANCE_DECIMALS = 9;
|
|
@@ -712,6 +714,16 @@ function rateToApy(rawRate) {
|
|
|
712
714
|
if (!rawRate || rawRate === "0") return 0;
|
|
713
715
|
return Number(BigInt(rawRate)) / 10 ** RATE_DECIMALS * 100;
|
|
714
716
|
}
|
|
717
|
+
function poolSaveApy(pool) {
|
|
718
|
+
const incentive = parseFloat(pool.supplyIncentiveApyInfo?.apy ?? "0");
|
|
719
|
+
if (incentive > 0) return incentive;
|
|
720
|
+
return rateToApy(pool.currentSupplyRate);
|
|
721
|
+
}
|
|
722
|
+
function poolBorrowApy(pool) {
|
|
723
|
+
const incentive = parseFloat(pool.borrowIncentiveApyInfo?.apy ?? "0");
|
|
724
|
+
if (incentive > 0) return incentive;
|
|
725
|
+
return rateToApy(pool.currentBorrowRate);
|
|
726
|
+
}
|
|
715
727
|
function parseLtv(rawLtv) {
|
|
716
728
|
if (!rawLtv || rawLtv === "0") return 0.75;
|
|
717
729
|
return Number(BigInt(rawLtv)) / 10 ** LTV_DECIMALS;
|
|
@@ -821,7 +833,7 @@ async function buildWithdrawTx(client, address, amount, options = {}) {
|
|
|
821
833
|
]);
|
|
822
834
|
const assetState = states.find((s) => s.assetId === pool.id);
|
|
823
835
|
const deposited = assetState ? compoundBalance(assetState.supplyBalance, pool.currentSupplyIndex, pool) : 0;
|
|
824
|
-
const effectiveAmount = Math.min(amount, Math.max(0, deposited -
|
|
836
|
+
const effectiveAmount = Math.min(amount, Math.max(0, deposited - withdrawDustBuffer(assetInfo.decimals)));
|
|
825
837
|
if (effectiveAmount <= 0) throw new T2000Error("NO_COLLATERAL", `Nothing to withdraw for ${assetInfo.displayName} on NAVI`);
|
|
826
838
|
const rawAmount = Number(stableToRaw(effectiveAmount, assetInfo.decimals));
|
|
827
839
|
if (rawAmount <= 0) {
|
|
@@ -864,7 +876,7 @@ async function addWithdrawToTx(tx, client, address, amount, options = {}) {
|
|
|
864
876
|
]);
|
|
865
877
|
const assetState = states.find((s) => s.assetId === pool.id);
|
|
866
878
|
const deposited = assetState ? compoundBalance(assetState.supplyBalance, pool.currentSupplyIndex, pool) : 0;
|
|
867
|
-
const effectiveAmount = Math.min(amount, Math.max(0, deposited -
|
|
879
|
+
const effectiveAmount = Math.min(amount, Math.max(0, deposited - withdrawDustBuffer(assetInfo.decimals)));
|
|
868
880
|
if (effectiveAmount <= 0) throw new T2000Error("NO_COLLATERAL", `Nothing to withdraw for ${assetInfo.displayName} on NAVI`);
|
|
869
881
|
const rawAmount = Number(stableToRaw(effectiveAmount, assetInfo.decimals));
|
|
870
882
|
if (rawAmount <= 0) {
|
|
@@ -1102,10 +1114,10 @@ async function getRates(client) {
|
|
|
1102
1114
|
const targetType = SUPPORTED_ASSETS[asset].type;
|
|
1103
1115
|
const pool = pools.find((p) => matchesCoinType(p.suiCoinType || p.coinType || "", targetType));
|
|
1104
1116
|
if (!pool) continue;
|
|
1105
|
-
let saveApy =
|
|
1106
|
-
let borrowApy =
|
|
1107
|
-
if (saveApy <= 0 || saveApy >
|
|
1108
|
-
if (borrowApy <= 0 || borrowApy >
|
|
1117
|
+
let saveApy = poolSaveApy(pool);
|
|
1118
|
+
let borrowApy = poolBorrowApy(pool);
|
|
1119
|
+
if (saveApy <= 0 || saveApy > 200) saveApy = 0;
|
|
1120
|
+
if (borrowApy <= 0 || borrowApy > 200) borrowApy = 0;
|
|
1109
1121
|
result[asset] = { saveApy, borrowApy };
|
|
1110
1122
|
}
|
|
1111
1123
|
if (!result.USDC) result.USDC = { saveApy: 4, borrowApy: 6 };
|
|
@@ -1130,7 +1142,7 @@ async function getPositions(client, addressOrKeypair) {
|
|
|
1130
1142
|
asset: symbol,
|
|
1131
1143
|
type: "save",
|
|
1132
1144
|
amount: supplyBal,
|
|
1133
|
-
apy:
|
|
1145
|
+
apy: poolSaveApy(pool)
|
|
1134
1146
|
});
|
|
1135
1147
|
}
|
|
1136
1148
|
if (borrowBal > 1e-4) {
|
|
@@ -1139,7 +1151,7 @@ async function getPositions(client, addressOrKeypair) {
|
|
|
1139
1151
|
asset: symbol,
|
|
1140
1152
|
type: "borrow",
|
|
1141
1153
|
amount: borrowBal,
|
|
1142
|
-
apy:
|
|
1154
|
+
apy: poolBorrowApy(pool)
|
|
1143
1155
|
});
|
|
1144
1156
|
}
|
|
1145
1157
|
}
|
|
@@ -4425,10 +4437,6 @@ To sell investment: t2000 invest sell ${params.amount} ${fromAsset}`,
|
|
|
4425
4437
|
if (!pos || pos.totalAmount <= 0) {
|
|
4426
4438
|
throw new T2000Error("INSUFFICIENT_INVESTMENT", `No ${params.asset} position to earn on`);
|
|
4427
4439
|
}
|
|
4428
|
-
if (pos.earning) {
|
|
4429
|
-
throw new T2000Error("INVEST_ALREADY_EARNING", `${params.asset} is already earning via ${pos.earningProtocol}`);
|
|
4430
|
-
}
|
|
4431
|
-
const { adapter, rate } = await this.registry.bestSaveRate(params.asset);
|
|
4432
4440
|
const assetInfo = SUPPORTED_ASSETS[params.asset];
|
|
4433
4441
|
const assetBalance = await this.client.getBalance({
|
|
4434
4442
|
owner: this._address,
|
|
@@ -4437,9 +4445,13 @@ To sell investment: t2000 invest sell ${params.amount} ${fromAsset}`,
|
|
|
4437
4445
|
const walletAmount = Number(assetBalance.totalBalance) / 10 ** assetInfo.decimals;
|
|
4438
4446
|
const gasReserve = params.asset === "SUI" ? GAS_RESERVE_MIN : 0;
|
|
4439
4447
|
const depositAmount = Math.max(0, walletAmount - gasReserve);
|
|
4448
|
+
if (pos.earning && depositAmount <= 0) {
|
|
4449
|
+
throw new T2000Error("INVEST_ALREADY_EARNING", `${params.asset} is already earning via ${pos.earningProtocol}`);
|
|
4450
|
+
}
|
|
4440
4451
|
if (depositAmount <= 0) {
|
|
4441
4452
|
throw new T2000Error("INSUFFICIENT_BALANCE", `No ${params.asset} available to deposit (wallet: ${walletAmount}, gas reserve: ${gasReserve})`);
|
|
4442
4453
|
}
|
|
4454
|
+
const { adapter, rate } = await this.registry.bestSaveRate(params.asset);
|
|
4443
4455
|
const gasResult = await executeWithGas(this.client, this.keypair, async () => {
|
|
4444
4456
|
const { tx } = await adapter.buildSaveTx(this._address, depositAmount, params.asset);
|
|
4445
4457
|
return tx;
|