@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.
Files changed (219) hide show
  1. package/.eslintignore +3 -0
  2. package/.eslintrc.json +90 -0
  3. package/.github/workflows/ci.yml +0 -0
  4. package/.github/workflows/main.yml +43 -0
  5. package/.prettierignore +3 -0
  6. package/.prettierrc +28 -0
  7. package/abis/IClearingCoordinator.abi.json +410 -0
  8. package/abis/IERC20Metadata.abi.json +224 -0
  9. package/abis/IFeeManager.abi.json +66 -0
  10. package/abis/IKSULocking.abi.json +754 -0
  11. package/abis/IKasuAllowList.abi.json +455 -0
  12. package/abis/IKasuController.abi.json +423 -0
  13. package/abis/IKasuNFTs.abi.json +1523 -0
  14. package/abis/IKsuPrice.abi.json +15 -0
  15. package/abis/ILendingPool.abi.json +1431 -0
  16. package/abis/ILendingPoolFactory.abi.json +97 -0
  17. package/abis/ILendingPoolManager.abi.json +1347 -0
  18. package/abis/ILendingPoolTranche.abi.json +1317 -0
  19. package/abis/IPriceFeed.abi.json +1 -0
  20. package/abis/ISystemVariables.abi.json +431 -0
  21. package/abis/IUserLoyaltyRewards.abi.json +288 -0
  22. package/abis/IUserManager.abi.json +321 -0
  23. package/abis/KSULockBonus.abi.json +91 -0
  24. package/abis/KasuPoolExternalTVL.abi.json +812 -0
  25. package/abis/Swapper.abi.json +146 -0
  26. package/copy-abi.ps1 +15 -0
  27. package/dist/bundle.cjs.js +21646 -0
  28. package/dist/bundle.esm.js +21644 -0
  29. package/dist/contracts/IClearingCoordinatorAbi.d.ts +169 -0
  30. package/dist/contracts/IClearingCoordinatorAbi.js +1 -0
  31. package/dist/contracts/IERC20MetadataAbi.d.ts +162 -0
  32. package/dist/contracts/IERC20MetadataAbi.js +1 -0
  33. package/dist/contracts/IFeeManagerAbi.d.ts +96 -0
  34. package/dist/contracts/IFeeManagerAbi.js +1 -0
  35. package/dist/contracts/IKSULockingAbi.d.ts +466 -0
  36. package/dist/contracts/IKSULockingAbi.js +1 -0
  37. package/dist/contracts/IKasuAllowListAbi.d.ts +304 -0
  38. package/dist/contracts/IKasuAllowListAbi.js +1 -0
  39. package/dist/contracts/IKasuControllerAbi.d.ts +309 -0
  40. package/dist/contracts/IKasuControllerAbi.js +1 -0
  41. package/dist/contracts/IKasuNFTsAbi.d.ts +1121 -0
  42. package/dist/contracts/IKasuNFTsAbi.js +1 -0
  43. package/dist/contracts/IKsuPriceAbi.d.ts +42 -0
  44. package/dist/contracts/IKsuPriceAbi.js +1 -0
  45. package/dist/contracts/ILendingPoolAbi.d.ts +968 -0
  46. package/dist/contracts/ILendingPoolAbi.js +1 -0
  47. package/dist/contracts/ILendingPoolFactoryAbi.d.ts +103 -0
  48. package/dist/contracts/ILendingPoolFactoryAbi.js +1 -0
  49. package/dist/contracts/ILendingPoolManagerAbi.d.ts +779 -0
  50. package/dist/contracts/ILendingPoolManagerAbi.js +1 -0
  51. package/dist/contracts/ILendingPoolTrancheAbi.d.ts +752 -0
  52. package/dist/contracts/ILendingPoolTrancheAbi.js +1 -0
  53. package/dist/contracts/ISystemVariablesAbi.d.ts +378 -0
  54. package/dist/contracts/ISystemVariablesAbi.js +1 -0
  55. package/dist/contracts/IUserLoyaltyRewardsAbi.d.ts +248 -0
  56. package/dist/contracts/IUserLoyaltyRewardsAbi.js +1 -0
  57. package/dist/contracts/IUserManagerAbi.d.ts +270 -0
  58. package/dist/contracts/IUserManagerAbi.js +1 -0
  59. package/dist/contracts/KSULockBonusAbi.d.ts +61 -0
  60. package/dist/contracts/KSULockBonusAbi.js +1 -0
  61. package/dist/contracts/KasuPoolExternalTVLAbi.d.ts +411 -0
  62. package/dist/contracts/KasuPoolExternalTVLAbi.js +1 -0
  63. package/dist/contracts/SwapperAbi.d.ts +120 -0
  64. package/dist/contracts/SwapperAbi.js +1 -0
  65. package/dist/contracts/common.d.ts +21 -0
  66. package/dist/contracts/common.js +1 -0
  67. package/dist/contracts/factories/IClearingCoordinatorAbi__factory.d.ts +320 -0
  68. package/dist/contracts/factories/IClearingCoordinatorAbi__factory.js +423 -0
  69. package/dist/contracts/factories/IERC20MetadataAbi__factory.d.ts +178 -0
  70. package/dist/contracts/factories/IERC20MetadataAbi__factory.js +237 -0
  71. package/dist/contracts/factories/IFeeManagerAbi__factory.d.ts +59 -0
  72. package/dist/contracts/factories/IFeeManagerAbi__factory.js +79 -0
  73. package/dist/contracts/factories/IKSULockingAbi__factory.d.ts +588 -0
  74. package/dist/contracts/factories/IKSULockingAbi__factory.js +767 -0
  75. package/dist/contracts/factories/IKasuAllowListAbi__factory.d.ts +354 -0
  76. package/dist/contracts/factories/IKasuAllowListAbi__factory.js +468 -0
  77. package/dist/contracts/factories/IKasuControllerAbi__factory.d.ts +339 -0
  78. package/dist/contracts/factories/IKasuControllerAbi__factory.js +436 -0
  79. package/dist/contracts/factories/IKasuNFTsAbi__factory.d.ts +1629 -0
  80. package/dist/contracts/factories/IKasuNFTsAbi__factory.js +2108 -0
  81. package/dist/contracts/factories/IKsuPriceAbi__factory.d.ts +18 -0
  82. package/dist/contracts/factories/IKsuPriceAbi__factory.js +28 -0
  83. package/dist/contracts/factories/ILendingPoolAbi__factory.d.ts +1109 -0
  84. package/dist/contracts/factories/ILendingPoolAbi__factory.js +1444 -0
  85. package/dist/contracts/factories/ILendingPoolFactoryAbi__factory.d.ts +81 -0
  86. package/dist/contracts/factories/ILendingPoolFactoryAbi__factory.js +110 -0
  87. package/dist/contracts/factories/ILendingPoolManagerAbi__factory.d.ts +1045 -0
  88. package/dist/contracts/factories/ILendingPoolManagerAbi__factory.js +1360 -0
  89. package/dist/contracts/factories/ILendingPoolTrancheAbi__factory.d.ts +1015 -0
  90. package/dist/contracts/factories/ILendingPoolTrancheAbi__factory.js +1330 -0
  91. package/dist/contracts/factories/ISystemVariablesAbi__factory.d.ts +338 -0
  92. package/dist/contracts/factories/ISystemVariablesAbi__factory.js +444 -0
  93. package/dist/contracts/factories/IUserLoyaltyRewardsAbi__factory.d.ts +230 -0
  94. package/dist/contracts/factories/IUserLoyaltyRewardsAbi__factory.js +301 -0
  95. package/dist/contracts/factories/IUserManagerAbi__factory.d.ts +250 -0
  96. package/dist/contracts/factories/IUserManagerAbi__factory.js +334 -0
  97. package/dist/contracts/factories/KSULockBonusAbi__factory.d.ts +76 -0
  98. package/dist/contracts/factories/KSULockBonusAbi__factory.js +104 -0
  99. package/dist/contracts/factories/KasuPoolExternalTVLAbi__factory.d.ts +631 -0
  100. package/dist/contracts/factories/KasuPoolExternalTVLAbi__factory.js +825 -0
  101. package/dist/contracts/factories/SwapperAbi__factory.d.ts +120 -0
  102. package/dist/contracts/factories/SwapperAbi__factory.js +159 -0
  103. package/dist/contracts/factories/index.d.ts +18 -0
  104. package/dist/contracts/factories/index.js +21 -0
  105. package/dist/contracts/index.d.ts +37 -0
  106. package/dist/contracts/index.js +19 -0
  107. package/dist/index.d.ts +17 -0
  108. package/dist/index.js +16 -0
  109. package/dist/sdk-config.d.ts +22 -0
  110. package/dist/sdk-config.js +9 -0
  111. package/dist/services/DataService/data-service.d.ts +37 -0
  112. package/dist/services/DataService/data-service.js +586 -0
  113. package/dist/services/DataService/directus-types.d.ts +187 -0
  114. package/dist/services/DataService/directus-types.js +1 -0
  115. package/dist/services/DataService/queries.d.ts +7 -0
  116. package/dist/services/DataService/queries.js +158 -0
  117. package/dist/services/DataService/subgraph-types.d.ts +95 -0
  118. package/dist/services/DataService/subgraph-types.js +1 -0
  119. package/dist/services/DataService/types.d.ts +169 -0
  120. package/dist/services/DataService/types.js +1 -0
  121. package/dist/services/Locking/locking.d.ts +61 -0
  122. package/dist/services/Locking/locking.js +344 -0
  123. package/dist/services/Locking/queries.d.ts +10 -0
  124. package/dist/services/Locking/queries.js +108 -0
  125. package/dist/services/Locking/types.d.ts +117 -0
  126. package/dist/services/Locking/types.js +1 -0
  127. package/dist/services/Portfolio/portfolio.d.ts +22 -0
  128. package/dist/services/Portfolio/portfolio.js +325 -0
  129. package/dist/services/Portfolio/queries.d.ts +2 -0
  130. package/dist/services/Portfolio/queries.js +164 -0
  131. package/dist/services/Portfolio/types.d.ts +206 -0
  132. package/dist/services/Portfolio/types.js +1 -0
  133. package/dist/services/Swapper/swapper.d.ts +11 -0
  134. package/dist/services/Swapper/swapper.js +97 -0
  135. package/dist/services/Swapper/types.d.ts +51 -0
  136. package/dist/services/Swapper/types.js +1 -0
  137. package/dist/services/UserLending/helper.d.ts +2 -0
  138. package/dist/services/UserLending/helper.js +16 -0
  139. package/dist/services/UserLending/queries.d.ts +8 -0
  140. package/dist/services/UserLending/queries.js +188 -0
  141. package/dist/services/UserLending/subgraph-types.d.ts +147 -0
  142. package/dist/services/UserLending/subgraph-types.js +6 -0
  143. package/dist/services/UserLending/types.d.ts +102 -0
  144. package/dist/services/UserLending/types.js +13 -0
  145. package/dist/services/UserLending/user-lending.d.ts +55 -0
  146. package/dist/services/UserLending/user-lending.js +490 -0
  147. package/dist/services/shared.d.ts +1 -0
  148. package/dist/services/shared.js +11 -0
  149. package/dist/tests/sample.test.d.ts +1 -0
  150. package/dist/tests/sample.test.js +32 -0
  151. package/index.ts +2 -0
  152. package/jest-config.ts +9 -0
  153. package/jest.config.js +8 -0
  154. package/package.json +49 -0
  155. package/rollup.config.js +45 -0
  156. package/src/contracts/IClearingCoordinatorAbi.ts +406 -0
  157. package/src/contracts/IERC20MetadataAbi.ts +364 -0
  158. package/src/contracts/IFeeManagerAbi.ts +182 -0
  159. package/src/contracts/IKSULockingAbi.ts +965 -0
  160. package/src/contracts/IKasuAllowListAbi.ts +605 -0
  161. package/src/contracts/IKasuControllerAbi.ts +703 -0
  162. package/src/contracts/IKasuNFTsAbi.ts +2744 -0
  163. package/src/contracts/IKsuPriceAbi.ts +87 -0
  164. package/src/contracts/ILendingPoolAbi.ts +2081 -0
  165. package/src/contracts/ILendingPoolFactoryAbi.ts +161 -0
  166. package/src/contracts/ILendingPoolManagerAbi.ts +1917 -0
  167. package/src/contracts/ILendingPoolTrancheAbi.ts +1911 -0
  168. package/src/contracts/ISystemVariablesAbi.ts +908 -0
  169. package/src/contracts/IUserLoyaltyRewardsAbi.ts +544 -0
  170. package/src/contracts/IUserManagerAbi.ts +612 -0
  171. package/src/contracts/KSULockBonusAbi.ts +124 -0
  172. package/src/contracts/KasuPoolExternalTVLAbi.ts +1071 -0
  173. package/src/contracts/SwapperAbi.ts +266 -0
  174. package/src/contracts/common.ts +44 -0
  175. package/src/contracts/factories/IClearingCoordinatorAbi__factory.ts +438 -0
  176. package/src/contracts/factories/IERC20MetadataAbi__factory.ts +248 -0
  177. package/src/contracts/factories/IFeeManagerAbi__factory.ts +90 -0
  178. package/src/contracts/factories/IKSULockingAbi__factory.ts +778 -0
  179. package/src/contracts/factories/IKasuAllowListAbi__factory.ts +479 -0
  180. package/src/contracts/factories/IKasuControllerAbi__factory.ts +447 -0
  181. package/src/contracts/factories/IKasuNFTsAbi__factory.ts +2116 -0
  182. package/src/contracts/factories/IKsuPriceAbi__factory.ts +36 -0
  183. package/src/contracts/factories/ILendingPoolAbi__factory.ts +1455 -0
  184. package/src/contracts/factories/ILendingPoolFactoryAbi__factory.ts +125 -0
  185. package/src/contracts/factories/ILendingPoolManagerAbi__factory.ts +1375 -0
  186. package/src/contracts/factories/ILendingPoolTrancheAbi__factory.ts +1345 -0
  187. package/src/contracts/factories/ISystemVariablesAbi__factory.ts +455 -0
  188. package/src/contracts/factories/IUserLoyaltyRewardsAbi__factory.ts +316 -0
  189. package/src/contracts/factories/IUserManagerAbi__factory.ts +345 -0
  190. package/src/contracts/factories/KSULockBonusAbi__factory.ts +115 -0
  191. package/src/contracts/factories/KasuPoolExternalTVLAbi__factory.ts +840 -0
  192. package/src/contracts/factories/SwapperAbi__factory.ts +167 -0
  193. package/src/contracts/factories/index.ts +21 -0
  194. package/src/contracts/index.ts +40 -0
  195. package/src/index.ts +27 -0
  196. package/src/sdk-config.ts +35 -0
  197. package/src/services/DataService/data-service.ts +1017 -0
  198. package/src/services/DataService/directus-types.ts +199 -0
  199. package/src/services/DataService/queries.ts +165 -0
  200. package/src/services/DataService/subgraph-types.ts +104 -0
  201. package/src/services/DataService/types.ts +184 -0
  202. package/src/services/Locking/locking.ts +541 -0
  203. package/src/services/Locking/queries.ts +118 -0
  204. package/src/services/Locking/types.ts +141 -0
  205. package/src/services/Portfolio/portfolio.ts +593 -0
  206. package/src/services/Portfolio/queries.ts +166 -0
  207. package/src/services/Portfolio/types.ts +220 -0
  208. package/src/services/Swapper/swapper.ts +158 -0
  209. package/src/services/Swapper/types.ts +54 -0
  210. package/src/services/UserLending/helper.ts +28 -0
  211. package/src/services/UserLending/queries.ts +196 -0
  212. package/src/services/UserLending/subgraph-types.ts +173 -0
  213. package/src/services/UserLending/types.ts +123 -0
  214. package/src/services/UserLending/user-lending.ts +933 -0
  215. package/src/services/shared.ts +11 -0
  216. package/src/tests/sample.test.ts +44 -0
  217. package/tsconfig.json +27 -0
  218. package/tsconfig.tsbuildinfo +1 -0
  219. package/versioner.sh +6 -0
