@t2000/sdk 0.17.14 → 0.17.16

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.
@@ -1,4 +1,4 @@
1
- export { w as AdapterCapability, x as AdapterPositions, y as AdapterTxResult, C as CetusAdapter, K as HealthInfo, L as LendingAdapter, s as LendingRates, N as NaviAdapter, O as PerpsAdapter, X as ProtocolDescriptor, Y as ProtocolRegistry, $ as SuilendAdapter, d as SwapAdapter, a0 as SwapQuote, a3 as allDescriptors, a4 as cetusDescriptor, a7 as naviDescriptor, aa as sentinelDescriptor, ad as suilendDescriptor } from '../index-D4cFY__D.cjs';
1
+ export { x as AdapterCapability, y as AdapterPositions, z as AdapterTxResult, K as CetusAdapter, O as HealthInfo, L as LendingAdapter, t as LendingRates, Q as NaviAdapter, U as PerpsAdapter, Z as ProtocolDescriptor, _ as ProtocolRegistry, a1 as SuilendAdapter, d as SwapAdapter, a2 as SwapQuote, a5 as allDescriptors, a6 as cetusDescriptor, a9 as naviDescriptor, ac as sentinelDescriptor, af as suilendDescriptor } from '../index-xQEri-Eu.cjs';
2
2
  import '@mysten/sui/transactions';
3
3
  import '@mysten/sui/jsonRpc';
4
4
  import '@mysten/sui/keypairs/ed25519';
@@ -1,4 +1,4 @@
1
- export { w as AdapterCapability, x as AdapterPositions, y as AdapterTxResult, C as CetusAdapter, K as HealthInfo, L as LendingAdapter, s as LendingRates, N as NaviAdapter, O as PerpsAdapter, X as ProtocolDescriptor, Y as ProtocolRegistry, $ as SuilendAdapter, d as SwapAdapter, a0 as SwapQuote, a3 as allDescriptors, a4 as cetusDescriptor, a7 as naviDescriptor, aa as sentinelDescriptor, ad as suilendDescriptor } from '../index-D4cFY__D.js';
1
+ export { x as AdapterCapability, y as AdapterPositions, z as AdapterTxResult, K as CetusAdapter, O as HealthInfo, L as LendingAdapter, t as LendingRates, Q as NaviAdapter, U as PerpsAdapter, Z as ProtocolDescriptor, _ as ProtocolRegistry, a1 as SuilendAdapter, d as SwapAdapter, a2 as SwapQuote, a5 as allDescriptors, a6 as cetusDescriptor, a9 as naviDescriptor, ac as sentinelDescriptor, af as suilendDescriptor } from '../index-xQEri-Eu.js';
2
2
  import '@mysten/sui/transactions';
3
3
  import '@mysten/sui/jsonRpc';
4
4
  import '@mysten/sui/keypairs/ed25519';
@@ -865,6 +865,172 @@ async function maxBorrowAmount(client, addressOrKeypair) {
865
865
  const maxAmount = Math.max(0, hf.supplied * ltv / MIN_HEALTH_FACTOR - hf.borrowed);
866
866
  return { maxAmount, healthFactorAfter: MIN_HEALTH_FACTOR, currentHF: hf.healthFactor };
867
867
  }
