@evaafi/sdk 0.5.4 → 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 +24 -2
- package/dist/api/math.js +185 -50
- package/dist/api/parser.d.ts +3 -3
- package/dist/api/parser.js +32 -19
- package/dist/api/prices.d.ts +1 -1
- package/dist/api/prices.js +4 -3
- 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 +10 -12
- package/dist/contracts/UserContract.js +7 -7
- package/dist/index.d.ts +6 -5
- package/dist/index.js +5 -3
- 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 -2
- package/src/api/math.ts +234 -50
- package/src/api/parser.ts +52 -37
- package/src/api/prices.ts +2 -1
- 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 +25 -67
- package/src/contracts/UserContract.ts +8 -10
- package/src/index.ts +4 -7
- 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/src/api/parser.ts
CHANGED
|
@@ -1,16 +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
|
+
calculateMaximumWithdrawAmount,
|
|
9
|
+
calculateMaximumWithdrawAmountOld,
|
|
8
10
|
calculatePresentValue,
|
|
9
11
|
getAvailableToBorrow,
|
|
12
|
+
isV5MainPoolContract,
|
|
10
13
|
presentValue,
|
|
11
14
|
} from './math';
|
|
12
15
|
import { loadMaybeMyRef, loadMyRef } from './helpers';
|
|
13
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';
|
|
14
19
|
|
|
15
20
|
// Will be in v6
|
|
16
21
|
/* export function createUserRewards(): DictionaryValue<UserRewards> {
|
|
@@ -202,11 +207,12 @@ export function parseUserLiteData(
|
|
|
202
207
|
userDataBOC: string,
|
|
203
208
|
assetsData: ExtendedAssetsData,
|
|
204
209
|
assetsConfig: ExtendedAssetsConfig,
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
testnet: boolean = false,
|
|
208
|
-
applyDust: boolean = false
|
|
210
|
+
poolConfig: PoolConfig,
|
|
211
|
+
applyDust: boolean = true
|
|
209
212
|
): UserLiteData {
|
|
213
|
+
const poolAssetsConfig = poolConfig.poolAssetsConfig;
|
|
214
|
+
const masterConstants = poolConfig.masterConstants;
|
|
215
|
+
|
|
210
216
|
const userSlice = Cell.fromBase64(userDataBOC).beginParse();
|
|
211
217
|
|
|
212
218
|
const codeVersion = userSlice.loadCoins();
|
|
@@ -239,19 +245,25 @@ export function parseUserLiteData(
|
|
|
239
245
|
}
|
|
240
246
|
*/
|
|
241
247
|
userSlice.endParse();
|
|
242
|
-
|
|
248
|
+
const isV5Main = isV5MainPoolContract(poolConfig);
|
|
243
249
|
const userBalances = Dictionary.empty<bigint, UserBalance>();
|
|
244
250
|
|
|
245
251
|
for (const [_, asset] of Object.entries(poolAssetsConfig)) {
|
|
246
252
|
const assetData = assetsData.get(asset.assetId) as ExtendedAssetData;
|
|
247
253
|
const assetConfig = assetsConfig.get(asset.assetId) as AssetConfig;
|
|
248
|
-
let principals = principalsDict.get(asset.assetId) || 0n;
|
|
249
254
|
|
|
250
|
-
|
|
251
|
-
|
|
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
|
+
};
|
|
252
265
|
principalsDict.set(asset.assetId, 0n);
|
|
253
266
|
}
|
|
254
|
-
const balance = presentValue(assetData.sRate, assetData.bRate, principals, masterConstants);
|
|
255
267
|
userBalances.set(asset.assetId, balance);
|
|
256
268
|
}
|
|
257
269
|
|
|
@@ -279,30 +291,36 @@ export function parseUserData(
|
|
|
279
291
|
assetsData: ExtendedAssetsData,
|
|
280
292
|
assetsConfig: ExtendedAssetsConfig,
|
|
281
293
|
prices: Dictionary<bigint, bigint>,
|
|
282
|
-
|
|
283
|
-
|
|
284
|
-
applyDust: boolean = false
|
|
294
|
+
poolConfig: PoolConfig,
|
|
295
|
+
applyDust: boolean = true
|
|
285
296
|
): UserData {
|
|
297
|
+
const poolAssetsConfig = poolConfig.poolAssetsConfig;
|
|
298
|
+
const masterConstants = poolConfig.masterConstants;
|
|
299
|
+
|
|
286
300
|
const withdrawalLimits = Dictionary.empty<bigint, bigint>();
|
|
287
301
|
const borrowLimits = Dictionary.empty<bigint, bigint>();
|
|
288
302
|
|
|
289
303
|
let supplyBalance = 0n;
|
|
290
304
|
let borrowBalance = 0n;
|
|
291
|
-
|
|
305
|
+
const isV5Main = isV5MainPoolContract(poolConfig);
|
|
306
|
+
|
|
307
|
+
for (const [_, asset] of Object.entries(poolAssetsConfig)) {
|
|
292
308
|
const assetData = assetsData.get(asset.assetId) as ExtendedAssetData;
|
|
293
309
|
const assetConfig = assetsConfig.get(asset.assetId) as AssetConfig;
|
|
294
|
-
let principals = userLiteData.principals.get(asset.assetId) || 0n;
|
|
295
310
|
|
|
296
|
-
|
|
297
|
-
|
|
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;
|
|
298
317
|
userLiteData.principals.set(asset.assetId, 0n);
|
|
299
318
|
}
|
|
300
319
|
|
|
301
|
-
const balance = presentValue(assetData.sRate, assetData.bRate, principals, masterConstants);
|
|
302
320
|
userLiteData.balances.set(asset.assetId, balance);
|
|
303
321
|
}
|
|
304
322
|
|
|
305
|
-
for (const [_, asset] of Object.entries(
|
|
323
|
+
for (const [_, asset] of Object.entries(poolAssetsConfig)) {
|
|
306
324
|
const assetConfig = assetsConfig.get(asset.assetId) as AssetConfig;
|
|
307
325
|
const balance = userLiteData.balances.get(asset.assetId) as UserBalance;
|
|
308
326
|
|
|
@@ -315,27 +333,24 @@ export function parseUserData(
|
|
|
315
333
|
}
|
|
316
334
|
|
|
317
335
|
const availableToBorrow = getAvailableToBorrow(assetsConfig, assetsData, userLiteData.principals, prices, masterConstants);
|
|
318
|
-
for (const [_, asset] of Object.entries(
|
|
336
|
+
for (const [_, asset] of Object.entries(poolAssetsConfig)) {
|
|
319
337
|
const assetConfig = assetsConfig.get(asset.assetId) as AssetConfig;
|
|
320
338
|
const assetData = assetsData.get(asset.assetId) as ExtendedAssetData;
|
|
321
339
|
const balance = userLiteData.balances.get(asset.assetId) as UserBalance;
|
|
322
|
-
|
|
340
|
+
|
|
323
341
|
if (balance.type === BalanceType.supply) {
|
|
324
|
-
|
|
325
|
-
|
|
326
|
-
|
|
327
|
-
|
|
328
|
-
|
|
329
|
-
|
|
330
|
-
|
|
331
|
-
|
|
332
|
-
|
|
333
|
-
|
|
334
|
-
|
|
335
|
-
|
|
336
|
-
0n,
|
|
337
|
-
),
|
|
338
|
-
);
|
|
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
|
+
}
|
|
339
354
|
}
|
|
340
355
|
borrowLimits.set(
|
|
341
356
|
asset.assetId,
|
|
@@ -349,7 +364,7 @@ export function parseUserData(
|
|
|
349
364
|
? 0
|
|
350
365
|
: Number(BigInt(1e9) - (availableToBorrow * BigInt(1e9)) / (borrowBalance + availableToBorrow)) / 1e7;
|
|
351
366
|
|
|
352
|
-
const liquidationData = calculateLiquidationData(assetsConfig, assetsData, userLiteData.principals, prices,
|
|
367
|
+
const liquidationData = calculateLiquidationData(assetsConfig, assetsData, userLiteData.principals, prices, poolConfig);
|
|
353
368
|
const healthFactor = 1 - Number(liquidationData.totalDebt) / Number(liquidationData.totalLimit);
|
|
354
369
|
|
|
355
370
|
return {
|
package/src/api/prices.ts
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import { beginCell, Cell, Dictionary } from '@ton/core';
|
|
2
2
|
import { PriceData } from '../types/Common';
|
|
3
|
+
import { MAIN_POOL_NFT_ID } from '../constants/general';
|
|
3
4
|
|
|
4
5
|
type NftData = {
|
|
5
6
|
ledgerIndex: number;
|
|
@@ -38,7 +39,7 @@ type OutputData = {
|
|
|
38
39
|
};
|
|
39
40
|
}
|
|
40
41
|
|
|
41
|
-
export async function
|
|
42
|
+
export async function getPrices(endpoints: string[] = ["api.stardust-mainnet.iotaledger.net"], nftId: string = MAIN_POOL_NFT_ID) {
|
|
42
43
|
return await Promise.any(endpoints.map(x => loadPrices(nftId, x)));
|
|
43
44
|
}
|
|
44
45
|
|
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,
|
|
@@ -16,10 +16,10 @@ import {
|
|
|
16
16
|
import { Maybe } from '@ton/core/dist/utils/maybe';
|
|
17
17
|
import { EvaaUser } from './UserContract';
|
|
18
18
|
import { parseMasterData } from '../api/parser';
|
|
19
|
-
import { MasterData, PoolAssetConfig, PoolConfig
|
|
19
|
+
import { MasterData, PoolAssetConfig, PoolConfig} from '../types/Master';
|
|
20
20
|
import { JettonWallet } from './JettonWallet';
|
|
21
21
|
import { getUserJettonWallet } from '../utils/userJettonWallet';
|
|
22
|
-
import {
|
|
22
|
+
import { getPrices, isTonAsset, MAINNET_POOL_CONFIG } from '..';
|
|
23
23
|
|
|
24
24
|
/**
|
|
25
25
|
* Parameters for the Evaa contract
|
|
@@ -31,51 +31,26 @@ export type EvaaParameters = {
|
|
|
31
31
|
debug?: boolean;
|
|
32
32
|
};
|
|
33
33
|
|
|
34
|
-
/**
|
|
35
|
-
* Parameters for the Jetton message
|
|
36
|
-
* @property responseAddress - address to send excesses
|
|
37
|
-
* @property forwardAmount - amount to forward to the destination address
|
|
38
|
-
*/
|
|
39
|
-
export type JettonMessageParameters = {
|
|
40
|
-
responseAddress?: Address;
|
|
41
|
-
forwardAmount?: bigint;
|
|
42
|
-
};
|
|
43
|
-
|
|
44
34
|
/**
|
|
45
35
|
* Base parameters for supply
|
|
46
36
|
* @property queryID - unique query ID
|
|
47
37
|
* @property includeUserCode - true to include user code for update (needed when user contract code version is outdated)
|
|
48
38
|
* @property amount - amount to supply
|
|
49
39
|
* @property userAddress - user address
|
|
50
|
-
* @property
|
|
40
|
+
* @property asset
|
|
51
41
|
*/
|
|
52
|
-
export type
|
|
42
|
+
export type SupplyParameters = {
|
|
43
|
+
asset: PoolAssetConfig
|
|
53
44
|
queryID: bigint;
|
|
54
45
|
includeUserCode: boolean;
|
|
55
46
|
amount: bigint;
|
|
56
47
|
userAddress: Address;
|
|
48
|
+
responseAddress?: Address;
|
|
49
|
+
forwardAmount?: bigint;
|
|
57
50
|
/* Will be in v6
|
|
58
51
|
amountToTransfer: bigint;
|
|
59
52
|
payload: Cell; */
|
|
60
|
-
}
|
|
61
|
-
/**
|
|
62
|
-
* Parameters for the TON supply message
|
|
63
|
-
* @property type - 'ton'
|
|
64
|
-
*/
|
|
65
|
-
export type TonSupplyParameters = SupplyBaseParameters & {
|
|
66
|
-
asset: PoolAssetConfig;
|
|
67
|
-
};
|
|
68
|
-
/**
|
|
69
|
-
* Parameters for the jetton supply message
|
|
70
|
-
* @property type - 'jetton'
|
|
71
|
-
*/
|
|
72
|
-
export type JettonSupplyParameters = SupplyBaseParameters &
|
|
73
|
-
JettonMessageParameters & {
|
|
74
|
-
asset: PoolJettonAssetConfig & PoolAssetConfig;
|
|
75
|
-
};
|
|
76
|
-
|
|
77
|
-
export type SupplyParameters = TonSupplyParameters | JettonSupplyParameters;
|
|
78
|
-
|
|
53
|
+
}
|
|
79
54
|
|
|
80
55
|
/**
|
|
81
56
|
* Parameters for the withdraw message
|
|
@@ -124,29 +99,16 @@ export type LiquidationBaseData = {
|
|
|
124
99
|
* @property includeUserCode - true to include user code for update (needed when user contract code version is outdated)
|
|
125
100
|
* @property priceData - price data cell. Can be obtained from the getPrices function
|
|
126
101
|
*/
|
|
127
|
-
export type
|
|
102
|
+
export type LiquidationParameters = LiquidationBaseData & {
|
|
103
|
+
asset: PoolAssetConfig;
|
|
128
104
|
queryID: bigint;
|
|
129
105
|
liquidatorAddress: Address;
|
|
130
106
|
includeUserCode: boolean;
|
|
131
107
|
priceData: Cell;
|
|
108
|
+
responseAddress?: Address;
|
|
109
|
+
forwardAmount?: bigint;
|
|
132
110
|
};
|
|
133
111
|
|
|
134
|
-
/**
|
|
135
|
-
* Parameters for the TON liquidation message
|
|
136
|
-
* @property type - 'ton'
|
|
137
|
-
*/
|
|
138
|
-
export type TonLiquidationParameters = LiquidationBaseParameters & {
|
|
139
|
-
asset: PoolAssetConfig;
|
|
140
|
-
};
|
|
141
|
-
/**
|
|
142
|
-
* Parameters for the jetton liquidation message
|
|
143
|
-
* @property type - 'jetton'
|
|
144
|
-
*/
|
|
145
|
-
export type JettonLiquidationParameters = LiquidationBaseParameters &
|
|
146
|
-
JettonMessageParameters & {
|
|
147
|
-
asset: PoolAssetConfig & PoolJettonAssetConfig
|
|
148
|
-
};
|
|
149
|
-
|
|
150
112
|
/**
|
|
151
113
|
* Evaa master contract wrapper
|
|
152
114
|
*/
|
|
@@ -172,17 +134,15 @@ export class Evaa implements Contract {
|
|
|
172
134
|
* @returns supply message as a cell
|
|
173
135
|
*/
|
|
174
136
|
createSupplyMessage(parameters: SupplyParameters): Cell {
|
|
175
|
-
if (
|
|
176
|
-
const jettonParams = parameters as JettonSupplyParameters;
|
|
177
|
-
|
|
137
|
+
if (!isTonAsset(parameters.asset)) {
|
|
178
138
|
return beginCell()
|
|
179
139
|
.storeUint(OPCODES.JETTON_TRANSFER, 32)
|
|
180
140
|
.storeUint(parameters.queryID, 64)
|
|
181
141
|
.storeCoins(parameters.amount)
|
|
182
142
|
.storeAddress(this.address)
|
|
183
|
-
.storeAddress(
|
|
143
|
+
.storeAddress(parameters.responseAddress ?? parameters.userAddress)
|
|
184
144
|
.storeBit(0)
|
|
185
|
-
.storeCoins(
|
|
145
|
+
.storeCoins(parameters.forwardAmount ?? FEES.SUPPLY_JETTON_FWD)
|
|
186
146
|
.storeBit(1)
|
|
187
147
|
.storeRef(
|
|
188
148
|
beginCell()
|
|
@@ -232,18 +192,16 @@ export class Evaa implements Contract {
|
|
|
232
192
|
* Create liquidation message
|
|
233
193
|
* @returns liquidation message as a cell
|
|
234
194
|
*/
|
|
235
|
-
createLiquidationMessage(parameters:
|
|
236
|
-
if (
|
|
237
|
-
const jettonParams = parameters as JettonLiquidationParameters;
|
|
238
|
-
|
|
195
|
+
createLiquidationMessage(parameters: LiquidationParameters): Cell {
|
|
196
|
+
if (!isTonAsset(parameters.asset)) {
|
|
239
197
|
return beginCell()
|
|
240
198
|
.storeUint(OPCODES.JETTON_TRANSFER, 32)
|
|
241
199
|
.storeUint(parameters.queryID, 64)
|
|
242
200
|
.storeCoins(parameters.liquidationAmount)
|
|
243
201
|
.storeAddress(this.address)
|
|
244
|
-
.storeAddress(
|
|
202
|
+
.storeAddress(parameters.responseAddress ?? parameters.liquidatorAddress)
|
|
245
203
|
.storeBit(0)
|
|
246
|
-
.storeCoins(
|
|
204
|
+
.storeCoins(parameters.forwardAmount ?? FEES.LIQUIDATION_JETTON_FWD)
|
|
247
205
|
.storeBit(1)
|
|
248
206
|
.storeRef(
|
|
249
207
|
beginCell()
|
|
@@ -323,11 +281,11 @@ export class Evaa implements Contract {
|
|
|
323
281
|
provider: ContractProvider,
|
|
324
282
|
via: Sender,
|
|
325
283
|
value: bigint,
|
|
326
|
-
parameters:
|
|
284
|
+
parameters: SupplyParameters,
|
|
327
285
|
) {
|
|
328
286
|
const message = this.createSupplyMessage(parameters);
|
|
329
287
|
|
|
330
|
-
if (
|
|
288
|
+
if (!isTonAsset(parameters.asset)) {
|
|
331
289
|
if (!via.address) {
|
|
332
290
|
throw Error('Via address is required for jetton supply');
|
|
333
291
|
}
|
|
@@ -360,11 +318,11 @@ export class Evaa implements Contract {
|
|
|
360
318
|
provider: ContractProvider,
|
|
361
319
|
via: Sender,
|
|
362
320
|
value: bigint,
|
|
363
|
-
parameters:
|
|
321
|
+
parameters: LiquidationParameters,
|
|
364
322
|
) {
|
|
365
323
|
const message = this.createLiquidationMessage(parameters);
|
|
366
324
|
|
|
367
|
-
if (
|
|
325
|
+
if (!isTonAsset(parameters.asset)) {
|
|
368
326
|
if (!via.address) {
|
|
369
327
|
throw Error('Via address is required for jetton liquidation');
|
|
370
328
|
}
|
|
@@ -424,9 +382,9 @@ export class Evaa implements Contract {
|
|
|
424
382
|
|
|
425
383
|
async getPrices(provider: ContractProvider, endpoints?: string[]) {
|
|
426
384
|
if ((endpoints?.length ?? 0) > 0) {
|
|
427
|
-
return await
|
|
385
|
+
return await getPrices(endpoints, this.poolConfig.nftId);
|
|
428
386
|
} else {
|
|
429
|
-
return await
|
|
387
|
+
return await getPrices(undefined, this.poolConfig.nftId);
|
|
430
388
|
}
|
|
431
389
|
}
|
|
432
390
|
}
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import { Address, beginCell, Cell, Contract, ContractProvider, Dictionary, Sender, SendMode } from '@ton/core';
|
|
2
2
|
import { UserData, UserLiteData } from '../types/User';
|
|
3
3
|
import { parseUserData, parseUserLiteData } from '../api/parser';
|
|
4
|
-
import {
|
|
4
|
+
import { ExtendedAssetsConfig, ExtendedAssetsData, PoolConfig } from '../types/Master';
|
|
5
5
|
import { LiquidationBaseData } from './MasterContract';
|
|
6
6
|
import { OPCODES } from '../constants/general';
|
|
7
7
|
import { MAINNET_POOL_CONFIG } from '../constants/pools';
|
|
@@ -41,8 +41,7 @@ export class EvaaUser implements Contract {
|
|
|
41
41
|
state.data!.toString('base64'),
|
|
42
42
|
assetsData,
|
|
43
43
|
assetsConfig,
|
|
44
|
-
this.poolConfig
|
|
45
|
-
this.poolConfig.masterConstants
|
|
44
|
+
this.poolConfig
|
|
46
45
|
);
|
|
47
46
|
this.lastSync = Math.floor(Date.now() / 1000);
|
|
48
47
|
} else {
|
|
@@ -64,7 +63,7 @@ export class EvaaUser implements Contract {
|
|
|
64
63
|
prices: Dictionary<bigint, bigint>,
|
|
65
64
|
): boolean {
|
|
66
65
|
if (this._liteData) {
|
|
67
|
-
this._data = parseUserData(this._liteData, assetsData, assetsConfig, prices, this.poolConfig
|
|
66
|
+
this._data = parseUserData(this._liteData, assetsData, assetsConfig, prices, this.poolConfig);
|
|
68
67
|
return true;
|
|
69
68
|
}
|
|
70
69
|
return false;
|
|
@@ -104,10 +103,9 @@ export class EvaaUser implements Contract {
|
|
|
104
103
|
state.data!.toString('base64'),
|
|
105
104
|
assetsData,
|
|
106
105
|
assetsConfig,
|
|
107
|
-
this.poolConfig
|
|
108
|
-
this.poolConfig.masterConstants
|
|
106
|
+
this.poolConfig
|
|
109
107
|
);
|
|
110
|
-
this._data = parseUserData(this._liteData, assetsData, assetsConfig, prices, this.poolConfig
|
|
108
|
+
this._data = parseUserData(this._liteData, assetsData, assetsConfig, prices, this.poolConfig);
|
|
111
109
|
this.lastSync = Math.floor(Date.now() / 1000);
|
|
112
110
|
} else {
|
|
113
111
|
this._data = { type: 'inactive' };
|
|
@@ -153,11 +151,11 @@ export class EvaaUser implements Contract {
|
|
|
153
151
|
|
|
154
152
|
return {
|
|
155
153
|
borrowerAddress: this._data.ownerAddress,
|
|
156
|
-
loanAsset: this._data.liquidationData.greatestLoanAsset,
|
|
157
|
-
collateralAsset: this._data.liquidationData.greatestCollateralAsset,
|
|
154
|
+
loanAsset: this._data.liquidationData.greatestLoanAsset.assetId,
|
|
155
|
+
collateralAsset: this._data.liquidationData.greatestCollateralAsset.assetId,
|
|
158
156
|
minCollateralAmount: this._data.liquidationData.minCollateralAmount,
|
|
159
157
|
liquidationAmount: this._data.liquidationData.liquidationAmount,
|
|
160
|
-
tonLiquidation: this._data.liquidationData.greatestLoanAsset === TON_MAINNET.assetId,
|
|
158
|
+
tonLiquidation: this._data.liquidationData.greatestLoanAsset.assetId === TON_MAINNET.assetId,
|
|
161
159
|
};
|
|
162
160
|
}
|
|
163
161
|
}
|
package/src/index.ts
CHANGED
|
@@ -9,6 +9,7 @@ export {
|
|
|
9
9
|
calculateAssetData,
|
|
10
10
|
calculateAssetInterest,
|
|
11
11
|
getAvailableToBorrow,
|
|
12
|
+
calculateMaximumWithdrawAmount,
|
|
12
13
|
presentValue,
|
|
13
14
|
calculateLiquidationData,
|
|
14
15
|
} from './api/math';
|
|
@@ -17,19 +18,14 @@ export {
|
|
|
17
18
|
export { createAssetData, createAssetConfig, parseMasterData, parseUserData, parseUserLiteData } from './api/parser';
|
|
18
19
|
|
|
19
20
|
// Prices
|
|
20
|
-
export {
|
|
21
|
+
export { getPrices } from './api/prices';
|
|
21
22
|
|
|
22
23
|
// Contracts' wrappers
|
|
23
24
|
export { JettonWallet } from './contracts/JettonWallet';
|
|
24
25
|
export {
|
|
25
26
|
EvaaParameters,
|
|
26
|
-
JettonMessageParameters,
|
|
27
|
-
TonSupplyParameters,
|
|
28
|
-
JettonSupplyParameters,
|
|
29
27
|
WithdrawParameters,
|
|
30
28
|
LiquidationBaseData,
|
|
31
|
-
TonLiquidationParameters,
|
|
32
|
-
JettonLiquidationParameters,
|
|
33
29
|
Evaa,
|
|
34
30
|
} from './contracts/MasterContract';
|
|
35
31
|
export { EvaaUser } from './contracts/UserContract';
|
|
@@ -80,10 +76,10 @@ export {
|
|
|
80
76
|
MAINNET_POOL_CONFIG,
|
|
81
77
|
TESTNET_POOL_CONFIG,
|
|
82
78
|
MAINNET_LP_POOL_CONFIG,
|
|
83
|
-
TESTNET_LP_POOL_CONFIG
|
|
84
79
|
} from './constants/pools';
|
|
85
80
|
|
|
86
81
|
export {
|
|
82
|
+
UNDEFINED_ASSET,
|
|
87
83
|
TON_MAINNET,
|
|
88
84
|
USDT_MAINNET,
|
|
89
85
|
TONUSDT_DEDUST_MAINNET,
|
|
@@ -99,6 +95,7 @@ export {
|
|
|
99
95
|
} from './constants/assets';
|
|
100
96
|
|
|
101
97
|
export * from './constants/assets'
|
|
98
|
+
export * from './utils/utils'
|
|
102
99
|
|
|
103
100
|
// Utils
|
|
104
101
|
export { getLastSentBoc, getTonConnectSender } from './utils/tonConnectSender';
|
package/src/types/Master.ts
CHANGED
|
@@ -6,19 +6,15 @@ export type MasterConstants = {
|
|
|
6
6
|
ASSET_PRICE_SCALE: bigint,
|
|
7
7
|
ASSET_RESERVE_FACTOR_SCALE: bigint,
|
|
8
8
|
ASSET_LIQUIDATION_RESERVE_FACTOR_SCALE: bigint,
|
|
9
|
+
ASSET_LIQUIDATION_THRESHOLD_SCALE: bigint,
|
|
10
|
+
ASSET_LIQUIDATION_BONUS_SCALE: bigint,
|
|
9
11
|
ASSET_ORIGINATION_FEE_SCALE: bigint
|
|
10
12
|
};
|
|
11
13
|
|
|
12
|
-
export type PoolAssetConfig = (PoolTonAssetConfig | PoolJettonAssetConfig) & {
|
|
13
|
-
name: string;
|
|
14
|
-
};
|
|
15
14
|
export type PoolAssetsConfig = PoolAssetConfig[];
|
|
16
15
|
|
|
17
|
-
export type
|
|
18
|
-
|
|
19
|
-
}
|
|
20
|
-
|
|
21
|
-
export type PoolJettonAssetConfig = {
|
|
16
|
+
export type PoolAssetConfig = {
|
|
17
|
+
name: string;
|
|
22
18
|
assetId: bigint;
|
|
23
19
|
jettonMasterAddress: Address;
|
|
24
20
|
jettonWalletCode: Cell;
|
|
@@ -115,3 +111,8 @@ export type MasterData = {
|
|
|
115
111
|
borrow: Dictionary<bigint, number>;
|
|
116
112
|
};
|
|
117
113
|
};
|
|
114
|
+
|
|
115
|
+
export type AgregatedBalances = {
|
|
116
|
+
totalBorrow: bigint;
|
|
117
|
+
totalSupply: bigint;
|
|
118
|
+
}
|
package/src/types/User.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { Address, Cell, Dictionary } from '@ton/core';
|
|
2
|
-
import { AssetConfig, ExtendedAssetData, ExtendedAssetsConfig, ExtendedAssetsData, MasterConfig, MasterConstants } from './Master';
|
|
2
|
+
import { AssetConfig, ExtendedAssetData, ExtendedAssetsConfig, ExtendedAssetsData, MasterConfig, MasterConstants, PoolAssetConfig, PoolConfig } from './Master';
|
|
3
3
|
|
|
4
4
|
export enum BalanceType {
|
|
5
5
|
supply = 'supply',
|
|
@@ -13,9 +13,9 @@ export type UserBalance = {
|
|
|
13
13
|
|
|
14
14
|
export type UserLiqudationData = {
|
|
15
15
|
greatestCollateralValue: bigint;
|
|
16
|
-
greatestCollateralAsset:
|
|
16
|
+
greatestCollateralAsset: PoolAssetConfig;
|
|
17
17
|
greatestLoanValue: bigint;
|
|
18
|
-
greatestLoanAsset:
|
|
18
|
+
greatestLoanAsset: PoolAssetConfig;
|
|
19
19
|
totalDebt: bigint;
|
|
20
20
|
totalLimit: bigint;
|
|
21
21
|
};
|
|
@@ -86,9 +86,9 @@ export type PredictHealthFactorArgs = {
|
|
|
86
86
|
balanceChangeType: BalanceChangeType;
|
|
87
87
|
amount: bigint; // always positive
|
|
88
88
|
tokenSymbol: string;
|
|
89
|
-
|
|
89
|
+
principals: Dictionary<bigint, bigint>;
|
|
90
90
|
prices: Dictionary<bigint, bigint>;
|
|
91
91
|
assetsData: ExtendedAssetsData;
|
|
92
92
|
assetsConfig: ExtendedAssetsConfig;
|
|
93
|
-
|
|
93
|
+
poolConfig: PoolConfig;
|
|
94
94
|
};
|
|
@@ -1,7 +1,11 @@
|
|
|
1
1
|
import { Address, beginCell, Cell, storeStateInit } from '@ton/core';
|
|
2
|
-
import { PoolAssetConfig
|
|
2
|
+
import { PoolAssetConfig } from '../types/Master';
|
|
3
|
+
import { UNDEFINED_ASSET } from '../constants/assets';
|
|
3
4
|
|
|
4
|
-
export function getUserJettonWallet(ownerAddress: Address, poolAssetConfig: PoolAssetConfig
|
|
5
|
+
export function getUserJettonWallet(ownerAddress: Address, poolAssetConfig: PoolAssetConfig) {
|
|
6
|
+
if (poolAssetConfig.name == 'TON' || poolAssetConfig.assetId === UNDEFINED_ASSET.assetId) {
|
|
7
|
+
throw new Error(`Cant getUserJettonWallet for ${poolAssetConfig.name} asset`)
|
|
8
|
+
}
|
|
5
9
|
const jettonMasterAddress = poolAssetConfig.jettonMasterAddress;
|
|
6
10
|
const jettonWalletCode = poolAssetConfig.jettonWalletCode;
|
|
7
11
|
|