@@ -0,0 +1,344 @@
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 { BigNumber, ethers } from 'ethers';
11
+ import { formatEther, formatUnits, parseUnits } from 'ethers/lib/utils';
12
+ import { GraphQLClient } from 'graphql-request';
13
+ import { IERC20MetadataAbi__factory, IKSULockingAbi__factory, IKsuPriceAbi__factory, ISystemVariablesAbi__factory, IUserLoyaltyRewardsAbi__factory, IUserManagerAbi__factory, } from '../../contracts';
14
+ import { getAllTrancheConfigurationsQuery, getAllTranchesQuery, } from '../DataService/queries';
15
+ import { claimedFeesQuery, getSystemVariablesQuery, getTotalUserLoyaltsRewardsQuery, lockingPeriodsQuery, lockingSummariesQuery, userEarnedrKsuQuery, userLocksQuery, userStakedKsuQuery, userTotalBonusAmountQuery, } from './queries';
16
+ export class KSULocking {
17
+ /**
18
+ *
19
+ */
20
+ constructor(_kasuConfig, signerOrProvider) {
21
+ this._kasuConfig = _kasuConfig;
22
+ this.apyBonuses = [0, 0.001, 0.002];
23
+ this._contractAbi = IKSULockingAbi__factory.connect(_kasuConfig.contracts.IKSULocking, signerOrProvider);
24
+ this._kasuToken = IERC20MetadataAbi__factory.connect(_kasuConfig.contracts.KSUToken, signerOrProvider);
25
+ this._userManagerAbi = IUserManagerAbi__factory.connect(_kasuConfig.contracts.UserManager, signerOrProvider);
26
+ this._systemVariablesAbi = ISystemVariablesAbi__factory.connect(_kasuConfig.contracts.SystemVariables, signerOrProvider);
27
+ this._ksuPriceAbi = IKsuPriceAbi__factory.connect(_kasuConfig.contracts.KsuPrice, signerOrProvider);
28
+ this._kasuBonusAddress = _kasuConfig.contracts.IKSULockBonus;
29
+ this._graph = new GraphQLClient(_kasuConfig.subgraphUrl);
30
+ this._userLoyaltyRewardsAbi = IUserLoyaltyRewardsAbi__factory.connect(_kasuConfig.contracts.UserLoyaltyRewards, signerOrProvider);
31
+ }
32
+ lockKSUTokens(amount, lockPeriod) {
33
+ return __awaiter(this, void 0, void 0, function* () {
34
+ return this._contractAbi.lock(amount, lockPeriod);
35
+ });
36
+ }
37
+ lockKSUWithPermit(amount, lockPeriod, permit) {
38
+ return __awaiter(this, void 0, void 0, function* () {
39
+ const amountBN = BigNumber.from(amount);
40
+ const lockPeriodBN = BigNumber.from(lockPeriod);
41
+ const ksuPermit = {
42
+ value: BigNumber.from(permit.value),
43
+ deadline: BigNumber.from(permit.deadline),
44
+ v: BigNumber.from(permit.v),
45
+ r: permit.r,
46
+ s: permit.s,
47
+ };
48
+ return this._contractAbi.lockWithPermit(amountBN, lockPeriodBN, ksuPermit);
49
+ });
50
+ }
51
+ unlockKSU(amount, userLockId) {
52
+ return __awaiter(this, void 0, void 0, function* () {
53
+ return this._contractAbi.unlock(amount, userLockId);
54
+ });
55
+ }
56
+ claimFees() {
57
+ return __awaiter(this, void 0, void 0, function* () {
58
+ return yield this._contractAbi.claimFees();
59
+ });
60
+ }
61
+ lockDetails(lockPeriod) {
62
+ return __awaiter(this, void 0, void 0, function* () {
63
+ const result = yield this._contractAbi.lockDetails(lockPeriod);
64
+ return {
65
+ rKsuMultiplier: result[0].toNumber(),
66
+ ksuBonusMultiplier: result[1].toNumber(),
67
+ isActive: result[2],
68
+ };
69
+ });
70
+ }
71
+ userTotalDeposits(userAddress) {
72
+ return __awaiter(this, void 0, void 0, function* () {
73
+ return yield this._contractAbi.userTotalDeposits(userAddress);
74
+ });
75
+ }
76
+ calculateUserLockProjectedProtocolFeeRewards(epochId, KSULocked, lockPeriod) {
77
+ return __awaiter(this, void 0, void 0, function* () {
78
+ if (KSULocked === 0) {
79
+ return 0;
80
+ }
81
+ const lockingSummarySubgraph = yield this._graph.request(lockingSummariesQuery);
82
+ const lockingSummary = lockingSummarySubgraph.lockingSummaries[0];
83
+ const totalRKSU = lockingSummary.totalRKsuAmount;
84
+ const lockDetails = yield this.lockDetails(lockPeriod);
85
+ const newUserRKSU = (KSULocked + KSULocked * lockDetails.ksuBonusMultiplier) *
86
+ lockDetails.rKsuMultiplier;
87
+ const totalRKSUAfterLock = totalRKSU + newUserRKSU;
88
+ const systemVariables = yield this._graph.request(getSystemVariablesQuery);
89
+ const performanceFee = parseFloat(systemVariables.systemVariables.performanceFee) / 100;
90
+ const ecosystemFee = parseFloat(systemVariables.systemVariables.ecosystemFeeRate) / 100;
91
+ let projectedYearlyPlatformInterest = 0;
92
+ const subgraphResults = yield this._graph.request(getAllTranchesQuery, {
93
+ unusedPools: this._kasuConfig.UNUSED_LENDING_POOL_IDS,
94
+ });
95
+ const subgraphConfigurationResults = yield this._graph.request(getAllTrancheConfigurationsQuery, {
96
+ unusedPools: this._kasuConfig.UNUSED_LENDING_POOL_IDS,
97
+ epochId,
98
+ });
99
+ for (const tranche of subgraphResults.lendingPoolTranches) {
100
+ const trancheConfig = subgraphConfigurationResults.lendingPoolTrancheConfigurations.find((r) => r.id == tranche.id);
101
+ if (!trancheConfig) {
102
+ console.warn("Couldn't find tranche configuration for id: ", tranche.id);
103
+ continue;
104
+ }
105
+ const interestUpdates = trancheConfig.lendingPoolTrancheInterestRateUpdates;
106
+ const interestRate = interestUpdates.length
107
+ ? interestUpdates[0].epochInterestRate
108
+ : trancheConfig.interestRate;
109
+ projectedYearlyPlatformInterest +=
110
+ parseFloat(tranche.balance) *
111
+ this.calculateApy(parseFloat(interestRate));
112
+ }
113
+ const totalExpectedEcosystemFees = projectedYearlyPlatformInterest * performanceFee * ecosystemFee;
114
+ return ((totalExpectedEcosystemFees * newUserRKSU) /
115
+ parseFloat(totalRKSUAfterLock));
116
+ });
117
+ }
118
+ calculateApy(epochInterestRate) {
119
+ const EPOCHS_IN_YEAR = 52.17857;
120
+ return (1 + epochInterestRate) ^ (EPOCHS_IN_YEAR - 1);
121
+ }
122
+ getUserLocks(userAddress) {
123
+ return __awaiter(this, void 0, void 0, function* () {
124
+ const result = yield this._graph.request(userLocksQuery, {
125
+ userAddress: userAddress.toLowerCase(),
126
+ });
127
+ const resultPromises = result.userLocks.map((userLock) => __awaiter(this, void 0, void 0, function* () {
128
+ const [, id] = userLock.id.split('-');
129
+ const rKSUtoUSDCRatio = yield this.getRKSUvsUSDCRatio(userLock.rKSUAmount, userAddress);
130
+ const loyaltyStatus = this.getLoyaltyLevelAndApyBonusFromRatio(rKSUtoUSDCRatio);
131
+ return {
132
+ id: BigNumber.from(id),
133
+ lockedAmount: userLock.ksuAmount,
134
+ rKSUAmount: userLock.rKSUAmount,
135
+ rKSUtoUSDCRatio: rKSUtoUSDCRatio,
136
+ apyBonus: loyaltyStatus.apyBonus,
137
+ loyaltyLevel: loyaltyStatus.loyaltyLevel,
138
+ startTime: parseFloat(userLock.startTimestamp),
139
+ endTime: parseFloat(userLock.endTimestamp),
140
+ lockPeriod: userLock.lockPeriod,
141
+ };
142
+ }));
143
+ const results = yield Promise.all(resultPromises);
144
+ return results.sort((a, b) => a.endTime - b.endTime);
145
+ });
146
+ }
147
+ getUserBonusData(userAddress) {
148
+ return __awaiter(this, void 0, void 0, function* () {
149
+ // TODO this userLocks query is kinda bad -> change to userLocksDepositInfo as u never need user locks by themselves
150
+ // whole function takes between 500 - 800ms
151
+ const DECIMALS = 18;
152
+ // Normalized user address
153
+ const normalizedAddress = userAddress.toLowerCase();
154
+ // Fetch data concurrently
155
+ const [userLocksData, resultLoyaltyRewards, protocolFeesEarned, userRewards,] = yield Promise.all([
156
+ this._graph.request(userLocksQuery, {
157
+ userAddress: normalizedAddress,
158
+ }),
159
+ this._graph.request(getTotalUserLoyaltsRewardsQuery, { userAddress: normalizedAddress }),
160
+ this._contractAbi.rewards(userAddress),
161
+ this._userLoyaltyRewardsAbi.userRewards(userAddress),
162
+ ]);
163
+ // Extract data and format with fallback
164
+ const { userLocks } = userLocksData;
165
+ const formattedKsuBonusAndRewards = ethers.utils.formatUnits(userRewards, DECIMALS);
166
+ const formattedProtocolFeesEarned = ethers.utils.formatUnits(protocolFeesEarned, DECIMALS);
167
+ const ksuBonusAndRewardsLifetime = resultLoyaltyRewards.user === null
168
+ ? '0.0'
169
+ : resultLoyaltyRewards.user.totalUserLoyaltyRewards;
170
+ const totalLockedAmount = userLocks.length === 0
171
+ ? '0.0'
172
+ : userLocks[0].userLockDepositsInfo.ksuLockedAmount;
173
+ // Construct response data
174
+ return {
175
+ ksuBonusAndRewards: formattedKsuBonusAndRewards,
176
+ ksuBonusAndRewardsLifetime: ksuBonusAndRewardsLifetime,
177
+ protocolFeesEarned: formattedProtocolFeesEarned,
178
+ totalLockedAmount: totalLockedAmount,
179
+ };
180
+ });
181
+ }
182
+ getLoyaltyLevelAndApyBonusFromRatio(rKSUtoUSDCRatio) {
183
+ if (rKSUtoUSDCRatio < 0.01) {
184
+ return { loyaltyLevel: 0, apyBonus: this.apyBonuses[0] };
185
+ }
186
+ else if (rKSUtoUSDCRatio < 0.05) {
187
+ return { loyaltyLevel: 1, apyBonus: this.apyBonuses[1] };
188
+ }
189
+ return { loyaltyLevel: 2, apyBonus: this.apyBonuses[2] };
190
+ }
191
+ getRKSUvsUSDCRatio(rKSUAmount, userAddress) {
192
+ return __awaiter(this, void 0, void 0, function* () {
193
+ const [depositedAmount, pendingAmount] = yield this._userManagerAbi.userTotalPendingAndActiveDepositedAmountForCurrentEpoch(userAddress);
194
+ const ksuPrice = yield this._systemVariablesAbi.ksuEpochTokenPrice();
195
+ const rKSUAmountBignumber = ethers.utils.parseUnits(rKSUAmount, 18);
196
+ const totalUserUsdcAmount = depositedAmount.toNumber() + pendingAmount.toNumber();
197
+ const rKSUInUSDC = rKSUAmountBignumber
198
+ .mul(ksuPrice)
199
+ .div(ethers.utils.parseUnits('1', 18))
200
+ .div(ethers.utils.parseUnits('1', 12));
201
+ return rKSUInUSDC.toNumber() / totalUserUsdcAmount;
202
+ });
203
+ }
204
+ getClaimableRewards(userAddress) {
205
+ return __awaiter(this, void 0, void 0, function* () {
206
+ const result = yield this._contractAbi.rewards(userAddress);
207
+ return result;
208
+ });
209
+ }
210
+ getKasuTokenPrice() {
211
+ return __awaiter(this, void 0, void 0, function* () {
212
+ const decimals = 18;
213
+ return Promise.resolve({
214
+ price: yield this._ksuPriceAbi.ksuTokenPrice(),
215
+ decimals,
216
+ });
217
+ });
218
+ }
219
+ getKasuEpochTokenPrice() {
220
+ return __awaiter(this, void 0, void 0, function* () {
221
+ const decimals = 18;
222
+ return Promise.resolve({
223
+ price: yield this._systemVariablesAbi.ksuEpochTokenPrice(),
224
+ decimals,
225
+ });
226
+ });
227
+ }
228
+ getLifetimeRewards(userAddress, rewardDecimals, claimableRewards) {
229
+ return __awaiter(this, void 0, void 0, function* () {
230
+ var _a, _b;
231
+ const result = yield this._graph.request(claimedFeesQuery, {
232
+ userAddress: userAddress.toLowerCase(),
233
+ });
234
+ const claimedRewards = (_b = (_a = result.userLockDepositsInfo) === null || _a === void 0 ? void 0 : _a.feesClaimed) !== null && _b !== void 0 ? _b : '0';
235
+ if (!claimableRewards) {
236
+ claimableRewards = yield this.getClaimableRewards(userAddress);
237
+ }
238
+ return claimableRewards.add(parseUnits(claimedRewards, rewardDecimals));
239
+ });
240
+ }
241
+ getLaunchBonusAmount(lockAmount, bonusMultiplier, bonusTokensLeft) {
242
+ const bonusMultiplierBN = ethers.utils.parseUnits(bonusMultiplier.toFixed(2), 2);
243
+ const projectedBonus = lockAmount.mul(bonusMultiplierBN).div(100);
244
+ if (bonusTokensLeft.gt(projectedBonus)) {
245
+ return projectedBonus;
246
+ }
247
+ else {
248
+ return bonusTokensLeft;
249
+ }
250
+ }
251
+ getUserStakedKsu(userAddress) {
252
+ return __awaiter(this, void 0, void 0, function* () {
253
+ const result = yield this._graph.request(userStakedKsuQuery, {
254
+ userAddress: userAddress.toLowerCase(),
255
+ });
256
+ if (result.userLockDepositsInfo == null) {
257
+ return '0';
258
+ }
259
+ return result.userLockDepositsInfo.ksuLockedAmount;
260
+ });
261
+ }
262
+ getUserEarnedrKsu(userAddress) {
263
+ return __awaiter(this, void 0, void 0, function* () {
264
+ const result = yield this._graph.request(userEarnedrKsuQuery, {
265
+ userAddress: userAddress.toLowerCase(),
266
+ });
267
+ if (result.userLockDepositsInfo == null) {
268
+ return '0';
269
+ }
270
+ return result.userLockDepositsInfo.rKSUAmount;
271
+ });
272
+ }
273
+ getUserTotalBonusAmount(userAddress) {
274
+ return __awaiter(this, void 0, void 0, function* () {
275
+ const result = yield this._graph.request(userTotalBonusAmountQuery, {
276
+ userAddress: userAddress.toLowerCase(),
277
+ });
278
+ if (result.userLockDepositsInfo == null) {
279
+ return '0';
280
+ }
281
+ return result.userLockDepositsInfo.totalKsuBonusAmount;
282
+ });
283
+ }
284
+ getLockingRewards(userAddress) {
285
+ return __awaiter(this, void 0, void 0, function* () {
286
+ const rewardDecimals = 6;
287
+ const claimableRewards = yield this.getClaimableRewards(userAddress);
288
+ const lifeTimeRewards = yield this.getLifetimeRewards(userAddress, rewardDecimals, claimableRewards);
289
+ return {
290
+ claimableRewards: formatUnits(claimableRewards, rewardDecimals),
291
+ lifeTimeRewards: formatUnits(lifeTimeRewards, rewardDecimals),
292
+ };
293
+ });
294
+ }
295
+ getTotalKsuStaked() {
296
+ return __awaiter(this, void 0, void 0, function* () {
297
+ return Promise.resolve(ethers.constants.Zero.toString());
298
+ });
299
+ }
300
+ getAvailableKsuBonus() {
301
+ return __awaiter(this, void 0, void 0, function* () {
302
+ const amount = yield this._kasuToken.balanceOf(this._kasuBonusAddress);
303
+ return formatEther(amount);
304
+ });
305
+ }
306
+ getUserKsuBalance(userAddress) {
307
+ return __awaiter(this, void 0, void 0, function* () {
308
+ const balance = yield this._kasuToken.balanceOf(userAddress.toLowerCase());
309
+ return formatEther(balance);
310
+ });
311
+ }
312
+ getNextEpochDate() {
313
+ return __awaiter(this, void 0, void 0, function* () {
314
+ return (yield this._systemVariablesAbi.nextEpochStartTimestamp()).toNumber();
315
+ });
316
+ }
317
+ getNextClearingPeriodDate() {
318
+ return __awaiter(this, void 0, void 0, function* () {
319
+ const nextEpochDate = yield this.getNextEpochDate();
320
+ const clearingPeriodLength = yield this._systemVariablesAbi.clearingPeriodLength();
321
+ const nextClearingPeriod = BigNumber.from(nextEpochDate)
322
+ .sub(clearingPeriodLength)
323
+ .toNumber();
324
+ const nowUnix = Date.now() / 1000;
325
+ if (nowUnix >= nextClearingPeriod) {
326
+ const oneWeek = 604800; // in seconds
327
+ return nextClearingPeriod + oneWeek;
328
+ }
329
+ return nextClearingPeriod;
330
+ });
331
+ }
332
+ getActiveLockPeriods() {
333
+ return __awaiter(this, void 0, void 0, function* () {
334
+ const data = yield this._graph.request(lockingPeriodsQuery);
335
+ return data.lockPeriods;
336
+ });
337
+ }
338
+ getProjectedApy() {
339
+ return '10.00';
340
+ }
341
+ getProjectedUSDC() {
342
+ return '20.00';
343
+ }
344
+ }
@@ -0,0 +1,10 @@
1
+ export declare const claimedFeesQuery: string;
2
+ export declare const userEarnedrKsuQuery: string;
3
+ export declare const userTotalBonusAmountQuery: string;
4
+ export declare const userStakedKsuQuery: string;
5
+ export declare const userLocksQuery: string;
6
+ export declare const lockingPeriodsQuery: string;
7
+ export declare const lockingSummariesQuery: string;
8
+ export declare const getAllTranchesQuery: string;
9
+ export declare const getTotalUserLoyaltsRewardsQuery: string;
10
+ export declare const getSystemVariablesQuery: string;
@@ -0,0 +1,108 @@
1
+ import { gql } from 'graphql-request';
2
+ export const claimedFeesQuery = gql `
3
+ query GetClaimedFeesForAddress($userAddress: String!) {
4
+ userLockDepositsInfo(id: $userAddress) {
5
+ feesClaimed
6
+ }
7
+ }
8
+ `;
9
+ export const userEarnedrKsuQuery = gql `
10
+ query GetClaimedFeesForAddress($userAddress: String!) {
11
+ userLockDepositsInfo(id: $userAddress) {
12
+ rKSUAmount
13
+ }
14
+ }
15
+ `;
16
+ export const userTotalBonusAmountQuery = gql `
17
+ query GetClaimedFeesForAddress($userAddress: String!) {
18
+ userLockDepositsInfo(id: $userAddress) {
19
+ totalKsuBonusAmount
20
+ }
21
+ }
22
+ `;
23
+ export const userStakedKsuQuery = gql `
24
+ query GetUserLockDepositsInfo($userAddress: String!) {
25
+ userLockDepositsInfo(id: $userAddress) {
26
+ ksuLockedAmount
27
+ }
28
+ }
29
+ `;
30
+ export const userLocksQuery = gql `
31
+ query GetUserLocks($userAddress: String!) {
32
+ userLocks(
33
+ where: { userLockDepositsInfo: $userAddress, ksuAmount_gt: 0 }
34
+ ) {
35
+ id
36
+ ksuAmount
37
+ endTimestamp
38
+ startTimestamp
39
+ lockPeriod {
40
+ rKSUMultiplier
41
+ lockPeriod
42
+ ksuBonusMultiplier
43
+ id
44
+ }
45
+ rKSUAmount
46
+ userLockDepositsInfo {
47
+ totalKsuBonusAmount
48
+ rKSUAmount
49
+ ksuLockedAmount
50
+ id
51
+ feesClaimed
52
+ }
53
+ }
54
+ }
55
+ `;
56
+ export const lockingPeriodsQuery = gql `
57
+ query LockingPeriodsQuery {
58
+ lockPeriods(orderBy: lockPeriod, where: { isActive: true }) {
59
+ rKSUMultiplier
60
+ lockPeriod
61
+ ksuBonusMultiplier
62
+ isActive
63
+ id
64
+ }
65
+ }
66
+ `;
67
+ export const lockingSummariesQuery = gql `
68
+ query LockingSummariesQuery {
69
+ lockingSummaries {
70
+ id
71
+ totalRKsuAmount
72
+ totalKsuLockedAmount
73
+ totalFeesEmitted
74
+ }
75
+ }
76
+ `;
77
+ export const getAllTranchesQuery = gql `
78
+ query getAllTranches($unusedPools: [String]!) {
79
+ lendingPoolTranches(where: { lendingPool_not_in: $unusedPools }) {
80
+ id
81
+ balance
82
+ }
83
+ }
84
+ `;
85
+ export const getTotalUserLoyaltsRewardsQuery = gql `
86
+ query getAllUserLoyaltsRewards($userAddress: String!) {
87
+ user(id: $userAddress) {
88
+ id
89
+ totalUserLoyaltyRewards
90
+ }
91
+ }
92
+ `;
93
+ export const getSystemVariablesQuery = gql `
94
+ query getSystemVariablesQuery {
95
+ systemVariables(id: "SYSTEM_VARIABLES") {
96
+ id
97
+ protocolFeeRate
98
+ priceUpdateEpoch
99
+ performanceFee
100
+ loyaltyThresholds
101
+ ksuEpochTokenPrice
102
+ epochDuration
103
+ ecosystemFeeRate
104
+ clearingPeriodLength
105
+ canOnlyDepositJuniorWhenUserHasRKSU
106
+ }
107
+ }
108
+ `;
@@ -0,0 +1,117 @@
1
+ import { BigNumber, Bytes } from 'ethers';
2
+ export interface RSVDeadlineValue {
3
+ value: number;
4
+ deadline: number;
5
+ v: number;
6
+ r: Bytes;
7
+ s: Bytes;
8
+ }
9
+ export interface LockPeriodInterface {
10
+ rKsuMultiplier: number;
11
+ ksuBonusMultiplier: number;
12
+ isActive: boolean;
13
+ }
14
+ export interface LockPeriod {
15
+ rKSUMultiplier: string;
16
+ lockPeriod: string;
17
+ ksuBonusMultiplier: string;
18
+ id: string;
19
+ }
20
+ export interface UserLock {
21
+ id: BigNumber;
22
+ lockedAmount: string;
23
+ rKSUAmount: string;
24
+ rKSUtoUSDCRatio: number;
25
+ apyBonus: number;
26
+ startTime: EpochTimeStamp;
27
+ endTime: EpochTimeStamp;
28
+ lockPeriod: LockPeriod;
29
+ }
30
+ export interface UserBonusData {
31
+ ksuBonusAndRewards: string;
32
+ protocolFeesEarned: string;
33
+ ksuBonusAndRewardsLifetime: string;
34
+ totalLockedAmount: string;
35
+ }
36
+ export interface GQLGetLockingPeriods {
37
+ lockPeriods: LockPeriod[];
38
+ }
39
+ export interface GQLClaimedFeesForAddress {
40
+ userLockDepositsInfo: {
41
+ feesClaimed: string;
42
+ } | undefined;
43
+ }
44
+ export interface GQLStakedAmountForAddress {
45
+ userLockDepositsInfo: {
46
+ ksuLockedAmount: string;
47
+ } | undefined;
48
+ }
49
+ export interface GQLEarnedRKsuForAddress {
50
+ userLockDepositsInfo: {
51
+ rKSUAmount: string;
52
+ } | undefined;
53
+ }
54
+ export interface GQLTotalBonusAmountForAddress {
55
+ userLockDepositsInfo: {
56
+ totalKsuBonusAmount: string;
57
+ } | undefined;
58
+ }
59
+ export interface UserLockDepositsInfo {
60
+ feesClaimed: string;
61
+ ksuLockedAmount: string;
62
+ rKSUAmount: string;
63
+ totalKsuBonusAmount: string;
64
+ id: string;
65
+ }
66
+ export interface GQLUserLocks {
67
+ userLocks: {
68
+ id: string;
69
+ ksuAmount: string;
70
+ endTimestamp: string;
71
+ startTimestamp: string;
72
+ rKSUAmount: string;
73
+ lockPeriod: LockPeriod;
74
+ userLockDepositsInfo: UserLockDepositsInfo;
75
+ }[];
76
+ }
77
+ export interface LockingSummarySubgraphResult {
78
+ lockingSummaries: {
79
+ id: string;
80
+ totalRKsuAmount: string;
81
+ totalKsuLockedAmount: string;
82
+ totalFeesEmitted: string;
83
+ }[];
84
+ }
85
+ export interface TrancheConfigurationSubgraph {
86
+ lendingPoolTrancheConfigurations: {
87
+ id: string;
88
+ interestRate: string;
89
+ }[];
90
+ }
91
+ export interface TrancheSubgraph {
92
+ id: string;
93
+ balance: string;
94
+ }
95
+ export interface TrancheSubgraphResult {
96
+ lendingPoolTranches: TrancheSubgraph[];
97
+ }
98
+ export interface totalUserLoyaltyRewards {
99
+ user: {
100
+ id: string;
101
+ totalUserLoyaltyRewards: string;
102
+ } | null;
103
+ }
104
+ export interface SystemVariables {
105
+ systemVariables: {
106
+ id: string;
107
+ protocolFeeRate: string;
108
+ priceUpdateEpoch: string;
109
+ performanceFee: string;
110
+ loyaltyThresholds: string;
111
+ ksuEpochTokenPrice: string;
112
+ epochDuration: string;
113
+ ecosystemFeeRate: string;
114
+ clearingPeriodLength: string;
115
+ canOnlyDepositJuniorWhenUserHasRKSU: boolean;
116
+ };
117
+ }
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1,22 @@
1
+ import { Provider } from '@ethersproject/providers';
2
+ import { Signer } from 'ethers';
3
+ import { SdkConfig } from '../../sdk-config';
4
+ import { PoolOverview } from '../DataService/types';
5
+ import { PortfolioLendingPool, PortfolioRewards, PortfolioSummary, UserLendingPoolTrancheFixedTermDepositLock } from './types';
6
+ export declare class Portfolio {
7
+ private _kasuConfig;
8
+ private _lockingService;
9
+ private _dataService;
10
+ private _userLendingService;
11
+ private readonly _graph;
12
+ private _systemVariablesAbi;
13
+ private _kasuNftContract;
14
+ readonly _signerOrProvider: Signer | Provider;
15
+ constructor(_kasuConfig: SdkConfig, signerOrProvider: Signer | Provider);
16
+ getPortfolioRewards(userAddress: string): Promise<PortfolioRewards>;
17
+ getUserNfts(userAddress: string): Promise<number[]>;
18
+ getPortfolioSummary(userAddress: string, portfolioLendingPools: PortfolioLendingPool[]): Promise<PortfolioSummary>;
19
+ getPortfolioLendingData(userAddress: string, poolOverviews: PoolOverview[], currentEpoch: string, provider?: Provider): Promise<PortfolioLendingPool[]>;
20
+ precisionToString(val: number): string;
21
+ calculateFixedTermDepositEarnings(fixedTermDepositLock: UserLendingPoolTrancheFixedTermDepositLock, totalTrancheBalance: string, totalTrancheShares: string): number;
22
+ }