868
+ var CERT_TYPE = "0x549e8b69270defbfafd4f94e17ec44cdbdd99820b33bda2278dea3b9a32d3f55::cert::CERT";
869
+ var DEEP_TYPE = "0xdeeb7a4662eec9f2f3def03fb937a663dddaa2e215b8078a284d026b7946c270::deep::DEEP";
870
+ var REWARD_FUNDS = {
871
+ [CERT_TYPE]: "0x7093cf7549d5e5b35bfde2177223d1050f71655c7f676a5e610ee70eb4d93b5c",
872
+ [DEEP_TYPE]: "0xc889d78b634f954979e80e622a2ae0fece824c0f6d9590044378a2563035f32f"
873
+ };
874
+ var REWARD_SYMBOLS = {
875
+ [CERT_TYPE]: "vSUI",
876
+ [DEEP_TYPE]: "DEEP"
877
+ };
878
+ var incentiveRulesCache = null;
879
+ async function getIncentiveRules(client) {
880
+ if (incentiveRulesCache && Date.now() - incentiveRulesCache.ts < CACHE_TTL) {
881
+ return incentiveRulesCache.data;
882
+ }
883
+ const [pools, obj] = await Promise.all([
884
+ getPools(),
885
+ client.getObject({
886
+ id: "0x62982dad27fb10bb314b3384d5de8d2ac2d72ab2dbeae5d801dbdb9efa816c80",
887
+ options: { showContent: true }
888
+ })
889
+ ]);
890
+ const rewardCoinMap = /* @__PURE__ */ new Map();
891
+ for (const pool of pools) {
892
+ const ct = (pool.suiCoinType || pool.coinType || "").toLowerCase();
893
+ const suffix = ct.split("::").slice(1).join("::");
894
+ const coins = pool.supplyIncentiveApyInfo?.rewardCoin;
895
+ if (Array.isArray(coins) && coins.length > 0) {
896
+ rewardCoinMap.set(suffix, coins[0]);
897
+ }
898
+ }
899
+ const result = /* @__PURE__ */ new Map();
900
+ if (obj.data?.content?.dataType !== "moveObject") {
901
+ incentiveRulesCache = { data: result, ts: Date.now() };
902
+ return result;
903
+ }
904
+ const fields = obj.data.content.fields;
905
+ const poolsObj = fields.pools;
906
+ const entries = poolsObj?.fields?.contents;
907
+ if (!Array.isArray(entries)) {
908
+ incentiveRulesCache = { data: result, ts: Date.now() };
909
+ return result;
910
+ }
911
+ for (const entry of entries) {
912
+ const ef = entry?.fields;
913
+ if (!ef) continue;
914
+ const key = String(ef.key ?? "");
915
+ const value = ef.value;
916
+ const rules = value?.fields?.rules;
917
+ const ruleEntries = rules?.fields?.contents;
918
+ if (!Array.isArray(ruleEntries)) continue;
919
+ const ruleIds = ruleEntries.map((re) => {
920
+ const rf = re?.fields;
921
+ return String(rf?.key ?? "");
922
+ }).filter(Boolean);
923
+ const suffix = key.split("::").slice(1).join("::").toLowerCase();
924
+ const full = key.toLowerCase();
925
+ const rewardCoin = rewardCoinMap.get(suffix) ?? rewardCoinMap.get(full) ?? null;
926
+ result.set(key, { ruleIds, rewardCoinType: rewardCoin });
927
+ }
928
+ incentiveRulesCache = { data: result, ts: Date.now() };
929
+ return result;
930
+ }
931
+ function stripPrefix(coinType) {
932
+ return coinType.replace(/^0x0*/, "");
933
+ }
934
+ async function getPendingRewards(client, address) {
935
+ const [pools, states, rules] = await Promise.all([
936
+ getPools(),
937
+ getUserState(client, address),
938
+ getIncentiveRules(client)
939
+ ]);
940
+ const rewards = [];
941
+ const deposited = states.filter((s) => s.supplyBalance > 0n);
942
+ if (deposited.length === 0) return rewards;
943
+ const rewardTotals = /* @__PURE__ */ new Map();
944
+ for (const state of deposited) {
945
+ const pool = pools.find((p) => p.id === state.assetId);
946
+ if (!pool) continue;
947
+ const boostedApr = parseFloat(pool.supplyIncentiveApyInfo?.boostedApr ?? "0");
948
+ if (boostedApr <= 0) continue;
949
+ const rewardCoins = pool.supplyIncentiveApyInfo?.rewardCoin;
950
+ if (!Array.isArray(rewardCoins) || rewardCoins.length === 0) continue;
951
+ const rewardType = rewardCoins[0];
952
+ const supplyBal = compoundBalance(state.supplyBalance, pool.currentSupplyIndex, pool);
953
+ const price = pool.token?.price ?? 0;
954
+ const depositUsd = supplyBal * price;
955
+ const annualRewardUsd = depositUsd * (boostedApr / 100);
956
+ const estimatedUsd = annualRewardUsd / 365;
957
+ rewardTotals.set(rewardType, (rewardTotals.get(rewardType) ?? 0) + estimatedUsd);
958
+ }
959
+ for (const [coinType, dailyUsd] of rewardTotals) {
960
+ rewards.push({
961
+ protocol: "navi",
962
+ coinType,
963
+ symbol: REWARD_SYMBOLS[coinType] ?? coinType.split("::").pop() ?? "UNKNOWN",
964
+ amount: 0,
965
+ estimatedValueUsd: dailyUsd
966
+ });
967
+ }
968
+ return rewards;
969
+ }
970
+ async function addClaimRewardsToTx(tx, client, address) {
971
+ const [config, pools, states, rules] = await Promise.all([
972
+ getConfig(),
973
+ getPools(),
974
+ getUserState(client, address),
975
+ getIncentiveRules(client)
976
+ ]);
977
+ const deposited = states.filter((s) => s.supplyBalance > 0n);
978
+ if (deposited.length === 0) return [];
979
+ const claimGroups = /* @__PURE__ */ new Map();
980
+ for (const state of deposited) {
981
+ const pool = pools.find((p) => p.id === state.assetId);
982
+ if (!pool) continue;
983
+ const boostedApr = parseFloat(pool.supplyIncentiveApyInfo?.boostedApr ?? "0");
984
+ if (boostedApr <= 0) continue;
985
+ const rewardCoins = pool.supplyIncentiveApyInfo?.rewardCoin;
986
+ if (!Array.isArray(rewardCoins) || rewardCoins.length === 0) continue;
987
+ const rewardType = rewardCoins[0];
988
+ const fundId = REWARD_FUNDS[rewardType];
989
+ if (!fundId) continue;
990
+ const coinType = pool.suiCoinType || pool.coinType || "";
991
+ const strippedType = stripPrefix(coinType);
992
+ const ruleData = Array.from(rules.entries()).find(
993
+ ([key]) => stripPrefix(key) === strippedType || key.split("::").slice(1).join("::").toLowerCase() === coinType.split("::").slice(1).join("::").toLowerCase()
994
+ );
995
+ if (!ruleData || ruleData[1].ruleIds.length === 0) continue;
996
+ const group = claimGroups.get(rewardType) ?? { assets: [], ruleIds: [] };
997
+ for (const ruleId of ruleData[1].ruleIds) {
998
+ group.assets.push(strippedType);
999
+ group.ruleIds.push(ruleId);
1000
+ }
1001
+ claimGroups.set(rewardType, group);
1002
+ }
1003
+ const claimed = [];
1004
+ for (const [rewardType, { assets, ruleIds }] of claimGroups) {
1005
+ const fundId = REWARD_FUNDS[rewardType];
1006
+ const [balance] = tx.moveCall({
1007
+ target: `${config.package}::incentive_v3::claim_reward`,
1008
+ typeArguments: [rewardType],
1009
+ arguments: [
1010
+ tx.object(CLOCK),
1011
+ tx.object(config.incentiveV3),
1012
+ tx.object(config.storage),
1013
+ tx.object(fundId),
1014
+ tx.pure(bcs.vector(bcs.string()).serialize(assets)),
1015
+ tx.pure(bcs.vector(bcs.Address).serialize(ruleIds))
1016
+ ]
1017
+ });
1018
+ const [coin] = tx.moveCall({
1019
+ target: "0x2::coin::from_balance",
1020
+ typeArguments: [rewardType],
1021
+ arguments: [balance]
1022
+ });
1023
+ tx.transferObjects([coin], address);
1024
+ claimed.push({
1025
+ protocol: "navi",
1026
+ coinType: rewardType,
1027
+ symbol: REWARD_SYMBOLS[rewardType] ?? "UNKNOWN",
1028
+ amount: 0,
1029
+ estimatedValueUsd: 0
1030
+ });
1031
+ }
1032
+ return claimed;
1033
+ }
868
1034
 
