@evaafi/sdk 0.5.5 → 0.5.6-a
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/api/math.d.ts +22 -3
- package/dist/api/math.js +141 -50
- package/dist/api/parser.d.ts +3 -3
- package/dist/api/parser.js +32 -15
- package/dist/config.d.ts +1 -0
- package/dist/config.js +4 -0
- package/dist/constants/assets.d.ts +2 -3
- package/dist/constants/assets.js +10 -2
- package/dist/constants/general.d.ts +3 -2
- package/dist/constants/general.js +6 -5
- package/dist/constants/pools.d.ts +0 -1
- package/dist/constants/pools.js +1 -14
- package/dist/constants.d.ts +4 -4
- package/dist/constants.js +15 -7
- package/dist/contracts/MasterContract.d.ts +13 -45
- package/dist/contracts/MasterContract.js +8 -10
- package/dist/contracts/UserContract.js +7 -7
- package/dist/index.d.ts +4 -3
- package/dist/index.js +3 -2
- package/dist/types/Master.d.ts +8 -7
- package/dist/types/User.d.ts +5 -5
- package/dist/utils/merkleProof.d.ts +4 -0
- package/dist/utils/merkleProof.js +108 -0
- package/dist/utils/priceUtils.d.ts +55 -0
- package/dist/utils/priceUtils.js +117 -0
- package/dist/utils/userJettonWallet.d.ts +2 -2
- package/dist/utils/userJettonWallet.js +4 -0
- package/dist/utils/utils.d.ts +2 -0
- package/dist/utils/utils.js +7 -0
- package/package.json +2 -3
- package/src/api/math.ts +169 -51
- package/src/api/parser.ts +50 -25
- package/src/constants/assets.ts +11 -3
- package/src/constants/general.ts +6 -4
- package/src/constants/pools.ts +1 -15
- package/src/contracts/MasterContract.ts +23 -65
- package/src/contracts/UserContract.ts +8 -10
- package/src/index.ts +2 -6
- package/src/types/Master.ts +9 -8
- package/src/types/User.ts +5 -5
- package/src/utils/userJettonWallet.ts +6 -2
- package/src/utils/utils.ts +6 -0
package/package.json
CHANGED
|
@@ -1,9 +1,8 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@evaafi/sdk",
|
|
3
|
-
"version": "0.5.
|
|
3
|
+
"version": "0.5.6a",
|
|
4
4
|
"description": "SDK for EVAA contracts",
|
|
5
5
|
"main": "dist/index.js",
|
|
6
|
-
"types": "src/index.ts",
|
|
7
6
|
"files": [
|
|
8
7
|
"dist",
|
|
9
8
|
"src"
|
|
@@ -21,7 +20,7 @@
|
|
|
21
20
|
"devDependencies": {
|
|
22
21
|
"@orbs-network/ton-access": "^2.3.3",
|
|
23
22
|
"@ton/core": "0.56.0",
|
|
24
|
-
"@tonconnect/sdk": "^3.0.
|
|
23
|
+
"@tonconnect/sdk": "^3.0.5",
|
|
25
24
|
"@types/jest": "^29.5.12",
|
|
26
25
|
"@types/node": "^20.10.4",
|
|
27
26
|
"crypto-js": "^4.2.0",
|
package/src/api/math.ts
CHANGED
|
@@ -1,7 +1,9 @@
|
|
|
1
|
-
import { AssetConfig, AssetData, AssetInterest, ExtendedAssetData, ExtendedAssetsConfig, ExtendedAssetsData, MasterConstants } from '../types/Master';
|
|
1
|
+
import { AgregatedBalances, AssetConfig, AssetData, AssetInterest, ExtendedAssetData, ExtendedAssetsConfig, ExtendedAssetsData, MasterConstants, PoolConfig } from '../types/Master';
|
|
2
2
|
import { Dictionary } from '@ton/core';
|
|
3
3
|
import { BalanceChangeType, BalanceType, LiquidationData, PredictHealthFactorArgs, UserBalance } from '../types/User';
|
|
4
4
|
import { sha256Hash } from '../utils/sha256BigInt';
|
|
5
|
+
import { TON_MAINNET, UNDEFINED_ASSET } from '../constants/assets';
|
|
6
|
+
import { MAINNET_POOL_CONFIG, TESTNET_POOL_CONFIG } from '..';
|
|
5
7
|
|
|
6
8
|
export function mulFactor(decimal: bigint, a: bigint, b: bigint): bigint {
|
|
7
9
|
return (a * b) / decimal;
|
|
@@ -12,8 +14,9 @@ export function mulDiv(x: bigint, y: bigint, z: bigint): bigint {
|
|
|
12
14
|
}
|
|
13
15
|
|
|
14
16
|
export function mulDivC(x: bigint, y: bigint, z: bigint): bigint {
|
|
15
|
-
const mul = x * y;
|
|
16
|
-
return mul / z + (mul % z ? 1n : 0n);
|
|
17
|
+
//const mul = x * y;
|
|
18
|
+
//return mul / z + (mul % z ? 1n : 0n);
|
|
19
|
+
return BigInt(Math.ceil(Number(x * y) / Number(z)));
|
|
17
20
|
}
|
|
18
21
|
|
|
19
22
|
export function bigIntMax(...args: bigint[]): bigint {
|
|
@@ -125,13 +128,115 @@ export function checkNotInDebtAtAll(principals: Dictionary<bigint, bigint>): boo
|
|
|
125
128
|
return principals.values().every(x => x >= 0n);
|
|
126
129
|
}
|
|
127
130
|
|
|
128
|
-
export function
|
|
131
|
+
export function getAgregatedBalances (
|
|
132
|
+
assetsData: ExtendedAssetsData,
|
|
133
|
+
assetsConfig: ExtendedAssetsConfig,
|
|
134
|
+
principals: Dictionary<bigint, bigint>,
|
|
135
|
+
prices: Dictionary<bigint, bigint>,
|
|
136
|
+
masterConstants: MasterConstants,
|
|
137
|
+
): AgregatedBalances {
|
|
138
|
+
let user_total_supply = 0n;
|
|
139
|
+
let user_total_borrow = 0n;
|
|
140
|
+
|
|
141
|
+
for (const [assetId, principal] of principals) {
|
|
142
|
+
|
|
143
|
+
if (principal) {
|
|
144
|
+
|
|
145
|
+
if (!prices.has(assetId)) {
|
|
146
|
+
return {totalSupply: 0n, totalBorrow: 0n};
|
|
147
|
+
}
|
|
148
|
+
const price = prices.get(assetId)!;
|
|
149
|
+
const assetData = assetsData.get(assetId)!;
|
|
150
|
+
const assetConfig = assetsConfig.get(assetId)!;
|
|
151
|
+
// console.log('price', price);
|
|
152
|
+
if (principal < 0) {
|
|
153
|
+
user_total_borrow += presentValue(assetData.sRate, assetData.bRate, principal, masterConstants).amount * price / 10n ** assetConfig.decimals;
|
|
154
|
+
} else {
|
|
155
|
+
user_total_supply += presentValue(assetData.sRate, assetData.bRate, principal, masterConstants).amount * price / 10n ** assetConfig.decimals;
|
|
156
|
+
}
|
|
157
|
+
// console.log('aggregated', assetId, presentValue(assetData.sRate, assetData.bRate, principal, masterConstants).type, presentValue(assetData.sRate, assetData.bRate, principal, masterConstants).amount * price / 10n ** assetConfig.decimals)
|
|
158
|
+
}
|
|
159
|
+
}
|
|
160
|
+
return {totalSupply: user_total_supply, totalBorrow: user_total_borrow};
|
|
161
|
+
}
|
|
162
|
+
|
|
163
|
+
/**
|
|
164
|
+
* @deprecated The method should be used only for main contract v5
|
|
165
|
+
*/
|
|
166
|
+
export function isV5MainPoolContract(poolConfig: PoolConfig): boolean {
|
|
167
|
+
if ((poolConfig.masterAddress == MAINNET_POOL_CONFIG.masterAddress && poolConfig.masterVersion == 5) ||
|
|
168
|
+
(poolConfig.masterAddress == TESTNET_POOL_CONFIG.masterAddress && poolConfig.masterVersion == 5)) {
|
|
169
|
+
return true;
|
|
170
|
+
} else {
|
|
171
|
+
return false;
|
|
172
|
+
}
|
|
173
|
+
}
|
|
174
|
+
|
|
175
|
+
/**
|
|
176
|
+
* @deprecated The method should be used only for main contract v5
|
|
177
|
+
*/
|
|
178
|
+
export function calculateMaximumWithdrawAmountOld(
|
|
129
179
|
assetsConfig: ExtendedAssetsConfig,
|
|
130
180
|
assetsData: ExtendedAssetsData,
|
|
131
181
|
principals: Dictionary<bigint, bigint>,
|
|
132
182
|
prices: Dictionary<bigint, bigint>,
|
|
133
183
|
masterConstants: MasterConstants,
|
|
134
|
-
assetId: bigint
|
|
184
|
+
assetId: bigint,
|
|
185
|
+
): bigint {
|
|
186
|
+
let withdrawAmountMax = 0n;
|
|
187
|
+
|
|
188
|
+
const assetConfig = assetsConfig.get(assetId) as AssetConfig;
|
|
189
|
+
const assetData = assetsData.get(assetId) as ExtendedAssetData;
|
|
190
|
+
const oldPrincipal = principals.get(assetId) as bigint;
|
|
191
|
+
const oldPresentValue = presentValue(assetData.sRate, assetData.bRate, oldPrincipal, masterConstants);
|
|
192
|
+
|
|
193
|
+
if (oldPresentValue.amount > assetConfig.dust) {
|
|
194
|
+
if(checkNotInDebtAtAll(principals)) {
|
|
195
|
+
withdrawAmountMax = oldPresentValue.amount;
|
|
196
|
+
} else {
|
|
197
|
+
if (!prices.has(assetId)) {
|
|
198
|
+
return 0n;
|
|
199
|
+
}
|
|
200
|
+
|
|
201
|
+
//const borrowable = getAvailableToBorrow(assetsConfig, assetsData, principals, prices, masterConstants);
|
|
202
|
+
const price = prices.get(assetId) as bigint;
|
|
203
|
+
const agregatedBalances = getAgregatedBalances(assetsData, assetsConfig, principals, prices, masterConstants);
|
|
204
|
+
let maxAmountToReclaim =
|
|
205
|
+
mulDiv(
|
|
206
|
+
agregatedBalances.totalSupply - mulDivC(agregatedBalances.totalBorrow, masterConstants.ASSET_COEFFICIENT_SCALE, assetConfig.collateralFactor),
|
|
207
|
+
10n**assetConfig.decimals,
|
|
208
|
+
price
|
|
209
|
+
);
|
|
210
|
+
|
|
211
|
+
withdrawAmountMax = bigIntMin(
|
|
212
|
+
maxAmountToReclaim,
|
|
213
|
+
oldPresentValue.amount
|
|
214
|
+
);
|
|
215
|
+
//console.log('agregatedBalances', agregatedBalances);
|
|
216
|
+
//console.log('masterConstants.ASSET_COEFFICIENT_SCALE', masterConstants.ASSET_COEFFICIENT_SCALE);
|
|
217
|
+
//console.log('assetConfig.collateralFactor', assetConfig.collateralFactor);
|
|
218
|
+
//console.log('sekay', maxAmountToReclaim, oldPresentValue.amount);
|
|
219
|
+
}
|
|
220
|
+
} else {
|
|
221
|
+
if (!prices.has(assetId)) {
|
|
222
|
+
return 0n;
|
|
223
|
+
}
|
|
224
|
+
|
|
225
|
+
const price = prices.get(assetId) as bigint;
|
|
226
|
+
|
|
227
|
+
return getAvailableToBorrow(assetsConfig, assetsData, principals, prices, masterConstants) * (10n ** assetConfig.decimals) / price;
|
|
228
|
+
}
|
|
229
|
+
|
|
230
|
+
return withdrawAmountMax;
|
|
231
|
+
}
|
|
232
|
+
|
|
233
|
+
export function calculateMaximumWithdrawAmount( // todo v6 ifelse dust not debt at all is fixed?
|
|
234
|
+
assetsConfig: ExtendedAssetsConfig,
|
|
235
|
+
assetsData: ExtendedAssetsData,
|
|
236
|
+
principals: Dictionary<bigint, bigint>,
|
|
237
|
+
prices: Dictionary<bigint, bigint>,
|
|
238
|
+
masterConstants: MasterConstants,
|
|
239
|
+
assetId: bigint,
|
|
135
240
|
): bigint {
|
|
136
241
|
let withdrawAmountMax = 0n;
|
|
137
242
|
|
|
@@ -152,6 +257,7 @@ export function calculateMaximumWithdrawAmount(
|
|
|
152
257
|
const price = prices.get(assetId) as bigint;
|
|
153
258
|
|
|
154
259
|
let maxAmountToReclaim = 0n;
|
|
260
|
+
|
|
155
261
|
if (assetConfig.collateralFactor == 0n) {
|
|
156
262
|
maxAmountToReclaim = oldPresentValue.amount;
|
|
157
263
|
}
|
|
@@ -230,74 +336,86 @@ export function presentValue(sRate: bigint, bRate: bigint, principalValue: bigin
|
|
|
230
336
|
}
|
|
231
337
|
}
|
|
232
338
|
|
|
339
|
+
/**
|
|
340
|
+
*
|
|
341
|
+
* @param assetsConfig
|
|
342
|
+
* @param assetsData
|
|
343
|
+
* @param principals
|
|
344
|
+
* @param prices
|
|
345
|
+
* @param poolConfig
|
|
346
|
+
* @returns can return UNDEFINED_ASSET if there are no assets
|
|
347
|
+
*/
|
|
233
348
|
export function calculateLiquidationData(
|
|
234
349
|
assetsConfig: ExtendedAssetsConfig,
|
|
235
350
|
assetsData: ExtendedAssetsData,
|
|
236
351
|
principals: Dictionary<bigint, bigint>,
|
|
237
352
|
prices: Dictionary<bigint, bigint>,
|
|
238
|
-
|
|
353
|
+
poolConfig: PoolConfig,
|
|
239
354
|
): LiquidationData {
|
|
240
|
-
let
|
|
241
|
-
let
|
|
242
|
-
let
|
|
243
|
-
let
|
|
355
|
+
let collateralValue = 0n;
|
|
356
|
+
let collateralAsset = UNDEFINED_ASSET;
|
|
357
|
+
let loanValue = 0n;
|
|
358
|
+
let loanAsset = UNDEFINED_ASSET;
|
|
244
359
|
let totalDebt = 0n;
|
|
245
360
|
let totalLimit = 0n;
|
|
246
361
|
|
|
247
|
-
for (const
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
362
|
+
for (const asset of poolConfig.poolAssetsConfig) {
|
|
363
|
+
if (!principals.has(asset.assetId)) {
|
|
364
|
+
continue;
|
|
365
|
+
}
|
|
366
|
+
const principal = principals.get(asset.assetId)!;
|
|
367
|
+
const assetConfig = assetsConfig.get(asset.assetId)!;
|
|
368
|
+
const assetData = assetsData.get(asset.assetId)!;
|
|
251
369
|
const balance =
|
|
252
370
|
principal > 0 ? (principal * assetData.sRate) / BigInt(1e12) : (principal * assetData.bRate) / BigInt(1e12);
|
|
253
371
|
if (balance > 0) {
|
|
254
|
-
const assetWorth = (balance * prices.get(
|
|
255
|
-
totalLimit += (assetWorth * assetConfig.liquidationThreshold) / masterConstants.ASSET_COEFFICIENT_SCALE;
|
|
256
|
-
if (assetWorth >
|
|
257
|
-
|
|
258
|
-
|
|
372
|
+
const assetWorth = (balance * prices.get(asset.assetId)!) / 10n ** assetConfig.decimals;
|
|
373
|
+
totalLimit += (assetWorth * assetConfig.liquidationThreshold) / poolConfig.masterConstants.ASSET_COEFFICIENT_SCALE;
|
|
374
|
+
if (assetWorth > collateralValue) {
|
|
375
|
+
collateralValue = assetWorth;
|
|
376
|
+
collateralAsset = asset;
|
|
259
377
|
}
|
|
260
378
|
} else if (balance < 0) {
|
|
261
|
-
const assetWorth = (-balance * prices.get(
|
|
379
|
+
const assetWorth = (-balance * prices.get(asset.assetId)!) / 10n ** assetConfig.decimals;
|
|
262
380
|
totalDebt += assetWorth;
|
|
263
|
-
if (assetWorth >
|
|
264
|
-
|
|
265
|
-
|
|
381
|
+
if (assetWorth > loanValue) {
|
|
382
|
+
loanValue = assetWorth;
|
|
383
|
+
loanAsset = asset;
|
|
266
384
|
}
|
|
267
385
|
}
|
|
268
386
|
}
|
|
269
387
|
|
|
270
|
-
if (totalLimit < totalDebt) {
|
|
271
|
-
const
|
|
388
|
+
if (collateralAsset.assetId !== UNDEFINED_ASSET.assetId && totalLimit < totalDebt) {
|
|
389
|
+
const loanAssetPrice = prices.get(loanAsset.assetId)!;
|
|
272
390
|
const values: bigint[] = [];
|
|
273
|
-
const
|
|
274
|
-
const
|
|
275
|
-
const liquidationBonus =
|
|
276
|
-
const
|
|
391
|
+
const collateralAssetConfig = assetsConfig.get(collateralAsset.assetId)!;
|
|
392
|
+
const loanAssetConfig = assetsConfig.get(loanAsset.assetId)!;
|
|
393
|
+
const liquidationBonus = collateralAssetConfig.liquidationBonus;
|
|
394
|
+
const loanScale = 10n ** loanAssetConfig.decimals;
|
|
277
395
|
values.push(
|
|
278
|
-
(bigIntMax(
|
|
279
|
-
|
|
280
|
-
masterConstants.ASSET_COEFFICIENT_SCALE) /
|
|
396
|
+
(bigIntMax(collateralValue / 2n, bigIntMin(collateralValue, 100_000_000_000n)) *
|
|
397
|
+
loanScale *
|
|
398
|
+
poolConfig.masterConstants.ASSET_COEFFICIENT_SCALE) /
|
|
281
399
|
liquidationBonus /
|
|
282
|
-
|
|
400
|
+
loanAssetPrice,
|
|
283
401
|
);
|
|
284
|
-
values.push((
|
|
402
|
+
values.push((loanValue * loanScale) / loanAssetPrice);
|
|
285
403
|
|
|
286
404
|
const liquidationAmount = (bigIntMin(...values) as bigint) - 5n;
|
|
287
|
-
const
|
|
288
|
-
const collateralDecimal = 10n **
|
|
405
|
+
const collateralAssetPrice: bigint = prices.get(collateralAsset.assetId)!;
|
|
406
|
+
const collateralDecimal = 10n ** collateralAssetConfig.decimals;
|
|
289
407
|
let minCollateralAmount =
|
|
290
|
-
(((liquidationAmount *
|
|
291
|
-
|
|
292
|
-
|
|
408
|
+
(((liquidationAmount * loanAssetPrice * liquidationBonus) / poolConfig.masterConstants.ASSET_LIQUIDATION_BONUS_SCALE) * collateralDecimal) /
|
|
409
|
+
collateralAssetPrice /
|
|
410
|
+
loanScale -
|
|
293
411
|
10n;
|
|
294
412
|
minCollateralAmount = (minCollateralAmount * 97n) / 100n;
|
|
295
413
|
if (minCollateralAmount / collateralDecimal >= 1n) {
|
|
296
414
|
return {
|
|
297
|
-
greatestCollateralAsset:
|
|
298
|
-
greatestCollateralValue:
|
|
299
|
-
greatestLoanAsset:
|
|
300
|
-
greatestLoanValue:
|
|
415
|
+
greatestCollateralAsset: collateralAsset,
|
|
416
|
+
greatestCollateralValue: collateralValue,
|
|
417
|
+
greatestLoanAsset: loanAsset,
|
|
418
|
+
greatestLoanValue: loanValue,
|
|
301
419
|
totalDebt,
|
|
302
420
|
totalLimit,
|
|
303
421
|
liquidable: true,
|
|
@@ -308,10 +426,10 @@ export function calculateLiquidationData(
|
|
|
308
426
|
}
|
|
309
427
|
|
|
310
428
|
return {
|
|
311
|
-
greatestCollateralAsset:
|
|
312
|
-
greatestCollateralValue:
|
|
313
|
-
greatestLoanAsset:
|
|
314
|
-
greatestLoanValue:
|
|
429
|
+
greatestCollateralAsset: collateralAsset,
|
|
430
|
+
greatestCollateralValue: collateralValue,
|
|
431
|
+
greatestLoanAsset: loanAsset,
|
|
432
|
+
greatestLoanValue: loanValue,
|
|
315
433
|
totalDebt,
|
|
316
434
|
totalLimit,
|
|
317
435
|
liquidable: false,
|
|
@@ -319,7 +437,7 @@ export function calculateLiquidationData(
|
|
|
319
437
|
}
|
|
320
438
|
|
|
321
439
|
export function predictHealthFactor(args: PredictHealthFactorArgs): number {
|
|
322
|
-
const liquidationData = calculateLiquidationData(args.assetsConfig, args.assetsData, args.
|
|
440
|
+
const liquidationData = calculateLiquidationData(args.assetsConfig, args.assetsData, args.principals, args.prices, args.poolConfig);
|
|
323
441
|
const tokenHash = sha256Hash(args.tokenSymbol);
|
|
324
442
|
|
|
325
443
|
const assetConfig = args.assetsConfig.get(tokenHash)!;
|
|
@@ -337,13 +455,13 @@ export function predictHealthFactor(args: PredictHealthFactorArgs): number {
|
|
|
337
455
|
|
|
338
456
|
if (currentAmount != null && currentAmount != 0n) {
|
|
339
457
|
if (changeType == BalanceChangeType.Borrow) {
|
|
340
|
-
totalBorrow += currentBalance * (1 + Number(assetConfig.originationFee) / Number(args.masterConstants.ASSET_ORIGINATION_FEE_SCALE));
|
|
458
|
+
totalBorrow += currentBalance * (1 + Number(assetConfig.originationFee) / Number(args.poolConfig.masterConstants.ASSET_ORIGINATION_FEE_SCALE));
|
|
341
459
|
} else if (changeType == BalanceChangeType.Repay) {
|
|
342
460
|
totalBorrow -= currentBalance;
|
|
343
461
|
} else if (changeType == BalanceChangeType.Withdraw) {
|
|
344
|
-
totalLimit -= currentBalance * Number(assetConfig.liquidationThreshold) / Number(args.masterConstants.ASSET_COEFFICIENT_SCALE);
|
|
462
|
+
totalLimit -= currentBalance * Number(assetConfig.liquidationThreshold) / Number(args.poolConfig.masterConstants.ASSET_COEFFICIENT_SCALE);
|
|
345
463
|
} else if (changeType == BalanceChangeType.Supply) {
|
|
346
|
-
totalLimit += currentBalance * Number(assetConfig.liquidationThreshold) / Number(args.masterConstants.ASSET_COEFFICIENT_SCALE);
|
|
464
|
+
totalLimit += currentBalance * Number(assetConfig.liquidationThreshold) / Number(args.poolConfig.masterConstants.ASSET_COEFFICIENT_SCALE);
|
|
347
465
|
}
|
|
348
466
|
}
|
|
349
467
|
if (Number(totalLimit) == 0) {
|
package/src/api/parser.ts
CHANGED
|
@@ -1,17 +1,21 @@
|
|
|
1
1
|
import { beginCell, Cell, Dictionary, DictionaryValue, Slice } from '@ton/core';
|
|
2
|
-
import { AssetConfig, AssetData, ExtendedAssetData, ExtendedAssetsConfig, ExtendedAssetsData, MasterConfig, MasterConstants, MasterData, PoolAssetsConfig } from '../types/Master';
|
|
2
|
+
import { AssetConfig, AssetData, ExtendedAssetData, ExtendedAssetsConfig, ExtendedAssetsData, MasterConfig, MasterConstants, MasterData, PoolAssetsConfig, PoolConfig } from '../types/Master';
|
|
3
3
|
import {
|
|
4
4
|
bigIntMax,
|
|
5
5
|
bigIntMin,
|
|
6
6
|
calculateAssetData,
|
|
7
7
|
calculateLiquidationData,
|
|
8
8
|
calculateMaximumWithdrawAmount,
|
|
9
|
+
calculateMaximumWithdrawAmountOld,
|
|
9
10
|
calculatePresentValue,
|
|
10
11
|
getAvailableToBorrow,
|
|
12
|
+
isV5MainPoolContract,
|
|
11
13
|
presentValue,
|
|
12
14
|
} from './math';
|
|
13
15
|
import { loadMaybeMyRef, loadMyRef } from './helpers';
|
|
14
16
|
import { BalanceType, UserBalance, UserData, UserLiteData, UserRewards } from '../types/User';
|
|
17
|
+
import { MAINNET_POOL_CONFIG, TESTNET_POOL_CONFIG } from '../constants/pools';
|
|
18
|
+
import { basename } from 'path';
|
|
15
19
|
|
|
16
20
|
// Will be in v6
|
|
17
21
|
/* export function createUserRewards(): DictionaryValue<UserRewards> {
|
|
@@ -203,11 +207,12 @@ export function parseUserLiteData(
|
|
|
203
207
|
userDataBOC: string,
|
|
204
208
|
assetsData: ExtendedAssetsData,
|
|
205
209
|
assetsConfig: ExtendedAssetsConfig,
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
testnet: boolean = false,
|
|
209
|
-
applyDust: boolean = false
|
|
210
|
+
poolConfig: PoolConfig,
|
|
211
|
+
applyDust: boolean = true
|
|
210
212
|
): UserLiteData {
|
|
213
|
+
const poolAssetsConfig = poolConfig.poolAssetsConfig;
|
|
214
|
+
const masterConstants = poolConfig.masterConstants;
|
|
215
|
+
|
|
211
216
|
const userSlice = Cell.fromBase64(userDataBOC).beginParse();
|
|
212
217
|
|
|
213
218
|
const codeVersion = userSlice.loadCoins();
|
|
@@ -240,17 +245,23 @@ export function parseUserLiteData(
|
|
|
240
245
|
}
|
|
241
246
|
*/
|
|
242
247
|
userSlice.endParse();
|
|
243
|
-
|
|
248
|
+
const isV5Main = isV5MainPoolContract(poolConfig);
|
|
244
249
|
const userBalances = Dictionary.empty<bigint, UserBalance>();
|
|
245
250
|
|
|
246
251
|
for (const [_, asset] of Object.entries(poolAssetsConfig)) {
|
|
247
252
|
const assetData = assetsData.get(asset.assetId) as ExtendedAssetData;
|
|
248
253
|
const assetConfig = assetsConfig.get(asset.assetId) as AssetConfig;
|
|
249
|
-
let principals = principalsDict.get(asset.assetId) || 0n;
|
|
250
|
-
const balance = presentValue(assetData.sRate, assetData.bRate, principals, masterConstants);
|
|
251
254
|
|
|
252
|
-
|
|
253
|
-
|
|
255
|
+
let principal = principalsDict.get(asset.assetId) || 0n;
|
|
256
|
+
let balance = presentValue(assetData.sRate, assetData.bRate, principal, masterConstants);
|
|
257
|
+
let leftBorder = isV5Main ? balance.amount : principal;
|
|
258
|
+
|
|
259
|
+
if (applyDust && (principal > 0 && (leftBorder < assetConfig.dust))) { // v6 will be abs(principals) < dust
|
|
260
|
+
principal = 0n;
|
|
261
|
+
balance = {
|
|
262
|
+
amount: 0n,
|
|
263
|
+
type: BalanceType.supply,
|
|
264
|
+
};
|
|
254
265
|
principalsDict.set(asset.assetId, 0n);
|
|
255
266
|
}
|
|
256
267
|
userBalances.set(asset.assetId, balance);
|
|
@@ -280,30 +291,36 @@ export function parseUserData(
|
|
|
280
291
|
assetsData: ExtendedAssetsData,
|
|
281
292
|
assetsConfig: ExtendedAssetsConfig,
|
|
282
293
|
prices: Dictionary<bigint, bigint>,
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
applyDust: boolean = false
|
|
294
|
+
poolConfig: PoolConfig,
|
|
295
|
+
applyDust: boolean = true
|
|
286
296
|
): UserData {
|
|
297
|
+
const poolAssetsConfig = poolConfig.poolAssetsConfig;
|
|
298
|
+
const masterConstants = poolConfig.masterConstants;
|
|
299
|
+
|
|
287
300
|
const withdrawalLimits = Dictionary.empty<bigint, bigint>();
|
|
288
301
|
const borrowLimits = Dictionary.empty<bigint, bigint>();
|
|
289
302
|
|
|
290
303
|
let supplyBalance = 0n;
|
|
291
304
|
let borrowBalance = 0n;
|
|
292
|
-
|
|
305
|
+
const isV5Main = isV5MainPoolContract(poolConfig);
|
|
306
|
+
|
|
307
|
+
for (const [_, asset] of Object.entries(poolAssetsConfig)) {
|
|
293
308
|
const assetData = assetsData.get(asset.assetId) as ExtendedAssetData;
|
|
294
309
|
const assetConfig = assetsConfig.get(asset.assetId) as AssetConfig;
|
|
295
|
-
let principals = userLiteData.principals.get(asset.assetId) || 0n;
|
|
296
|
-
const balance = presentValue(assetData.sRate, assetData.bRate, principals, masterConstants);
|
|
297
310
|
|
|
298
|
-
|
|
299
|
-
|
|
311
|
+
let principal = userLiteData.principals.get(asset.assetId) || 0n;
|
|
312
|
+
const balance = presentValue(assetData.sRate, assetData.bRate, principal, masterConstants);
|
|
313
|
+
let leftBorder = isV5Main ? balance.amount : principal;
|
|
314
|
+
|
|
315
|
+
if (applyDust && (principal > 0 && (leftBorder < assetConfig.dust))) { // v6 will be abs(principals) < dust
|
|
316
|
+
principal = 0n;
|
|
300
317
|
userLiteData.principals.set(asset.assetId, 0n);
|
|
301
318
|
}
|
|
302
319
|
|
|
303
320
|
userLiteData.balances.set(asset.assetId, balance);
|
|
304
321
|
}
|
|
305
322
|
|
|
306
|
-
for (const [_, asset] of Object.entries(
|
|
323
|
+
for (const [_, asset] of Object.entries(poolAssetsConfig)) {
|
|
307
324
|
const assetConfig = assetsConfig.get(asset.assetId) as AssetConfig;
|
|
308
325
|
const balance = userLiteData.balances.get(asset.assetId) as UserBalance;
|
|
309
326
|
|
|
@@ -316,16 +333,24 @@ export function parseUserData(
|
|
|
316
333
|
}
|
|
317
334
|
|
|
318
335
|
const availableToBorrow = getAvailableToBorrow(assetsConfig, assetsData, userLiteData.principals, prices, masterConstants);
|
|
319
|
-
for (const [_, asset] of Object.entries(
|
|
336
|
+
for (const [_, asset] of Object.entries(poolAssetsConfig)) {
|
|
320
337
|
const assetConfig = assetsConfig.get(asset.assetId) as AssetConfig;
|
|
321
338
|
const assetData = assetsData.get(asset.assetId) as ExtendedAssetData;
|
|
322
339
|
const balance = userLiteData.balances.get(asset.assetId) as UserBalance;
|
|
323
340
|
|
|
324
341
|
if (balance.type === BalanceType.supply) {
|
|
325
|
-
|
|
326
|
-
|
|
327
|
-
|
|
328
|
-
|
|
342
|
+
if (isV5MainPoolContract(poolConfig)) {
|
|
343
|
+
withdrawalLimits.set(
|
|
344
|
+
asset.assetId,
|
|
345
|
+
calculateMaximumWithdrawAmountOld(assetsConfig, assetsData, userLiteData.principals, prices, masterConstants, asset.assetId)
|
|
346
|
+
);
|
|
347
|
+
}
|
|
348
|
+
else {
|
|
349
|
+
withdrawalLimits.set(
|
|
350
|
+
asset.assetId,
|
|
351
|
+
calculateMaximumWithdrawAmount(assetsConfig, assetsData, userLiteData.principals, prices, masterConstants, asset.assetId)
|
|
352
|
+
);
|
|
353
|
+
}
|
|
329
354
|
}
|
|
330
355
|
borrowLimits.set(
|
|
331
356
|
asset.assetId,
|
|
@@ -339,7 +364,7 @@ export function parseUserData(
|
|
|
339
364
|
? 0
|
|
340
365
|
: Number(BigInt(1e9) - (availableToBorrow * BigInt(1e9)) / (borrowBalance + availableToBorrow)) / 1e7;
|
|
341
366
|
|
|
342
|
-
const liquidationData = calculateLiquidationData(assetsConfig, assetsData, userLiteData.principals, prices,
|
|
367
|
+
const liquidationData = calculateLiquidationData(assetsConfig, assetsData, userLiteData.principals, prices, poolConfig);
|
|
343
368
|
const healthFactor = 1 - Number(liquidationData.totalDebt) / Number(liquidationData.totalLimit);
|
|
344
369
|
|
|
345
370
|
return {
|
package/src/constants/assets.ts
CHANGED
|
@@ -1,13 +1,21 @@
|
|
|
1
1
|
import { Address, Cell } from "@ton/core";
|
|
2
2
|
import { PoolAssetConfig } from "../types/Master";
|
|
3
3
|
import { sha256Hash } from "../utils/sha256BigInt";
|
|
4
|
-
import { JETTON_WALLET_STANDART_CODE, JETTON_WALLET_STANDART_CODE_TESTNET } from "./general";
|
|
4
|
+
import { JETTON_WALLET_STANDART_CODE, JETTON_WALLET_STANDART_CODE_TESTNET, NULL_ADDRESS } from "./general";
|
|
5
|
+
|
|
6
|
+
export const UNDEFINED_ASSET: PoolAssetConfig = {
|
|
7
|
+
name: 'undefined_asset',
|
|
8
|
+
assetId: -1n,
|
|
9
|
+
jettonMasterAddress: NULL_ADDRESS, // fake
|
|
10
|
+
jettonWalletCode: Cell.EMPTY
|
|
11
|
+
}
|
|
5
12
|
|
|
6
13
|
export const TON_MAINNET: PoolAssetConfig = {
|
|
7
14
|
name: 'TON',
|
|
8
|
-
assetId: sha256Hash('TON')
|
|
15
|
+
assetId: sha256Hash('TON'),
|
|
16
|
+
jettonMasterAddress: NULL_ADDRESS, // fake
|
|
17
|
+
jettonWalletCode: Cell.EMPTY
|
|
9
18
|
}
|
|
10
|
-
|
|
11
19
|
export const TON_TESTNET = TON_MAINNET;
|
|
12
20
|
|
|
13
21
|
export const JUSDT_MAINNET: PoolAssetConfig = {
|
package/src/constants/general.ts
CHANGED
|
@@ -7,15 +7,17 @@ export const MASTER_CONSTANTS = {
|
|
|
7
7
|
ASSET_PRICE_SCALE: BigInt(1e9),
|
|
8
8
|
ASSET_RESERVE_FACTOR_SCALE: 10000n,
|
|
9
9
|
ASSET_LIQUIDATION_RESERVE_FACTOR_SCALE: 10000n,
|
|
10
|
-
ASSET_ORIGINATION_FEE_SCALE: BigInt(1e9)
|
|
10
|
+
ASSET_ORIGINATION_FEE_SCALE: BigInt(1e9),
|
|
11
|
+
ASSET_LIQUIDATION_THRESHOLD_SCALE: 10_000n,
|
|
12
|
+
ASSET_LIQUIDATION_BONUS_SCALE: 10_000n,
|
|
11
13
|
};
|
|
12
14
|
|
|
15
|
+
export const NULL_ADDRESS = Address.parse('UQAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAJKZ');
|
|
16
|
+
|
|
13
17
|
export const EVAA_MASTER_MAINNET = Address.parse('EQC8rUZqR_pWV1BylWUlPNBzyiTYVoBEmQkMIQDZXICfnuRr');
|
|
14
18
|
export const MAINNET_VERSION = 5;
|
|
15
|
-
export const EVAA_MASTER_TESTNET = Address.parse('EQCoIxsE8m0Q_Ui9uM-2RPtVWXqHK0ttuW2Mccuaj4FfdkLl');
|
|
19
|
+
export const EVAA_MASTER_TESTNET = Address.parse('EQCoIxsE8m0Q_Ui9uM-2RPtVWXqHK0ttuW2Mccuaj4FfdkLl'); // EQBghPVKxgauOyrcyNYNwE2MRRnebaNpDGpVDQLbml_LIXnK
|
|
16
20
|
export const TESTNET_VERSION = 5;
|
|
17
|
-
export const EVAA_LP_TESTNET = Address.parse('EQBghPVKxgauOyrcyNYNwE2MRRnebaNpDGpVDQLbml_LIXnK');
|
|
18
|
-
export const EVAA_LP_TESTNET_VERSION = 5;
|
|
19
21
|
export const EVAA_LP_MAINNET = Address.parse('EQBIlZX2URWkXCSg3QF2MJZU-wC5XkBoLww-hdWk2G37Jc6N');
|
|
20
22
|
export const EVAA_LP_MAINNET_VERSION = 0;
|
|
21
23
|
|
package/src/constants/pools.ts
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import { Address } from "@ton/core";
|
|
2
2
|
import { JUSDC_MAINNET, JUSDC_TESTNET, JUSDT_MAINNET, JUSDT_TESTNET, STTON_MAINNET, STTON_TESTNET, TON_MAINNET, TON_STORM_MAINNET, TONUSDT_DEDUST_MAINNET, TSTON_MAINNET, USDT_MAINNET, USDT_STORM_MAINNET } from "./assets";
|
|
3
3
|
import { PoolConfig } from "../types/Master";
|
|
4
|
-
import { EVAA_MASTER_MAINNET, EVAA_MASTER_TESTNET, LENDING_CODE, MAINNET_VERSION, MASTER_CONSTANTS, MAIN_POOL_NFT_ID, TESTNET_VERSION, LP_POOL_NFT_ID,
|
|
4
|
+
import { EVAA_MASTER_MAINNET, EVAA_MASTER_TESTNET, LENDING_CODE, MAINNET_VERSION, MASTER_CONSTANTS, MAIN_POOL_NFT_ID, TESTNET_VERSION, LP_POOL_NFT_ID, EVAA_LP_MAINNET, EVAA_LP_MAINNET_VERSION } from "./general";
|
|
5
5
|
|
|
6
6
|
export const MAINNET_POOL_CONFIG: PoolConfig = {
|
|
7
7
|
masterAddress: EVAA_MASTER_MAINNET,
|
|
@@ -33,20 +33,6 @@ export const TESTNET_POOL_CONFIG: PoolConfig = {
|
|
|
33
33
|
lendingCode: LENDING_CODE
|
|
34
34
|
};
|
|
35
35
|
|
|
36
|
-
export const TESTNET_LP_POOL_CONFIG: PoolConfig = {
|
|
37
|
-
masterAddress: EVAA_LP_TESTNET,
|
|
38
|
-
masterVersion: EVAA_LP_TESTNET_VERSION,
|
|
39
|
-
masterConstants: MASTER_CONSTANTS,
|
|
40
|
-
nftId: LP_POOL_NFT_ID,
|
|
41
|
-
poolAssetsConfig: [
|
|
42
|
-
TON_MAINNET,
|
|
43
|
-
JUSDT_TESTNET,
|
|
44
|
-
JUSDC_TESTNET,
|
|
45
|
-
STTON_TESTNET
|
|
46
|
-
],
|
|
47
|
-
lendingCode: LENDING_CODE
|
|
48
|
-
};
|
|
49
|
-
|
|
50
36
|
export const MAINNET_LP_POOL_CONFIG: PoolConfig = {
|
|
51
37
|
masterAddress: EVAA_LP_MAINNET,
|
|
52
38
|
masterVersion: EVAA_LP_MAINNET_VERSION,
|