@defisaver/positions-sdk 2.0.13 → 2.0.14-dev-portfolio
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/CLAUDE.md +32 -0
- package/cjs/aaveV2/index.js +1 -0
- package/cjs/aaveV3/index.d.ts +12 -0
- package/cjs/aaveV3/index.js +93 -1
- package/cjs/claiming/aaveV3.d.ts +9 -0
- package/cjs/claiming/aaveV3.js +148 -0
- package/cjs/claiming/compV3.d.ts +15 -0
- package/cjs/claiming/compV3.js +34 -0
- package/cjs/claiming/index.d.ts +6 -0
- package/cjs/claiming/index.js +46 -0
- package/cjs/claiming/king.d.ts +4 -0
- package/cjs/claiming/king.js +72 -0
- package/cjs/claiming/morphoBlue.d.ts +6 -0
- package/cjs/claiming/morphoBlue.js +113 -0
- package/cjs/claiming/spark.d.ts +6 -0
- package/cjs/claiming/spark.js +188 -0
- package/cjs/config/contracts.d.ts +2667 -0
- package/cjs/config/contracts.js +103 -2
- package/cjs/constants/index.d.ts +4 -0
- package/cjs/constants/index.js +6 -2
- package/cjs/contracts.d.ts +2882 -23
- package/cjs/contracts.js +10 -1
- package/cjs/index.d.ts +2 -1
- package/cjs/index.js +3 -1
- package/cjs/liquity/index.d.ts +11 -0
- package/cjs/liquity/index.js +39 -1
- package/cjs/liquityV2/index.d.ts +8 -0
- package/cjs/liquityV2/index.js +161 -0
- package/cjs/markets/aave/marketAssets.js +1 -1
- package/cjs/markets/compound/marketsAssets.js +2 -2
- package/cjs/markets/spark/marketAssets.js +1 -1
- package/cjs/morphoBlue/index.d.ts +5 -0
- package/cjs/morphoBlue/index.js +38 -4
- package/cjs/portfolio/index.d.ts +6 -1
- package/cjs/portfolio/index.js +256 -10
- package/cjs/services/utils.d.ts +5 -0
- package/cjs/services/utils.js +33 -1
- package/cjs/services/viem.d.ts +12 -12
- package/cjs/spark/index.js +1 -0
- package/cjs/staking/staking.js +3 -1
- package/cjs/types/claiming.d.ts +93 -0
- package/cjs/types/claiming.js +27 -0
- package/cjs/umbrella/index.d.ts +5 -0
- package/cjs/umbrella/index.js +50 -0
- package/cjs/umbrella/umbrellaUtils.d.ts +22 -0
- package/cjs/umbrella/umbrellaUtils.js +34 -0
- package/esm/aaveV2/index.js +1 -0
- package/esm/aaveV3/index.d.ts +12 -0
- package/esm/aaveV3/index.js +91 -1
- package/esm/claiming/aaveV3.d.ts +9 -0
- package/esm/claiming/aaveV3.js +139 -0
- package/esm/claiming/compV3.d.ts +15 -0
- package/esm/claiming/compV3.js +30 -0
- package/esm/claiming/index.d.ts +6 -0
- package/esm/claiming/index.js +6 -0
- package/esm/claiming/king.d.ts +4 -0
- package/esm/claiming/king.js +64 -0
- package/esm/claiming/morphoBlue.d.ts +6 -0
- package/esm/claiming/morphoBlue.js +104 -0
- package/esm/claiming/spark.d.ts +6 -0
- package/esm/claiming/spark.js +179 -0
- package/esm/config/contracts.d.ts +2667 -0
- package/esm/config/contracts.js +102 -1
- package/esm/constants/index.d.ts +4 -0
- package/esm/constants/index.js +5 -1
- package/esm/contracts.d.ts +2882 -23
- package/esm/contracts.js +9 -0
- package/esm/index.d.ts +2 -1
- package/esm/index.js +2 -1
- package/esm/liquity/index.d.ts +11 -0
- package/esm/liquity/index.js +38 -1
- package/esm/liquityV2/index.d.ts +8 -0
- package/esm/liquityV2/index.js +162 -1
- package/esm/markets/aave/marketAssets.js +1 -1
- package/esm/markets/compound/marketsAssets.js +2 -2
- package/esm/markets/spark/marketAssets.js +1 -1
- package/esm/morphoBlue/index.d.ts +5 -0
- package/esm/morphoBlue/index.js +38 -5
- package/esm/portfolio/index.d.ts +6 -1
- package/esm/portfolio/index.js +260 -14
- package/esm/services/utils.d.ts +5 -0
- package/esm/services/utils.js +31 -0
- package/esm/services/viem.d.ts +12 -12
- package/esm/spark/index.js +1 -0
- package/esm/staking/staking.js +3 -1
- package/esm/types/claiming.d.ts +93 -0
- package/esm/types/claiming.js +24 -0
- package/esm/umbrella/index.d.ts +5 -0
- package/esm/umbrella/index.js +46 -0
- package/esm/umbrella/umbrellaUtils.d.ts +22 -0
- package/esm/umbrella/umbrellaUtils.js +28 -0
- package/package.json +2 -2
- package/src/aaveV2/index.ts +2 -1
- package/src/aaveV3/index.ts +100 -2
- package/src/claiming/aaveV3.ts +163 -0
- package/src/claiming/compV3.ts +23 -0
- package/src/claiming/index.ts +13 -0
- package/src/claiming/king.ts +66 -0
- package/src/claiming/morphoBlue.ts +119 -0
- package/src/claiming/spark.ts +226 -0
- package/src/config/contracts.ts +105 -5
- package/src/constants/index.ts +5 -1
- package/src/contracts.ts +14 -1
- package/src/index.ts +2 -0
- package/src/liquity/index.ts +57 -2
- package/src/liquityV2/index.ts +177 -2
- package/src/markets/aave/marketAssets.ts +1 -1
- package/src/markets/compound/marketsAssets.ts +2 -2
- package/src/markets/spark/marketAssets.ts +1 -1
- package/src/morphoBlue/index.ts +43 -6
- package/src/portfolio/index.ts +263 -15
- package/src/services/utils.ts +37 -1
- package/src/spark/index.ts +2 -1
- package/src/staking/staking.ts +2 -1
- package/src/types/claiming.ts +109 -0
- package/src/umbrella/index.ts +70 -0
- package/src/umbrella/umbrellaUtils.ts +30 -0
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
2
|
+
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
3
|
+
return new (P || (P = Promise))(function (resolve, reject) {
|
|
4
|
+
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
5
|
+
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
6
|
+
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
7
|
+
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
8
|
+
});
|
|
9
|
+
};
|
|
10
|
+
import { AaveUmbrellaViewViem, AaveV3ViewContractViem } from '../contracts';
|
|
11
|
+
import { compareAddresses, convertHybridArraysToObjects, getEthAmountForDecimals } from '../services/utils';
|
|
12
|
+
import { findMatching, tokenEntries } from './umbrellaUtils';
|
|
13
|
+
const umbrella = '0xD400fc38ED4732893174325693a63C30ee3881a8';
|
|
14
|
+
const aaveOracle = '0x54586bE62E3c3580375aE3723C145253060Ca0C2';
|
|
15
|
+
export const getUmbrellaData = (provider, network, address) => __awaiter(void 0, void 0, void 0, function* () {
|
|
16
|
+
const umbrellaView = AaveUmbrellaViewViem(provider, network);
|
|
17
|
+
const aaveV3View = AaveV3ViewContractViem(provider, network);
|
|
18
|
+
const [tokensAggregatedData, additionalUmbrellaStakingData, userAggregatedData] = yield Promise.all([
|
|
19
|
+
umbrellaView.read.getTokensAggregatedData([umbrella, aaveOracle]),
|
|
20
|
+
aaveV3View.read.getAdditionalUmbrellaStakingData([umbrella, address]),
|
|
21
|
+
umbrellaView.read.getUserAggregatedData([umbrella, address]),
|
|
22
|
+
]);
|
|
23
|
+
return Object.fromEntries(tokenEntries.map(([symbol, tokenAddr]) => {
|
|
24
|
+
// @ts-ignore
|
|
25
|
+
const fromTokens = convertHybridArraysToObjects(findMatching(tokensAggregatedData, tokenAddr));
|
|
26
|
+
// @ts-ignore
|
|
27
|
+
const fromUser = convertHybridArraysToObjects(findMatching(userAggregatedData, tokenAddr));
|
|
28
|
+
// @ts-ignore
|
|
29
|
+
const fromAdditionalData = convertHybridArraysToObjects(findMatching(additionalUmbrellaStakingData, tokenAddr));
|
|
30
|
+
const data = Object.assign(Object.assign(Object.assign(Object.assign({}, fromTokens), fromUser), fromAdditionalData), { account: {
|
|
31
|
+
stakeUserBalance: (fromUser === null || fromUser === void 0 ? void 0 : fromUser.stakeUserBalance) || '0',
|
|
32
|
+
rewardsTokenUserData: convertHybridArraysToObjects(fromUser === null || fromUser === void 0 ? void 0 : fromUser.rewardsTokenUserData) || [],
|
|
33
|
+
userCooldownAmount: (fromAdditionalData === null || fromAdditionalData === void 0 ? void 0 : fromAdditionalData.userCooldownAmount) || '0',
|
|
34
|
+
userEndOfCooldown: (fromAdditionalData === null || fromAdditionalData === void 0 ? void 0 : fromAdditionalData.userEndOfCooldown) || '0',
|
|
35
|
+
userWithdrawalWindow: (fromAdditionalData === null || fromAdditionalData === void 0 ? void 0 : fromAdditionalData.userWithdrawalWindow) || '0',
|
|
36
|
+
} });
|
|
37
|
+
const stakeTokenData = data.stakeTokenData;
|
|
38
|
+
return [symbol, Object.assign(Object.assign({}, data), { stakeTokenData: Object.assign(Object.assign({}, stakeTokenData), { price: getEthAmountForDecimals(stakeTokenData.price, 8) }), stkTokenToWaTokenRate: getEthAmountForDecimals(data.stkTokenToWaTokenRate, stakeTokenData.decimals), waTokenToATokenRate: getEthAmountForDecimals(data.waTokenToATokenRate, stakeTokenData.decimals), totalShares: getEthAmountForDecimals(data.totalShares, stakeTokenData.decimals), totalAssets: getEthAmountForDecimals(data.totalAssets, stakeTokenData.decimals), targetLiquidity: getEthAmountForDecimals(data.targetLiquidity, stakeTokenData.decimals), rewardsTokenData: data.rewardsTokenData.map((tokenData) => (Object.assign(Object.assign({}, tokenData), { rewardTokenData: Object.assign(Object.assign({}, tokenData.rewardTokenData), { price: getEthAmountForDecimals(tokenData.rewardTokenData.price, 8) }) }))), rewardsEmissionRates: data.rewardsEmissionRates.map((rate, i) => {
|
|
39
|
+
const tokenData = data.rewardsTokenData[i].rewardTokenData;
|
|
40
|
+
return getEthAmountForDecimals(rate, tokenData.decimals);
|
|
41
|
+
}), account: Object.assign(Object.assign({}, data.account), { stakeUserBalance: getEthAmountForDecimals(data.account.stakeUserBalance, stakeTokenData.decimals), userCooldownAmount: getEthAmountForDecimals(data.account.userCooldownAmount, stakeTokenData.decimals), rewardsTokenUserData: data.account.rewardsTokenUserData.map((reward) => {
|
|
42
|
+
const tokenData = data.rewardsTokenData.find((tknData) => compareAddresses(tknData.rewardTokenData.token, reward.reward));
|
|
43
|
+
return (Object.assign(Object.assign({}, reward), { currentReward: getEthAmountForDecimals(reward.currentReward, tokenData.rewardTokenData.decimals) }));
|
|
44
|
+
}) }) })];
|
|
45
|
+
}));
|
|
46
|
+
});
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
export declare enum UmbrellaStaking {
|
|
2
|
+
UmbrellaGHO = "UmbrellaGHO",
|
|
3
|
+
UmbrellaUSDC = "UmbrellaUSDC",
|
|
4
|
+
UmbrellaUSDT = "UmbrellaUSDT",
|
|
5
|
+
UmbrellaETH = "UmbrellaETH"
|
|
6
|
+
}
|
|
7
|
+
export declare const tokenMapping: {
|
|
8
|
+
readonly UmbrellaUSDC: "0x6bf183243FdD1e306ad2C4450BC7dcf6f0bf8Aa6";
|
|
9
|
+
readonly UmbrellaUSDT: "0xA484Ab92fe32B143AEE7019fC1502b1dAA522D31";
|
|
10
|
+
readonly UmbrellaETH: "0xaAFD07D53A7365D3e9fb6F3a3B09EC19676B73Ce";
|
|
11
|
+
readonly UmbrellaGHO: "0x4f827A63755855cDf3e8f3bcD20265C833f15033";
|
|
12
|
+
};
|
|
13
|
+
export declare const normalize: (addr?: string) => string;
|
|
14
|
+
export declare const tokenEntries: string[][];
|
|
15
|
+
export declare const extractAddress: (item?: {
|
|
16
|
+
stakeToken?: string;
|
|
17
|
+
stkToken?: string;
|
|
18
|
+
stakeTokenData?: {
|
|
19
|
+
token?: string;
|
|
20
|
+
};
|
|
21
|
+
}) => string;
|
|
22
|
+
export declare const findMatching: (dataset: any[], targetAddr: string) => any;
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
export var UmbrellaStaking;
|
|
2
|
+
(function (UmbrellaStaking) {
|
|
3
|
+
UmbrellaStaking["UmbrellaGHO"] = "UmbrellaGHO";
|
|
4
|
+
UmbrellaStaking["UmbrellaUSDC"] = "UmbrellaUSDC";
|
|
5
|
+
UmbrellaStaking["UmbrellaUSDT"] = "UmbrellaUSDT";
|
|
6
|
+
UmbrellaStaking["UmbrellaETH"] = "UmbrellaETH";
|
|
7
|
+
})(UmbrellaStaking || (UmbrellaStaking = {}));
|
|
8
|
+
export const tokenMapping = {
|
|
9
|
+
[UmbrellaStaking.UmbrellaUSDC]: '0x6bf183243FdD1e306ad2C4450BC7dcf6f0bf8Aa6',
|
|
10
|
+
[UmbrellaStaking.UmbrellaUSDT]: '0xA484Ab92fe32B143AEE7019fC1502b1dAA522D31',
|
|
11
|
+
[UmbrellaStaking.UmbrellaETH]: '0xaAFD07D53A7365D3e9fb6F3a3B09EC19676B73Ce',
|
|
12
|
+
[UmbrellaStaking.UmbrellaGHO]: '0x4f827A63755855cDf3e8f3bcD20265C833f15033',
|
|
13
|
+
};
|
|
14
|
+
// ----- chat gpt code for result mapping ---------
|
|
15
|
+
export const normalize = (addr) => { var _a, _b; return (_b = (_a = addr === null || addr === void 0 ? void 0 : addr.toLowerCase) === null || _a === void 0 ? void 0 : _a.call(addr)) !== null && _b !== void 0 ? _b : ''; };
|
|
16
|
+
export const tokenEntries = Object.entries(tokenMapping).map(([symbol, address]) => [
|
|
17
|
+
symbol,
|
|
18
|
+
normalize(address),
|
|
19
|
+
]);
|
|
20
|
+
// 👇 Define how to extract the token address from each source type
|
|
21
|
+
export const extractAddress = (item) => {
|
|
22
|
+
var _a;
|
|
23
|
+
return normalize((item === null || item === void 0 ? void 0 : item.stakeToken)
|
|
24
|
+
|| (item === null || item === void 0 ? void 0 : item.stkToken)
|
|
25
|
+
|| ((_a = item === null || item === void 0 ? void 0 : item.stakeTokenData) === null || _a === void 0 ? void 0 : _a.token));
|
|
26
|
+
};
|
|
27
|
+
// 👇 Utility to find the matching object in a dataset
|
|
28
|
+
export const findMatching = (dataset, targetAddr) => dataset.find((item) => extractAddress(item) === targetAddr);
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@defisaver/positions-sdk",
|
|
3
|
-
"version": "2.0.
|
|
3
|
+
"version": "2.0.14-dev-portfolio",
|
|
4
4
|
"description": "",
|
|
5
5
|
"main": "./cjs/index.js",
|
|
6
6
|
"module": "./esm/index.js",
|
|
@@ -21,7 +21,7 @@
|
|
|
21
21
|
"author": "",
|
|
22
22
|
"license": "ISC",
|
|
23
23
|
"dependencies": {
|
|
24
|
-
"@defisaver/tokens": "^1.7.
|
|
24
|
+
"@defisaver/tokens": "^1.7.1",
|
|
25
25
|
"@types/lodash": "^4.17.15",
|
|
26
26
|
"@types/memoizee": "^0.4.12",
|
|
27
27
|
"decimal.js": "^10.6.0",
|
package/src/aaveV2/index.ts
CHANGED
|
@@ -103,7 +103,8 @@ export const _getAaveV2AccountBalances = async (provider: Client, network: Netwo
|
|
|
103
103
|
// @ts-ignore
|
|
104
104
|
const protocolDataProviderContract = createViemContractFromConfigFunc(market.protocolData, market.protocolDataAddress)(provider, network);
|
|
105
105
|
|
|
106
|
-
|
|
106
|
+
// @ts-ignore
|
|
107
|
+
const reserveTokens = await protocolDataProviderContract.read.getAllReservesTokens(setViemBlockNumber(block)) as { symbol: string, tokenAddress: EthAddress }[];
|
|
107
108
|
const symbols = reserveTokens.map(({ symbol }: { symbol: string }) => symbol);
|
|
108
109
|
const _addresses = reserveTokens.map(({ tokenAddress }: { tokenAddress: EthAddress }) => tokenAddress);
|
|
109
110
|
|
package/src/aaveV3/index.ts
CHANGED
|
@@ -3,8 +3,10 @@ import { Client } from 'viem';
|
|
|
3
3
|
import Dec from 'decimal.js';
|
|
4
4
|
import {
|
|
5
5
|
AaveIncentiveDataProviderV3ContractViem,
|
|
6
|
+
AaveIncentivesControllerViem,
|
|
6
7
|
AaveV3ViewContractViem,
|
|
7
8
|
createViemContractFromConfigFunc,
|
|
9
|
+
StkAAVEViem,
|
|
8
10
|
} from '../contracts';
|
|
9
11
|
import { aaveAnyGetAggregatedPositionData, aaveV3IsInIsolationMode, aaveV3IsInSiloedMode } from '../helpers/aaveHelpers';
|
|
10
12
|
import { AAVE_V3 } from '../markets/aave';
|
|
@@ -26,9 +28,10 @@ import {
|
|
|
26
28
|
EModeCategoryDataMapping,
|
|
27
29
|
} from '../types/aave';
|
|
28
30
|
import {
|
|
29
|
-
Blockish, EthAddress, EthereumProvider, NetworkNumber, PositionBalances,
|
|
31
|
+
Blockish, EthAddress, EthereumProvider, HexString, NetworkNumber, PositionBalances,
|
|
30
32
|
} from '../types/common';
|
|
31
33
|
import { getViemProvider, setViemBlockNumber } from '../services/viem';
|
|
34
|
+
import { SECONDS_PER_DAY, SECONDS_PER_YEAR } from '../constants';
|
|
32
35
|
|
|
33
36
|
export const aaveV3EmodeCategoriesMapping = (extractedState: any, usedAssets: AaveV3UsedAssets) => {
|
|
34
37
|
const { eModeCategoriesData }: { assetsData: AaveV3AssetsData, eModeCategoriesData: EModeCategoriesData } = extractedState;
|
|
@@ -337,7 +340,8 @@ export const _getAaveV3AccountBalances = async (provider: Client, network: Netwo
|
|
|
337
340
|
// @ts-ignore
|
|
338
341
|
const protocolDataProviderContract = createViemContractFromConfigFunc(market.protocolData, market.protocolDataAddress)(provider, network);
|
|
339
342
|
|
|
340
|
-
|
|
343
|
+
// @ts-ignore
|
|
344
|
+
const reserveTokens = await protocolDataProviderContract.read.getAllReservesTokens(setViemBlockNumber(block)) as { symbol: string, tokenAddress: EthAddress }[];
|
|
341
345
|
const symbols = reserveTokens.map(({ symbol }: { symbol: string }) => symbol);
|
|
342
346
|
const _addresses = reserveTokens.map(({ tokenAddress }: { tokenAddress: EthAddress }) => tokenAddress);
|
|
343
347
|
|
|
@@ -486,3 +490,97 @@ export const getAaveV3FullPositionData = async (provider: EthereumProvider, netw
|
|
|
486
490
|
const positionData = await getAaveV3AccountData(provider, network, address, { assetsData: marketData.assetsData, selectedMarket: market, eModeCategoriesData: marketData.eModeCategoriesData });
|
|
487
491
|
return positionData;
|
|
488
492
|
};
|
|
493
|
+
|
|
494
|
+
// aTokens eligible for AAVE rewards
|
|
495
|
+
export const REWARDABLE_ASSETS = [
|
|
496
|
+
'0x028171bCA77440897B824Ca71D1c56caC55b68A3', // DAI
|
|
497
|
+
'0x6C3c78838c761c6Ac7bE9F59fe808ea2A6E4379d',
|
|
498
|
+
'0xD37EE7e4f452C6638c96536e68090De8cBcdb583', // GUSD
|
|
499
|
+
'0x279AF5b99540c1A3A7E3CDd326e19659401eF99e',
|
|
500
|
+
'0xBcca60bB61934080951369a648Fb03DF4F96263C', // USDC
|
|
501
|
+
'0x619beb58998eD2278e08620f97007e1116D5D25b',
|
|
502
|
+
'0x3Ed3B47Dd13EC9a98b44e6204A523E766B225811', // USDT
|
|
503
|
+
'0x531842cEbbdD378f8ee36D171d6cC9C4fcf475Ec',
|
|
504
|
+
'0x9ff58f4fFB29fA2266Ab25e75e2A8b3503311656', // WBTC
|
|
505
|
+
'0x9c39809Dec7F95F5e0713634a4D0701329B3b4d2',
|
|
506
|
+
'0x030bA81f1c18d280636F32af80b9AAd02Cf0854e', // WETH
|
|
507
|
+
'0xF63B34710400CAd3e044cFfDcAb00a0f32E33eCf',
|
|
508
|
+
'0xa06bC25B5805d5F8d82847D191Cb4Af5A3e873E0', // LINK
|
|
509
|
+
'0x0b8f12b1788BFdE65Aa1ca52E3e9F3Ba401be16D',
|
|
510
|
+
'0x6C5024Cd4F8A59110119C56f8933403A539555EB', // SUSD
|
|
511
|
+
'0xdC6a3Ab17299D9C2A412B0e0a4C1f55446AE0817',
|
|
512
|
+
'0x5165d24277cD063F5ac44Efd447B27025e888f37', // YFI
|
|
513
|
+
'0x7EbD09022Be45AD993BAA1CEc61166Fcc8644d97',
|
|
514
|
+
'0xF256CC7847E919FAc9B808cC216cAc87CCF2f47a', // xSUSHI
|
|
515
|
+
'0xfAFEDF95E21184E3d880bd56D4806c4b8d31c69A',
|
|
516
|
+
'0xB9D7CB55f463405CDfBe4E90a6D2Df01C2B92BF1', // UNI
|
|
517
|
+
'0x5BdB050A92CADcCfCDcCCBFC17204a1C9cC0Ab73',
|
|
518
|
+
'0xc713e5E149D5D0715DcD1c156a020976e7E56B88', // MKR
|
|
519
|
+
'0xba728eAd5e496BE00DCF66F650b6d7758eCB50f8',
|
|
520
|
+
'0x101cc05f4A51C0319f570d5E146a8C625198e636', // TUSD
|
|
521
|
+
'0x01C0eb1f8c6F1C1bF74ae028697ce7AA2a8b0E92',
|
|
522
|
+
'0xc9BC48c72154ef3e5425641a3c747242112a46AF', // RAI
|
|
523
|
+
'0xB5385132EE8321977FfF44b60cDE9fE9AB0B4e6b',
|
|
524
|
+
'0x272F97b7a56a387aE942350bBC7Df5700f8a4576', // BAL
|
|
525
|
+
'0x13210D4Fe0d5402bd7Ecbc4B5bC5cFcA3b71adB0',
|
|
526
|
+
'0x2e8f4bdbe3d47d7d7de490437aea9915d930f1a3', // USDP
|
|
527
|
+
'0xfdb93b3b10936cf81fa59a02a7523b6e2149b2b7',
|
|
528
|
+
'0xA361718326c15715591c299427c62086F69923D9', // BUSD
|
|
529
|
+
'0xbA429f7011c9fa04cDd46a2Da24dc0FF0aC6099c',
|
|
530
|
+
'0xd4937682df3C8aEF4FE912A96A74121C0829E664', // FRAX
|
|
531
|
+
'0xfE8F19B17fFeF0fDbfe2671F248903055AFAA8Ca',
|
|
532
|
+
'0x683923dB55Fead99A79Fa01A27EeC3cB19679cC3', // FEI
|
|
533
|
+
'0xC2e10006AccAb7B45D9184FcF5b7EC7763f5BaAe',
|
|
534
|
+
'0x8dAE6Cb04688C62d939ed9B68d32Bc62e49970b1', // CRV
|
|
535
|
+
'0x00ad8eBF64F141f1C81e9f8f792d3d1631c6c684',
|
|
536
|
+
'0x6F634c6135D2EBD550000ac92F494F9CB8183dAe', // DPI
|
|
537
|
+
'0x4dDff5885a67E4EffeC55875a3977D7E60F82ae0',
|
|
538
|
+
] as const;
|
|
539
|
+
|
|
540
|
+
export const fetchYearlyMeritApyForStakingGho = async () => {
|
|
541
|
+
try {
|
|
542
|
+
const response = await fetch('https://apps.aavechan.com/api/merit/aprs', { signal: AbortSignal.timeout(5000) });
|
|
543
|
+
const data = await response.json();
|
|
544
|
+
const apr = data?.currentAPR?.actionsAPR?.['ethereum-stkgho'] || '0' as string;
|
|
545
|
+
const apy = aprToApy(apr);
|
|
546
|
+
const apyWithDFSBonus = new Dec(apy).mul(1.05).toString(); // 5% bonus for DFS users
|
|
547
|
+
return apyWithDFSBonus;
|
|
548
|
+
} catch (e) {
|
|
549
|
+
const message = 'External API Failure: Failed to fetch yearly merit APY for staking GHO';
|
|
550
|
+
console.error(message, e);
|
|
551
|
+
return '0';
|
|
552
|
+
}
|
|
553
|
+
};
|
|
554
|
+
|
|
555
|
+
export const getStakeAaveData = async (provider: Client, network: NetworkNumber, address: EthAddress) => {
|
|
556
|
+
const stkGhoAddress = getAssetInfo('stkGHO').address as HexString;
|
|
557
|
+
const stkAaveAddress = getAssetInfo('stkAAVE').address as HexString;
|
|
558
|
+
|
|
559
|
+
const AaveIncentivesController = AaveIncentivesControllerViem(provider, network);
|
|
560
|
+
const stkAAVE = StkAAVEViem(provider, network);
|
|
561
|
+
const stkGHO = createViemContractFromConfigFunc('Erc20', stkGhoAddress as HexString)(provider, network);
|
|
562
|
+
|
|
563
|
+
|
|
564
|
+
const [aaveRewardsBalance, emissionsPerSecond, stkAAVEBalance, stkAAVETotalSupply, stkGHOBalance, ghoMeritApy] = await Promise.all([
|
|
565
|
+
AaveIncentivesController.read.getRewardsBalance([REWARDABLE_ASSETS, address]),
|
|
566
|
+
stkAAVE.read.assets([stkAaveAddress]),
|
|
567
|
+
stkAAVE.read.balanceOf([address]),
|
|
568
|
+
stkAAVE.read.totalSupply(),
|
|
569
|
+
stkGHO.read.balanceOf([address]),
|
|
570
|
+
fetchYearlyMeritApyForStakingGho(),
|
|
571
|
+
]);
|
|
572
|
+
|
|
573
|
+
|
|
574
|
+
const stkAaveApy = new Dec(assetAmountInEth(emissionsPerSecond[0].toString(), 'GHO') || 0).mul(SECONDS_PER_YEAR).mul(100).div(assetAmountInEth(stkAAVETotalSupply.toString(), 'stkAAVE'))
|
|
575
|
+
.toString();
|
|
576
|
+
return {
|
|
577
|
+
activatedCooldown: '0',
|
|
578
|
+
activatedCooldownAmount: '0',
|
|
579
|
+
stkAaveRewardsBalance: '0',
|
|
580
|
+
aaveRewardsBalance: assetAmountInEth(aaveRewardsBalance.toString(), 'AAVE'),
|
|
581
|
+
stkAaveBalance: assetAmountInEth(stkAAVEBalance.toString(), 'stkAAVE'),
|
|
582
|
+
stkGhoBalance: assetAmountInEth(stkGHOBalance.toString(), 'GHO'),
|
|
583
|
+
ghoMeritApy,
|
|
584
|
+
stkAaveApy,
|
|
585
|
+
};
|
|
586
|
+
};
|
|
@@ -0,0 +1,163 @@
|
|
|
1
|
+
import { Client } from 'viem';
|
|
2
|
+
import Dec from 'decimal.js';
|
|
3
|
+
import { EthAddress, NetworkNumber } from '../types/common';
|
|
4
|
+
import { ClaimableToken, ClaimType } from '../types/claiming';
|
|
5
|
+
import {
|
|
6
|
+
AaveIncentiveDataProviderV3ContractViem,
|
|
7
|
+
AaveRewardsControllerViem,
|
|
8
|
+
} from '../contracts';
|
|
9
|
+
import { compareAddresses, getEthAmountForDecimals, wethToEth } from '../services/utils';
|
|
10
|
+
|
|
11
|
+
type AaveReward = {
|
|
12
|
+
amount: string;
|
|
13
|
+
symbol: string;
|
|
14
|
+
underlyingAsset: string;
|
|
15
|
+
rewardTokenAddress: string;
|
|
16
|
+
aTokenAddresses: string[];
|
|
17
|
+
};
|
|
18
|
+
|
|
19
|
+
/**
|
|
20
|
+
* won't cover all cases
|
|
21
|
+
*/
|
|
22
|
+
export const getAaveUnderlyingSymbol = (_symbol = '') => {
|
|
23
|
+
let symbol = _symbol
|
|
24
|
+
.replace(/^aEthLido/, '')
|
|
25
|
+
.replace(/^aEthEtherFi/, '')
|
|
26
|
+
.replace(/^aEth/, '')
|
|
27
|
+
.replace(/^aArb/, '')
|
|
28
|
+
.replace(/^aOpt/, '')
|
|
29
|
+
.replace(/^aBas/, '');
|
|
30
|
+
if (symbol.startsWith('a')) symbol = symbol.slice(1);
|
|
31
|
+
return wethToEth(symbol);
|
|
32
|
+
};
|
|
33
|
+
|
|
34
|
+
const mapAaveRewardsToClaimableTokens = (aaveRewards: AaveReward[], marketAddress: EthAddress, walletAddress: EthAddress) => aaveRewards.map(reward => ({
|
|
35
|
+
symbol: reward.symbol,
|
|
36
|
+
amount: reward.amount,
|
|
37
|
+
claimType: ClaimType.AAVE_REWARDS as const,
|
|
38
|
+
tokenAddress: reward.rewardTokenAddress as EthAddress,
|
|
39
|
+
underlyingSymbol: reward.underlyingAsset,
|
|
40
|
+
walletAddress,
|
|
41
|
+
label: 'AAVE Rewards',
|
|
42
|
+
additionalClaimFields: {
|
|
43
|
+
marketAddress,
|
|
44
|
+
aTokenAddresses: reward.aTokenAddresses,
|
|
45
|
+
isAaveToken: reward.symbol.startsWith('a'),
|
|
46
|
+
},
|
|
47
|
+
}));
|
|
48
|
+
|
|
49
|
+
export async function getUnclaimedRewardsForAllMarkets(
|
|
50
|
+
provider: Client,
|
|
51
|
+
network: NetworkNumber,
|
|
52
|
+
walletAddress: EthAddress,
|
|
53
|
+
marketAddress: EthAddress,
|
|
54
|
+
): Promise<ClaimableToken[]> {
|
|
55
|
+
const contract = AaveIncentiveDataProviderV3ContractViem(provider, network);
|
|
56
|
+
const tokensData = await contract.read.getUserReservesIncentivesData([marketAddress, walletAddress]);
|
|
57
|
+
const allTokensDataArrays = tokensData.reduce((acc: any[], val) => {
|
|
58
|
+
acc.push(val.aTokenIncentivesUserData.userRewardsInformation);
|
|
59
|
+
acc.push(val.vTokenIncentivesUserData.userRewardsInformation);
|
|
60
|
+
return acc;
|
|
61
|
+
}, []);
|
|
62
|
+
const allATokens = tokensData.reduce((acc, val) => {
|
|
63
|
+
acc.push(val.aTokenIncentivesUserData.tokenAddress);
|
|
64
|
+
acc.push(val.vTokenIncentivesUserData.tokenAddress);
|
|
65
|
+
return acc;
|
|
66
|
+
}, [] as string[]);
|
|
67
|
+
// array of rewards for each market (aToken/vToken)
|
|
68
|
+
// reward can be any token like wstETH, but also aToken like aWETH
|
|
69
|
+
const aaveRewardsController = AaveRewardsControllerViem(provider, network);
|
|
70
|
+
const rewardsPerAAsset = await Promise.all(allATokens.map(
|
|
71
|
+
(token: string) => aaveRewardsController.read.getAllUserRewards([[token as EthAddress], walletAddress])));
|
|
72
|
+
|
|
73
|
+
// match amounts to token symbol, parse decimal amounts
|
|
74
|
+
const rewardsPerAAssetWithInfo = rewardsPerAAsset.map((rewardForAAsset, aaIndex) => {
|
|
75
|
+
const rewardsList = rewardForAAsset[0];
|
|
76
|
+
const amounts = rewardForAAsset[1];
|
|
77
|
+
|
|
78
|
+
const rewardsDataArraysForAAsset = allTokensDataArrays[aaIndex];
|
|
79
|
+
const aTokenAddress = allATokens[aaIndex];
|
|
80
|
+
return rewardsList.map((rewardTokenAddress, rewardIndex) => {
|
|
81
|
+
const dataArrIndex = rewardsDataArraysForAAsset.findIndex((arr: { rewardTokenAddress: string | undefined; }) => compareAddresses(arr.rewardTokenAddress, rewardTokenAddress));
|
|
82
|
+
const amountWei = amounts[rewardIndex];
|
|
83
|
+
const amount = getEthAmountForDecimals(amountWei.toString(), rewardsDataArraysForAAsset[dataArrIndex]?.rewardTokenDecimals);
|
|
84
|
+
const symbol = rewardsDataArraysForAAsset[dataArrIndex]?.rewardTokenSymbol || '';
|
|
85
|
+
const underlyingAsset = getAaveUnderlyingSymbol(symbol);
|
|
86
|
+
return ({
|
|
87
|
+
amount,
|
|
88
|
+
symbol,
|
|
89
|
+
underlyingAsset,
|
|
90
|
+
rewardTokenAddress,
|
|
91
|
+
aTokenAddress,
|
|
92
|
+
});
|
|
93
|
+
});
|
|
94
|
+
});
|
|
95
|
+
|
|
96
|
+
// sum all unclaimed rewards of all markets per each token
|
|
97
|
+
// (e.g. how much awstETH is claimable in total from both aUSDC and awstETH markets)
|
|
98
|
+
const totalUnclaimedPerRewardToken: Record<string, any> = {};
|
|
99
|
+
rewardsPerAAssetWithInfo
|
|
100
|
+
.flat()
|
|
101
|
+
.forEach(({
|
|
102
|
+
amount, symbol, underlyingAsset, rewardTokenAddress, aTokenAddress,
|
|
103
|
+
}) => {
|
|
104
|
+
if (+amount > 0) {
|
|
105
|
+
if (!totalUnclaimedPerRewardToken[symbol]) {
|
|
106
|
+
totalUnclaimedPerRewardToken[symbol] = {
|
|
107
|
+
amount, symbol, underlyingAsset, rewardTokenAddress, aTokenAddresses: [aTokenAddress],
|
|
108
|
+
};
|
|
109
|
+
} else {
|
|
110
|
+
totalUnclaimedPerRewardToken[symbol].amount =
|
|
111
|
+
new Dec(totalUnclaimedPerRewardToken[symbol].amount).add(amount).toString();
|
|
112
|
+
totalUnclaimedPerRewardToken[symbol].aTokenAddresses.push(aTokenAddress);
|
|
113
|
+
}
|
|
114
|
+
}
|
|
115
|
+
}, []);
|
|
116
|
+
|
|
117
|
+
return mapAaveRewardsToClaimableTokens(Object.values(totalUnclaimedPerRewardToken), marketAddress, walletAddress);
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
export async function getMeritUnclaimedRewards(account: EthAddress, network: NetworkNumber, acceptMorpho: boolean = true): Promise<ClaimableToken[]> {
|
|
121
|
+
const res = await fetch(`https://api.merkl.xyz/v4/users/${account}/rewards?chainId=${network}`,
|
|
122
|
+
{ signal: AbortSignal.timeout(3000) });
|
|
123
|
+
const data = await res.json();
|
|
124
|
+
|
|
125
|
+
const claimableTokens: ClaimableToken[] = [];
|
|
126
|
+
|
|
127
|
+
data.forEach((item: { rewards: any[]; }) => {
|
|
128
|
+
item.rewards.forEach(reward => {
|
|
129
|
+
const {
|
|
130
|
+
token,
|
|
131
|
+
amount,
|
|
132
|
+
claimed,
|
|
133
|
+
proofs,
|
|
134
|
+
} = reward;
|
|
135
|
+
|
|
136
|
+
const isTokenMorpho = token.symbol === 'MORPHO';
|
|
137
|
+
if (!token || !token.symbol || amount === '0' || (isTokenMorpho && !acceptMorpho)) return;
|
|
138
|
+
|
|
139
|
+
const unclaimedAmount = new Dec(amount).minus(claimed || 0).toString();
|
|
140
|
+
if (unclaimedAmount === '0') return;
|
|
141
|
+
|
|
142
|
+
const unclaimed = getEthAmountForDecimals(unclaimedAmount, token.decimals);
|
|
143
|
+
|
|
144
|
+
claimableTokens.push({
|
|
145
|
+
claimType: ClaimType.AAVE_MERIT_REWARDS,
|
|
146
|
+
amount: unclaimed,
|
|
147
|
+
symbol: token.symbol,
|
|
148
|
+
tokenAddress: token.address,
|
|
149
|
+
walletAddress: account,
|
|
150
|
+
label: 'AAVE Merit Rewards',
|
|
151
|
+
underlyingSymbol: getAaveUnderlyingSymbol(token.symbol),
|
|
152
|
+
additionalClaimFields: {
|
|
153
|
+
accumulated: amount,
|
|
154
|
+
proof: proofs,
|
|
155
|
+
decimals: token.decimals,
|
|
156
|
+
unclaimed: unclaimedAmount,
|
|
157
|
+
},
|
|
158
|
+
});
|
|
159
|
+
});
|
|
160
|
+
});
|
|
161
|
+
|
|
162
|
+
return claimableTokens;
|
|
163
|
+
}
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
import { Client } from 'viem';
|
|
2
|
+
import { assetAmountInEth, getAssetInfoByAddress } from '@defisaver/tokens';
|
|
3
|
+
import { EthAddress, NetworkNumber } from '../types/common';
|
|
4
|
+
import { CompV3ViewContractViem } from '../contracts';
|
|
5
|
+
import { ClaimType } from '../types/claiming';
|
|
6
|
+
|
|
7
|
+
export const getCompoundV3Rewards = async (provider: Client, network: NetworkNumber, user: EthAddress, market: any) => {
|
|
8
|
+
const compV3View = CompV3ViewContractViem(provider, network);
|
|
9
|
+
const rewards = await compV3View.read.getRewardsOwed([market, user]);
|
|
10
|
+
if (!rewards || rewards.owed.toString() === '0' || getAssetInfoByAddress(rewards.token, network).symbol !== 'COMP') return [];
|
|
11
|
+
return [{
|
|
12
|
+
symbol: 'COMP',
|
|
13
|
+
underlyingSymbol: 'COMP',
|
|
14
|
+
tokenAddress: rewards.token,
|
|
15
|
+
amount: assetAmountInEth(rewards.owed.toString() || 0, 'COMP'),
|
|
16
|
+
walletAddress: user,
|
|
17
|
+
label: 'Compound V3',
|
|
18
|
+
claimType: ClaimType.COMPOUND_V3_COMP,
|
|
19
|
+
additionalClaimFields: {
|
|
20
|
+
marketAddress: market,
|
|
21
|
+
},
|
|
22
|
+
}];
|
|
23
|
+
};
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import * as aaveV3Claim from './aaveV3';
|
|
2
|
+
import * as compV3Claim from './compV3';
|
|
3
|
+
import * as kingV3Claim from './king';
|
|
4
|
+
import * as morphoBlueClaim from './morphoBlue';
|
|
5
|
+
import * as sparkClaim from './spark';
|
|
6
|
+
|
|
7
|
+
export {
|
|
8
|
+
aaveV3Claim,
|
|
9
|
+
compV3Claim,
|
|
10
|
+
kingV3Claim,
|
|
11
|
+
morphoBlueClaim,
|
|
12
|
+
sparkClaim,
|
|
13
|
+
};
|
|
@@ -0,0 +1,66 @@
|
|
|
1
|
+
import Dec from 'decimal.js';
|
|
2
|
+
import { assetAmountInEth } from '@defisaver/tokens';
|
|
3
|
+
import { Client } from 'viem';
|
|
4
|
+
import { UUPSViem } from '../contracts';
|
|
5
|
+
import { EthAddress, HexString, NetworkNumber } from '../types/common';
|
|
6
|
+
import { ClaimType } from '../types/claiming';
|
|
7
|
+
|
|
8
|
+
export const fetchKingRewards = async (walletAddress: EthAddress) => {
|
|
9
|
+
try {
|
|
10
|
+
const res = await fetch(`https://fe.defisaver.com/api/etherfi/get-king-rewards/${walletAddress}`,
|
|
11
|
+
{ signal: AbortSignal.timeout(5000) });
|
|
12
|
+
|
|
13
|
+
if (!res.ok) throw new Error(await res.text());
|
|
14
|
+
|
|
15
|
+
return await res.json();
|
|
16
|
+
} catch (err) {
|
|
17
|
+
console.error('External API Error: Error fetching KING rewards:', err);
|
|
18
|
+
return { Amount: '0', Root: '', Proofs: [] };
|
|
19
|
+
}
|
|
20
|
+
};
|
|
21
|
+
|
|
22
|
+
export const getKingRewards = async (provider: Client, network: NetworkNumber, walletAddresses: EthAddress[]) => {
|
|
23
|
+
// Fetch all API data in parallel (these are external API calls, can't be batched with multicall)
|
|
24
|
+
const apiDataPromises = walletAddresses.map(address => fetchKingRewards(address));
|
|
25
|
+
const apiDataArray = await Promise.all(apiDataPromises);
|
|
26
|
+
|
|
27
|
+
// Batch all contract calls using multicall
|
|
28
|
+
const contract = UUPSViem(provider, network);
|
|
29
|
+
const cumulativePromises = walletAddresses.map(address => contract.read.cumulativeClaimed([address]),
|
|
30
|
+
);
|
|
31
|
+
const cumulativeResults = await Promise.all(cumulativePromises);
|
|
32
|
+
|
|
33
|
+
// Process results
|
|
34
|
+
const results: Record<string, any[]> = {};
|
|
35
|
+
|
|
36
|
+
for (let i = 0; i < walletAddresses.length; i++) {
|
|
37
|
+
const walletAddress = walletAddresses[i];
|
|
38
|
+
const data = apiDataArray[i];
|
|
39
|
+
const cumulative = cumulativeResults[i];
|
|
40
|
+
|
|
41
|
+
const allRewardsAmount = assetAmountInEth(data.Amount, 'KING');
|
|
42
|
+
const claimedAmount = assetAmountInEth(cumulative.toString(), 'KING');
|
|
43
|
+
const amountToClaim = new Dec(allRewardsAmount).sub(claimedAmount);
|
|
44
|
+
|
|
45
|
+
if (amountToClaim.lessThanOrEqualTo('0')) {
|
|
46
|
+
results[walletAddress.toLowerCase() as EthAddress] = [];
|
|
47
|
+
} else {
|
|
48
|
+
results[walletAddress.toLowerCase() as EthAddress] = [{
|
|
49
|
+
symbol: 'KING',
|
|
50
|
+
underlyingSymbol: 'KING',
|
|
51
|
+
tokenAddress: '0x8F08B70456eb22f6109F57b8fafE862ED28E6040',
|
|
52
|
+
amount: amountToClaim.toString(),
|
|
53
|
+
walletAddress,
|
|
54
|
+
label: 'weETH',
|
|
55
|
+
claimType: ClaimType.KING_REWARDS,
|
|
56
|
+
additionalClaimFields: {
|
|
57
|
+
allRewardsAmount,
|
|
58
|
+
merkleRoot: data.Root,
|
|
59
|
+
merkleProofs: data.Proofs,
|
|
60
|
+
},
|
|
61
|
+
}];
|
|
62
|
+
}
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
return results;
|
|
66
|
+
};
|
|
@@ -0,0 +1,119 @@
|
|
|
1
|
+
import { assetAmountInEth, getAssetInfoByAddress } from '@defisaver/tokens';
|
|
2
|
+
import { Client } from 'viem';
|
|
3
|
+
import Dec from 'decimal.js';
|
|
4
|
+
import { ClaimableToken, ClaimType, MorphoClaimableToken } from '../types/claiming';
|
|
5
|
+
import { EthAddress, NetworkNumber } from '../types/common';
|
|
6
|
+
import { createViemContractFromConfigFunc } from '../contracts';
|
|
7
|
+
import { wethToEth } from '../services/utils';
|
|
8
|
+
|
|
9
|
+
const MORPHO_ALLOWED_CONTRACTS = [
|
|
10
|
+
'0x330eefa8a787552DC5cAd3C3cA644844B1E61Ddb',
|
|
11
|
+
'0x678dDC1d07eaa166521325394cDEb1E4c086DF43',
|
|
12
|
+
'0x2efd4625d0c149ebadf118ec5446c6de24d916a4',
|
|
13
|
+
];
|
|
14
|
+
|
|
15
|
+
const MORPHO_ALLOWED_TOKENS = [
|
|
16
|
+
'MORPHO',
|
|
17
|
+
'MORPHO Legacy',
|
|
18
|
+
];
|
|
19
|
+
|
|
20
|
+
export const getMorphoUnderlyingSymbol = (_symbol: string) => {
|
|
21
|
+
if (_symbol === 'MORPHO Legacy') return 'MORPHO';
|
|
22
|
+
return wethToEth(_symbol);
|
|
23
|
+
};
|
|
24
|
+
|
|
25
|
+
|
|
26
|
+
export const getMorphoBlueRewardsInfo = async (address: EthAddress) => {
|
|
27
|
+
if (!address) return { claimable: '0' };
|
|
28
|
+
const res = await fetch(`https://rewards.morpho.org/v1/users/${address}/distributions`,
|
|
29
|
+
{ signal: AbortSignal.timeout(3000) });
|
|
30
|
+
|
|
31
|
+
if (!res.ok) throw new Error(await res.text());
|
|
32
|
+
|
|
33
|
+
return res.json();
|
|
34
|
+
};
|
|
35
|
+
|
|
36
|
+
export const fetchMorphoBlueRewards = async (
|
|
37
|
+
provider: Client,
|
|
38
|
+
network: NetworkNumber,
|
|
39
|
+
walletAddresses: EthAddress[],
|
|
40
|
+
): Promise<Record<string, ClaimableToken[]>> => {
|
|
41
|
+
// Fetch all API data in parallel (these are external API calls, can't be batched with multicall)
|
|
42
|
+
const apiDataPromises = walletAddresses.map(address => getMorphoBlueRewardsInfo(address));
|
|
43
|
+
const apiDataArray = await Promise.all(apiDataPromises);
|
|
44
|
+
|
|
45
|
+
// Process API data to get claimable tokens for each wallet
|
|
46
|
+
const allClaimableTokens: Array<{ walletAddress: EthAddress; tokens: ClaimableToken[] }> = [];
|
|
47
|
+
const contractCallsData: Array<{ walletAddress: EthAddress; tokenAddress: EthAddress; distributor: EthAddress }> = [];
|
|
48
|
+
|
|
49
|
+
for (let i = 0; i < walletAddresses.length; i++) {
|
|
50
|
+
const walletAddress = walletAddresses[i];
|
|
51
|
+
const data = apiDataArray[i];
|
|
52
|
+
|
|
53
|
+
const claimableTokens = data?.data?.reduce((acc: ClaimableToken[], reward: any) => {
|
|
54
|
+
const token = getAssetInfoByAddress(reward.asset.address);
|
|
55
|
+
if (!MORPHO_ALLOWED_CONTRACTS.includes(reward?.distributor?.address) || !MORPHO_ALLOWED_TOKENS.includes(token.symbol)) {
|
|
56
|
+
return acc;
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
// Store contract call data for batching
|
|
60
|
+
contractCallsData.push({
|
|
61
|
+
walletAddress,
|
|
62
|
+
tokenAddress: reward.asset.address,
|
|
63
|
+
distributor: reward?.distributor?.address,
|
|
64
|
+
});
|
|
65
|
+
|
|
66
|
+
return [
|
|
67
|
+
...acc,
|
|
68
|
+
{
|
|
69
|
+
symbol: token.symbol,
|
|
70
|
+
underlyingSymbol: getMorphoUnderlyingSymbol(token.symbol),
|
|
71
|
+
tokenAddress: reward.asset.address,
|
|
72
|
+
amount: assetAmountInEth(reward.claimable, token.symbol),
|
|
73
|
+
walletAddress,
|
|
74
|
+
label: token.symbol,
|
|
75
|
+
claimType: ClaimType.MORPHO,
|
|
76
|
+
additionalClaimFields: {
|
|
77
|
+
originalAmount: reward.claimable,
|
|
78
|
+
distributor: reward?.distributor?.address,
|
|
79
|
+
merkleProofs: reward?.proof,
|
|
80
|
+
isLegacy: token.symbol === 'MORPHO Legacy',
|
|
81
|
+
txData: reward?.tx_data,
|
|
82
|
+
},
|
|
83
|
+
}];
|
|
84
|
+
}, []) || [];
|
|
85
|
+
|
|
86
|
+
allClaimableTokens.push({ walletAddress, tokens: claimableTokens });
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
// Batch all contract calls using multicall
|
|
90
|
+
const contractPromises = contractCallsData.map(({ walletAddress, tokenAddress, distributor }) => {
|
|
91
|
+
const distributorContract = createViemContractFromConfigFunc('MorphoDistributor', distributor)(provider, network);
|
|
92
|
+
return distributorContract.read.claimed([walletAddress, tokenAddress]);
|
|
93
|
+
});
|
|
94
|
+
const contractResults = await Promise.all(contractPromises);
|
|
95
|
+
|
|
96
|
+
// Process results
|
|
97
|
+
const results: Record<string, ClaimableToken[]> = {};
|
|
98
|
+
let contractCallIndex = 0;
|
|
99
|
+
|
|
100
|
+
for (const { walletAddress, tokens } of allClaimableTokens) {
|
|
101
|
+
const updatedTokens: ClaimableToken[] = [];
|
|
102
|
+
|
|
103
|
+
for (let i = 0; i < tokens.length; i++) {
|
|
104
|
+
const claimableToken = tokens[i];
|
|
105
|
+
const claimed = assetAmountInEth(contractResults[contractCallIndex].toString(), claimableToken.underlyingSymbol);
|
|
106
|
+
contractCallIndex++;
|
|
107
|
+
|
|
108
|
+
const updatedToken = { ...claimableToken };
|
|
109
|
+
updatedToken.amount = new Dec(claimableToken.amount).sub(claimed).toString();
|
|
110
|
+
updatedTokens.push(updatedToken);
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
results[walletAddress.toLowerCase() as EthAddress] = updatedTokens.filter(
|
|
114
|
+
(claimableToken) => new Dec(claimableToken.amount).gt(0),
|
|
115
|
+
);
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
return results;
|
|
119
|
+
};
|