@t2000/sdk 0.16.22 → 0.16.24
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/LICENSE +21 -0
- package/dist/adapters/index.cjs +13 -8
- package/dist/adapters/index.cjs.map +1 -1
- package/dist/adapters/index.js +13 -8
- package/dist/adapters/index.js.map +1 -1
- package/dist/index.cjs +60 -45
- package/dist/index.cjs.map +1 -1
- package/dist/index.js +60 -45
- package/dist/index.js.map +1 -1
- package/package.json +12 -12
package/dist/index.cjs
CHANGED
|
@@ -439,11 +439,18 @@ async function queryBalance(client, address) {
|
|
|
439
439
|
const stableBalancePromises = STABLE_ASSETS.map(
|
|
440
440
|
(asset) => client.getBalance({ owner: address, coinType: SUPPORTED_ASSETS[asset].type }).then((b) => ({ asset, amount: Number(b.totalBalance) / 10 ** SUPPORTED_ASSETS[asset].decimals }))
|
|
441
441
|
);
|
|
442
|
-
const
|
|
442
|
+
const nonSuiInvestmentAssets = Object.keys(INVESTMENT_ASSETS).filter((a) => a !== "SUI");
|
|
443
|
+
const investBalancePromises = nonSuiInvestmentAssets.map(
|
|
444
|
+
(asset) => client.getBalance({ owner: address, coinType: INVESTMENT_ASSETS[asset].type }).then((b) => ({ asset, amount: Number(b.totalBalance) / 10 ** INVESTMENT_ASSETS[asset].decimals }))
|
|
445
|
+
);
|
|
446
|
+
const [suiBalance, suiPriceUsd, ...rest] = await Promise.all([
|
|
443
447
|
client.getBalance({ owner: address, coinType: SUPPORTED_ASSETS.SUI.type }),
|
|
444
448
|
fetchSuiPrice(client),
|
|
445
|
-
...stableBalancePromises
|
|
449
|
+
...stableBalancePromises,
|
|
450
|
+
...investBalancePromises
|
|
446
451
|
]);
|
|
452
|
+
const stableResults = rest.slice(0, STABLE_ASSETS.length);
|
|
453
|
+
const investResults = rest.slice(STABLE_ASSETS.length);
|
|
447
454
|
const stables = {};
|
|
448
455
|
let totalStables = 0;
|
|
449
456
|
for (const { asset, amount } of stableResults) {
|
|
@@ -454,6 +461,13 @@ async function queryBalance(client, address) {
|
|
|
454
461
|
const savings = 0;
|
|
455
462
|
const usdEquiv = suiAmount * suiPriceUsd;
|
|
456
463
|
const total = totalStables + savings + usdEquiv;
|
|
464
|
+
const assets = {
|
|
465
|
+
USDC: stables.USDC ?? 0,
|
|
466
|
+
SUI: suiAmount
|
|
467
|
+
};
|
|
468
|
+
for (const { asset, amount } of investResults) {
|
|
469
|
+
assets[asset] = amount;
|
|
470
|
+
}
|
|
457
471
|
return {
|
|
458
472
|
available: totalStables,
|
|
459
473
|
savings,
|
|
@@ -466,10 +480,7 @@ async function queryBalance(client, address) {
|
|
|
466
480
|
},
|
|
467
481
|
total,
|
|
468
482
|
stables,
|
|
469
|
-
assets
|
|
470
|
-
USDC: stables.USDC ?? 0,
|
|
471
|
-
SUI: suiAmount
|
|
472
|
-
}
|
|
483
|
+
assets
|
|
473
484
|
};
|
|
474
485
|
}
|
|
475
486
|
|
|
@@ -696,12 +707,17 @@ function normalizeHealthFactor(raw) {
|
|
|
696
707
|
const v = raw / 10 ** RATE_DECIMALS;
|
|
697
708
|
return v > 1e5 ? Infinity : v;
|
|
698
709
|
}
|
|
699
|
-
function
|
|
710
|
+
function naviStorageDecimals(poolId, tokenDecimals) {
|
|
711
|
+
if (poolId <= 10) return NAVI_BALANCE_DECIMALS;
|
|
712
|
+
return tokenDecimals;
|
|
713
|
+
}
|
|
714
|
+
function compoundBalance(rawBalance, currentIndex, pool) {
|
|
700
715
|
if (!rawBalance || !currentIndex || currentIndex === "0") return 0;
|
|
701
716
|
const scale = BigInt("1" + "0".repeat(RATE_DECIMALS));
|
|
702
717
|
const half = scale / 2n;
|
|
703
718
|
const result = (rawBalance * BigInt(currentIndex) + half) / scale;
|
|
704
|
-
|
|
719
|
+
const decimals = pool ? naviStorageDecimals(pool.id, pool.token.decimals) : NAVI_BALANCE_DECIMALS;
|
|
720
|
+
return Number(result) / 10 ** decimals;
|
|
705
721
|
}
|
|
706
722
|
async function getUserState(client, address) {
|
|
707
723
|
const config = await getConfig();
|
|
@@ -785,7 +801,7 @@ async function buildWithdrawTx(client, address, amount, options = {}) {
|
|
|
785
801
|
getUserState(client, address)
|
|
786
802
|
]);
|
|
787
803
|
const assetState = states.find((s) => s.assetId === pool.id);
|
|
788
|
-
const deposited = assetState ? compoundBalance(assetState.supplyBalance, pool.currentSupplyIndex) : 0;
|
|
804
|
+
const deposited = assetState ? compoundBalance(assetState.supplyBalance, pool.currentSupplyIndex, pool) : 0;
|
|
789
805
|
const effectiveAmount = Math.min(amount, Math.max(0, deposited - WITHDRAW_DUST_BUFFER));
|
|
790
806
|
if (effectiveAmount <= 0) throw new T2000Error("NO_COLLATERAL", `Nothing to withdraw for ${assetInfo.displayName} on NAVI`);
|
|
791
807
|
const rawAmount = Number(stableToRaw(effectiveAmount, assetInfo.decimals));
|
|
@@ -828,7 +844,7 @@ async function addWithdrawToTx(tx, client, address, amount, options = {}) {
|
|
|
828
844
|
getUserState(client, address)
|
|
829
845
|
]);
|
|
830
846
|
const assetState = states.find((s) => s.assetId === pool.id);
|
|
831
|
-
const deposited = assetState ? compoundBalance(assetState.supplyBalance, pool.currentSupplyIndex) : 0;
|
|
847
|
+
const deposited = assetState ? compoundBalance(assetState.supplyBalance, pool.currentSupplyIndex, pool) : 0;
|
|
832
848
|
const effectiveAmount = Math.min(amount, Math.max(0, deposited - WITHDRAW_DUST_BUFFER));
|
|
833
849
|
if (effectiveAmount <= 0) throw new T2000Error("NO_COLLATERAL", `Nothing to withdraw for ${assetInfo.displayName} on NAVI`);
|
|
834
850
|
const rawAmount = Number(stableToRaw(effectiveAmount, assetInfo.decimals));
|
|
@@ -999,8 +1015,8 @@ async function getHealthFactor(client, addressOrKeypair) {
|
|
|
999
1015
|
for (const state of states) {
|
|
1000
1016
|
const pool = pools.find((p) => p.id === state.assetId);
|
|
1001
1017
|
if (!pool) continue;
|
|
1002
|
-
const supplyBal = compoundBalance(state.supplyBalance, pool.currentSupplyIndex);
|
|
1003
|
-
const borrowBal = compoundBalance(state.borrowBalance, pool.currentBorrowIndex);
|
|
1018
|
+
const supplyBal = compoundBalance(state.supplyBalance, pool.currentSupplyIndex, pool);
|
|
1019
|
+
const borrowBal = compoundBalance(state.borrowBalance, pool.currentBorrowIndex, pool);
|
|
1004
1020
|
const price = pool.token?.price ?? 1;
|
|
1005
1021
|
supplied += supplyBal * price;
|
|
1006
1022
|
borrowed += borrowBal * price;
|
|
@@ -1087,8 +1103,8 @@ async function getPositions(client, addressOrKeypair) {
|
|
|
1087
1103
|
const pool = pools.find((p) => p.id === state.assetId);
|
|
1088
1104
|
if (!pool) continue;
|
|
1089
1105
|
const symbol = resolvePoolSymbol(pool);
|
|
1090
|
-
const supplyBal = compoundBalance(state.supplyBalance, pool.currentSupplyIndex);
|
|
1091
|
-
const borrowBal = compoundBalance(state.borrowBalance, pool.currentBorrowIndex);
|
|
1106
|
+
const supplyBal = compoundBalance(state.supplyBalance, pool.currentSupplyIndex, pool);
|
|
1107
|
+
const borrowBal = compoundBalance(state.borrowBalance, pool.currentBorrowIndex, pool);
|
|
1092
1108
|
if (supplyBal > 1e-4) {
|
|
1093
1109
|
positions.push({
|
|
1094
1110
|
protocol: "navi",
|
|
@@ -3468,45 +3484,44 @@ To access invested funds: t2000 invest sell ${params.amount} ${asset}`,
|
|
|
3468
3484
|
}
|
|
3469
3485
|
let investmentValue = 0;
|
|
3470
3486
|
let investmentCostBasis = 0;
|
|
3487
|
+
const trackedAmounts = {};
|
|
3488
|
+
const trackedCostBasis = {};
|
|
3489
|
+
const earningAssetSet = /* @__PURE__ */ new Set();
|
|
3471
3490
|
for (const pos of portfolioPositions) {
|
|
3472
3491
|
if (!(pos.asset in INVESTMENT_ASSETS)) continue;
|
|
3473
|
-
|
|
3474
|
-
|
|
3475
|
-
|
|
3476
|
-
investmentCostBasis += pos.costBasis;
|
|
3477
|
-
if (pos.asset === "SUI") {
|
|
3478
|
-
const gasSui = Math.max(0, bal.gasReserve.sui);
|
|
3479
|
-
bal.gasReserve = { sui: gasSui, usdEquiv: gasSui * price };
|
|
3480
|
-
}
|
|
3481
|
-
} else if (pos.asset === "SUI") {
|
|
3482
|
-
const actualHeld = Math.min(pos.totalAmount, bal.gasReserve.sui);
|
|
3483
|
-
investmentValue += actualHeld * price;
|
|
3484
|
-
if (actualHeld < pos.totalAmount && pos.totalAmount > 0) {
|
|
3485
|
-
investmentCostBasis += pos.costBasis * (actualHeld / pos.totalAmount);
|
|
3486
|
-
} else {
|
|
3487
|
-
investmentCostBasis += pos.costBasis;
|
|
3488
|
-
}
|
|
3489
|
-
const gasSui = Math.max(0, bal.gasReserve.sui - pos.totalAmount);
|
|
3490
|
-
bal.gasReserve = { sui: gasSui, usdEquiv: gasSui * price };
|
|
3491
|
-
} else {
|
|
3492
|
-
investmentValue += pos.totalAmount * price;
|
|
3493
|
-
investmentCostBasis += pos.costBasis;
|
|
3494
|
-
}
|
|
3492
|
+
trackedAmounts[pos.asset] = (trackedAmounts[pos.asset] ?? 0) + pos.totalAmount;
|
|
3493
|
+
trackedCostBasis[pos.asset] = (trackedCostBasis[pos.asset] ?? 0) + pos.costBasis;
|
|
3494
|
+
if (pos.earning) earningAssetSet.add(pos.asset);
|
|
3495
3495
|
}
|
|
3496
|
-
let strategySuiTotal = 0;
|
|
3497
3496
|
for (const key of this.portfolio.getAllStrategyKeys()) {
|
|
3498
3497
|
for (const sp of this.portfolio.getStrategyPositions(key)) {
|
|
3499
3498
|
if (!(sp.asset in INVESTMENT_ASSETS)) continue;
|
|
3500
|
-
|
|
3501
|
-
|
|
3502
|
-
investmentCostBasis += sp.costBasis;
|
|
3503
|
-
if (sp.asset === "SUI") strategySuiTotal += sp.totalAmount;
|
|
3499
|
+
trackedAmounts[sp.asset] = (trackedAmounts[sp.asset] ?? 0) + sp.totalAmount;
|
|
3500
|
+
trackedCostBasis[sp.asset] = (trackedCostBasis[sp.asset] ?? 0) + sp.costBasis;
|
|
3504
3501
|
}
|
|
3505
3502
|
}
|
|
3506
|
-
|
|
3507
|
-
const
|
|
3508
|
-
const
|
|
3509
|
-
|
|
3503
|
+
for (const asset of Object.keys(INVESTMENT_ASSETS)) {
|
|
3504
|
+
const price = assetPrices[asset] ?? 0;
|
|
3505
|
+
const tracked = trackedAmounts[asset] ?? 0;
|
|
3506
|
+
const costBasis = trackedCostBasis[asset] ?? 0;
|
|
3507
|
+
if (asset === "SUI") {
|
|
3508
|
+
const actualSui = earningAssetSet.has("SUI") ? tracked : Math.min(tracked, bal.gasReserve.sui);
|
|
3509
|
+
investmentValue += actualSui * price;
|
|
3510
|
+
if (actualSui < tracked && tracked > 0) {
|
|
3511
|
+
investmentCostBasis += costBasis * (actualSui / tracked);
|
|
3512
|
+
} else {
|
|
3513
|
+
investmentCostBasis += costBasis;
|
|
3514
|
+
}
|
|
3515
|
+
if (!earningAssetSet.has("SUI")) {
|
|
3516
|
+
const gasSui = Math.max(0, bal.gasReserve.sui - tracked);
|
|
3517
|
+
bal.gasReserve = { sui: gasSui, usdEquiv: gasSui * price };
|
|
3518
|
+
}
|
|
3519
|
+
} else {
|
|
3520
|
+
const onChainAmount = bal.assets[asset] ?? 0;
|
|
3521
|
+
const effectiveAmount = Math.max(tracked, onChainAmount);
|
|
3522
|
+
investmentValue += effectiveAmount * price;
|
|
3523
|
+
investmentCostBasis += costBasis;
|
|
3524
|
+
}
|
|
3510
3525
|
}
|
|
3511
3526
|
bal.investment = investmentValue;
|
|
3512
3527
|
bal.investmentPnL = investmentValue - investmentCostBasis;
|