impermax-sdk 1.0.0

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 (103) hide show
  1. package/.idea/impermax-sdk.iml +12 -0
  2. package/.idea/misc.xml +6 -0
  3. package/.idea/modules.xml +8 -0
  4. package/.idea/workspace.xml +642 -0
  5. package/abis/ImpermaxABI.ts +3 -0
  6. package/abis/contracts/BAllowance.json +4735 -0
  7. package/abis/contracts/BDeployer.json +1195 -0
  8. package/abis/contracts/BInterestRateModel.json +10796 -0
  9. package/abis/contracts/BSetter.json +6219 -0
  10. package/abis/contracts/BStorage.json +2613 -0
  11. package/abis/contracts/Borrowable.json +19937 -0
  12. package/abis/contracts/CDeployer.json +1104 -0
  13. package/abis/contracts/CSetter.json +5094 -0
  14. package/abis/contracts/CStorage.json +516 -0
  15. package/abis/contracts/ClaimAggregator.json +2015 -0
  16. package/abis/contracts/Collateral.json +21615 -0
  17. package/abis/contracts/ERC20.json +819 -0
  18. package/abis/contracts/Factory.json +21986 -0
  19. package/abis/contracts/FarmingPool.json +8601 -0
  20. package/abis/contracts/IBDeployer.json +351 -0
  21. package/abis/contracts/IBorrowTracker.json +346 -0
  22. package/abis/contracts/IBorrowable.json +13207 -0
  23. package/abis/contracts/ICDeployer.json +294 -0
  24. package/abis/contracts/IClaimable.json +406 -0
  25. package/abis/contracts/ICollateral.json +8952 -0
  26. package/abis/contracts/IERC20.json +2376 -0
  27. package/abis/contracts/IFactory.json +3660 -0
  28. package/abis/contracts/IFarmingPool.json +3584 -0
  29. package/abis/contracts/IImpermaxCallee.json +679 -0
  30. package/abis/contracts/IMerkleDistributor.json +1134 -0
  31. package/abis/contracts/IPoolToken.json +5343 -0
  32. package/abis/contracts/IRouter01.json +6891 -0
  33. package/abis/contracts/IRouter02.json +7283 -0
  34. package/abis/contracts/ISimpleUniswapOracle.json +1469 -0
  35. package/abis/contracts/IStakedLPToken.json +7309 -0
  36. package/abis/contracts/IStakingRewards.json +1036 -0
  37. package/abis/contracts/IUniswapV2Callee.json +403 -0
  38. package/abis/contracts/IUniswapV2ERC20.json +3155 -0
  39. package/abis/contracts/IUniswapV2Factory.json +1690 -0
  40. package/abis/contracts/IUniswapV2Pair.json +6761 -0
  41. package/abis/contracts/IWETH.json +561 -0
  42. package/abis/contracts/ImpermaxChef.json +20945 -0
  43. package/abis/contracts/ImpermaxERC20.json +12095 -0
  44. package/abis/contracts/Math.json +1966 -0
  45. package/abis/contracts/MockERC20.json +8884 -0
  46. package/abis/contracts/PoolToken.json +10784 -0
  47. package/abis/contracts/Router01.json +43963 -0
  48. package/abis/contracts/SafeMath.json +6828 -0
  49. package/abis/contracts/SimpleUniswapOracle.json +9640 -0
  50. package/abis/contracts/TransferHelper.json +4875 -0
  51. package/abis/contracts/UQ112x112.json +1201 -0
  52. package/abis/contracts/UniswapV2ERC20.json +10969 -0
  53. package/abis/contracts/UniswapV2Factory.json +5521 -0
  54. package/abis/contracts/UniswapV2Library.json +13789 -0
  55. package/abis/contracts/UniswapV2Pair.json +30782 -0
  56. package/abis/contracts/WETH9.json +6613 -0
  57. package/config/amms.ts +199 -0
  58. package/config/contracts/claim-aggregators.ts +16 -0
  59. package/config/contracts/impermax-chef.ts +16 -0
  60. package/config/contracts/imxes.ts +16 -0
  61. package/config/contracts/merkle-distributors.ts +13 -0
  62. package/config/contracts/routers.ts +36 -0
  63. package/config/contracts/simple-uniswap-oracles.ts +33 -0
  64. package/config/contracts/weths.ts +18 -0
  65. package/config/debank-ids.ts +15 -0
  66. package/config/endpoints/merkle-distributors.ts +13 -0
  67. package/config/eth.ts +32 -0
  68. package/config/factories.ts +26 -0
  69. package/config/farms.ts +119 -0
  70. package/config/general.ts +8 -0
  71. package/config/subgraphs.ts +69 -0
  72. package/config/types.ts +81 -0
  73. package/impermax-router/Account.ts +123 -0
  74. package/impermax-router/AccountBorrowable.ts +110 -0
  75. package/impermax-router/AccountCollateral.ts +40 -0
  76. package/impermax-router/AccountLendingPool.ts +231 -0
  77. package/impermax-router/AccountPoolToken.ts +76 -0
  78. package/impermax-router/Borrowable.ts +86 -0
  79. package/impermax-router/Collateral.ts +26 -0
  80. package/impermax-router/ContractsHelper.ts +64 -0
  81. package/impermax-router/ImpermaxFactory.ts +47 -0
  82. package/impermax-router/Interactions.ts +94 -0
  83. package/impermax-router/InteractionsLendingPool.ts +129 -0
  84. package/impermax-router/InteractionsPoolToken.ts +187 -0
  85. package/impermax-router/LendingPool.ts +256 -0
  86. package/impermax-router/PoolToken.ts +112 -0
  87. package/impermax-router/index.ts +49 -0
  88. package/impermax-router/interfaces.ts +233 -0
  89. package/index.ts +5 -0
  90. package/package.json +23 -0
  91. package/subgraph/Account.ts +93 -0
  92. package/subgraph/AccountLendingPool.ts +60 -0
  93. package/subgraph/AccountPoolToken.ts +60 -0
  94. package/subgraph/LendingPool.ts +179 -0
  95. package/subgraph/PoolToken.ts +381 -0
  96. package/subgraph/PriceHelper.ts +150 -0
  97. package/subgraph/SolidexHelper.ts +54 -0
  98. package/subgraph/index.ts +166 -0
  99. package/subgraph/initializer.ts +509 -0
  100. package/subgraph/query.ts +224 -0
  101. package/tsconfig.json +16 -0
  102. package/utils/ether-utils.ts +22 -0
  103. package/utils/index.ts +12 -0
