impermax-sdk 1.0.1 → 1.0.2

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 (209) hide show
  1. package/lib/esm/abis/ImpermaxABI.d.ts +2 -0
  2. package/lib/esm/abis/ImpermaxABI.js +3 -0
  3. package/lib/esm/abis/contracts/ClaimAggregator.json +2015 -0
  4. package/lib/esm/abis/contracts/IBorrowable.json +13207 -0
  5. package/lib/esm/abis/contracts/IClaimable.json +406 -0
  6. package/lib/esm/abis/contracts/ICollateral.json +8952 -0
  7. package/lib/esm/abis/contracts/IERC20.json +2376 -0
  8. package/lib/esm/abis/contracts/IFactory.json +3660 -0
  9. package/lib/esm/abis/contracts/IFarmingPool.json +3584 -0
  10. package/lib/esm/abis/contracts/IMerkleDistributor.json +1134 -0
  11. package/lib/esm/abis/contracts/IRouter02.json +7283 -0
  12. package/lib/esm/abis/contracts/ISimpleUniswapOracle.json +1469 -0
  13. package/lib/esm/abis/contracts/IStakedLPToken.json +7309 -0
  14. package/lib/esm/abis/contracts/IStakingRewards.json +1036 -0
  15. package/lib/esm/abis/contracts/IUniswapV2Factory.json +1690 -0
  16. package/lib/esm/abis/contracts/IUniswapV2Pair.json +6761 -0
  17. package/lib/esm/abis/contracts/ImpermaxChef.json +20945 -0
  18. package/lib/esm/config/amms.d.ts +25 -0
  19. package/lib/esm/config/amms.js +196 -0
  20. package/lib/esm/config/contracts/claim-aggregators.d.ts +2 -0
  21. package/lib/esm/config/contracts/claim-aggregators.js +18 -0
  22. package/lib/esm/config/contracts/impermax-chef.d.ts +2 -0
  23. package/lib/esm/config/contracts/impermax-chef.js +18 -0
  24. package/lib/esm/config/contracts/imxes.d.ts +2 -0
  25. package/lib/esm/config/contracts/imxes.js +18 -0
  26. package/lib/esm/config/contracts/merkle-distributors.d.ts +4 -0
  27. package/lib/esm/config/contracts/merkle-distributors.js +13 -0
  28. package/lib/esm/config/contracts/routers.d.ts +2 -0
  29. package/lib/esm/config/contracts/routers.js +38 -0
  30. package/lib/esm/config/contracts/simple-uniswap-oracles.d.ts +2 -0
  31. package/lib/esm/config/contracts/simple-uniswap-oracles.js +35 -0
  32. package/lib/esm/config/contracts/weths.d.ts +2 -0
  33. package/lib/esm/config/contracts/weths.js +19 -0
  34. package/lib/esm/config/debank-ids.d.ts +2 -0
  35. package/lib/esm/config/debank-ids.js +17 -0
  36. package/lib/esm/config/endpoints/merkle-distributors.d.ts +4 -0
  37. package/lib/esm/config/endpoints/merkle-distributors.js +13 -0
  38. package/lib/esm/config/eth.d.ts +3 -0
  39. package/lib/esm/config/eth.js +32 -0
  40. package/lib/esm/config/factories.d.ts +6 -0
  41. package/lib/esm/config/factories.js +24 -0
  42. package/lib/esm/config/farms.d.ts +59 -0
  43. package/lib/esm/config/farms.js +114 -0
  44. package/lib/esm/config/general.d.ts +3 -0
  45. package/lib/esm/config/general.js +8 -0
  46. package/lib/esm/config/subgraphs.d.ts +5 -0
  47. package/lib/esm/config/subgraphs.js +68 -0
  48. package/lib/esm/config/types.d.ts +67 -0
  49. package/lib/esm/config/types.js +54 -0
  50. package/lib/esm/impermax-router/Account.d.ts +31 -0
  51. package/lib/esm/impermax-router/Account.js +154 -0
  52. package/lib/esm/impermax-router/AccountBorrowable.d.ts +22 -0
  53. package/lib/esm/impermax-router/AccountBorrowable.js +141 -0
  54. package/lib/esm/impermax-router/AccountCollateral.d.ts +11 -0
  55. package/lib/esm/impermax-router/AccountCollateral.js +63 -0
  56. package/lib/esm/impermax-router/AccountLendingPool.d.ts +57 -0
  57. package/lib/esm/impermax-router/AccountLendingPool.js +266 -0
  58. package/lib/esm/impermax-router/AccountPoolToken.d.ts +29 -0
  59. package/lib/esm/impermax-router/AccountPoolToken.js +89 -0
  60. package/lib/esm/impermax-router/Borrowable.d.ts +27 -0
  61. package/lib/esm/impermax-router/Borrowable.js +128 -0
  62. package/lib/esm/impermax-router/Collateral.d.ts +9 -0
  63. package/lib/esm/impermax-router/Collateral.js +45 -0
  64. package/lib/esm/impermax-router/ContractsHelper.d.ts +28 -0
  65. package/lib/esm/impermax-router/ContractsHelper.js +50 -0
  66. package/lib/esm/impermax-router/ImpermaxFactory.d.ts +15 -0
  67. package/lib/esm/impermax-router/ImpermaxFactory.js +42 -0
  68. package/lib/esm/impermax-router/Interactions.d.ts +16 -0
  69. package/lib/esm/impermax-router/Interactions.js +105 -0
  70. package/lib/esm/impermax-router/InteractionsLendingPool.d.ts +43 -0
  71. package/lib/esm/impermax-router/InteractionsLendingPool.js +124 -0
  72. package/lib/esm/impermax-router/InteractionsPoolToken.d.ts +23 -0
  73. package/lib/esm/impermax-router/InteractionsPoolToken.js +194 -0
  74. package/lib/esm/impermax-router/LendingPool.d.ts +69 -0
  75. package/lib/esm/impermax-router/LendingPool.js +291 -0
  76. package/lib/esm/impermax-router/PoolToken.d.ts +36 -0
  77. package/lib/esm/impermax-router/PoolToken.js +140 -0
  78. package/lib/esm/impermax-router/index.d.ts +20 -0
  79. package/lib/esm/impermax-router/index.js +34 -0
  80. package/lib/esm/impermax-router/interfaces.d.ts +218 -0
  81. package/lib/esm/impermax-router/interfaces.js +15 -0
  82. package/{index.ts → lib/esm/index.d.ts} +1 -3
  83. package/lib/esm/index.js +10 -0
  84. package/lib/esm/subgraph/Account.d.ts +18 -0
  85. package/lib/esm/subgraph/Account.js +111 -0
  86. package/lib/esm/subgraph/AccountLendingPool.d.ts +19 -0
  87. package/lib/esm/subgraph/AccountLendingPool.js +71 -0
  88. package/lib/esm/subgraph/AccountPoolToken.d.ts +14 -0
  89. package/lib/esm/subgraph/AccountPoolToken.js +75 -0
  90. package/lib/esm/subgraph/LendingPool.d.ts +38 -0
  91. package/lib/esm/subgraph/LendingPool.js +204 -0
  92. package/lib/esm/subgraph/PoolToken.d.ts +64 -0
  93. package/lib/esm/subgraph/PoolToken.js +462 -0
  94. package/lib/esm/subgraph/PriceHelper.d.ts +21 -0
  95. package/lib/esm/subgraph/PriceHelper.js +166 -0
  96. package/lib/esm/subgraph/SolidexHelper.d.ts +16 -0
  97. package/lib/esm/subgraph/SolidexHelper.js +58 -0
  98. package/lib/esm/subgraph/index.d.ts +73 -0
  99. package/lib/esm/subgraph/index.js +185 -0
  100. package/lib/esm/subgraph/initializer.d.ts +34 -0
  101. package/lib/esm/subgraph/initializer.js +517 -0
  102. package/lib/esm/subgraph/query.d.ts +10 -0
  103. package/lib/esm/subgraph/query.js +229 -0
  104. package/lib/esm/utils/ether-utils.d.ts +5 -0
  105. package/lib/esm/utils/ether-utils.js +26 -0
  106. package/lib/esm/utils/index.d.ts +3 -0
  107. package/lib/esm/utils/index.js +16 -0
  108. package/package.json +9 -3
  109. package/.idea/impermax-sdk.iml +0 -12
  110. package/.idea/misc.xml +0 -6
  111. package/.idea/modules.xml +0 -8
  112. package/.idea/workspace.xml +0 -642
  113. package/abis/ImpermaxABI.ts +0 -3
  114. package/abis/contracts/BAllowance.json +0 -4735
  115. package/abis/contracts/BDeployer.json +0 -1195
  116. package/abis/contracts/BInterestRateModel.json +0 -10796
  117. package/abis/contracts/BSetter.json +0 -6219
  118. package/abis/contracts/BStorage.json +0 -2613
  119. package/abis/contracts/Borrowable.json +0 -19937
  120. package/abis/contracts/CDeployer.json +0 -1104
  121. package/abis/contracts/CSetter.json +0 -5094
  122. package/abis/contracts/CStorage.json +0 -516
  123. package/abis/contracts/ClaimAggregator.json +0 -2015
  124. package/abis/contracts/Collateral.json +0 -21615
  125. package/abis/contracts/ERC20.json +0 -819
  126. package/abis/contracts/Factory.json +0 -21986
  127. package/abis/contracts/FarmingPool.json +0 -8601
  128. package/abis/contracts/IBDeployer.json +0 -351
  129. package/abis/contracts/IBorrowTracker.json +0 -346
  130. package/abis/contracts/IBorrowable.json +0 -13207
  131. package/abis/contracts/ICDeployer.json +0 -294
  132. package/abis/contracts/IClaimable.json +0 -406
  133. package/abis/contracts/ICollateral.json +0 -8952
  134. package/abis/contracts/IERC20.json +0 -2376
  135. package/abis/contracts/IFactory.json +0 -3660
  136. package/abis/contracts/IFarmingPool.json +0 -3584
  137. package/abis/contracts/IImpermaxCallee.json +0 -679
  138. package/abis/contracts/IMerkleDistributor.json +0 -1134
  139. package/abis/contracts/IPoolToken.json +0 -5343
  140. package/abis/contracts/IRouter01.json +0 -6891
  141. package/abis/contracts/IRouter02.json +0 -7283
  142. package/abis/contracts/ISimpleUniswapOracle.json +0 -1469
  143. package/abis/contracts/IStakedLPToken.json +0 -7309
  144. package/abis/contracts/IStakingRewards.json +0 -1036
  145. package/abis/contracts/IUniswapV2Callee.json +0 -403
  146. package/abis/contracts/IUniswapV2ERC20.json +0 -3155
  147. package/abis/contracts/IUniswapV2Factory.json +0 -1690
  148. package/abis/contracts/IUniswapV2Pair.json +0 -6761
  149. package/abis/contracts/IWETH.json +0 -561
  150. package/abis/contracts/ImpermaxChef.json +0 -20945
  151. package/abis/contracts/ImpermaxERC20.json +0 -12095
  152. package/abis/contracts/Math.json +0 -1966
  153. package/abis/contracts/MockERC20.json +0 -8884
  154. package/abis/contracts/PoolToken.json +0 -10784
  155. package/abis/contracts/Router01.json +0 -43963
  156. package/abis/contracts/SafeMath.json +0 -6828
  157. package/abis/contracts/SimpleUniswapOracle.json +0 -9640
  158. package/abis/contracts/TransferHelper.json +0 -4875
  159. package/abis/contracts/UQ112x112.json +0 -1201
  160. package/abis/contracts/UniswapV2ERC20.json +0 -10969
  161. package/abis/contracts/UniswapV2Factory.json +0 -5521
  162. package/abis/contracts/UniswapV2Library.json +0 -13789
  163. package/abis/contracts/UniswapV2Pair.json +0 -30782
  164. package/abis/contracts/WETH9.json +0 -6613
  165. package/config/amms.ts +0 -199
  166. package/config/contracts/claim-aggregators.ts +0 -16
  167. package/config/contracts/impermax-chef.ts +0 -16
  168. package/config/contracts/imxes.ts +0 -16
  169. package/config/contracts/merkle-distributors.ts +0 -13
  170. package/config/contracts/routers.ts +0 -36
  171. package/config/contracts/simple-uniswap-oracles.ts +0 -33
  172. package/config/contracts/weths.ts +0 -18
  173. package/config/debank-ids.ts +0 -15
  174. package/config/endpoints/merkle-distributors.ts +0 -13
  175. package/config/eth.ts +0 -32
  176. package/config/factories.ts +0 -26
  177. package/config/farms.ts +0 -119
  178. package/config/general.ts +0 -8
  179. package/config/subgraphs.ts +0 -69
  180. package/config/types.ts +0 -81
  181. package/impermax-router/Account.ts +0 -123
  182. package/impermax-router/AccountBorrowable.ts +0 -110
  183. package/impermax-router/AccountCollateral.ts +0 -40
  184. package/impermax-router/AccountLendingPool.ts +0 -231
  185. package/impermax-router/AccountPoolToken.ts +0 -76
  186. package/impermax-router/Borrowable.ts +0 -86
  187. package/impermax-router/Collateral.ts +0 -26
  188. package/impermax-router/ContractsHelper.ts +0 -64
  189. package/impermax-router/ImpermaxFactory.ts +0 -47
  190. package/impermax-router/Interactions.ts +0 -94
  191. package/impermax-router/InteractionsLendingPool.ts +0 -129
  192. package/impermax-router/InteractionsPoolToken.ts +0 -187
  193. package/impermax-router/LendingPool.ts +0 -256
  194. package/impermax-router/PoolToken.ts +0 -112
  195. package/impermax-router/index.ts +0 -49
  196. package/impermax-router/interfaces.ts +0 -233
  197. package/subgraph/Account.ts +0 -93
  198. package/subgraph/AccountLendingPool.ts +0 -60
  199. package/subgraph/AccountPoolToken.ts +0 -60
  200. package/subgraph/LendingPool.ts +0 -179
  201. package/subgraph/PoolToken.ts +0 -381
  202. package/subgraph/PriceHelper.ts +0 -150
  203. package/subgraph/SolidexHelper.ts +0 -54
  204. package/subgraph/index.ts +0 -166
  205. package/subgraph/initializer.ts +0 -509
  206. package/subgraph/query.ts +0 -224
  207. package/tsconfig.json +0 -16
  208. package/utils/ether-utils.ts +0 -22
  209. package/utils/index.ts +0 -12
