@kasufinance/kasu-sdk 1.0.3 → 1.0.4
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/bundle.cjs.js +133 -32
- package/dist/bundle.esm.js +133 -32
- package/dist/contracts/IClearingCoordinatorAbi.js +1 -0
- package/dist/contracts/IClearingCoordinatorAbi.js.map +1 -0
- package/dist/contracts/IERC20MetadataAbi.js +1 -0
- package/dist/contracts/IERC20MetadataAbi.js.map +1 -0
- package/dist/contracts/IFeeManagerAbi.js +1 -0
- package/dist/contracts/IFeeManagerAbi.js.map +1 -0
- package/dist/contracts/IKSULockingAbi.js +1 -0
- package/dist/contracts/IKSULockingAbi.js.map +1 -0
- package/dist/contracts/IKasuAllowListAbi.js +1 -0
- package/dist/contracts/IKasuAllowListAbi.js.map +1 -0
- package/dist/contracts/IKasuControllerAbi.js +1 -0
- package/dist/contracts/IKasuControllerAbi.js.map +1 -0
- package/dist/contracts/IKasuNFTsAbi.js +1 -0
- package/dist/contracts/IKasuNFTsAbi.js.map +1 -0
- package/dist/contracts/IKsuPriceAbi.js +1 -0
- package/dist/contracts/IKsuPriceAbi.js.map +1 -0
- package/dist/contracts/ILendingPoolAbi.js +1 -0
- package/dist/contracts/ILendingPoolAbi.js.map +1 -0
- package/dist/contracts/ILendingPoolFactoryAbi.js +1 -0
- package/dist/contracts/ILendingPoolFactoryAbi.js.map +1 -0
- package/dist/contracts/ILendingPoolManagerAbi.js +1 -0
- package/dist/contracts/ILendingPoolManagerAbi.js.map +1 -0
- package/dist/contracts/ILendingPoolTrancheAbi.js +1 -0
- package/dist/contracts/ILendingPoolTrancheAbi.js.map +1 -0
- package/dist/contracts/ISystemVariablesAbi.js +1 -0
- package/dist/contracts/ISystemVariablesAbi.js.map +1 -0
- package/dist/contracts/IUserLoyaltyRewardsAbi.js +1 -0
- package/dist/contracts/IUserLoyaltyRewardsAbi.js.map +1 -0
- package/dist/contracts/IUserManagerAbi.js +1 -0
- package/dist/contracts/IUserManagerAbi.js.map +1 -0
- package/dist/contracts/KSULockBonusAbi.js +1 -0
- package/dist/contracts/KSULockBonusAbi.js.map +1 -0
- package/dist/contracts/KasuPoolExternalTVLAbi.js +1 -0
- package/dist/contracts/KasuPoolExternalTVLAbi.js.map +1 -0
- package/dist/contracts/SwapperAbi.js +1 -0
- package/dist/contracts/SwapperAbi.js.map +1 -0
- package/dist/contracts/common.js +1 -0
- package/dist/contracts/common.js.map +1 -0
- package/dist/contracts/factories/IClearingCoordinatorAbi__factory.js +1 -0
- package/dist/contracts/factories/IClearingCoordinatorAbi__factory.js.map +1 -0
- package/dist/contracts/factories/IERC20MetadataAbi__factory.js +1 -0
- package/dist/contracts/factories/IERC20MetadataAbi__factory.js.map +1 -0
- package/dist/contracts/factories/IFeeManagerAbi__factory.js +1 -0
- package/dist/contracts/factories/IFeeManagerAbi__factory.js.map +1 -0
- package/dist/contracts/factories/IKSULockingAbi__factory.js +1 -0
- package/dist/contracts/factories/IKSULockingAbi__factory.js.map +1 -0
- package/dist/contracts/factories/IKasuAllowListAbi__factory.js +1 -0
- package/dist/contracts/factories/IKasuAllowListAbi__factory.js.map +1 -0
- package/dist/contracts/factories/IKasuControllerAbi__factory.js +1 -0
- package/dist/contracts/factories/IKasuControllerAbi__factory.js.map +1 -0
- package/dist/contracts/factories/IKasuNFTsAbi__factory.js +1 -0
- package/dist/contracts/factories/IKasuNFTsAbi__factory.js.map +1 -0
- package/dist/contracts/factories/IKsuPriceAbi__factory.js +1 -0
- package/dist/contracts/factories/IKsuPriceAbi__factory.js.map +1 -0
- package/dist/contracts/factories/ILendingPoolAbi__factory.js +1 -0
- package/dist/contracts/factories/ILendingPoolAbi__factory.js.map +1 -0
- package/dist/contracts/factories/ILendingPoolFactoryAbi__factory.js +1 -0
- package/dist/contracts/factories/ILendingPoolFactoryAbi__factory.js.map +1 -0
- package/dist/contracts/factories/ILendingPoolManagerAbi__factory.js +1 -0
- package/dist/contracts/factories/ILendingPoolManagerAbi__factory.js.map +1 -0
- package/dist/contracts/factories/ILendingPoolTrancheAbi__factory.js +1 -0
- package/dist/contracts/factories/ILendingPoolTrancheAbi__factory.js.map +1 -0
- package/dist/contracts/factories/ISystemVariablesAbi__factory.js +1 -0
- package/dist/contracts/factories/ISystemVariablesAbi__factory.js.map +1 -0
- package/dist/contracts/factories/IUserLoyaltyRewardsAbi__factory.js +1 -0
- package/dist/contracts/factories/IUserLoyaltyRewardsAbi__factory.js.map +1 -0
- package/dist/contracts/factories/IUserManagerAbi__factory.js +1 -0
- package/dist/contracts/factories/IUserManagerAbi__factory.js.map +1 -0
- package/dist/contracts/factories/KSULockBonusAbi__factory.js +1 -0
- package/dist/contracts/factories/KSULockBonusAbi__factory.js.map +1 -0
- package/dist/contracts/factories/KasuPoolExternalTVLAbi__factory.js +1 -0
- package/dist/contracts/factories/KasuPoolExternalTVLAbi__factory.js.map +1 -0
- package/dist/contracts/factories/SwapperAbi__factory.js +1 -0
- package/dist/contracts/factories/SwapperAbi__factory.js.map +1 -0
- package/dist/contracts/factories/index.js +1 -0
- package/dist/contracts/factories/index.js.map +1 -0
- package/dist/contracts/index.js +1 -0
- package/dist/contracts/index.js.map +1 -0
- package/dist/index.js +1 -0
- package/dist/index.js.map +1 -0
- package/dist/sdk-config.js +1 -0
- package/dist/sdk-config.js.map +1 -0
- package/dist/services/DataService/data-service.js +1 -0
- package/dist/services/DataService/data-service.js.map +1 -0
- package/dist/services/DataService/directus-types.js +1 -0
- package/dist/services/DataService/directus-types.js.map +1 -0
- package/dist/services/DataService/queries.js +1 -0
- package/dist/services/DataService/queries.js.map +1 -0
- package/dist/services/DataService/subgraph-types.js +1 -0
- package/dist/services/DataService/subgraph-types.js.map +1 -0
- package/dist/services/DataService/types.js +1 -0
- package/dist/services/DataService/types.js.map +1 -0
- package/dist/services/Locking/locking.js +1 -0
- package/dist/services/Locking/locking.js.map +1 -0
- package/dist/services/Locking/queries.js +1 -0
- package/dist/services/Locking/queries.js.map +1 -0
- package/dist/services/Locking/types.js +1 -0
- package/dist/services/Locking/types.js.map +1 -0
- package/dist/services/Portfolio/portfolio.d.ts +40 -2
- package/dist/services/Portfolio/portfolio.js +137 -33
- package/dist/services/Portfolio/portfolio.js.map +1 -0
- package/dist/services/Portfolio/queries.js +1 -0
- package/dist/services/Portfolio/queries.js.map +1 -0
- package/dist/services/Portfolio/types.js +1 -0
- package/dist/services/Portfolio/types.js.map +1 -0
- package/dist/services/Swapper/swapper.js +1 -0
- package/dist/services/Swapper/swapper.js.map +1 -0
- package/dist/services/Swapper/types.js +1 -0
- package/dist/services/Swapper/types.js.map +1 -0
- package/dist/services/UserLending/helper.js +1 -0
- package/dist/services/UserLending/helper.js.map +1 -0
- package/dist/services/UserLending/queries.js +1 -0
- package/dist/services/UserLending/queries.js.map +1 -0
- package/dist/services/UserLending/subgraph-types.js +1 -0
- package/dist/services/UserLending/subgraph-types.js.map +1 -0
- package/dist/services/UserLending/types.js +1 -0
- package/dist/services/UserLending/types.js.map +1 -0
- package/dist/services/UserLending/user-lending.js +1 -0
- package/dist/services/UserLending/user-lending.js.map +1 -0
- package/dist/services/shared.js +1 -0
- package/dist/services/shared.js.map +1 -0
- package/dist/tests/sample.test.js +1 -0
- package/dist/tests/sample.test.js.map +1 -0
- package/package.json +2 -1
- package/src/services/Portfolio/portfolio.ts +261 -45
- package/tsconfig.json +1 -1
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"sample.test.js","sourceRoot":"","sources":["../../src/tests/sample.test.ts"],"names":[],"mappings":";;;;;;;;;AAAA,OAAO,KAAK,MAAM,MAAM,QAAQ,CAAC;AAEjC,OAAO,EAAE,OAAO,EAAE,MAAM,IAAI,CAAC;AAC7B,OAAO,EAAE,SAAS,EAAE,MAAM,eAAe,CAAC;AAE1C,IAAI,CAAC,cAAc,EAAE,GAAS,EAAE;IAC5B,MAAM,QAAQ,GAAG,IAAI,MAAM,CAAC,SAAS,CAAC,eAAe,CACjD,0BAA0B,CAC7B,CAAC;IAEF,MAAM,MAAM,GAAG,IAAI,MAAM,CAAC,MAAM,CAC5B,kEAAkE,EAClE,QAAQ,CACX,CAAC;IACF,MAAM,MAAM,GAAG,IAAI,SAAS,CACxB,6EAA6E,EAC7E,mGAAmG,EACnG;QACI,WAAW,EAAE,4CAA4C;QACzD,aAAa,EAAE,4CAA4C;QAC3D,QAAQ,EAAE,4CAA4C;QACtD,WAAW,EAAE,4CAA4C;QACzD,kBAAkB,EAAE,4CAA4C;QAChE,aAAa,EAAE,4CAA4C;QAC3D,eAAe,EAAE,4CAA4C;QAC7D,kBAAkB,EAAE,4CAA4C;QAChE,QAAQ,EAAE,4CAA4C;QACtD,mBAAmB,EAAE,4CAA4C;QACjE,QAAQ,EAAE,4CAA4C;QACtD,WAAW,EAAE,4CAA4C;KAC5D,EAED,mCAAmC,EAEnC,CAAC,EAAE,CAAC,CACP,CAAC;IACF,MAAM,GAAG,GAAG,IAAI,OAAO,CAAC,MAAM,EAAE,MAAM,CAAC,CAAC;IAExC,OAAO,CAAC,GAAG,CACP,MAAM,GAAG,CAAC,WAAW,CAAC,eAAe,CACjC,4CAA4C,CAC/C,CACJ,CAAC;AACN,CAAC,CAAA,CAAC,CAAC"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@kasufinance/kasu-sdk",
|
|
3
|
-
"version": "1.0.
|
|
3
|
+
"version": "1.0.4",
|
|
4
4
|
"description": "",
|
|
5
5
|
"main": "dist/bundle.cjs.js",
|
|
6
6
|
"module": "dist/bundle.esm.js",
|
|
@@ -9,6 +9,7 @@
|
|
|
9
9
|
"test": "jest --config=jest-config.ts",
|
|
10
10
|
"build-tc": "typechain --target ethers-v5 --out-dir src/contracts './abis/*.json'",
|
|
11
11
|
"build": "eslint . && tsc --build --force tsconfig.json",
|
|
12
|
+
"build:watch": "npx tsc -b --force --watch",
|
|
12
13
|
"rollup-build": "rollup -c",
|
|
13
14
|
"lint": "eslint . --fix"
|
|
14
15
|
},
|
|
@@ -8,11 +8,28 @@ import {
|
|
|
8
8
|
IKasuNFTsAbi__factory,
|
|
9
9
|
ISystemVariablesAbi,
|
|
10
10
|
ISystemVariablesAbi__factory,
|
|
11
|
+
IUserLoyaltyRewardsAbi__factory,
|
|
11
12
|
} from '../../contracts';
|
|
12
13
|
import { SdkConfig } from '../../sdk-config';
|
|
13
14
|
import { DataService } from '../DataService/data-service';
|
|
15
|
+
import {
|
|
16
|
+
getAllTrancheConfigurationsQuery,
|
|
17
|
+
getAllTranchesQuery,
|
|
18
|
+
} from '../DataService/queries';
|
|
19
|
+
import {
|
|
20
|
+
TrancheConfigurationSubgraphResult,
|
|
21
|
+
TrancheSubgraphResult,
|
|
22
|
+
} from '../DataService/subgraph-types';
|
|
14
23
|
import { PoolOverview } from '../DataService/types';
|
|
15
24
|
import { KSULocking } from '../Locking/locking';
|
|
25
|
+
import {
|
|
26
|
+
getSystemVariablesQuery,
|
|
27
|
+
lockingSummariesQuery,
|
|
28
|
+
} from '../Locking/queries';
|
|
29
|
+
import {
|
|
30
|
+
LockingSummarySubgraphResult,
|
|
31
|
+
SystemVariables,
|
|
32
|
+
} from '../Locking/types';
|
|
16
33
|
import { UserLending } from '../UserLending/user-lending';
|
|
17
34
|
|
|
18
35
|
import { lendingPortfolioQuery } from './queries';
|
|
@@ -33,6 +50,9 @@ export class Portfolio {
|
|
|
33
50
|
private readonly _graph: GraphQLClient;
|
|
34
51
|
|
|
35
52
|
private _systemVariablesAbi: ISystemVariablesAbi;
|
|
53
|
+
private _userLoyaltyRewardsAbi: ReturnType<
|
|
54
|
+
typeof IUserLoyaltyRewardsAbi__factory.connect
|
|
55
|
+
>;
|
|
36
56
|
private _kasuNftContract: IKasuNFTsAbi;
|
|
37
57
|
readonly _signerOrProvider: Signer | Provider;
|
|
38
58
|
|
|
@@ -47,6 +67,10 @@ export class Portfolio {
|
|
|
47
67
|
_kasuConfig.contracts.SystemVariables,
|
|
48
68
|
signerOrProvider,
|
|
49
69
|
);
|
|
70
|
+
this._userLoyaltyRewardsAbi = IUserLoyaltyRewardsAbi__factory.connect(
|
|
71
|
+
_kasuConfig.contracts.UserLoyaltyRewards,
|
|
72
|
+
signerOrProvider,
|
|
73
|
+
);
|
|
50
74
|
this._userLendingService = new UserLending(
|
|
51
75
|
_kasuConfig,
|
|
52
76
|
signerOrProvider,
|
|
@@ -58,49 +82,17 @@ export class Portfolio {
|
|
|
58
82
|
this._graph = new GraphQLClient(_kasuConfig.subgraphUrl);
|
|
59
83
|
}
|
|
60
84
|
|
|
61
|
-
|
|
62
|
-
const userLockingData =
|
|
63
|
-
await this._lockingService.getUserBonusData(userAddress);
|
|
64
|
-
const lockingRewards =
|
|
65
|
-
await this._lockingService.getLockingRewards(userAddress);
|
|
66
|
-
const ksuLaunchBonus =
|
|
67
|
-
await this._lockingService.getUserTotalBonusAmount(userAddress);
|
|
68
|
-
return {
|
|
69
|
-
bonusYieldEarnings: {
|
|
70
|
-
claimableBalance: {
|
|
71
|
-
ksuAmount: userLockingData.ksuBonusAndRewards,
|
|
72
|
-
},
|
|
73
|
-
lifeTime: {
|
|
74
|
-
ksuAmount: userLockingData.ksuBonusAndRewardsLifetime,
|
|
75
|
-
},
|
|
76
|
-
},
|
|
77
|
-
protocolFees: {
|
|
78
|
-
claimableBalance: {
|
|
79
|
-
usdcAmount: lockingRewards.claimableRewards,
|
|
80
|
-
},
|
|
81
|
-
lifeTime: { usdcAmount: lockingRewards.lifeTimeRewards },
|
|
82
|
-
},
|
|
83
|
-
ksuLaunchBonus: { lifeTime: { ksuAmount: ksuLaunchBonus } },
|
|
84
|
-
};
|
|
85
|
-
}
|
|
86
|
-
|
|
87
|
-
async getUserNfts(userAddress: string): Promise<number[]> {
|
|
88
|
-
const usernfts = await this._kasuNftContract.tokensOfOwner(userAddress);
|
|
89
|
-
|
|
90
|
-
return usernfts.map((nft) => nft.toNumber());
|
|
91
|
-
}
|
|
92
|
-
|
|
93
|
-
async getPortfolioSummary(
|
|
94
|
-
userAddress: string,
|
|
85
|
+
private computeYieldMetrics(
|
|
95
86
|
portfolioLendingPools: PortfolioLendingPool[],
|
|
96
|
-
):
|
|
97
|
-
|
|
98
|
-
|
|
87
|
+
): {
|
|
88
|
+
totalInvestments: number;
|
|
89
|
+
totalYieldEarned: number;
|
|
90
|
+
weightedApy: number;
|
|
91
|
+
weeklyYieldEarnings: number;
|
|
92
|
+
} {
|
|
99
93
|
let totalInvestments = 0;
|
|
100
|
-
|
|
101
|
-
let weightedApy = 0;
|
|
102
94
|
let totalYieldEarned = 0;
|
|
103
|
-
|
|
95
|
+
let weightedApy = 0;
|
|
104
96
|
let weeklyYieldEarnings = 0;
|
|
105
97
|
|
|
106
98
|
for (const pool of portfolioLendingPools) {
|
|
@@ -139,6 +131,233 @@ export class Portfolio {
|
|
|
139
131
|
}
|
|
140
132
|
}
|
|
141
133
|
|
|
134
|
+
return {
|
|
135
|
+
totalInvestments,
|
|
136
|
+
totalYieldEarned,
|
|
137
|
+
weightedApy,
|
|
138
|
+
weeklyYieldEarnings,
|
|
139
|
+
};
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
/**
|
|
143
|
+
* Calculate aggregate yield metrics for a set of portfolio lending pools.
|
|
144
|
+
* Accepts pre-fetched pool data and returns totals without performing any network calls.
|
|
145
|
+
*/
|
|
146
|
+
public calculateYieldMetrics(
|
|
147
|
+
portfolioLendingPools: PortfolioLendingPool[],
|
|
148
|
+
): {
|
|
149
|
+
totalInvestments: number;
|
|
150
|
+
totalYieldEarned: number;
|
|
151
|
+
weightedApy: number;
|
|
152
|
+
weeklyYieldEarnings: number;
|
|
153
|
+
} {
|
|
154
|
+
return this.computeYieldMetrics(portfolioLendingPools);
|
|
155
|
+
}
|
|
156
|
+
|
|
157
|
+
/**
|
|
158
|
+
* Calculate the user's weekly protocol fee share (USDC) given tranche balances/configs,
|
|
159
|
+
* system variables and locking summary. All inputs are expected to be pre-fetched.
|
|
160
|
+
*/
|
|
161
|
+
public calculateWeeklyProtocolFees(params: {
|
|
162
|
+
trancheBalances: TrancheSubgraphResult;
|
|
163
|
+
trancheConfigurations: TrancheConfigurationSubgraphResult;
|
|
164
|
+
systemVariables: SystemVariables;
|
|
165
|
+
lockingSummary: LockingSummarySubgraphResult;
|
|
166
|
+
userRksuAmount: string;
|
|
167
|
+
}): number {
|
|
168
|
+
const {
|
|
169
|
+
trancheBalances,
|
|
170
|
+
trancheConfigurations,
|
|
171
|
+
systemVariables,
|
|
172
|
+
lockingSummary,
|
|
173
|
+
userRksuAmount,
|
|
174
|
+
} = params;
|
|
175
|
+
|
|
176
|
+
const totalRKsuAmount = parseFloat(
|
|
177
|
+
lockingSummary.lockingSummaries[0]?.totalRKsuAmount ?? '0',
|
|
178
|
+
);
|
|
179
|
+
const userRKsuAmount = parseFloat(userRksuAmount || '0');
|
|
180
|
+
|
|
181
|
+
const performanceFee =
|
|
182
|
+
parseFloat(systemVariables.systemVariables.performanceFee) / 100;
|
|
183
|
+
const ecosystemFeeRate =
|
|
184
|
+
parseFloat(systemVariables.systemVariables.ecosystemFeeRate) / 100;
|
|
185
|
+
|
|
186
|
+
if (
|
|
187
|
+
totalRKsuAmount <= 0 ||
|
|
188
|
+
userRKsuAmount <= 0 ||
|
|
189
|
+
performanceFee <= 0 ||
|
|
190
|
+
ecosystemFeeRate <= 0
|
|
191
|
+
) {
|
|
192
|
+
return 0;
|
|
193
|
+
}
|
|
194
|
+
|
|
195
|
+
let totalWeeklyEcosystemFees = 0;
|
|
196
|
+
|
|
197
|
+
for (const tranche of trancheBalances.lendingPoolTranches) {
|
|
198
|
+
const trancheConfig =
|
|
199
|
+
trancheConfigurations.lendingPoolTrancheConfigurations.find(
|
|
200
|
+
(config) => config.id === tranche.id,
|
|
201
|
+
);
|
|
202
|
+
|
|
203
|
+
if (!trancheConfig) continue;
|
|
204
|
+
|
|
205
|
+
const interestUpdates =
|
|
206
|
+
trancheConfig.lendingPoolTrancheInterestRateUpdates;
|
|
207
|
+
|
|
208
|
+
const interestRate = interestUpdates.length
|
|
209
|
+
? interestUpdates[0].epochInterestRate
|
|
210
|
+
: trancheConfig.interestRate;
|
|
211
|
+
|
|
212
|
+
totalWeeklyEcosystemFees +=
|
|
213
|
+
parseFloat(tranche.balance) *
|
|
214
|
+
parseFloat(interestRate) *
|
|
215
|
+
performanceFee *
|
|
216
|
+
ecosystemFeeRate;
|
|
217
|
+
}
|
|
218
|
+
|
|
219
|
+
return (
|
|
220
|
+
(totalWeeklyEcosystemFees * userRKsuAmount) / totalRKsuAmount
|
|
221
|
+
);
|
|
222
|
+
}
|
|
223
|
+
|
|
224
|
+
/**
|
|
225
|
+
* Calculate the user's weekly KASU bonus rewards (KASU) based on current deposits,
|
|
226
|
+
* loyalty level, reward rate, and KSU epoch price. Inputs should be pre-fetched.
|
|
227
|
+
*/
|
|
228
|
+
public async calculateWeeklyKsuBonusRewards(params: {
|
|
229
|
+
userAddress: string;
|
|
230
|
+
userRksuAmount: string;
|
|
231
|
+
userDepositAmounts: [BigNumber, BigNumber];
|
|
232
|
+
ksuEpochPrice: { price: BigNumber; decimals: number };
|
|
233
|
+
}): Promise<number> {
|
|
234
|
+
const { userAddress, userRksuAmount, userDepositAmounts, ksuEpochPrice } =
|
|
235
|
+
params;
|
|
236
|
+
|
|
237
|
+
const totalUserDeposits = userDepositAmounts[0].add(
|
|
238
|
+
userDepositAmounts[1],
|
|
239
|
+
);
|
|
240
|
+
|
|
241
|
+
if (totalUserDeposits.isZero()) return 0;
|
|
242
|
+
|
|
243
|
+
const rKSUtoUSDCRatio = await this._lockingService.getRKSUvsUSDCRatio(
|
|
244
|
+
userRksuAmount,
|
|
245
|
+
userAddress,
|
|
246
|
+
);
|
|
247
|
+
const { loyaltyLevel } =
|
|
248
|
+
this._lockingService.getLoyaltyLevelAndApyBonusFromRatio(
|
|
249
|
+
rKSUtoUSDCRatio,
|
|
250
|
+
);
|
|
251
|
+
|
|
252
|
+
const loyaltyRewardRate =
|
|
253
|
+
await this._userLoyaltyRewardsAbi.loyaltyEpochRewardRates(
|
|
254
|
+
loyaltyLevel,
|
|
255
|
+
);
|
|
256
|
+
|
|
257
|
+
if (loyaltyRewardRate.isZero() || ksuEpochPrice.price.isZero()) {
|
|
258
|
+
return 0;
|
|
259
|
+
}
|
|
260
|
+
|
|
261
|
+
const reward = totalUserDeposits
|
|
262
|
+
.mul(loyaltyRewardRate)
|
|
263
|
+
.mul(BigNumber.from(10).pow(12))
|
|
264
|
+
.div(ksuEpochPrice.price);
|
|
265
|
+
|
|
266
|
+
return parseFloat(formatEther(reward));
|
|
267
|
+
}
|
|
268
|
+
|
|
269
|
+
async getPortfolioRewards(userAddress: string): Promise<PortfolioRewards> {
|
|
270
|
+
const userLockingData =
|
|
271
|
+
await this._lockingService.getUserBonusData(userAddress);
|
|
272
|
+
const lockingRewards =
|
|
273
|
+
await this._lockingService.getLockingRewards(userAddress);
|
|
274
|
+
const ksuLaunchBonus =
|
|
275
|
+
await this._lockingService.getUserTotalBonusAmount(userAddress);
|
|
276
|
+
return {
|
|
277
|
+
bonusYieldEarnings: {
|
|
278
|
+
claimableBalance: {
|
|
279
|
+
ksuAmount: userLockingData.ksuBonusAndRewards,
|
|
280
|
+
},
|
|
281
|
+
lifeTime: {
|
|
282
|
+
ksuAmount: userLockingData.ksuBonusAndRewardsLifetime,
|
|
283
|
+
},
|
|
284
|
+
},
|
|
285
|
+
protocolFees: {
|
|
286
|
+
claimableBalance: {
|
|
287
|
+
usdcAmount: lockingRewards.claimableRewards,
|
|
288
|
+
},
|
|
289
|
+
lifeTime: { usdcAmount: lockingRewards.lifeTimeRewards },
|
|
290
|
+
},
|
|
291
|
+
ksuLaunchBonus: { lifeTime: { ksuAmount: ksuLaunchBonus } },
|
|
292
|
+
};
|
|
293
|
+
}
|
|
294
|
+
|
|
295
|
+
async getUserNfts(userAddress: string): Promise<number[]> {
|
|
296
|
+
const usernfts = await this._kasuNftContract.tokensOfOwner(userAddress);
|
|
297
|
+
|
|
298
|
+
return usernfts.map((nft) => nft.toNumber());
|
|
299
|
+
}
|
|
300
|
+
|
|
301
|
+
async getPortfolioSummary(
|
|
302
|
+
userAddress: string,
|
|
303
|
+
portfolioLendingPools: PortfolioLendingPool[],
|
|
304
|
+
currentEpoch: string,
|
|
305
|
+
): Promise<PortfolioSummary> {
|
|
306
|
+
const epochId = currentEpoch.toString();
|
|
307
|
+
const [
|
|
308
|
+
userLockingData,
|
|
309
|
+
userRksuAmount,
|
|
310
|
+
lockingSummary,
|
|
311
|
+
systemVariables,
|
|
312
|
+
trancheBalances,
|
|
313
|
+
trancheConfigurations,
|
|
314
|
+
userDepositAmounts,
|
|
315
|
+
ksuEpochPrice,
|
|
316
|
+
] = await Promise.all([
|
|
317
|
+
this._lockingService.getUserBonusData(userAddress),
|
|
318
|
+
this._lockingService.getUserEarnedrKsu(userAddress),
|
|
319
|
+
this._graph.request<LockingSummarySubgraphResult>(
|
|
320
|
+
lockingSummariesQuery,
|
|
321
|
+
),
|
|
322
|
+
this._graph.request<SystemVariables>(getSystemVariablesQuery),
|
|
323
|
+
this._graph.request<TrancheSubgraphResult>(getAllTranchesQuery, {
|
|
324
|
+
unusedPools: this._kasuConfig.UNUSED_LENDING_POOL_IDS,
|
|
325
|
+
}),
|
|
326
|
+
this._graph.request<TrancheConfigurationSubgraphResult>(
|
|
327
|
+
getAllTrancheConfigurationsQuery,
|
|
328
|
+
{
|
|
329
|
+
unusedPools: this._kasuConfig.UNUSED_LENDING_POOL_IDS,
|
|
330
|
+
epochId,
|
|
331
|
+
},
|
|
332
|
+
),
|
|
333
|
+
this._userLendingService.getUserTotalPendingAndActiveDepositedAmountForCurrentEpoch(
|
|
334
|
+
userAddress,
|
|
335
|
+
),
|
|
336
|
+
this._lockingService.getKasuEpochTokenPrice(),
|
|
337
|
+
]);
|
|
338
|
+
const {
|
|
339
|
+
totalInvestments,
|
|
340
|
+
totalYieldEarned,
|
|
341
|
+
weightedApy,
|
|
342
|
+
weeklyYieldEarnings,
|
|
343
|
+
} = this.computeYieldMetrics(portfolioLendingPools);
|
|
344
|
+
|
|
345
|
+
const weeklyProtocolFeesEarned = this.calculateWeeklyProtocolFees({
|
|
346
|
+
trancheBalances,
|
|
347
|
+
trancheConfigurations,
|
|
348
|
+
systemVariables,
|
|
349
|
+
lockingSummary,
|
|
350
|
+
userRksuAmount,
|
|
351
|
+
});
|
|
352
|
+
|
|
353
|
+
const weeklyKsuBonusRewards =
|
|
354
|
+
await this.calculateWeeklyKsuBonusRewards({
|
|
355
|
+
userAddress,
|
|
356
|
+
userRksuAmount,
|
|
357
|
+
userDepositAmounts,
|
|
358
|
+
ksuEpochPrice,
|
|
359
|
+
});
|
|
360
|
+
|
|
142
361
|
return {
|
|
143
362
|
current: {
|
|
144
363
|
totalKsuLocked: userLockingData.totalLockedAmount,
|
|
@@ -153,8 +372,8 @@ export class Portfolio {
|
|
|
153
372
|
},
|
|
154
373
|
weekly: {
|
|
155
374
|
yieldEarnings: weeklyYieldEarnings.toString(),
|
|
156
|
-
protocolFeesEarned:
|
|
157
|
-
ksuBonusRewards:
|
|
375
|
+
protocolFeesEarned: weeklyProtocolFeesEarned.toString(),
|
|
376
|
+
ksuBonusRewards: weeklyKsuBonusRewards.toString(),
|
|
158
377
|
},
|
|
159
378
|
lifetime: {
|
|
160
379
|
yieldEarnings: totalYieldEarned.toString(),
|
|
@@ -269,9 +488,6 @@ export class Portfolio {
|
|
|
269
488
|
({ poolId }) => poolId === poolOverview.id,
|
|
270
489
|
);
|
|
271
490
|
if (!userPoolBalance) {
|
|
272
|
-
console.warn(
|
|
273
|
-
`Could not find user pool balance for ${poolOverview.id}`,
|
|
274
|
-
);
|
|
275
491
|
continue;
|
|
276
492
|
}
|
|
277
493
|
totalInvestments = userPoolBalance.balance.add(totalInvestments);
|