@@ -0,0 +1,224 @@
1
+ import gql from "graphql-tag";
2
+ import { Address, Factory } from '../config/types';
3
+ import { LIQUIDATION_FEE_FACTORIES } from '../config/factories';
4
+
5
+ export function blockQuery(timestamp: number) {
6
+ return gql`{
7
+ blocks (first: 1, orderBy: timestamp, orderDirection: desc, where: { timestamp_gt: ${timestamp}, timestamp_lt: ${timestamp+600} }) {
8
+ number
9
+ }
10
+ _meta {
11
+ block {
12
+ number
13
+ }
14
+ }
15
+ }`;
16
+ }
17
+
18
+ export function lendingPoolsQuery(factory: Factory,addressesFilter: Address[]) {
19
+ const tokenStr = `{
20
+ id
21
+ symbol
22
+ name
23
+ decimals
24
+ derivedUSD
25
+ }`;
26
+ const distributorStr = `{
27
+ id
28
+ }`;
29
+ const farmingPoolStr = `{
30
+ epochAmount
31
+ epochBegin
32
+ segmentLength
33
+ vestingBegin
34
+ sharePercentage
35
+ distributor ${distributorStr}
36
+ }`;
37
+ const borrowableStr = `{
38
+ id
39
+ underlying ${tokenStr}
40
+ totalBalance
41
+ totalBorrows
42
+ borrowRate
43
+ reserveFactor
44
+ kinkBorrowRate
45
+ kinkUtilizationRate
46
+ borrowIndex
47
+ accrualTimestamp
48
+ exchangeRate
49
+ totalBalanceUSD
50
+ totalSupplyUSD
51
+ totalBorrowsUSD
52
+ farmingPool ${farmingPoolStr}
53
+ }`;
54
+ const collateralStr = `{
55
+ id
56
+ totalBalance
57
+ totalBalanceUSD
58
+ safetyMargin
59
+ liquidationIncentive
60
+ ${LIQUIDATION_FEE_FACTORIES.includes(factory) ? "liquidationFee" : ""}
61
+ exchangeRate
62
+ }`;
63
+ const pairStr = `{
64
+ factory
65
+ reserve0
66
+ reserve1
67
+ totalSupply
68
+ reserveUSD
69
+ token0Price
70
+ token1Price
71
+ derivedUSD
72
+ uniswapV2PairAddress
73
+ uniswapV2Factory
74
+ isStakedLPToken
75
+ exchangeRate
76
+ stakingRewards
77
+ masterChef
78
+ pid
79
+ stakedTotalSupply
80
+ rewards {
81
+ id
82
+ rewardRate
83
+ rewardsToken ${tokenStr}
84
+ periodFinish
85
+ }
86
+ }`;
87
+ const id_in_string = addressesFilter.length > 0 ? `where: {id_in: ["${addressesFilter.join('","').toLowerCase()}"]}` : "";
88
+ return gql`{
89
+ lendingPools(first: 1000, orderBy: totalBorrowsUSD, orderDirection: desc ${ id_in_string }) {
90
+ id
91
+ borrowable0 ${ borrowableStr }
92
+ borrowable1 ${ borrowableStr }
93
+ collateral ${ collateralStr }
94
+ pair ${ pairStr }
95
+ }
96
+ _meta {
97
+ block {
98
+ number
99
+ }
100
+ hasIndexingErrors
101
+ }
102
+ }`;
103
+ }
104
+
105
+ export function lendingPoolsPastQuery(blockNumber: number, addressesFilter: Address[]) {
106
+ const borrowablePastStr = `{
107
+ borrowIndex
108
+ accrualTimestamp
109
+ exchangeRate
110
+ }`;
111
+ const id_in_string = addressesFilter.length > 0 ? `where: {id_in: ["${addressesFilter.join('","').toLowerCase()}"]}` : "";
112
+ return gql`{
113
+ lendingPools(first: 1000, orderBy: totalBorrowsUSD, orderDirection: desc, block: {number: ${blockNumber}} ${ id_in_string }) {
114
+ id
115
+ borrowable0 ${ borrowablePastStr }
116
+ borrowable1 ${ borrowablePastStr }
117
+ }
118
+ }`;
119
+ }
120
+
121
+ export function impermaxChefQuery() {
122
+ return gql`{
123
+ impermaxChefs(first: 1) {
124
+ id
125
+ rewardToken
126
+ rewardPerSec
127
+ totalAllocPoint
128
+ }
129
+ pools(first: 1000) {
130
+ id
131
+ allocPoint
132
+ rewarder {id}
133
+ }
134
+ impermaxRewarders(first: 1000) {
135
+ rewardToken
136
+ rewardPerSec
137
+ totalAllocPoint
138
+ pools(first: 1000) {
139
+ borrowable
140
+ allocPoint
141
+ }
142
+ }
143
+ }`;
144
+ }
145
+
146
+ export function pastVolumeQuery(blockNumber: number, addressesFilter: Address[]) {
147
+ const id_in_string = addressesFilter.length > 0 ? `where: {id_in: ["${addressesFilter.join('","').toLowerCase()}"]}` : "";
148
+ return gql`{
149
+ pairs (block: {number: ${blockNumber}} ${id_in_string}) {
150
+ id
151
+ volumeUSD
152
+ }
153
+ }`;
154
+ }
155
+
156
+ export function currentVolumeAndReservesQuery(addressesFilter: Address[]) {
157
+ const id_in_string = addressesFilter.length > 0 ? `where: {id_in: ["${addressesFilter.join('","').toLowerCase()}"]}` : "";
158
+ return gql`{
159
+ pairs (${id_in_string}) {
160
+ id
161
+ reserveUSD
162
+ volumeUSD
163
+ }
164
+ }`;
165
+ }
166
+
167
+ export function tvlQuery() {
168
+ return gql`{
169
+ impermaxFactories(first: 1) {
170
+ totalBalanceUSD
171
+ totalSupplyUSD
172
+ totalBorrowsUSD
173
+ }
174
+ }`;
175
+ }
176
+
177
+ export function userQuery(account: Address) {
178
+ return gql`{
179
+ user(id: "${account.toLowerCase()}") {
180
+ collateralPositions(first:1000) {
181
+ balance
182
+ collateral {
183
+ lendingPool {
184
+ id
185
+ }
186
+ }
187
+ }
188
+ supplyPositions(first:1000) {
189
+ balance
190
+ borrowable {
191
+ underlying {
192
+ id
193
+ }
194
+ lendingPool {
195
+ id
196
+ }
197
+ }
198
+ }
199
+ borrowPositions(first:1000) {
200
+ borrowBalance
201
+ borrowIndex
202
+ borrowable {
203
+ underlying {
204
+ id
205
+ }
206
+ lendingPool {
207
+ id
208
+ }
209
+ }
210
+ }
211
+ }
212
+ }`;
213
+ }
214
+
215
+ export function ximxQuery() {
216
+ return gql`{
217
+ ximxes(first: 1) {
218
+ totalSupply
219
+ totalBalance
220
+ exchangeRate
221
+ dailyAPR
222
+ }
223
+ }`;
224
+ }
package/tsconfig.json ADDED
@@ -0,0 +1,16 @@
1
+ {
2
+ "compilerOptions": {
3
+ "outDir": "./dist/www/build",
4
+ "sourceMap": true,
5
+ "resolveJsonModule": true,
6
+ "noImplicitAny": true,
7
+ "esModuleInterop": true,
8
+ "module": "commonjs",
9
+ "target": "es6",
10
+ "jsx": "react",
11
+ "lib": ["es6", "dom", "es2017"]
12
+ },
13
+ "exclude": [
14
+ "node_modules/**/*"
15
+ ]
16
+ }
@@ -0,0 +1,22 @@
1
+ import { formatUnits, parseUnits } from 'ethers/lib/utils';
2
+ import { BigNumber } from 'ethers';
3
+
4
+ export function balanceToDecimal(s: string): string {
5
+ return formatUnits(s);
6
+ }
7
+
8
+ export function formatToDecimals(n: number, decimals: number = 2) : string {
9
+ if (n === null || n === undefined || isNaN(n)) return "-";
10
+ if (n == Infinity) return "Infinity"; //return "∞";
11
+ return (Math.round(n * (10 ** decimals)) / (10 ** decimals)).toFixed(decimals);
12
+ }
13
+
14
+ export function decimalToBalance(d: string | number, decimals = 18): BigNumber {
15
+ const n = parseFloat(d.toString());
16
+ const s = formatToDecimals(Math.max(n, 0), decimals);
17
+ return parseUnits(s, decimals);
18
+ }
19
+
20
+ export function address(n: number): string {
21
+ return `0x${n.toString(16).padStart(40, '0')}`;
22
+ }
package/utils/index.ts ADDED
@@ -0,0 +1,12 @@
1
+ export function toAPR(n: number) : number {
2
+ const SECONDS_IN_YEAR = 365 * 24 * 3600;
3
+ return n * SECONDS_IN_YEAR;
4
+ }
5
+
6
+ export function impermanentLoss(priceSwing: number) {
7
+ return Math.sqrt(priceSwing) / (priceSwing + 1) * 2;
8
+ }
9
+
10
+ export function isValidNumber(n: number) : boolean {
11
+ return !(n === null || n === undefined || isNaN(n));
12
+ }