@@ -0,0 +1,141 @@
1
+ "use strict";
2
+ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
3
+ function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
4
+ return new (P || (P = Promise))(function (resolve, reject) {
5
+ function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
6
+ function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
7
+ function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
8
+ step((generator = generator.apply(thisArg, _arguments || [])).next());
9
+ });
10
+ };
11
+ var __importDefault = (this && this.__importDefault) || function (mod) {
12
+ return (mod && mod.__esModule) ? mod : { "default": mod };
13
+ };
14
+ Object.defineProperty(exports, "__esModule", { value: true });
15
+ const interfaces_1 = require("./interfaces");
16
+ const AccountPoolToken_1 = __importDefault(require("./AccountPoolToken"));
17
+ const types_1 = require("../config/types");
18
+ class AccountBorrowable extends AccountPoolToken_1.default {
19
+ constructor() {
20
+ super(...arguments);
21
+ this.borrowableCache = {};
22
+ }
23
+ cleanCache() {
24
+ super.cleanCache();
25
+ this.borrowableCache = {};
26
+ }
27
+ // Borrowed
28
+ initializeBorrowed() {
29
+ return __awaiter(this, void 0, void 0, function* () {
30
+ const borrowable = yield this.poolToken.getPoolToken();
31
+ const balance = yield borrowable.methods.borrowBalance(this.getAccount()).call();
32
+ const storedAmount = yield yield this.poolToken.normalize(balance);
33
+ const accrualTimestamp = yield this.getAccrualTimestamp();
34
+ const borrowRate = yield this.getBorrowRate();
35
+ return storedAmount * (1 + (Date.now() / 1000 - accrualTimestamp) * borrowRate);
36
+ });
37
+ }
38
+ getBorrowed() {
39
+ return __awaiter(this, void 0, void 0, function* () {
40
+ if (!this.borrowableCache.borrowed)
41
+ this.borrowableCache.borrowed = this.initializeBorrowed();
42
+ return this.borrowableCache.borrowed;
43
+ });
44
+ }
45
+ getBorrowedUSD() {
46
+ return __awaiter(this, void 0, void 0, function* () {
47
+ const borrowed = yield this.getBorrowed();
48
+ const tokenPrice = yield this.getTokenPriceAccurate();
49
+ return borrowed * tokenPrice;
50
+ });
51
+ }
52
+ // Max Borrowable
53
+ getMaxBorrowable() {
54
+ return __awaiter(this, void 0, void 0, function* () {
55
+ const availableCash = yield this.poolToken.getTotalBalance();
56
+ const { valueCollateral, valueA, valueB } = yield this.accountLendingPool.getValues(interfaces_1.NO_CHANGES);
57
+ const [valueBorrowed, valueOther] = this.poolToken.poolTokenType == types_1.PoolTokenType.BorrowableA ? [valueA, valueB] : [valueB, valueA];
58
+ const safetyMargin = (yield this.accountLendingPool.getSafetyMargin()) * this.getUiMargin();
59
+ const liquidationPenalty = yield this.accountLendingPool.getLiquidationPenalty();
60
+ const actualCollateral = valueCollateral / liquidationPenalty;
61
+ const totalValueBorrowable1 = (actualCollateral * Math.sqrt(safetyMargin) - valueOther) / safetyMargin;
62
+ const totalValueBorrowable2 = (actualCollateral / Math.sqrt(safetyMargin) - valueOther) * safetyMargin;
63
+ const maxValueBorrowable = Math.min(totalValueBorrowable1, totalValueBorrowable2) - valueBorrowed;
64
+ const price = yield this.poolToken.getMarketPriceDenomLP();
65
+ return Math.max(0, Math.min(availableCash, maxValueBorrowable / price));
66
+ });
67
+ }
68
+ // Farming Shares
69
+ initializeFarmingShares() {
70
+ return __awaiter(this, void 0, void 0, function* () {
71
+ if (!(yield this.poolToken.hasFarming()))
72
+ return 0;
73
+ if (yield this.poolToken.hasImpermaxChef()) {
74
+ const c = (yield this.poolToken.getImpermaxChef().methods.userInfo(yield this.poolToken.getPoolTokenAddress(), this.getAccount()).call()).shares;
75
+ return c;
76
+ }
77
+ else {
78
+ const farmingPool = yield this.poolToken.getFarmingPool();
79
+ return (yield farmingPool.methods.recipients(this.getAccount()).call()).shares;
80
+ }
81
+ });
82
+ }
83
+ getFarmingShares() {
84
+ return __awaiter(this, void 0, void 0, function* () {
85
+ if (!this.borrowableCache.farmingShares)
86
+ this.borrowableCache.farmingShares = this.initializeFarmingShares();
87
+ return this.borrowableCache.farmingShares;
88
+ });
89
+ }
90
+ // Available Reward
91
+ initializeAvailableIMXReward() {
92
+ return __awaiter(this, void 0, void 0, function* () {
93
+ if (!(yield this.poolToken.hasFarming()))
94
+ return 0;
95
+ if (yield this.poolToken.hasImpermaxChef()) {
96
+ const borrowableAddress = yield this.poolToken.getPoolTokenAddress();
97
+ try {
98
+ return (yield this.poolToken.getImpermaxChef().methods.pendingReward(borrowableAddress, this.getAccount()).call()) / 1e18;
99
+ }
100
+ catch (_a) {
101
+ console.log("Warning: pending reward negative");
102
+ return 0;
103
+ }
104
+ }
105
+ else {
106
+ const farmingPool = yield this.poolToken.getFarmingPool();
107
+ return (yield farmingPool.methods.claim().call({ from: this.getAccount() })) / 1e18;
108
+ }
109
+ });
110
+ }
111
+ getAvailableIMXReward() {
112
+ return __awaiter(this, void 0, void 0, function* () {
113
+ if (!this.borrowableCache.availableIMXReward)
114
+ this.borrowableCache.availableIMXReward = this.initializeAvailableIMXReward();
115
+ return this.borrowableCache.availableIMXReward;
116
+ });
117
+ }
118
+ initializeAvailableRewarderReward() {
119
+ return __awaiter(this, void 0, void 0, function* () {
120
+ if (!(yield this.poolToken.hasRewarder()))
121
+ return 0;
122
+ const borrowableAddress = yield this.poolToken.getPoolTokenAddress();
123
+ const rewarder = yield this.poolToken.getRewarder();
124
+ try {
125
+ return (yield rewarder.methods.pendingReward(borrowableAddress, this.getAccount()).call()) / 1e18;
126
+ }
127
+ catch (_a) {
128
+ console.log("Warning: pending reward negative");
129
+ return 0;
130
+ }
131
+ });
132
+ }
133
+ getAvailableRewarderReward() {
134
+ return __awaiter(this, void 0, void 0, function* () {
135
+ if (!this.borrowableCache.availableRewarderReward)
136
+ this.borrowableCache.availableRewarderReward = this.initializeAvailableRewarderReward();
137
+ return this.borrowableCache.availableRewarderReward;
138
+ });
139
+ }
140
+ }
141
+ exports.default = AccountBorrowable;
@@ -0,0 +1,11 @@
1
+ import AccountPoolToken from "./AccountPoolToken";
2
+ import Collateral from "./Collateral";
3
+ export default class AccountCollateral extends AccountPoolToken {
4
+ poolToken: Collateral;
5
+ collateralCache: {};
6
+ cleanCache(): void;
7
+ fixStakedLPExchangeRate(n: Promise<number>): Promise<number>;
8
+ getAvailableBalanceUSD(): Promise<number>;
9
+ getDepositedUSD(): Promise<number>;
10
+ getMaxWithdrawable(): Promise<number>;
11
+ }
@@ -0,0 +1,63 @@
1
+ "use strict";
2
+ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
3
+ function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
4
+ return new (P || (P = Promise))(function (resolve, reject) {
5
+ function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
6
+ function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
7
+ function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
8
+ step((generator = generator.apply(thisArg, _arguments || [])).next());
9
+ });
10
+ };
11
+ var __importDefault = (this && this.__importDefault) || function (mod) {
12
+ return (mod && mod.__esModule) ? mod : { "default": mod };
13
+ };
14
+ Object.defineProperty(exports, "__esModule", { value: true });
15
+ const interfaces_1 = require("./interfaces");
16
+ const AccountPoolToken_1 = __importDefault(require("./AccountPoolToken"));
17
+ class AccountCollateral extends AccountPoolToken_1.default {
18
+ constructor() {
19
+ super(...arguments);
20
+ this.collateralCache = {};
21
+ }
22
+ cleanCache() {
23
+ super.cleanCache();
24
+ this.collateralCache = {};
25
+ }
26
+ fixStakedLPExchangeRate(n) {
27
+ return __awaiter(this, void 0, void 0, function* () {
28
+ const stakedLPExchangeRate = yield this.poolToken.lendingPool.getStakedLPExchangeRate();
29
+ return (yield n) / stakedLPExchangeRate;
30
+ });
31
+ }
32
+ getAvailableBalanceUSD() {
33
+ const _super = Object.create(null, {
34
+ getAvailableBalanceUSD: { get: () => super.getAvailableBalanceUSD }
35
+ });
36
+ return __awaiter(this, void 0, void 0, function* () {
37
+ return this.fixStakedLPExchangeRate(_super.getAvailableBalanceUSD.call(this));
38
+ });
39
+ }
40
+ getDepositedUSD() {
41
+ const _super = Object.create(null, {
42
+ getDepositedUSD: { get: () => super.getDepositedUSD }
43
+ });
44
+ return __awaiter(this, void 0, void 0, function* () {
45
+ return this.fixStakedLPExchangeRate(_super.getDepositedUSD.call(this));
46
+ });
47
+ }
48
+ // Max Withdrawable
49
+ getMaxWithdrawable() {
50
+ return __awaiter(this, void 0, void 0, function* () {
51
+ const deposited = yield this.getDeposited();
52
+ const availableCash = yield this.poolToken.getTotalBalance();
53
+ const { valueCollateral, valueA, valueB } = yield this.accountLendingPool.getValues(interfaces_1.NO_CHANGES);
54
+ const safetyMargin = (yield this.accountLendingPool.getSafetyMargin()) * this.getUiMargin();
55
+ const liquidationPenalty = yield this.accountLendingPool.getLiquidationPenalty();
56
+ const actualCollateral = valueCollateral / liquidationPenalty;
57
+ const maxWithdrawable1 = (actualCollateral - (valueA + valueB * safetyMargin) / Math.sqrt(safetyMargin)) * liquidationPenalty;
58
+ const maxWithdrawable2 = (actualCollateral - (valueB + valueA * safetyMargin) / Math.sqrt(safetyMargin)) * liquidationPenalty;
59
+ return Math.max(0, Math.min(deposited, availableCash, maxWithdrawable1, maxWithdrawable2) / this.getDust());
60
+ });
61
+ }
62
+ }
63
+ exports.default = AccountCollateral;
@@ -0,0 +1,57 @@
1
+ import { Changes, PendingRewardUI, Values } from "./interfaces";
2
+ import Account from "./Account";
3
+ import AccountBorrowable from "./AccountBorrowable";
4
+ import AccountCollateral from "./AccountCollateral";
5
+ import LendingPool from "./LendingPool";
6
+ import { PoolTokenType } from '../config/types';
7
+ export default class AccountLendingPool {
8
+ account: Account;
9
+ lendingPool: LendingPool;
10
+ accountPoolTokens: {
11
+ [PoolTokenType.Collateral]: AccountCollateral;
12
+ [PoolTokenType.BorrowableA]: AccountBorrowable;
13
+ [PoolTokenType.BorrowableB]: AccountBorrowable;
14
+ };
15
+ cache: {
16
+ availableReward?: Promise<PendingRewardUI[]>;
17
+ oldAvailableReward?: Promise<number>;
18
+ };
19
+ constructor(account: Account, lendingPool: LendingPool);
20
+ cleanCache(): void;
21
+ pta: () => AccountBorrowable;
22
+ ptb: () => AccountBorrowable;
23
+ ptc: () => AccountCollateral;
24
+ getSafetyMargin: () => Promise<number>;
25
+ getLiquidationPenalty: () => Promise<number>;
26
+ getUiMargin: () => number;
27
+ getBalanceUSD(): Promise<number>;
28
+ getSuppliedUSD(): Promise<number>;
29
+ getDebtUSD(): Promise<number>;
30
+ getEquityUSD(): Promise<number>;
31
+ getLPEquityUSD(): Promise<number>;
32
+ getLPEquity(): Promise<number>;
33
+ getAccountAPR(): Promise<number>;
34
+ getValuesFromPrice(changes: Changes, priceA: number, priceB: number): Promise<{
35
+ valueCollateral: number;
36
+ valueA: number;
37
+ valueB: number;
38
+ }>;
39
+ getValues(changes: Changes): Promise<{
40
+ valueCollateral: number;
41
+ valueA: number;
42
+ valueB: number;
43
+ }>;
44
+ getMarketValues(changes: Changes): Promise<{
45
+ valueCollateral: number;
46
+ valueA: number;
47
+ valueB: number;
48
+ }>;
49
+ getLeverage(changes?: Changes): Promise<number>;
50
+ getLiquidationPriceSwingsGivenValues(values: Values): Promise<[number, number]>;
51
+ getLiquidationPricesGivenValues(values: Values): Promise<[number, number]>;
52
+ getLiquidationPriceSwings(changes?: Changes): Promise<[number, number]>;
53
+ getLiquidationPrices(changes?: Changes): Promise<[number, number]>;
54
+ getMaxLeverage(): Promise<number>;
55
+ getMaxDeleverage(slippage: number): Promise<number>;
56
+ getAvailableReward(): Promise<PendingRewardUI[]>;
57
+ }
@@ -0,0 +1,266 @@
1
+ "use strict";
2
+ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
3
+ function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
4
+ return new (P || (P = Promise))(function (resolve, reject) {
5
+ function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
6
+ function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
7
+ function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
8
+ step((generator = generator.apply(thisArg, _arguments || [])).next());
9
+ });
10
+ };
11
+ var __importDefault = (this && this.__importDefault) || function (mod) {
12
+ return (mod && mod.__esModule) ? mod : { "default": mod };
13
+ };
14
+ Object.defineProperty(exports, "__esModule", { value: true });
15
+ const interfaces_1 = require("./interfaces");
16
+ const AccountBorrowable_1 = __importDefault(require("./AccountBorrowable"));
17
+ const AccountCollateral_1 = __importDefault(require("./AccountCollateral"));
18
+ const types_1 = require("../config/types");
19
+ class AccountLendingPool {
20
+ constructor(account, lendingPool) {
21
+ this.cache = {};
22
+ // Shortcuts
23
+ this.pta = () => this.accountPoolTokens[types_1.PoolTokenType.BorrowableA];
24
+ this.ptb = () => this.accountPoolTokens[types_1.PoolTokenType.BorrowableB];
25
+ this.ptc = () => this.accountPoolTokens[types_1.PoolTokenType.Collateral];
26
+ this.getSafetyMargin = () => __awaiter(this, void 0, void 0, function* () { return (yield this.lendingPool.getSubgraphLendingPool()).getSafetyMargin(); });
27
+ this.getLiquidationPenalty = () => __awaiter(this, void 0, void 0, function* () { return (yield this.lendingPool.getSubgraphLendingPool()).getLiquidationPenalty(); });
28
+ this.getUiMargin = () => this.account.router.uiMargin;
29
+ this.account = account;
30
+ this.lendingPool = lendingPool;
31
+ this.accountPoolTokens = {
32
+ [types_1.PoolTokenType.Collateral]: new AccountCollateral_1.default(this, types_1.PoolTokenType.Collateral),
33
+ [types_1.PoolTokenType.BorrowableA]: new AccountBorrowable_1.default(this, types_1.PoolTokenType.BorrowableA),
34
+ [types_1.PoolTokenType.BorrowableB]: new AccountBorrowable_1.default(this, types_1.PoolTokenType.BorrowableB),
35
+ };
36
+ }
37
+ cleanCache() {
38
+ this.cache = {};
39
+ this.pta().cleanCache();
40
+ this.ptb().cleanCache();
41
+ this.ptc().cleanCache();
42
+ }
43
+ // Balance
44
+ getBalanceUSD() {
45
+ return __awaiter(this, void 0, void 0, function* () {
46
+ const depositedAUSD = yield this.pta().getDepositedUSD();
47
+ const depositedBUSD = yield this.ptb().getDepositedUSD();
48
+ const depositedCUSD = yield this.ptc().getDepositedUSD();
49
+ return depositedAUSD + depositedBUSD + depositedCUSD;
50
+ });
51
+ }
52
+ // Supplied
53
+ getSuppliedUSD() {
54
+ return __awaiter(this, void 0, void 0, function* () {
55
+ const depositedAUSD = yield this.pta().getDepositedUSD();
56
+ const depositedBUSD = yield this.ptb().getDepositedUSD();
57
+ return depositedAUSD + depositedBUSD;
58
+ });
59
+ }
60
+ // Debt
61
+ getDebtUSD() {
62
+ return __awaiter(this, void 0, void 0, function* () {
63
+ const borrowedAUSD = yield this.pta().getBorrowedUSD();
64
+ const borrowedBUSD = yield this.ptb().getBorrowedUSD();
65
+ return borrowedAUSD + borrowedBUSD;
66
+ });
67
+ }
68
+ // Equity
69
+ getEquityUSD() {
70
+ return __awaiter(this, void 0, void 0, function* () {
71
+ const balanceUSD = yield this.getBalanceUSD();
72
+ const debtUSD = yield this.getDebtUSD();
73
+ return balanceUSD - debtUSD;
74
+ });
75
+ }
76
+ getLPEquityUSD() {
77
+ return __awaiter(this, void 0, void 0, function* () {
78
+ const collateralUSD = yield this.ptc().getDepositedUSD();
79
+ const debtUSD = yield this.getDebtUSD();
80
+ return collateralUSD - debtUSD;
81
+ });
82
+ }
83
+ getLPEquity() {
84
+ return __awaiter(this, void 0, void 0, function* () {
85
+ const lpEquityUSD = yield this.getLPEquityUSD();
86
+ const tokenPrice = yield this.ptc().getTokenPriceAccurate();
87
+ const stakedLPExchangeRate = yield this.lendingPool.getStakedLPExchangeRate();
88
+ return lpEquityUSD / tokenPrice * stakedLPExchangeRate;
89
+ });
90
+ }
91
+ // Debt
92
+ getAccountAPR() {
93
+ return __awaiter(this, void 0, void 0, function* () {
94
+ const depositedAUSD = yield this.pta().getDepositedUSD();
95
+ const depositedBUSD = yield this.ptb().getDepositedUSD();
96
+ const totalSupplied = depositedAUSD + depositedBUSD;
97
+ const supplyAPRA = yield this.pta().getSupplyAPR();
98
+ const supplyAPRB = yield this.ptb().getSupplyAPR();
99
+ return totalSupplied > 0 ? (depositedAUSD * supplyAPRA + depositedBUSD * supplyAPRB) / totalSupplied : 0;
100
+ });
101
+ }
102
+ // Values
103
+ getValuesFromPrice(changes, priceA, priceB) {
104
+ return __awaiter(this, void 0, void 0, function* () {
105
+ const valueCollateral = (yield this.ptc().getDeposited()) + changes.changeCollateral;
106
+ const amountA = (yield this.pta().getBorrowed()) + changes.changeBorrowedA;
107
+ const amountB = (yield this.ptb().getBorrowed()) + changes.changeBorrowedB;
108
+ const valueA = amountA * priceA;
109
+ const valueB = amountB * priceB;
110
+ return {
111
+ valueCollateral: valueCollateral > 0 ? valueCollateral : 0,
112
+ valueA: valueA > 0 ? valueA : 0,
113
+ valueB: valueB > 0 ? valueB : 0,
114
+ };
115
+ });
116
+ }
117
+ getValues(changes) {
118
+ return __awaiter(this, void 0, void 0, function* () {
119
+ const [priceA, priceB] = yield this.lendingPool.getPriceDenomLP();
120
+ return this.getValuesFromPrice(changes, priceA, priceB);
121
+ });
122
+ }
123
+ getMarketValues(changes) {
124
+ return __awaiter(this, void 0, void 0, function* () {
125
+ const [priceA, priceB] = yield this.lendingPool.getMarketPriceDenomLP();
126
+ return this.getValuesFromPrice(changes, priceA, priceB);
127
+ });
128
+ }
129
+ // Leverage
130
+ getLeverage(changes) {
131
+ return __awaiter(this, void 0, void 0, function* () {
132
+ if (!changes)
133
+ changes = interfaces_1.NO_CHANGES;
134
+ const { valueCollateral, valueA, valueB } = yield this.getValues(changes);
135
+ const valueDebt = valueA + valueB;
136
+ if (valueDebt == 0)
137
+ return 1;
138
+ const equity = valueCollateral - valueDebt;
139
+ if (equity <= 0)
140
+ return Infinity;
141
+ return valueDebt / equity + 1;
142
+ });
143
+ }
144
+ // Liquidation Threshold
145
+ getLiquidationPriceSwingsGivenValues(values) {
146
+ return __awaiter(this, void 0, void 0, function* () {
147
+ if (!values)
148
+ return [Infinity, Infinity];
149
+ const { valueCollateral, valueA, valueB } = values;
150
+ if (valueA + valueB == 0)
151
+ return [Infinity, Infinity];
152
+ const safetyMargin = yield this.getSafetyMargin();
153
+ const liquidationPenalty = yield this.getLiquidationPenalty();
154
+ const actualCollateral = valueCollateral / liquidationPenalty;
155
+ const rad = Math.sqrt(Math.pow(actualCollateral, 2) - 4 * valueA * valueB);
156
+ if (!rad)
157
+ return [0, 0];
158
+ const t = (actualCollateral + rad) / (2 * Math.sqrt(safetyMargin));
159
+ let priceSwingA = Math.pow((t / valueA), 2);
160
+ let priceSwingB = Math.pow((t / valueB), 2);
161
+ return [priceSwingA, priceSwingB];
162
+ });
163
+ }
164
+ getLiquidationPricesGivenValues(values) {
165
+ return __awaiter(this, void 0, void 0, function* () {
166
+ const currentPrice = yield this.lendingPool.getTWAPPrice();
167
+ const [priceSwingA, priceSwingB] = yield this.getLiquidationPriceSwingsGivenValues(values);
168
+ return !this.account.router.priceInverted ? [currentPrice / priceSwingB, currentPrice * priceSwingA] : [currentPrice / priceSwingA, currentPrice * priceSwingB];
169
+ });
170
+ }
171
+ getLiquidationPriceSwings(changes) {
172
+ return __awaiter(this, void 0, void 0, function* () {
173
+ const values = yield this.getValues(changes);
174
+ return this.getLiquidationPriceSwingsGivenValues(values);
175
+ });
176
+ }
177
+ getLiquidationPrices(changes) {
178
+ return __awaiter(this, void 0, void 0, function* () {
179
+ if (!changes)
180
+ changes = interfaces_1.NO_CHANGES;
181
+ const values = yield this.getValues(changes);
182
+ return this.getLiquidationPricesGivenValues(values);
183
+ });
184
+ }
185
+ // Max Leverage
186
+ getMaxLeverage() {
187
+ return __awaiter(this, void 0, void 0, function* () {
188
+ const availableCashA = yield this.pta().poolToken.getTotalBalance();
189
+ const availableCashB = yield this.ptb().poolToken.getTotalBalance();
190
+ const [priceA, priceB] = yield this.lendingPool.getMarketPriceDenomLP();
191
+ const [priceATWAP,] = yield this.lendingPool.getPriceDenomLP();
192
+ const diff = priceA > priceATWAP ? priceA / priceATWAP : priceATWAP / priceA;
193
+ const adjustFactor = 1 / diff;
194
+ const availableCashValue1 = availableCashA * priceA;
195
+ const availableCashValue2 = availableCashB * priceB;
196
+ const { valueCollateral, valueA, valueB } = yield this.getValues(interfaces_1.NO_CHANGES);
197
+ const safetyMargin = (yield this.getSafetyMargin()) * this.getUiMargin();
198
+ const liquidationPenalty = yield this.getLiquidationPenalty();
199
+ const actualCollateral = valueCollateral / liquidationPenalty;
200
+ const num1 = actualCollateral * Math.sqrt(safetyMargin) - valueA * safetyMargin - valueB;
201
+ const num2 = actualCollateral * Math.sqrt(safetyMargin) - valueB * safetyMargin - valueA;
202
+ const den = safetyMargin + 1 - 2 * Math.sqrt(safetyMargin) / liquidationPenalty;
203
+ const additionalValueBorrowablePerSide = Math.min(num1 / den, num2 / den, availableCashValue1, availableCashValue2) * adjustFactor;
204
+ const valueDebt = valueA + valueB;
205
+ const equity = valueCollateral - valueDebt;
206
+ if (equity == 0)
207
+ return 1;
208
+ return (valueDebt + additionalValueBorrowablePerSide * 2) / equity + 1;
209
+ });
210
+ }
211
+ // Max Deleverage
212
+ getMaxDeleverage(slippage) {
213
+ return __awaiter(this, void 0, void 0, function* () {
214
+ const { valueCollateral, valueA, valueB } = yield this.getMarketValues(interfaces_1.NO_CHANGES);
215
+ const minRepayPerSide = valueCollateral / 2 / Math.sqrt(slippage);
216
+ if (minRepayPerSide >= valueA && minRepayPerSide >= valueB) {
217
+ return this.ptc().getDeposited();
218
+ }
219
+ if (minRepayPerSide * 2 < valueA + valueB) {
220
+ return 0;
221
+ }
222
+ return Math.min(valueA, valueB) * 2 * Math.sqrt(slippage);
223
+ });
224
+ }
225
+ // Available Reward
226
+ getAvailableReward() {
227
+ return __awaiter(this, void 0, void 0, function* () {
228
+ const result = [];
229
+ const imxAmount = (yield this.pta().getAvailableIMXReward()) + (yield this.ptb().getAvailableIMXReward());
230
+ if (imxAmount > 0)
231
+ result.push({
232
+ symbol: "IMX",
233
+ amount: imxAmount
234
+ });
235
+ const symbols = [
236
+ yield this.pta().poolToken.getRewarderTokenSymbol(),
237
+ yield this.ptb().poolToken.getRewarderTokenSymbol()
238
+ ];
239
+ const rewarderAmounts = [
240
+ yield this.pta().getAvailableRewarderReward(),
241
+ yield this.ptb().getAvailableRewarderReward()
242
+ ];
243
+ if (symbols[0] === symbols[1]) {
244
+ const rewarderAmount = rewarderAmounts[0] + rewarderAmounts[1];
245
+ if (rewarderAmount === 0)
246
+ return result;
247
+ result.push({
248
+ symbol: symbols[0],
249
+ amount: rewarderAmount
250
+ });
251
+ }
252
+ else {
253
+ for (let i = 0; i < 2; i++) {
254
+ if (rewarderAmounts[i] === 0)
255
+ continue;
256
+ result.push({
257
+ symbol: symbols[i],
258
+ amount: rewarderAmounts[i]
259
+ });
260
+ }
261
+ }
262
+ return result;
263
+ });
264
+ }
265
+ }
266
+ exports.default = AccountLendingPool;
@@ -0,0 +1,29 @@
1
+ import AccountLendingPool from "./AccountLendingPool";
2
+ import PoolToken from "./PoolToken";
3
+ import { PoolTokenType } from '../config/types';
4
+ export default class AccountPoolToken {
5
+ accountLendingPool: AccountLendingPool;
6
+ poolToken: PoolToken;
7
+ cache: {
8
+ availableBalance?: Promise<number>;
9
+ deposited?: Promise<number>;
10
+ };
11
+ constructor(accountLendingPool: AccountLendingPool, poolTokenType: PoolTokenType);
12
+ cleanCache(): void;
13
+ getAccount: () => string;
14
+ getNetwrok: () => import("../config/types").Networks;
15
+ getEth: () => any;
16
+ getDust: () => number;
17
+ getUiMargin: () => number;
18
+ getTokenPriceAccurate: () => Promise<number>;
19
+ getSupplyAPR: () => Promise<number>;
20
+ getBorrowRate: () => Promise<number>;
21
+ getAccrualTimestamp: () => Promise<number>;
22
+ private initializeAvailableBalance;
23
+ getAvailableBalance(): Promise<number>;
24
+ getAvailableBalanceUSD(): Promise<number>;
25
+ private initializeDeposited;
26
+ getDeposited(): Promise<number>;
27
+ getDepositedUSD(): Promise<number>;
28
+ getMaxWithdrawable(): Promise<number>;
29
+ }
@@ -0,0 +1,89 @@
1
+ "use strict";
2
+ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
3
+ function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
4
+ return new (P || (P = Promise))(function (resolve, reject) {
5
+ function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
6
+ function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
7
+ function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
8
+ step((generator = generator.apply(thisArg, _arguments || [])).next());
9
+ });
10
+ };
11
+ Object.defineProperty(exports, "__esModule", { value: true });
12
+ const weths_1 = require("../config/contracts/weths");
13
+ class AccountPoolToken {
14
+ constructor(accountLendingPool, poolTokenType) {
15
+ this.cache = {};
16
+ // Shortcuts
17
+ this.getAccount = () => this.accountLendingPool.account.account;
18
+ this.getNetwrok = () => this.accountLendingPool.account.router.network;
19
+ this.getEth = () => this.accountLendingPool.account.router.web3.eth;
20
+ this.getDust = () => this.accountLendingPool.account.router.dust;
21
+ this.getUiMargin = () => this.accountLendingPool.account.router.uiMargin;
22
+ this.getTokenPriceAccurate = () => __awaiter(this, void 0, void 0, function* () { return (yield this.poolToken.getSubgraphPoolToken()).getTokenPriceAccurate(); });
23
+ this.getSupplyAPR = () => __awaiter(this, void 0, void 0, function* () { return (yield this.poolToken.getSubgraphPoolToken()).getSupplyAPR(); });
24
+ this.getBorrowRate = () => __awaiter(this, void 0, void 0, function* () { return (yield this.poolToken.getSubgraphPoolToken()).getBorrowRate(); });
25
+ this.getAccrualTimestamp = () => __awaiter(this, void 0, void 0, function* () { return (yield this.poolToken.getSubgraphPoolToken()).getAccrualTimestamp(); });
26
+ this.accountLendingPool = accountLendingPool;
27
+ this.poolToken = accountLendingPool.lendingPool.poolTokens[poolTokenType];
28
+ }
29
+ cleanCache() {
30
+ this.cache = {};
31
+ }
32
+ // Available Balance
33
+ initializeAvailableBalance() {
34
+ return __awaiter(this, void 0, void 0, function* () {
35
+ const token = yield this.poolToken.getToken();
36
+ if (token._address.toLowerCase() == weths_1.WETH[this.getNetwrok()].toLowerCase()) {
37
+ return (yield this.getEth().getBalance(this.getAccount())) / 1e18 / this.getDust();
38
+ }
39
+ const balance = yield token.methods.balanceOf(this.getAccount()).call();
40
+ return (yield this.poolToken.normalize(balance)) / this.getDust();
41
+ });
42
+ }
43
+ getAvailableBalance() {
44
+ return __awaiter(this, void 0, void 0, function* () {
45
+ if (!this.cache.availableBalance)
46
+ this.cache.availableBalance = this.initializeAvailableBalance();
47
+ return this.cache.availableBalance;
48
+ });
49
+ }
50
+ getAvailableBalanceUSD() {
51
+ return __awaiter(this, void 0, void 0, function* () {
52
+ const availableBalance = yield this.getAvailableBalance();
53
+ const tokenPrice = yield this.getTokenPriceAccurate();
54
+ return availableBalance * tokenPrice;
55
+ });
56
+ }
57
+ // Deposited
58
+ initializeDeposited() {
59
+ return __awaiter(this, void 0, void 0, function* () {
60
+ const poolToken = yield this.poolToken.getPoolToken();
61
+ const exchangeRate = yield this.poolToken.getExchangeRate();
62
+ const balance = yield poolToken.methods.balanceOf(this.getAccount()).call();
63
+ return (yield this.poolToken.normalize(balance)) * exchangeRate;
64
+ });
65
+ }
66
+ getDeposited() {
67
+ return __awaiter(this, void 0, void 0, function* () {
68
+ if (!this.cache.deposited)
69
+ this.cache.deposited = this.initializeDeposited();
70
+ return this.cache.deposited;
71
+ });
72
+ }
73
+ getDepositedUSD() {
74
+ return __awaiter(this, void 0, void 0, function* () {
75
+ const deposited = yield this.getDeposited();
76
+ const tokenPrice = yield this.getTokenPriceAccurate();
77
+ return deposited * tokenPrice;
78
+ });
79
+ }
80
+ // Max Withdrawable
81
+ getMaxWithdrawable() {
82
+ return __awaiter(this, void 0, void 0, function* () {
83
+ const deposited = yield this.getDeposited();
84
+ const availableCash = yield this.poolToken.getTotalBalance();
85
+ return Math.min(deposited, availableCash) / this.getDust();
86
+ });
87
+ }
88
+ }
89
+ exports.default = AccountPoolToken;