869
1035
  // src/adapters/navi.ts
870
1036
  var descriptor = {
@@ -952,6 +1118,12 @@ var NaviAdapter = class {
952
1118
  const normalized = normalizeAsset(asset);
953
1119
  return addRepayToTx(tx, this.client, address, coin, { asset: normalized });
954
1120
  }
1121
+ async getPendingRewards(address) {
1122
+ return getPendingRewards(this.client, address);
1123
+ }
1124
+ async addClaimRewardsToTx(tx, address) {
1125
+ return addClaimRewardsToTx(tx, this.client, address);
1126
+ }
955
1127
  };
956
1128
  var DEFAULT_SLIPPAGE_BPS = 300;
957
1129
  function createAggregatorClient(client, signer) {
@@ -1276,15 +1448,17 @@ function parseReserve(raw, index) {
1276
1448
  const dMgr = f(r.deposits_pool_reward_manager);
1277
1449
  const rawRewards = Array.isArray(dMgr?.pool_rewards) ? dMgr.pool_rewards : [];
1278
1450
  const now = Date.now();
1279
- const depositPoolRewards = rawRewards.filter((rw) => rw !== null).map((rw) => {
1451
+ const depositPoolRewards = rawRewards.map((rw, idx) => {
1452
+ if (rw === null) return null;
1280
1453
  const rwf = f(rw);
1281
1454
  return {
1282
1455
  coinType: str(f(rwf.coin_type)?.name),
1283
1456
  totalRewards: num(rwf.total_rewards),
1284
1457
  startTimeMs: num(rwf.start_time_ms),
1285
- endTimeMs: num(rwf.end_time_ms)
1458
+ endTimeMs: num(rwf.end_time_ms),
1459
+ rewardIndex: idx
1286
1460
  };
1287
- }).filter((rw) => rw.endTimeMs > now && rw.totalRewards > 0);
1461
+ }).filter((rw) => rw !== null && rw.endTimeMs > now && rw.totalRewards > 0);
1288
1462
  return {
1289
1463
  coinType: str(coinTypeField?.name),
1290
1464
  mintDecimals: num(r.mint_decimals),
@@ -1835,6 +2009,108 @@ var SuilendAdapter = class {
1835
2009
  }
1836
2010
  return all;
1837
2011
  }
2012
+ // -- Claim Rewards --------------------------------------------------------
2013
+ isClaimableReward(coinType) {
2014
+ const ct = coinType.toLowerCase();
2015
+ return ct.includes("spring_sui") || ct.includes("deep::deep") || ct.includes("cert::cert");
2016
+ }
2017
+ async getPendingRewards(address) {
2018
+ const caps = await this.fetchObligationCaps(address);
2019
+ if (caps.length === 0) return [];
2020
+ const [reserves, obligation] = await Promise.all([
2021
+ this.loadReserves(true),
2022
+ this.fetchObligation(caps[0].obligationId)
2023
+ ]);
2024
+ const rewards = [];
2025
+ const rewardEstimates = /* @__PURE__ */ new Map();
2026
+ for (const dep of obligation.deposits) {
2027
+ const reserve = reserves[dep.reserveIdx];
2028
+ if (!reserve) continue;
2029
+ const ratio = cTokenRatio(reserve);
2030
+ const amount = dep.ctokenAmount * ratio / 10 ** reserve.mintDecimals;
2031
+ const price = reserve.price;
2032
+ const depositUsd = amount * price;
2033
+ for (const rw of reserve.depositPoolRewards) {
2034
+ if (!this.isClaimableReward(rw.coinType)) continue;
2035
+ const rewardReserve = reserves.find((r) => {
2036
+ try {
2037
+ return normalizeStructTag(r.coinType) === normalizeStructTag(rw.coinType);
2038
+ } catch {
2039
+ return false;
2040
+ }
2041
+ });
2042
+ const rewardPrice = rewardReserve?.price ?? 0;
2043
+ const rewardDecimals = rewardReserve?.mintDecimals ?? 9;
2044
+ const durationMs = rw.endTimeMs - rw.startTimeMs;
2045
+ if (durationMs <= 0) continue;
2046
+ const annualTokens = rw.totalRewards / 10 ** rewardDecimals * (MS_PER_YEAR / durationMs);
2047
+ const totalDepositValue = reserve.depositTotalShares / 10 ** reserve.mintDecimals * price;
2048
+ if (totalDepositValue <= 0) continue;
2049
+ const userShare = depositUsd / totalDepositValue;
2050
+ const dailyRewardUsd = annualTokens * rewardPrice / 365 * userShare;
2051
+ rewardEstimates.set(rw.coinType, (rewardEstimates.get(rw.coinType) ?? 0) + dailyRewardUsd);
2052
+ }
2053
+ }
2054
+ for (const [coinType, dailyUsd] of rewardEstimates) {
2055
+ const symbol = coinType.includes("spring_sui") ? "SPRING_SUI" : coinType.includes("deep::") ? "DEEP" : coinType.split("::").pop() ?? "UNKNOWN";
2056
+ rewards.push({
2057
+ protocol: "suilend",
2058
+ coinType,
2059
+ symbol,
2060
+ amount: 0,
2061
+ estimatedValueUsd: dailyUsd
2062
+ });
2063
+ }
2064
+ return rewards;
2065
+ }
2066
+ async addClaimRewardsToTx(tx, address) {
2067
+ const caps = await this.fetchObligationCaps(address);
2068
+ if (caps.length === 0) return [];
2069
+ const [pkg, reserves, obligation] = await Promise.all([
2070
+ this.resolvePackage(),
2071
+ this.loadReserves(true),
2072
+ this.fetchObligation(caps[0].obligationId)
2073
+ ]);
2074
+ const claimsByToken = /* @__PURE__ */ new Map();
2075
+ const claimed = [];
2076
+ for (const dep of obligation.deposits) {
2077
+ const reserve = reserves[dep.reserveIdx];
2078
+ if (!reserve) continue;
2079
+ for (const rw of reserve.depositPoolRewards) {
2080
+ if (!this.isClaimableReward(rw.coinType)) continue;
2081
+ const [coin] = tx.moveCall({
2082
+ target: `${pkg}::lending_market::claim_rewards`,
2083
+ typeArguments: [LENDING_MARKET_TYPE, rw.coinType],
2084
+ arguments: [
2085
+ tx.object(LENDING_MARKET_ID),
2086
+ tx.object(caps[0].id),
2087
+ tx.object(CLOCK2),
2088
+ tx.pure.u64(reserve.arrayIndex),
2089
+ tx.pure.u64(rw.rewardIndex),
2090
+ tx.pure.bool(true)
2091
+ ]
2092
+ });
2093
+ const existing = claimsByToken.get(rw.coinType) ?? [];
2094
+ existing.push(coin);
2095
+ claimsByToken.set(rw.coinType, existing);
2096
+ }
2097
+ }
2098
+ for (const [coinType, coins] of claimsByToken) {
2099
+ if (coins.length > 1) {
2100
+ tx.mergeCoins(coins[0], coins.slice(1));
2101
+ }
2102
+ tx.transferObjects([coins[0]], address);
2103
+ const symbol = coinType.includes("spring_sui") ? "SPRING_SUI" : coinType.includes("deep::") ? "DEEP" : coinType.split("::").pop() ?? "UNKNOWN";
2104
+ claimed.push({
2105
+ protocol: "suilend",
2106
+ coinType,
2107
+ symbol,
2108
+ amount: 0,
2109
+ estimatedValueUsd: 0
2110
+ });
2111
+ }
2112
+ return claimed;
2113
+ }
1838
2114
  };
1839
2115
  var descriptor4 = {
1840
2116
  id: "sentinel",