@kasufinance/kasu-sdk 1.0.1
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/.eslintignore +3 -0
- package/.eslintrc.json +90 -0
- package/.github/workflows/ci.yml +0 -0
- package/.github/workflows/main.yml +43 -0
- package/.prettierignore +3 -0
- package/.prettierrc +28 -0
- package/abis/IClearingCoordinator.abi.json +410 -0
- package/abis/IERC20Metadata.abi.json +224 -0
- package/abis/IFeeManager.abi.json +66 -0
- package/abis/IKSULocking.abi.json +754 -0
- package/abis/IKasuAllowList.abi.json +455 -0
- package/abis/IKasuController.abi.json +423 -0
- package/abis/IKasuNFTs.abi.json +1523 -0
- package/abis/IKsuPrice.abi.json +15 -0
- package/abis/ILendingPool.abi.json +1431 -0
- package/abis/ILendingPoolFactory.abi.json +97 -0
- package/abis/ILendingPoolManager.abi.json +1347 -0
- package/abis/ILendingPoolTranche.abi.json +1317 -0
- package/abis/IPriceFeed.abi.json +1 -0
- package/abis/ISystemVariables.abi.json +431 -0
- package/abis/IUserLoyaltyRewards.abi.json +288 -0
- package/abis/IUserManager.abi.json +321 -0
- package/abis/KSULockBonus.abi.json +91 -0
- package/abis/KasuPoolExternalTVL.abi.json +812 -0
- package/abis/Swapper.abi.json +146 -0
- package/copy-abi.ps1 +15 -0
- package/dist/bundle.cjs.js +21646 -0
- package/dist/bundle.esm.js +21644 -0
- package/dist/contracts/IClearingCoordinatorAbi.d.ts +169 -0
- package/dist/contracts/IClearingCoordinatorAbi.js +1 -0
- package/dist/contracts/IERC20MetadataAbi.d.ts +162 -0
- package/dist/contracts/IERC20MetadataAbi.js +1 -0
- package/dist/contracts/IFeeManagerAbi.d.ts +96 -0
- package/dist/contracts/IFeeManagerAbi.js +1 -0
- package/dist/contracts/IKSULockingAbi.d.ts +466 -0
- package/dist/contracts/IKSULockingAbi.js +1 -0
- package/dist/contracts/IKasuAllowListAbi.d.ts +304 -0
- package/dist/contracts/IKasuAllowListAbi.js +1 -0
- package/dist/contracts/IKasuControllerAbi.d.ts +309 -0
- package/dist/contracts/IKasuControllerAbi.js +1 -0
- package/dist/contracts/IKasuNFTsAbi.d.ts +1121 -0
- package/dist/contracts/IKasuNFTsAbi.js +1 -0
- package/dist/contracts/IKsuPriceAbi.d.ts +42 -0
- package/dist/contracts/IKsuPriceAbi.js +1 -0
- package/dist/contracts/ILendingPoolAbi.d.ts +968 -0
- package/dist/contracts/ILendingPoolAbi.js +1 -0
- package/dist/contracts/ILendingPoolFactoryAbi.d.ts +103 -0
- package/dist/contracts/ILendingPoolFactoryAbi.js +1 -0
- package/dist/contracts/ILendingPoolManagerAbi.d.ts +779 -0
- package/dist/contracts/ILendingPoolManagerAbi.js +1 -0
- package/dist/contracts/ILendingPoolTrancheAbi.d.ts +752 -0
- package/dist/contracts/ILendingPoolTrancheAbi.js +1 -0
- package/dist/contracts/ISystemVariablesAbi.d.ts +378 -0
- package/dist/contracts/ISystemVariablesAbi.js +1 -0
- package/dist/contracts/IUserLoyaltyRewardsAbi.d.ts +248 -0
- package/dist/contracts/IUserLoyaltyRewardsAbi.js +1 -0
- package/dist/contracts/IUserManagerAbi.d.ts +270 -0
- package/dist/contracts/IUserManagerAbi.js +1 -0
- package/dist/contracts/KSULockBonusAbi.d.ts +61 -0
- package/dist/contracts/KSULockBonusAbi.js +1 -0
- package/dist/contracts/KasuPoolExternalTVLAbi.d.ts +411 -0
- package/dist/contracts/KasuPoolExternalTVLAbi.js +1 -0
- package/dist/contracts/SwapperAbi.d.ts +120 -0
- package/dist/contracts/SwapperAbi.js +1 -0
- package/dist/contracts/common.d.ts +21 -0
- package/dist/contracts/common.js +1 -0
- package/dist/contracts/factories/IClearingCoordinatorAbi__factory.d.ts +320 -0
- package/dist/contracts/factories/IClearingCoordinatorAbi__factory.js +423 -0
- package/dist/contracts/factories/IERC20MetadataAbi__factory.d.ts +178 -0
- package/dist/contracts/factories/IERC20MetadataAbi__factory.js +237 -0
- package/dist/contracts/factories/IFeeManagerAbi__factory.d.ts +59 -0
- package/dist/contracts/factories/IFeeManagerAbi__factory.js +79 -0
- package/dist/contracts/factories/IKSULockingAbi__factory.d.ts +588 -0
- package/dist/contracts/factories/IKSULockingAbi__factory.js +767 -0
- package/dist/contracts/factories/IKasuAllowListAbi__factory.d.ts +354 -0
- package/dist/contracts/factories/IKasuAllowListAbi__factory.js +468 -0
- package/dist/contracts/factories/IKasuControllerAbi__factory.d.ts +339 -0
- package/dist/contracts/factories/IKasuControllerAbi__factory.js +436 -0
- package/dist/contracts/factories/IKasuNFTsAbi__factory.d.ts +1629 -0
- package/dist/contracts/factories/IKasuNFTsAbi__factory.js +2108 -0
- package/dist/contracts/factories/IKsuPriceAbi__factory.d.ts +18 -0
- package/dist/contracts/factories/IKsuPriceAbi__factory.js +28 -0
- package/dist/contracts/factories/ILendingPoolAbi__factory.d.ts +1109 -0
- package/dist/contracts/factories/ILendingPoolAbi__factory.js +1444 -0
- package/dist/contracts/factories/ILendingPoolFactoryAbi__factory.d.ts +81 -0
- package/dist/contracts/factories/ILendingPoolFactoryAbi__factory.js +110 -0
- package/dist/contracts/factories/ILendingPoolManagerAbi__factory.d.ts +1045 -0
- package/dist/contracts/factories/ILendingPoolManagerAbi__factory.js +1360 -0
- package/dist/contracts/factories/ILendingPoolTrancheAbi__factory.d.ts +1015 -0
- package/dist/contracts/factories/ILendingPoolTrancheAbi__factory.js +1330 -0
- package/dist/contracts/factories/ISystemVariablesAbi__factory.d.ts +338 -0
- package/dist/contracts/factories/ISystemVariablesAbi__factory.js +444 -0
- package/dist/contracts/factories/IUserLoyaltyRewardsAbi__factory.d.ts +230 -0
- package/dist/contracts/factories/IUserLoyaltyRewardsAbi__factory.js +301 -0
- package/dist/contracts/factories/IUserManagerAbi__factory.d.ts +250 -0
- package/dist/contracts/factories/IUserManagerAbi__factory.js +334 -0
- package/dist/contracts/factories/KSULockBonusAbi__factory.d.ts +76 -0
- package/dist/contracts/factories/KSULockBonusAbi__factory.js +104 -0
- package/dist/contracts/factories/KasuPoolExternalTVLAbi__factory.d.ts +631 -0
- package/dist/contracts/factories/KasuPoolExternalTVLAbi__factory.js +825 -0
- package/dist/contracts/factories/SwapperAbi__factory.d.ts +120 -0
- package/dist/contracts/factories/SwapperAbi__factory.js +159 -0
- package/dist/contracts/factories/index.d.ts +18 -0
- package/dist/contracts/factories/index.js +21 -0
- package/dist/contracts/index.d.ts +37 -0
- package/dist/contracts/index.js +19 -0
- package/dist/index.d.ts +17 -0
- package/dist/index.js +16 -0
- package/dist/sdk-config.d.ts +22 -0
- package/dist/sdk-config.js +9 -0
- package/dist/services/DataService/data-service.d.ts +37 -0
- package/dist/services/DataService/data-service.js +586 -0
- package/dist/services/DataService/directus-types.d.ts +187 -0
- package/dist/services/DataService/directus-types.js +1 -0
- package/dist/services/DataService/queries.d.ts +7 -0
- package/dist/services/DataService/queries.js +158 -0
- package/dist/services/DataService/subgraph-types.d.ts +95 -0
- package/dist/services/DataService/subgraph-types.js +1 -0
- package/dist/services/DataService/types.d.ts +169 -0
- package/dist/services/DataService/types.js +1 -0
- package/dist/services/Locking/locking.d.ts +61 -0
- package/dist/services/Locking/locking.js +344 -0
- package/dist/services/Locking/queries.d.ts +10 -0
- package/dist/services/Locking/queries.js +108 -0
- package/dist/services/Locking/types.d.ts +117 -0
- package/dist/services/Locking/types.js +1 -0
- package/dist/services/Portfolio/portfolio.d.ts +22 -0
- package/dist/services/Portfolio/portfolio.js +325 -0
- package/dist/services/Portfolio/queries.d.ts +2 -0
- package/dist/services/Portfolio/queries.js +164 -0
- package/dist/services/Portfolio/types.d.ts +206 -0
- package/dist/services/Portfolio/types.js +1 -0
- package/dist/services/Swapper/swapper.d.ts +11 -0
- package/dist/services/Swapper/swapper.js +97 -0
- package/dist/services/Swapper/types.d.ts +51 -0
- package/dist/services/Swapper/types.js +1 -0
- package/dist/services/UserLending/helper.d.ts +2 -0
- package/dist/services/UserLending/helper.js +16 -0
- package/dist/services/UserLending/queries.d.ts +8 -0
- package/dist/services/UserLending/queries.js +188 -0
- package/dist/services/UserLending/subgraph-types.d.ts +147 -0
- package/dist/services/UserLending/subgraph-types.js +6 -0
- package/dist/services/UserLending/types.d.ts +102 -0
- package/dist/services/UserLending/types.js +13 -0
- package/dist/services/UserLending/user-lending.d.ts +55 -0
- package/dist/services/UserLending/user-lending.js +490 -0
- package/dist/services/shared.d.ts +1 -0
- package/dist/services/shared.js +11 -0
- package/dist/tests/sample.test.d.ts +1 -0
- package/dist/tests/sample.test.js +32 -0
- package/index.ts +2 -0
- package/jest-config.ts +9 -0
- package/jest.config.js +8 -0
- package/package.json +49 -0
- package/rollup.config.js +45 -0
- package/src/contracts/IClearingCoordinatorAbi.ts +406 -0
- package/src/contracts/IERC20MetadataAbi.ts +364 -0
- package/src/contracts/IFeeManagerAbi.ts +182 -0
- package/src/contracts/IKSULockingAbi.ts +965 -0
- package/src/contracts/IKasuAllowListAbi.ts +605 -0
- package/src/contracts/IKasuControllerAbi.ts +703 -0
- package/src/contracts/IKasuNFTsAbi.ts +2744 -0
- package/src/contracts/IKsuPriceAbi.ts +87 -0
- package/src/contracts/ILendingPoolAbi.ts +2081 -0
- package/src/contracts/ILendingPoolFactoryAbi.ts +161 -0
- package/src/contracts/ILendingPoolManagerAbi.ts +1917 -0
- package/src/contracts/ILendingPoolTrancheAbi.ts +1911 -0
- package/src/contracts/ISystemVariablesAbi.ts +908 -0
- package/src/contracts/IUserLoyaltyRewardsAbi.ts +544 -0
- package/src/contracts/IUserManagerAbi.ts +612 -0
- package/src/contracts/KSULockBonusAbi.ts +124 -0
- package/src/contracts/KasuPoolExternalTVLAbi.ts +1071 -0
- package/src/contracts/SwapperAbi.ts +266 -0
- package/src/contracts/common.ts +44 -0
- package/src/contracts/factories/IClearingCoordinatorAbi__factory.ts +438 -0
- package/src/contracts/factories/IERC20MetadataAbi__factory.ts +248 -0
- package/src/contracts/factories/IFeeManagerAbi__factory.ts +90 -0
- package/src/contracts/factories/IKSULockingAbi__factory.ts +778 -0
- package/src/contracts/factories/IKasuAllowListAbi__factory.ts +479 -0
- package/src/contracts/factories/IKasuControllerAbi__factory.ts +447 -0
- package/src/contracts/factories/IKasuNFTsAbi__factory.ts +2116 -0
- package/src/contracts/factories/IKsuPriceAbi__factory.ts +36 -0
- package/src/contracts/factories/ILendingPoolAbi__factory.ts +1455 -0
- package/src/contracts/factories/ILendingPoolFactoryAbi__factory.ts +125 -0
- package/src/contracts/factories/ILendingPoolManagerAbi__factory.ts +1375 -0
- package/src/contracts/factories/ILendingPoolTrancheAbi__factory.ts +1345 -0
- package/src/contracts/factories/ISystemVariablesAbi__factory.ts +455 -0
- package/src/contracts/factories/IUserLoyaltyRewardsAbi__factory.ts +316 -0
- package/src/contracts/factories/IUserManagerAbi__factory.ts +345 -0
- package/src/contracts/factories/KSULockBonusAbi__factory.ts +115 -0
- package/src/contracts/factories/KasuPoolExternalTVLAbi__factory.ts +840 -0
- package/src/contracts/factories/SwapperAbi__factory.ts +167 -0
- package/src/contracts/factories/index.ts +21 -0
- package/src/contracts/index.ts +40 -0
- package/src/index.ts +27 -0
- package/src/sdk-config.ts +35 -0
- package/src/services/DataService/data-service.ts +1017 -0
- package/src/services/DataService/directus-types.ts +199 -0
- package/src/services/DataService/queries.ts +165 -0
- package/src/services/DataService/subgraph-types.ts +104 -0
- package/src/services/DataService/types.ts +184 -0
- package/src/services/Locking/locking.ts +541 -0
- package/src/services/Locking/queries.ts +118 -0
- package/src/services/Locking/types.ts +141 -0
- package/src/services/Portfolio/portfolio.ts +593 -0
- package/src/services/Portfolio/queries.ts +166 -0
- package/src/services/Portfolio/types.ts +220 -0
- package/src/services/Swapper/swapper.ts +158 -0
- package/src/services/Swapper/types.ts +54 -0
- package/src/services/UserLending/helper.ts +28 -0
- package/src/services/UserLending/queries.ts +196 -0
- package/src/services/UserLending/subgraph-types.ts +173 -0
- package/src/services/UserLending/types.ts +123 -0
- package/src/services/UserLending/user-lending.ts +933 -0
- package/src/services/shared.ts +11 -0
- package/src/tests/sample.test.ts +44 -0
- package/tsconfig.json +27 -0
- package/tsconfig.tsbuildinfo +1 -0
- package/versioner.sh +6 -0
|
@@ -0,0 +1,586 @@
|
|
|
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
|
+
var __rest = (this && this.__rest) || function (s, e) {
|
|
11
|
+
var t = {};
|
|
12
|
+
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0)
|
|
13
|
+
t[p] = s[p];
|
|
14
|
+
if (s != null && typeof Object.getOwnPropertySymbols === "function")
|
|
15
|
+
for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) {
|
|
16
|
+
if (e.indexOf(p[i]) < 0 && Object.prototype.propertyIsEnumerable.call(s, p[i]))
|
|
17
|
+
t[p[i]] = s[p[i]];
|
|
18
|
+
}
|
|
19
|
+
return t;
|
|
20
|
+
};
|
|
21
|
+
import { authentication, createDirectus, readItems, readSingleton, rest, } from '@directus/sdk';
|
|
22
|
+
import { formatUnits } from 'ethers/lib/utils';
|
|
23
|
+
import { GraphQLClient } from 'graphql-request';
|
|
24
|
+
import { KasuPoolExternalTVLAbi__factory, } from '../../contracts';
|
|
25
|
+
import { getSystemVariablesQuery } from '../Locking/queries';
|
|
26
|
+
import { filterArray } from '../shared';
|
|
27
|
+
import { getAllTrancheConfigurationsQuery, getAllTranchesQuery, getLendingPoolWithdrawalAndDepositsQuery, getPlumeTvl, getPoolNameQuery, getPoolOverviewQuery, } from './queries';
|
|
28
|
+
export class DataService {
|
|
29
|
+
constructor(_kasuConfig, signerOrProvider) {
|
|
30
|
+
this._kasuConfig = _kasuConfig;
|
|
31
|
+
this._plumeTvl = new Map();
|
|
32
|
+
this._externalTvlAbi = KasuPoolExternalTVLAbi__factory.connect(_kasuConfig.contracts.ExternalTVL, signerOrProvider);
|
|
33
|
+
this._graph = new GraphQLClient(_kasuConfig.subgraphUrl);
|
|
34
|
+
this._plumeGraph = new GraphQLClient(_kasuConfig.plumeSubgraphUrl);
|
|
35
|
+
this._directus = createDirectus(_kasuConfig.directusUrl)
|
|
36
|
+
.with(authentication())
|
|
37
|
+
.with(rest());
|
|
38
|
+
}
|
|
39
|
+
getUrlFromFile(fileName) {
|
|
40
|
+
return `${this._kasuConfig.directusUrl}assets/${fileName}`;
|
|
41
|
+
}
|
|
42
|
+
calculatePoolCapacity(poolCapacities, poolCapacityPercentages, index, spillOver, lendingPoolSubgraph, lendingPoolConfig) {
|
|
43
|
+
const desiredDrawAmount = lendingPoolConfig.desiredDrawAmount; // subgraph
|
|
44
|
+
const desiredTrancheRatio = lendingPoolConfig.tranchesConfig[index].desiredRatio;
|
|
45
|
+
const targetDrawAmount = parseFloat(desiredDrawAmount) *
|
|
46
|
+
(parseFloat(desiredTrancheRatio) / 100);
|
|
47
|
+
const pendingDeposits = parseFloat(lendingPoolSubgraph.pendingPool.totalPendingDepositAmounts[index]) - spillOver;
|
|
48
|
+
const balance = parseFloat(lendingPoolSubgraph.tranches[index].balance); // subgraph
|
|
49
|
+
const targetAmount = balance + targetDrawAmount;
|
|
50
|
+
const remainingCapacity = Math.max(targetDrawAmount - pendingDeposits, 0);
|
|
51
|
+
const remainingCapacityPercentage = targetAmount != 0 ? remainingCapacity / targetAmount : 0;
|
|
52
|
+
const remainingCapacitySpillover = Math.min(targetDrawAmount - pendingDeposits, 0);
|
|
53
|
+
poolCapacities.push(remainingCapacity);
|
|
54
|
+
poolCapacityPercentages.push(remainingCapacityPercentage);
|
|
55
|
+
if (index === lendingPoolConfig.tranchesConfig.length - 1) {
|
|
56
|
+
return {
|
|
57
|
+
poolCapacity: poolCapacities,
|
|
58
|
+
poolCapacityPercentage: poolCapacityPercentages,
|
|
59
|
+
};
|
|
60
|
+
}
|
|
61
|
+
return this.calculatePoolCapacity(poolCapacities, poolCapacityPercentages, index + 1, remainingCapacitySpillover, lendingPoolSubgraph, lendingPoolConfig);
|
|
62
|
+
}
|
|
63
|
+
calculateApyForTranche(interestRate) {
|
|
64
|
+
const EPOCHS_IN_YEAR = 52.17857;
|
|
65
|
+
return Math.pow((1 + parseFloat(interestRate)), EPOCHS_IN_YEAR) - 1;
|
|
66
|
+
}
|
|
67
|
+
getPlatformOverview() {
|
|
68
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
69
|
+
return yield this._directus.request(readSingleton('PlatformOverview'));
|
|
70
|
+
});
|
|
71
|
+
}
|
|
72
|
+
getNftBoost() {
|
|
73
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
74
|
+
return yield this._directus.request(readSingleton('NftBoost'));
|
|
75
|
+
});
|
|
76
|
+
}
|
|
77
|
+
getOffChainTvl(poolIds) {
|
|
78
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
79
|
+
const tvlMap = new Map();
|
|
80
|
+
try {
|
|
81
|
+
const results = yield Promise.all(poolIds.map((id) => __awaiter(this, void 0, void 0, function* () {
|
|
82
|
+
return [
|
|
83
|
+
id,
|
|
84
|
+
yield this._externalTvlAbi.externalTVLOfPool(id),
|
|
85
|
+
];
|
|
86
|
+
})));
|
|
87
|
+
for (const [id, tvl] of results) {
|
|
88
|
+
tvlMap.set(id, formatUnits(tvl, 6));
|
|
89
|
+
}
|
|
90
|
+
}
|
|
91
|
+
catch (error) {
|
|
92
|
+
console.error('getOffChainTvl :', error);
|
|
93
|
+
}
|
|
94
|
+
return tvlMap;
|
|
95
|
+
});
|
|
96
|
+
}
|
|
97
|
+
getPoolOverview(epochId, id_in) {
|
|
98
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
99
|
+
var _a, _b, _c;
|
|
100
|
+
const trancheNames = [
|
|
101
|
+
['Senior'],
|
|
102
|
+
['Junior', 'Senior'],
|
|
103
|
+
['Junior', 'Mezzanine', 'Senior'],
|
|
104
|
+
];
|
|
105
|
+
const poolOverviewResults = yield this._graph.request(getPoolOverviewQuery(id_in), {
|
|
106
|
+
unusedPools: this._kasuConfig.UNUSED_LENDING_POOL_IDS,
|
|
107
|
+
epochId,
|
|
108
|
+
});
|
|
109
|
+
if (!this._plumeTvl.size) {
|
|
110
|
+
const plumeResults = yield this._plumeGraph.request(getPlumeTvl);
|
|
111
|
+
for (const pool of plumeResults.lendingPools) {
|
|
112
|
+
this._plumeTvl.set(pool.id, pool.balance);
|
|
113
|
+
}
|
|
114
|
+
}
|
|
115
|
+
if (!this._directusPoolOverview) {
|
|
116
|
+
this._directusPoolOverview = yield this._directus.request(readItems('PoolOverview', {
|
|
117
|
+
filter: {
|
|
118
|
+
enabled: {
|
|
119
|
+
_eq: true,
|
|
120
|
+
},
|
|
121
|
+
},
|
|
122
|
+
}));
|
|
123
|
+
}
|
|
124
|
+
const offchainTvlMap = yield this.getOffChainTvl(poolOverviewResults.lendingPools.map(({ id }) => id));
|
|
125
|
+
const retn = [];
|
|
126
|
+
for (const lendingPoolSubgraph of poolOverviewResults.lendingPools) {
|
|
127
|
+
const lendingPoolDirectus = this._directusPoolOverview.find((r) => r.id.toLowerCase() == lendingPoolSubgraph.id);
|
|
128
|
+
const lendingPoolConfig = lendingPoolSubgraph.configuration;
|
|
129
|
+
if (!lendingPoolDirectus) {
|
|
130
|
+
console.warn("Couldn't find directus pool for id: ", lendingPoolSubgraph.id);
|
|
131
|
+
continue;
|
|
132
|
+
}
|
|
133
|
+
const poolCapacities = this.calculatePoolCapacity([], [], 0, 0, lendingPoolSubgraph, lendingPoolConfig);
|
|
134
|
+
const tranches = lendingPoolSubgraph.tranches
|
|
135
|
+
.map((tranche) => {
|
|
136
|
+
const trancheConfig = lendingPoolSubgraph.configuration.tranchesConfig.find((r) => r.id == tranche.id);
|
|
137
|
+
if (!trancheConfig) {
|
|
138
|
+
console.warn("Couldn't find tranche config for id: ", tranche.id);
|
|
139
|
+
return null;
|
|
140
|
+
}
|
|
141
|
+
const interestUpdates = trancheConfig.lendingPoolTrancheInterestRateUpdates;
|
|
142
|
+
const interestRate = interestUpdates.length
|
|
143
|
+
? interestUpdates[0].epochInterestRate
|
|
144
|
+
: trancheConfig.interestRate;
|
|
145
|
+
const baseApy = this.calculateApyForTranche(interestRate);
|
|
146
|
+
let minApy = baseApy;
|
|
147
|
+
let maxApy = baseApy;
|
|
148
|
+
const fixedTermConfig = trancheConfig.lendingPoolTrancheFixedTermConfigs.map((fixedTermConfig) => {
|
|
149
|
+
const fixedTermApy = this.calculateApyForTranche(fixedTermConfig.epochInterestRate);
|
|
150
|
+
minApy = Math.min(minApy, fixedTermApy);
|
|
151
|
+
maxApy = Math.max(maxApy, fixedTermApy);
|
|
152
|
+
return {
|
|
153
|
+
configId: fixedTermConfig.configId,
|
|
154
|
+
apy: fixedTermApy.toString(),
|
|
155
|
+
epochLockDuration: fixedTermConfig.epochLockDuration,
|
|
156
|
+
epochInterestRate: fixedTermConfig.epochInterestRate,
|
|
157
|
+
fixedTermDepositStatus: fixedTermConfig.fixedTermDepositStatus,
|
|
158
|
+
fixedTermDepositAllowlist: fixedTermConfig.fixedTermDepositAllowlist.map((allowList) => ({
|
|
159
|
+
isAllowlisted: allowList.isAllowlisted,
|
|
160
|
+
userId: allowList.user.id,
|
|
161
|
+
})),
|
|
162
|
+
};
|
|
163
|
+
});
|
|
164
|
+
const averageApy = fixedTermConfig.length
|
|
165
|
+
? fixedTermConfig.reduce((total, cur) => (total += parseFloat(cur.apy)), 0) / fixedTermConfig.length
|
|
166
|
+
: baseApy;
|
|
167
|
+
return {
|
|
168
|
+
id: tranche.id,
|
|
169
|
+
apy: baseApy.toString(),
|
|
170
|
+
minApy: minApy.toString(),
|
|
171
|
+
maxApy: maxApy.toString(),
|
|
172
|
+
averageApy: averageApy.toString(),
|
|
173
|
+
interestRate,
|
|
174
|
+
maximumDeposit: trancheConfig.maxDepositAmount,
|
|
175
|
+
minimumDeposit: trancheConfig.minDepositAmount,
|
|
176
|
+
poolCapacityPercentage: poolCapacities.poolCapacityPercentage[parseInt(tranche.orderId)].toString(),
|
|
177
|
+
poolCapacity: poolCapacities.poolCapacity[parseInt(tranche.orderId)].toString(),
|
|
178
|
+
name: trancheNames[lendingPoolSubgraph.tranches.length - 1][parseInt(tranche.orderId)],
|
|
179
|
+
fixedTermConfig,
|
|
180
|
+
};
|
|
181
|
+
})
|
|
182
|
+
.filter((tranche) => tranche !== null);
|
|
183
|
+
const poolCapacitySum = poolCapacities.poolCapacity.reduce((a, b) => a + b, 0);
|
|
184
|
+
const poolCapacityPercentageSum = poolCapacities.poolCapacityPercentage.reduce((a, b) => a + b, 0) / poolCapacities.poolCapacity.length;
|
|
185
|
+
const trancheBalanceSum = lendingPoolSubgraph.tranches.reduce((a, b) => a + parseFloat(b.balance), 0);
|
|
186
|
+
let averageApy = 0;
|
|
187
|
+
for (const tranche of lendingPoolSubgraph.tranches) {
|
|
188
|
+
const trancheData = tranches.find((r) => r.id.toLowerCase() == tranche.id);
|
|
189
|
+
if (!trancheData) {
|
|
190
|
+
console.warn("Couldn't find tranche directus for id: ", tranche.id);
|
|
191
|
+
continue;
|
|
192
|
+
}
|
|
193
|
+
const weight = trancheBalanceSum == 0
|
|
194
|
+
? 0
|
|
195
|
+
: parseFloat(tranche.balance) / trancheBalanceSum;
|
|
196
|
+
averageApy += parseFloat(trancheData.averageApy) * weight;
|
|
197
|
+
}
|
|
198
|
+
const plumeTvl = lendingPoolDirectus.plumeStrategy &&
|
|
199
|
+
this._plumeTvl.has(lendingPoolDirectus.plumeStrategy)
|
|
200
|
+
? (_a = this._plumeTvl.get(lendingPoolDirectus.plumeStrategy)) !== null && _a !== void 0 ? _a : '0'
|
|
201
|
+
: '0';
|
|
202
|
+
const offChainTvl = (_b = offchainTvlMap.get(lendingPoolSubgraph.id)) !== null && _b !== void 0 ? _b : '0';
|
|
203
|
+
const poolOverview = {
|
|
204
|
+
id: lendingPoolSubgraph.id,
|
|
205
|
+
security: lendingPoolDirectus.security,
|
|
206
|
+
enabled: lendingPoolDirectus.enabled,
|
|
207
|
+
isOversubscribed: lendingPoolDirectus.oversubscribed,
|
|
208
|
+
apy: averageApy,
|
|
209
|
+
description: lendingPoolDirectus.description,
|
|
210
|
+
liteDescription: lendingPoolDirectus.liteDescription,
|
|
211
|
+
thumbnailImageUrl: this.getUrlFromFile(lendingPoolDirectus.thumbnailImage),
|
|
212
|
+
bannerImageUrl: this.getUrlFromFile(lendingPoolDirectus.bannerImage),
|
|
213
|
+
strategyDeckUrl: this.getUrlFromFile(lendingPoolDirectus.strategyDeck),
|
|
214
|
+
assetClass: lendingPoolDirectus.assetClass,
|
|
215
|
+
industryExposure: lendingPoolDirectus.industryExposure,
|
|
216
|
+
poolApyStructure: lendingPoolDirectus.poolApyStructure,
|
|
217
|
+
apyExpiryDate: lendingPoolDirectus.apyExpiryDate,
|
|
218
|
+
poolInvestmentTerm: lendingPoolDirectus.poolInvestmentTerm,
|
|
219
|
+
loanStructure: lendingPoolDirectus.loanStructure,
|
|
220
|
+
poolName: (_c = lendingPoolDirectus.poolName) !== null && _c !== void 0 ? _c : lendingPoolSubgraph.name,
|
|
221
|
+
subheading: lendingPoolDirectus.subheading,
|
|
222
|
+
totalValueLocked: {
|
|
223
|
+
total: (parseFloat(lendingPoolSubgraph.balance) +
|
|
224
|
+
parseFloat(plumeTvl) +
|
|
225
|
+
parseFloat(offChainTvl)).toString(),
|
|
226
|
+
offchain: offChainTvl,
|
|
227
|
+
plume: plumeTvl,
|
|
228
|
+
},
|
|
229
|
+
loansUnderManagement: lendingPoolDirectus.loansUnderManagement,
|
|
230
|
+
yieldEarned: lendingPoolSubgraph.totalUserInterestAmount,
|
|
231
|
+
poolCapacity: poolCapacitySum.toString(),
|
|
232
|
+
poolCapacityPercentage: poolCapacityPercentageSum.toString(),
|
|
233
|
+
activeLoans: lendingPoolDirectus.activeLoans,
|
|
234
|
+
loanFundsOriginated: lendingPoolDirectus.loanFundsOriginated,
|
|
235
|
+
tranches: tranches,
|
|
236
|
+
isActive: !lendingPoolSubgraph.isStopped,
|
|
237
|
+
requestEpochsInAdvance: lendingPoolSubgraph.configuration
|
|
238
|
+
.lendingPoolWithdrawalConfig.requestEpochsInAdvance,
|
|
239
|
+
};
|
|
240
|
+
// show only enabled pools from cms
|
|
241
|
+
if (poolOverview.enabled) {
|
|
242
|
+
retn.push(poolOverview);
|
|
243
|
+
}
|
|
244
|
+
}
|
|
245
|
+
return retn;
|
|
246
|
+
});
|
|
247
|
+
}
|
|
248
|
+
getPerformanceFee() {
|
|
249
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
250
|
+
const systemVariables = yield this._graph.request(getSystemVariablesQuery);
|
|
251
|
+
return parseFloat(systemVariables.systemVariables.performanceFee);
|
|
252
|
+
});
|
|
253
|
+
}
|
|
254
|
+
getRiskManagement(id_in) {
|
|
255
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
256
|
+
const [resultDirectusRiskManagement, resultDirectusRiskManagementItem, resultDirectusRiskManagementGroups,] = yield Promise.all([
|
|
257
|
+
this._directus.request(readItems('RiskManagement')),
|
|
258
|
+
this._directus.request(readItems('RiskManagementItems')),
|
|
259
|
+
this._directus.request(readItems('RiskManagementGroups')),
|
|
260
|
+
]);
|
|
261
|
+
const groupMapper = resultDirectusRiskManagementGroups.reduce((acc, cur) => (Object.assign(Object.assign({}, acc), { [cur.name]: cur })), {});
|
|
262
|
+
const itemMapper = resultDirectusRiskManagementItem.reduce((acc, cur) => {
|
|
263
|
+
var _a;
|
|
264
|
+
const groupName = (_a = groupMapper[cur.group.key]) === null || _a === void 0 ? void 0 : _a.name;
|
|
265
|
+
if (!groupName)
|
|
266
|
+
return acc;
|
|
267
|
+
return Object.assign(Object.assign({}, acc), { [cur.name]: Object.assign(Object.assign({}, cur), { group: groupName }) });
|
|
268
|
+
}, {});
|
|
269
|
+
const riskManagement = resultDirectusRiskManagement.map((risk) => {
|
|
270
|
+
const items = risk.riskManagementItems
|
|
271
|
+
.map((data) => {
|
|
272
|
+
const { riskManagementItem } = data, rest = __rest(data, ["riskManagementItem"]);
|
|
273
|
+
const item = itemMapper[riskManagementItem.key];
|
|
274
|
+
if (!item)
|
|
275
|
+
return null;
|
|
276
|
+
return Object.assign(Object.assign({}, rest), item);
|
|
277
|
+
})
|
|
278
|
+
.filter((item) => item !== null);
|
|
279
|
+
return {
|
|
280
|
+
id: risk.id,
|
|
281
|
+
poolIdFK: risk.poolIdFK.toLowerCase(),
|
|
282
|
+
items,
|
|
283
|
+
riskPerformance: {
|
|
284
|
+
id: risk.id,
|
|
285
|
+
firstLossCapital: risk.firstLossCapital,
|
|
286
|
+
poolLossRate: risk.poolLossRate,
|
|
287
|
+
independentRiskScore: risk.independentRiskScore,
|
|
288
|
+
communityRating: risk.communityRating,
|
|
289
|
+
},
|
|
290
|
+
};
|
|
291
|
+
});
|
|
292
|
+
return filterArray(riskManagement, id_in);
|
|
293
|
+
});
|
|
294
|
+
}
|
|
295
|
+
getPoolDelegateProfileAndHistory(id_in) {
|
|
296
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
297
|
+
var _a;
|
|
298
|
+
const poolDelegateProfileAndHistoryDirectus = (yield this._directus.request(readItems('PoolDelegateProfileAndHistory', {
|
|
299
|
+
// @ts-expect-error this is correct and works
|
|
300
|
+
fields: ['*', { otherPools: ['*'] }],
|
|
301
|
+
})));
|
|
302
|
+
const [poolNames, directusPoolNames] = yield Promise.all([
|
|
303
|
+
this._graph.request(getPoolNameQuery, {
|
|
304
|
+
ids: poolDelegateProfileAndHistoryDirectus.flatMap((directus) => directus.otherPools.map((delegate) => delegate.PoolOverview_id)),
|
|
305
|
+
unusedPools: this._kasuConfig.UNUSED_LENDING_POOL_IDS,
|
|
306
|
+
}),
|
|
307
|
+
this._directus.request(readItems('PoolOverview', {
|
|
308
|
+
fields: ['id', 'poolName', 'subheading', 'oversubscribed'],
|
|
309
|
+
})),
|
|
310
|
+
]);
|
|
311
|
+
const retn = [];
|
|
312
|
+
for (const data of poolDelegateProfileAndHistoryDirectus) {
|
|
313
|
+
const otherKASUPools = [];
|
|
314
|
+
for (const otherPool of data.otherPools) {
|
|
315
|
+
const found = poolNames.lendingPools.find(({ id }) => otherPool.PoolOverview_id === id);
|
|
316
|
+
if (!found)
|
|
317
|
+
continue;
|
|
318
|
+
const directusName = directusPoolNames.find(({ id }) => id === found.id);
|
|
319
|
+
const poolName = (_a = directusName === null || directusName === void 0 ? void 0 : directusName.poolName) !== null && _a !== void 0 ? _a : found.name;
|
|
320
|
+
otherKASUPools.push({
|
|
321
|
+
id: found.id,
|
|
322
|
+
name: (directusName === null || directusName === void 0 ? void 0 : directusName.subheading)
|
|
323
|
+
? `${poolName} - ${directusName.subheading}`
|
|
324
|
+
: poolName,
|
|
325
|
+
isActive: !found.isStopped,
|
|
326
|
+
isOversubscribed: Boolean(directusName === null || directusName === void 0 ? void 0 : directusName.oversubscribed),
|
|
327
|
+
});
|
|
328
|
+
}
|
|
329
|
+
retn.push({
|
|
330
|
+
id: data.id,
|
|
331
|
+
delegateLendingHistory: data.delegateLendingHistory,
|
|
332
|
+
assetClasses: data.assetClasses,
|
|
333
|
+
otherKASUPools,
|
|
334
|
+
totalLoanFundsOriginated: data.totalLoanFundsOriginated,
|
|
335
|
+
totalLoansOriginated: data.totalLoansOriginated,
|
|
336
|
+
loansUnderManagement: data.loansUnderManagement,
|
|
337
|
+
historicLossRate: data.historicLossRate,
|
|
338
|
+
});
|
|
339
|
+
}
|
|
340
|
+
return filterArray(retn, id_in);
|
|
341
|
+
});
|
|
342
|
+
}
|
|
343
|
+
getPoolTranches(epochId, id_in) {
|
|
344
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
345
|
+
const subgraphResults = yield this._graph.request(getAllTranchesQuery, {
|
|
346
|
+
unusedPools: this._kasuConfig.UNUSED_LENDING_POOL_IDS,
|
|
347
|
+
});
|
|
348
|
+
const subgraphConfigurationResults = yield this._graph.request(getAllTrancheConfigurationsQuery, {
|
|
349
|
+
unusedPools: this._kasuConfig.UNUSED_LENDING_POOL_IDS,
|
|
350
|
+
epochId,
|
|
351
|
+
});
|
|
352
|
+
const retn = [];
|
|
353
|
+
for (const trancheSubgraph of subgraphResults.lendingPoolTranches) {
|
|
354
|
+
const configuration = subgraphConfigurationResults.lendingPoolTrancheConfigurations.find((r) => r.id == trancheSubgraph.id);
|
|
355
|
+
if (!configuration) {
|
|
356
|
+
console.warn("Couldn't find tranche configuration for id: ", trancheSubgraph.id);
|
|
357
|
+
continue;
|
|
358
|
+
}
|
|
359
|
+
const interestUpdates = configuration.lendingPoolTrancheInterestRateUpdates;
|
|
360
|
+
const interestRate = interestUpdates.length
|
|
361
|
+
? interestUpdates[0].epochInterestRate
|
|
362
|
+
: configuration.interestRate;
|
|
363
|
+
const tranche = {
|
|
364
|
+
id: trancheSubgraph.id,
|
|
365
|
+
poolIdFK: trancheSubgraph.lendingPool.id.toLowerCase(),
|
|
366
|
+
apy: this.calculateApyForTranche(interestRate).toString(),
|
|
367
|
+
minimalDepositThreshold: configuration.minDepositAmount,
|
|
368
|
+
maximalDepositThreshold: configuration.maxDepositAmount,
|
|
369
|
+
};
|
|
370
|
+
retn.push(tranche);
|
|
371
|
+
}
|
|
372
|
+
return filterArray(retn, id_in);
|
|
373
|
+
});
|
|
374
|
+
}
|
|
375
|
+
getBadAndDoubtfulDebts(id_in) {
|
|
376
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
377
|
+
const [badAndDoubtfulDebtsItemsDirectus, badAndDoubtfulDebtsDirectus] = yield Promise.all([
|
|
378
|
+
this._directus.request(readItems('BadAndDoubtfulDebtsItems')),
|
|
379
|
+
this._directus.request(readItems('BadAndDoubtfulDebts')),
|
|
380
|
+
]);
|
|
381
|
+
const itemsMapper = badAndDoubtfulDebtsItemsDirectus.reduce((acc, cur) => (Object.assign(Object.assign({}, acc), { [cur.id]: cur })), {});
|
|
382
|
+
const test = badAndDoubtfulDebtsDirectus.map((debts) => {
|
|
383
|
+
const items = debts.items
|
|
384
|
+
.map((debtItem) => {
|
|
385
|
+
const { item } = debtItem, metric = __rest(debtItem, ["item"]);
|
|
386
|
+
const itemMetric = itemsMapper[item.key];
|
|
387
|
+
if (!itemMetric)
|
|
388
|
+
return null;
|
|
389
|
+
return Object.assign(Object.assign({}, metric), { item: itemMetric });
|
|
390
|
+
})
|
|
391
|
+
.filter((item) => item !== null);
|
|
392
|
+
return Object.assign(Object.assign({}, debts), { items });
|
|
393
|
+
});
|
|
394
|
+
return filterArray(test, id_in);
|
|
395
|
+
});
|
|
396
|
+
}
|
|
397
|
+
getPoolCreditMetrics(id_in) {
|
|
398
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
399
|
+
const [keyCreditMetricsDirectus, poolCreditMetricsDirectus] = yield Promise.all([
|
|
400
|
+
this._directus.request(readItems('KeyCreditMetrics')),
|
|
401
|
+
this._directus.request(readItems('PoolCreditMetrics')),
|
|
402
|
+
]);
|
|
403
|
+
const keyCreditMetricsMapper = keyCreditMetricsDirectus.reduce((acc, cur) => (Object.assign(Object.assign({}, acc), { [cur.name]: cur })), {});
|
|
404
|
+
const poolCreditMetrics = poolCreditMetricsDirectus.map((pool) => {
|
|
405
|
+
const metrics = pool.keyCreditMetrics
|
|
406
|
+
.map((data) => {
|
|
407
|
+
const { keyCreditMetric } = data, metric = __rest(data, ["keyCreditMetric"]);
|
|
408
|
+
const keyMetric = keyCreditMetricsMapper[keyCreditMetric.key];
|
|
409
|
+
if (!keyMetric)
|
|
410
|
+
return null;
|
|
411
|
+
return Object.assign(Object.assign({}, metric), { keyCreditMetric: keyMetric });
|
|
412
|
+
})
|
|
413
|
+
.filter((metric) => metric !== null);
|
|
414
|
+
return Object.assign(Object.assign({}, pool), { keyCreditMetrics: metrics });
|
|
415
|
+
});
|
|
416
|
+
return filterArray(poolCreditMetrics, id_in);
|
|
417
|
+
});
|
|
418
|
+
}
|
|
419
|
+
getFinancialReportingDocuments(id_in) {
|
|
420
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
421
|
+
const [financialReportingDocumentsDirectus, financialReportingDocumentsItemsDirectus,] = yield Promise.all([
|
|
422
|
+
this._directus.request(readItems('FinancialReportingDocuments')),
|
|
423
|
+
this._directus.request(readItems('FinancialReportingDocumentsItems')),
|
|
424
|
+
]);
|
|
425
|
+
const documentItemsMapper = financialReportingDocumentsItemsDirectus.reduce((acc, cur) => (Object.assign(Object.assign({}, acc), { [cur.id]: cur })), {});
|
|
426
|
+
const mapDocumentFilePath = financialReportingDocumentsDirectus
|
|
427
|
+
.map((report) => {
|
|
428
|
+
if (!report.documents)
|
|
429
|
+
return null;
|
|
430
|
+
const documents = report.documents
|
|
431
|
+
.map((documentId) => {
|
|
432
|
+
const document = documentItemsMapper[documentId];
|
|
433
|
+
if (!document)
|
|
434
|
+
return null;
|
|
435
|
+
return Object.assign(Object.assign({}, document), { document: this.getUrlFromFile(document.document) });
|
|
436
|
+
})
|
|
437
|
+
.filter((document) => document !== null);
|
|
438
|
+
return Object.assign(Object.assign({}, report), { documents });
|
|
439
|
+
})
|
|
440
|
+
.filter((report) => report !== null);
|
|
441
|
+
return filterArray(mapDocumentFilePath, id_in);
|
|
442
|
+
});
|
|
443
|
+
}
|
|
444
|
+
getRepayments(id_in) {
|
|
445
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
446
|
+
const [poolRepaymentDirectus, poolRepaymentItemsDirectus] = yield Promise.all([
|
|
447
|
+
this._directus.request(readItems('PoolRepayments', {
|
|
448
|
+
filter: {
|
|
449
|
+
poolIdFK: {
|
|
450
|
+
_nin: this._kasuConfig.UNUSED_LENDING_POOL_IDS,
|
|
451
|
+
},
|
|
452
|
+
},
|
|
453
|
+
})),
|
|
454
|
+
this._directus.request(readItems('PoolRepaymentItems')),
|
|
455
|
+
]);
|
|
456
|
+
const repaymentItemsMapper = poolRepaymentItemsDirectus.reduce((acc, cur) => (Object.assign(Object.assign({}, acc), { [cur.name]: cur })), {});
|
|
457
|
+
const lendingPoolsWithdrawalsAndDepositsSubgraph = yield this._graph.request(getLendingPoolWithdrawalAndDepositsQuery, { unusedPools: this._kasuConfig.UNUSED_LENDING_POOL_IDS });
|
|
458
|
+
const retn = [];
|
|
459
|
+
for (const data of poolRepaymentDirectus) {
|
|
460
|
+
const lendingPoolSubgraph = lendingPoolsWithdrawalsAndDepositsSubgraph.lendingPools.find((pool) => pool.id == data.poolIdFK);
|
|
461
|
+
if (lendingPoolSubgraph === undefined) {
|
|
462
|
+
continue;
|
|
463
|
+
}
|
|
464
|
+
const currentDepositRequests = parseFloat(lendingPoolSubgraph.pendingPool.totalPendingDepositsAmount);
|
|
465
|
+
let sumTotalPendingWithdrawalShares = 0.0;
|
|
466
|
+
lendingPoolSubgraph.pendingPool.totalPendingWithdrawalShares.forEach((shares) => {
|
|
467
|
+
sumTotalPendingWithdrawalShares += parseFloat(shares);
|
|
468
|
+
});
|
|
469
|
+
const cumulativeLendingAndWithdrawals = data.CumulativeLendingAndWithdrawals.map((fund) => {
|
|
470
|
+
var _a;
|
|
471
|
+
let calculatedValue = 0;
|
|
472
|
+
if (fund.key === 'netLendingWithdrawals') {
|
|
473
|
+
calculatedValue =
|
|
474
|
+
parseFloat(lendingPoolSubgraph.totalDepositsAccepted) -
|
|
475
|
+
parseFloat(lendingPoolSubgraph.totalWithdrawalsAccepted);
|
|
476
|
+
}
|
|
477
|
+
if (fund.key === 'cumulativeLending') {
|
|
478
|
+
calculatedValue = parseFloat(lendingPoolSubgraph.totalDepositsAccepted);
|
|
479
|
+
}
|
|
480
|
+
if (fund.key === 'cumulativeLendingWithdrawals') {
|
|
481
|
+
calculatedValue = parseFloat(lendingPoolSubgraph.totalWithdrawalsAccepted);
|
|
482
|
+
}
|
|
483
|
+
const repaymentItem = repaymentItemsMapper[fund.repaymentItem.key];
|
|
484
|
+
if (!repaymentItem)
|
|
485
|
+
return null;
|
|
486
|
+
return Object.assign({ value: (_a = fund.value) !== null && _a !== void 0 ? _a : calculatedValue }, repaymentItem);
|
|
487
|
+
}).filter((item) => item !== null);
|
|
488
|
+
const lendingAndWithdrawalRequests = data.LendingAndWithdrawalRequests.map((fund) => {
|
|
489
|
+
var _a;
|
|
490
|
+
let calculatedValue = 0;
|
|
491
|
+
if (fund.key === 'netLendingWithdrawalRequests') {
|
|
492
|
+
calculatedValue =
|
|
493
|
+
currentDepositRequests -
|
|
494
|
+
sumTotalPendingWithdrawalShares;
|
|
495
|
+
}
|
|
496
|
+
if (fund.key === 'currentLendingRequests') {
|
|
497
|
+
calculatedValue = currentDepositRequests;
|
|
498
|
+
}
|
|
499
|
+
if (fund.key === 'currentLendingWithdrawalRequests') {
|
|
500
|
+
calculatedValue = sumTotalPendingWithdrawalShares;
|
|
501
|
+
}
|
|
502
|
+
const repaymentItem = repaymentItemsMapper[fund.repaymentItem.key];
|
|
503
|
+
if (!repaymentItem)
|
|
504
|
+
return null;
|
|
505
|
+
return Object.assign({ value: (_a = fund.value) !== null && _a !== void 0 ? _a : calculatedValue }, repaymentItem);
|
|
506
|
+
}).filter((item) => item !== null);
|
|
507
|
+
const poolRepayment = {
|
|
508
|
+
id: data.id,
|
|
509
|
+
poolIdFK: data.poolIdFK.toLowerCase(),
|
|
510
|
+
currentTotalEndBorrowers: data.currentTotalEndBorrowers,
|
|
511
|
+
repaymentsFileUrl: this.getUrlFromFile(data.repaymentsFile),
|
|
512
|
+
upcomingLendingFundsFlow: data.UpcomingLendingFundsFlow.map((fund) => {
|
|
513
|
+
const repaymentItem = repaymentItemsMapper[fund.repaymentItem.key];
|
|
514
|
+
if (!repaymentItem)
|
|
515
|
+
return null;
|
|
516
|
+
return Object.assign({ value: fund.value }, repaymentItem);
|
|
517
|
+
}).filter((item) => item !== null),
|
|
518
|
+
cumulativeLendingFundsFlow: data.CumulativeLendingFundsFlow.map((fund) => {
|
|
519
|
+
const repaymentItem = repaymentItemsMapper[fund.repaymentItem.key];
|
|
520
|
+
if (!repaymentItem)
|
|
521
|
+
return null;
|
|
522
|
+
return Object.assign({ value: fund.value }, repaymentItem);
|
|
523
|
+
}).filter((item) => item !== null),
|
|
524
|
+
cumulativeLendingAndWithdrawals,
|
|
525
|
+
lendingAndWithdrawalRequests,
|
|
526
|
+
};
|
|
527
|
+
retn.push(poolRepayment);
|
|
528
|
+
}
|
|
529
|
+
return filterArray(retn, id_in);
|
|
530
|
+
});
|
|
531
|
+
}
|
|
532
|
+
getLendingTotals(poolOverviews) {
|
|
533
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
534
|
+
const riskManagements = yield this.getRiskManagement();
|
|
535
|
+
const retn = {
|
|
536
|
+
totalValueLocked: 0,
|
|
537
|
+
loansUnderManagement: 0,
|
|
538
|
+
totalLoanFundsOriginated: 0,
|
|
539
|
+
totalLossRate: 0,
|
|
540
|
+
totalYieldEarned: 0,
|
|
541
|
+
};
|
|
542
|
+
for (const poolOverview of poolOverviews) {
|
|
543
|
+
retn.totalValueLocked += parseFloat(poolOverview.totalValueLocked.total);
|
|
544
|
+
retn.loansUnderManagement += parseFloat(poolOverview.loansUnderManagement);
|
|
545
|
+
retn.totalLoanFundsOriginated += parseFloat(poolOverview.loanFundsOriginated);
|
|
546
|
+
retn.totalYieldEarned += parseFloat(poolOverview.yieldEarned);
|
|
547
|
+
}
|
|
548
|
+
for (const poolOverview of poolOverviews) {
|
|
549
|
+
const riskManagement = riskManagements.find((rm) => rm.poolIdFK.toLowerCase() === poolOverview.id.toLowerCase());
|
|
550
|
+
if (!riskManagement) {
|
|
551
|
+
console.warn("Couldn't find risk management for id: ", poolOverview.id);
|
|
552
|
+
continue;
|
|
553
|
+
}
|
|
554
|
+
const weight = retn.totalLoanFundsOriginated === 0
|
|
555
|
+
? 0
|
|
556
|
+
: parseFloat(poolOverview.loanFundsOriginated) /
|
|
557
|
+
retn.totalLoanFundsOriginated;
|
|
558
|
+
retn.totalLossRate +=
|
|
559
|
+
riskManagement.riskPerformance.poolLossRate * weight;
|
|
560
|
+
}
|
|
561
|
+
return retn;
|
|
562
|
+
});
|
|
563
|
+
}
|
|
564
|
+
calculateCompounding(amount, epochInterestRate, daysInvested, interestFee) {
|
|
565
|
+
const COMPOUNDING_PERIOD = 7;
|
|
566
|
+
const earnedAmount = [];
|
|
567
|
+
const epochInterestRateAfterFee = epochInterestRate * (1 - interestFee);
|
|
568
|
+
for (let i = 0; i < daysInvested + 1; i++) {
|
|
569
|
+
const investmentEpochs = i / COMPOUNDING_PERIOD;
|
|
570
|
+
const interestEarned = amount *
|
|
571
|
+
(Math.pow(1 + epochInterestRateAfterFee, investmentEpochs) - 1);
|
|
572
|
+
earnedAmount.push(interestEarned);
|
|
573
|
+
}
|
|
574
|
+
return earnedAmount;
|
|
575
|
+
}
|
|
576
|
+
calculateBonusInterestEarnings(earnedAmounts, amount, bonusEpochInterest) {
|
|
577
|
+
const COMPOUNDING_PERIOD = 7;
|
|
578
|
+
const dailyInterest = bonusEpochInterest / COMPOUNDING_PERIOD;
|
|
579
|
+
const bonusInterestEarned = [0];
|
|
580
|
+
for (let i = 1; i < earnedAmounts.length; i++) {
|
|
581
|
+
const bonusInterest = (amount + earnedAmounts[i]) * dailyInterest;
|
|
582
|
+
bonusInterestEarned.push(bonusInterestEarned[i - 1] + bonusInterest);
|
|
583
|
+
}
|
|
584
|
+
return bonusInterestEarned;
|
|
585
|
+
}
|
|
586
|
+
}
|