@t2000/sdk 0.9.5 → 0.9.7
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 +39 -25
- package/dist/adapters/index.cjs.map +1 -1
- package/dist/adapters/index.js +39 -25
- package/dist/adapters/index.js.map +1 -1
- package/dist/index.cjs +39 -25
- package/dist/index.cjs.map +1 -1
- package/dist/index.js +39 -25
- package/dist/index.js.map +1 -1
- package/package.json +2 -1
package/dist/index.cjs
CHANGED
|
@@ -11,6 +11,7 @@ var promises = require('fs/promises');
|
|
|
11
11
|
var path = require('path');
|
|
12
12
|
var os = require('os');
|
|
13
13
|
var bcs = require('@mysten/sui/bcs');
|
|
14
|
+
var pythSuiJs = require('@pythnetwork/pyth-sui-js');
|
|
14
15
|
var aggregatorSdk = require('@cetusprotocol/aggregator-sdk');
|
|
15
16
|
|
|
16
17
|
// src/t2000.ts
|
|
@@ -510,6 +511,7 @@ var NAVI_BALANCE_DECIMALS = 9;
|
|
|
510
511
|
var CONFIG_API = "https://open-api.naviprotocol.io/api/navi/config?env=prod";
|
|
511
512
|
var POOLS_API = "https://open-api.naviprotocol.io/api/navi/pools?env=prod";
|
|
512
513
|
var PACKAGE_API = "https://open-api.naviprotocol.io/api/package";
|
|
514
|
+
var PYTH_HERMES_URL = "https://hermes.pyth.network/";
|
|
513
515
|
var packageCache = null;
|
|
514
516
|
function toBigInt(v) {
|
|
515
517
|
if (typeof v === "bigint") return v;
|
|
@@ -604,18 +606,32 @@ function addOracleUpdate(tx, config, pool) {
|
|
|
604
606
|
]
|
|
605
607
|
});
|
|
606
608
|
}
|
|
607
|
-
function
|
|
608
|
-
const
|
|
609
|
-
|
|
610
|
-
|
|
611
|
-
|
|
612
|
-
|
|
613
|
-
|
|
614
|
-
|
|
615
|
-
|
|
616
|
-
|
|
609
|
+
async function refreshStableOracles(tx, client, config, pools) {
|
|
610
|
+
const stableTypes = STABLE_ASSETS.map((a) => SUPPORTED_ASSETS[a].type);
|
|
611
|
+
const stablePools = pools.filter((p) => {
|
|
612
|
+
const ct = p.suiCoinType || p.coinType || "";
|
|
613
|
+
return stableTypes.some((t) => matchesCoinType(ct, t));
|
|
614
|
+
});
|
|
615
|
+
const feeds = (config.oracle.feeds ?? []).filter(
|
|
616
|
+
(f2) => stablePools.some((p) => p.id === f2.assetId)
|
|
617
|
+
);
|
|
618
|
+
if (feeds.length === 0) return;
|
|
619
|
+
const pythFeedIds = feeds.map((f2) => f2.pythPriceFeedId).filter(Boolean);
|
|
620
|
+
if (pythFeedIds.length > 0 && config.oracle.pythStateId && config.oracle.wormholeStateId) {
|
|
621
|
+
try {
|
|
622
|
+
const connection = new pythSuiJs.SuiPriceServiceConnection(PYTH_HERMES_URL);
|
|
623
|
+
const priceUpdateData = await connection.getPriceFeedsUpdateData(pythFeedIds);
|
|
624
|
+
const pythClient = new pythSuiJs.SuiPythClient(
|
|
625
|
+
client,
|
|
626
|
+
config.oracle.pythStateId,
|
|
627
|
+
config.oracle.wormholeStateId
|
|
628
|
+
);
|
|
629
|
+
await pythClient.updatePriceFeeds(tx, priceUpdateData, pythFeedIds);
|
|
630
|
+
} catch {
|
|
631
|
+
}
|
|
632
|
+
}
|
|
633
|
+
for (const pool of stablePools) {
|
|
617
634
|
addOracleUpdate(tx, config, pool);
|
|
618
|
-
updated.add(pool.id);
|
|
619
635
|
}
|
|
620
636
|
}
|
|
621
637
|
function rateToApy(rawRate) {
|
|
@@ -643,7 +659,7 @@ function compoundBalance(rawBalance, currentIndex) {
|
|
|
643
659
|
const result = (rawBalance * BigInt(currentIndex) + half) / scale;
|
|
644
660
|
return Number(result) / 10 ** NAVI_BALANCE_DECIMALS;
|
|
645
661
|
}
|
|
646
|
-
async function getUserState(client, address
|
|
662
|
+
async function getUserState(client, address) {
|
|
647
663
|
const config = await getConfig();
|
|
648
664
|
const tx = new transactions.Transaction();
|
|
649
665
|
tx.moveCall({
|
|
@@ -661,7 +677,6 @@ async function getUserState(client, address, includeZero = false) {
|
|
|
661
677
|
supplyBalance: toBigInt(s.supply_balance),
|
|
662
678
|
borrowBalance: toBigInt(s.borrow_balance)
|
|
663
679
|
}));
|
|
664
|
-
if (includeZero) return mapped;
|
|
665
680
|
return mapped.filter((s) => s.supplyBalance !== 0n || s.borrowBalance !== 0n);
|
|
666
681
|
}
|
|
667
682
|
async function fetchCoins(client, owner, coinType) {
|
|
@@ -719,20 +734,20 @@ async function buildSaveTx(client, address, amount, options = {}) {
|
|
|
719
734
|
async function buildWithdrawTx(client, address, amount, options = {}) {
|
|
720
735
|
const asset = options.asset ?? "USDC";
|
|
721
736
|
const assetInfo = SUPPORTED_ASSETS[asset];
|
|
722
|
-
const [config, pool, pools,
|
|
737
|
+
const [config, pool, pools, states] = await Promise.all([
|
|
723
738
|
getConfig(),
|
|
724
739
|
getPool(asset),
|
|
725
740
|
getPools(),
|
|
726
|
-
getUserState(client, address
|
|
741
|
+
getUserState(client, address)
|
|
727
742
|
]);
|
|
728
|
-
const assetState =
|
|
743
|
+
const assetState = states.find((s) => s.assetId === pool.id);
|
|
729
744
|
const deposited = assetState ? compoundBalance(assetState.supplyBalance, pool.currentSupplyIndex) : 0;
|
|
730
745
|
const effectiveAmount = Math.min(amount, Math.max(0, deposited - WITHDRAW_DUST_BUFFER));
|
|
731
746
|
if (effectiveAmount <= 0) throw new T2000Error("NO_COLLATERAL", `Nothing to withdraw for ${assetInfo.displayName} on NAVI`);
|
|
732
747
|
const rawAmount = Number(stableToRaw(effectiveAmount, assetInfo.decimals));
|
|
733
748
|
const tx = new transactions.Transaction();
|
|
734
749
|
tx.setSender(address);
|
|
735
|
-
|
|
750
|
+
await refreshStableOracles(tx, client, config, pools);
|
|
736
751
|
const [balance] = tx.moveCall({
|
|
737
752
|
target: `${config.package}::incentive_v3::withdraw_v2`,
|
|
738
753
|
arguments: [
|
|
@@ -759,18 +774,18 @@ async function buildWithdrawTx(client, address, amount, options = {}) {
|
|
|
759
774
|
async function addWithdrawToTx(tx, client, address, amount, options = {}) {
|
|
760
775
|
const asset = options.asset ?? "USDC";
|
|
761
776
|
const assetInfo = SUPPORTED_ASSETS[asset];
|
|
762
|
-
const [config, pool, pools,
|
|
777
|
+
const [config, pool, pools, states] = await Promise.all([
|
|
763
778
|
getConfig(),
|
|
764
779
|
getPool(asset),
|
|
765
780
|
getPools(),
|
|
766
|
-
getUserState(client, address
|
|
781
|
+
getUserState(client, address)
|
|
767
782
|
]);
|
|
768
|
-
const assetState =
|
|
783
|
+
const assetState = states.find((s) => s.assetId === pool.id);
|
|
769
784
|
const deposited = assetState ? compoundBalance(assetState.supplyBalance, pool.currentSupplyIndex) : 0;
|
|
770
785
|
const effectiveAmount = Math.min(amount, Math.max(0, deposited - WITHDRAW_DUST_BUFFER));
|
|
771
786
|
if (effectiveAmount <= 0) throw new T2000Error("NO_COLLATERAL", `Nothing to withdraw for ${assetInfo.displayName} on NAVI`);
|
|
772
787
|
const rawAmount = Number(stableToRaw(effectiveAmount, assetInfo.decimals));
|
|
773
|
-
|
|
788
|
+
await refreshStableOracles(tx, client, config, pools);
|
|
774
789
|
const [balance] = tx.moveCall({
|
|
775
790
|
target: `${config.package}::incentive_v3::withdraw_v2`,
|
|
776
791
|
arguments: [
|
|
@@ -851,15 +866,14 @@ async function buildBorrowTx(client, address, amount, options = {}) {
|
|
|
851
866
|
const asset = options.asset ?? "USDC";
|
|
852
867
|
const assetInfo = SUPPORTED_ASSETS[asset];
|
|
853
868
|
const rawAmount = Number(stableToRaw(amount, assetInfo.decimals));
|
|
854
|
-
const [config, pool, pools
|
|
869
|
+
const [config, pool, pools] = await Promise.all([
|
|
855
870
|
getConfig(),
|
|
856
871
|
getPool(asset),
|
|
857
|
-
getPools()
|
|
858
|
-
getUserState(client, address, true)
|
|
872
|
+
getPools()
|
|
859
873
|
]);
|
|
860
874
|
const tx = new transactions.Transaction();
|
|
861
875
|
tx.setSender(address);
|
|
862
|
-
|
|
876
|
+
await refreshStableOracles(tx, client, config, pools);
|
|
863
877
|
const [balance] = tx.moveCall({
|
|
864
878
|
target: `${config.package}::incentive_v3::borrow_v2`,
|
|
865
879
|
arguments: [
|