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