@evaafi/sdk 0.6.0-a → 0.6.1-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/liquidation.d.ts +136 -0
- package/dist/api/liquidation.js +252 -0
- package/dist/api/math.d.ts +34 -7
- package/dist/api/math.js +94 -30
- package/dist/api/parser.js +4 -9
- package/dist/constants/assets.d.ts +12 -0
- package/dist/constants/assets.js +25 -13
- package/dist/constants/general.d.ts +4 -1
- package/dist/constants/general.js +10 -9
- package/dist/constants/pools.js +2 -2
- package/dist/contracts/MasterContract.d.ts +5 -1
- package/dist/contracts/MasterContract.js +15 -9
- package/dist/contracts/UserContract.d.ts +1 -0
- package/dist/contracts/UserContract.js +1 -0
- package/dist/index.d.ts +5 -4
- package/dist/index.js +20 -2
- package/dist/types/Master.d.ts +4 -1
- package/dist/types/User.d.ts +7 -0
- package/dist/utils/utils.d.ts +1 -0
- package/dist/utils/utils.js +6 -1
- package/package.json +2 -1
- package/src/api/liquidation.ts +346 -0
- package/src/api/math.ts +166 -75
- package/src/api/parser.ts +4 -9
- package/src/constants/assets.ts +26 -12
- package/src/constants/general.ts +12 -9
- package/src/constants/pools.ts +2 -2
- package/src/contracts/MasterContract.ts +18 -11
- package/src/contracts/UserContract.ts +1 -0
- package/src/index.ts +30 -6
- package/src/types/Master.ts +5 -2
- package/src/types/User.ts +8 -0
- package/src/utils/utils.ts +4 -0
|
@@ -0,0 +1,136 @@
|
|
|
1
|
+
import { Dictionary } from '@ton/core';
|
|
2
|
+
import { AssetConfig, AssetData, ExtendedAssetsConfig, ExtendedAssetsData, MasterConstants, PoolAssetConfig, PoolConfig } from '../types/Master';
|
|
3
|
+
import { UserBalance } from '../types/User';
|
|
4
|
+
export declare function findAssetById(assetId: bigint, poolConfig: PoolConfig): PoolAssetConfig | undefined;
|
|
5
|
+
export type AssetsValues = {
|
|
6
|
+
loanAssets: Dictionary<bigint, bigint>;
|
|
7
|
+
collateralAssets: Dictionary<bigint, bigint>;
|
|
8
|
+
};
|
|
9
|
+
export type SelectedAssets = {
|
|
10
|
+
selectedLoanId: bigint;
|
|
11
|
+
selectedCollateralId: bigint;
|
|
12
|
+
selectedLoanValue?: bigint;
|
|
13
|
+
selectedCollateralValue?: bigint;
|
|
14
|
+
};
|
|
15
|
+
export declare function calculateAssetsValues(principalsDict: Dictionary<bigint, bigint>, pricesDict: Dictionary<bigint, bigint>, assetsConfigDict: ExtendedAssetsConfig, assetsDataDict: ExtendedAssetsData, poolConfig: PoolConfig): AssetsValues;
|
|
16
|
+
export declare function selectGreatestAssets(principalsDict: Dictionary<bigint, bigint>, pricesDict: Dictionary<bigint, bigint>, assetsConfigDict: ExtendedAssetsConfig, assetsDataDict: ExtendedAssetsData, poolConfig: PoolConfig): SelectedAssets;
|
|
17
|
+
/**
|
|
18
|
+
* This function shows how to calculate min collateral amount value.
|
|
19
|
+
* when liquidator has not enough of loan asset to cover the full loan.
|
|
20
|
+
* @param transferredAmount amount of loan asset liquidator want to transfer.
|
|
21
|
+
* @param maxLiquidationAmount max liquidation amount value calculated.
|
|
22
|
+
* @param maxCollateralReward max collateral reward amount calculated.
|
|
23
|
+
* @returns minCollateralAmount value for safe liquidation.
|
|
24
|
+
*/
|
|
25
|
+
export declare function calculateMinCollateralByTransferredAmount(transferredAmount: bigint, maxLiquidationAmount: bigint, maxCollateralReward: bigint): bigint;
|
|
26
|
+
/**
|
|
27
|
+
* Calculates liquidation amount and corresponding collateral amount
|
|
28
|
+
* @param supplyAmount user total supply worth amount
|
|
29
|
+
* @param borrowAmount user total borrow worth amount
|
|
30
|
+
* @param masterConstants evaa master contract constants
|
|
31
|
+
* @param loanAsset loan asset pool config
|
|
32
|
+
* @param collateralAsset collateral asset pool config
|
|
33
|
+
* @param principalsDict user principals
|
|
34
|
+
* @param assetsDataDict assets data collection
|
|
35
|
+
* @param assetsConfigDict assets config collection
|
|
36
|
+
* @param pricesDict assets prices
|
|
37
|
+
* @returns maxLiquidationAmount max loan asset amount to transfer
|
|
38
|
+
* @returns maxCollateralRewardAmount max collateral reward amount, which can be obtained
|
|
39
|
+
*/
|
|
40
|
+
export declare function calculateLiquidationAmounts(loanAsset: PoolAssetConfig, collateralAsset: PoolAssetConfig, supplyAmount: bigint, borrowAmount: bigint, // from calculate health params
|
|
41
|
+
principalsDict: Dictionary<bigint, bigint>, pricesDict: Dictionary<bigint, bigint>, assetsDataDict: ExtendedAssetsData, assetsConfigDict: ExtendedAssetsConfig, masterConstants: MasterConstants): {
|
|
42
|
+
maxLiquidationAmount: bigint;
|
|
43
|
+
maxCollateralRewardAmount: bigint;
|
|
44
|
+
};
|
|
45
|
+
/**
|
|
46
|
+
* Check if the user is subject to liquidation
|
|
47
|
+
* @param args
|
|
48
|
+
*/
|
|
49
|
+
export declare function isLiquidatable(args: {
|
|
50
|
+
assetsData: ExtendedAssetsData;
|
|
51
|
+
assetsConfig: ExtendedAssetsConfig;
|
|
52
|
+
principals: Dictionary<bigint, bigint>;
|
|
53
|
+
prices: Dictionary<bigint, bigint>;
|
|
54
|
+
poolConfig: PoolConfig;
|
|
55
|
+
}): boolean;
|
|
56
|
+
/**
|
|
57
|
+
* Determines if the provided pair of assets forms a bad debt.
|
|
58
|
+
* @param totalSupply total supply worth amount
|
|
59
|
+
* @param totalBorrow total borrow worth amount
|
|
60
|
+
* @param liquidationBonus collateral liquidation bonus value
|
|
61
|
+
* @param masterConstants pool constants
|
|
62
|
+
*/
|
|
63
|
+
export declare function isBadDebt(totalSupply: bigint, totalBorrow: bigint, liquidationBonus: bigint, masterConstants: MasterConstants): boolean;
|
|
64
|
+
/**
|
|
65
|
+
* Adds reserve to liquidation amount
|
|
66
|
+
* @param amount raw liquidation amount
|
|
67
|
+
* @param reserveFactor asset reserve factor
|
|
68
|
+
* @param factorScale asset reserve factor scale
|
|
69
|
+
* @returns liquidation amount with reserve
|
|
70
|
+
*/
|
|
71
|
+
export declare function addReserve(amount: bigint, reserveFactor: bigint, factorScale: bigint): bigint;
|
|
72
|
+
/**
|
|
73
|
+
* Deducts reserve from liquidation amount
|
|
74
|
+
* @param amount liquidation amount with reserve
|
|
75
|
+
* @param reserveFactor asset reserve factor
|
|
76
|
+
* @param reserveFactorScale asset reserve factor scale
|
|
77
|
+
* @returns liquidation amount without reserve
|
|
78
|
+
*/
|
|
79
|
+
export declare function deductReserve(amount: bigint, reserveFactor: bigint, reserveFactorScale: bigint): bigint;
|
|
80
|
+
/**
|
|
81
|
+
* Converts worth value to specified asset amount
|
|
82
|
+
* @param worth worth value (decimals = 9)
|
|
83
|
+
* @param scale asset scale (10^asset_decimals)
|
|
84
|
+
* @param price asset price
|
|
85
|
+
*/
|
|
86
|
+
export declare function toAssetAmount(worth: bigint, scale: bigint, price: bigint): bigint;
|
|
87
|
+
/**
|
|
88
|
+
* Converts calculates worth value of specified asset amount
|
|
89
|
+
* @param amount worth value (decimals = 9)
|
|
90
|
+
* @param scale asset scale (10^asset_decimals)
|
|
91
|
+
* @param price asset price
|
|
92
|
+
*/
|
|
93
|
+
export declare function toAssetWorth(amount: bigint, scale: bigint, price: bigint): bigint;
|
|
94
|
+
/**
|
|
95
|
+
* Adds liquidation bonus to reward value.
|
|
96
|
+
* @param value reward asset value
|
|
97
|
+
* @param bonus liquidation bonus factor
|
|
98
|
+
* @param scale liquidation bonus scale
|
|
99
|
+
*/
|
|
100
|
+
export declare function addLiquidationBonus(value: bigint, bonus: bigint, scale: bigint): bigint;
|
|
101
|
+
/**
|
|
102
|
+
* Deducts liquidation bonus from reward value.
|
|
103
|
+
* @param value reward asset value
|
|
104
|
+
* @param bonus liquidation bonus factor
|
|
105
|
+
* @param scale liquidation bonus scale
|
|
106
|
+
*/
|
|
107
|
+
export declare function deductLiquidationBonus(value: bigint, bonus: bigint, scale: bigint): bigint;
|
|
108
|
+
export type SuccessType = {
|
|
109
|
+
ok: true;
|
|
110
|
+
};
|
|
111
|
+
export type FailType = {
|
|
112
|
+
ok: false;
|
|
113
|
+
};
|
|
114
|
+
export type PreparedAssetInfo = {
|
|
115
|
+
config: AssetConfig;
|
|
116
|
+
data: AssetData;
|
|
117
|
+
price: bigint;
|
|
118
|
+
principal: bigint;
|
|
119
|
+
scale: bigint;
|
|
120
|
+
liquidationReserveFactor: bigint;
|
|
121
|
+
liquidationBonus: bigint;
|
|
122
|
+
present: UserBalance;
|
|
123
|
+
balance: bigint;
|
|
124
|
+
dust: bigint;
|
|
125
|
+
};
|
|
126
|
+
export type PreparedAssetInfoResult = (PreparedAssetInfo & SuccessType) | FailType;
|
|
127
|
+
/**
|
|
128
|
+
* Prepares extended asset info
|
|
129
|
+
* @param assetId
|
|
130
|
+
* @param assetsConfigDict
|
|
131
|
+
* @param assetsDataDict
|
|
132
|
+
* @param pricesDict
|
|
133
|
+
* @param principalsDict
|
|
134
|
+
* @param masterConstants
|
|
135
|
+
*/
|
|
136
|
+
export declare function prepareAssetInfo(assetId: bigint, assetsConfigDict: ExtendedAssetsConfig, assetsDataDict: ExtendedAssetsData, pricesDict: Dictionary<bigint, bigint>, principalsDict: Dictionary<bigint, bigint>, masterConstants: MasterConstants): PreparedAssetInfoResult;
|
|
@@ -0,0 +1,252 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.prepareAssetInfo = exports.deductLiquidationBonus = exports.addLiquidationBonus = exports.toAssetWorth = exports.toAssetAmount = exports.deductReserve = exports.addReserve = exports.isBadDebt = exports.isLiquidatable = exports.calculateLiquidationAmounts = exports.calculateMinCollateralByTransferredAmount = exports.selectGreatestAssets = exports.calculateAssetsValues = exports.findAssetById = void 0;
|
|
4
|
+
const core_1 = require("@ton/core");
|
|
5
|
+
const math_1 = require("./math");
|
|
6
|
+
const User_1 = require("../types/User");
|
|
7
|
+
function findAssetById(assetId, poolConfig) {
|
|
8
|
+
return poolConfig.poolAssetsConfig.find(asset => asset.assetId === assetId);
|
|
9
|
+
}
|
|
10
|
+
exports.findAssetById = findAssetById;
|
|
11
|
+
function calculateAssetsValues(principalsDict, pricesDict, assetsConfigDict, assetsDataDict, poolConfig) {
|
|
12
|
+
const loanAssets = core_1.Dictionary.empty();
|
|
13
|
+
const collateralAssets = core_1.Dictionary.empty();
|
|
14
|
+
for (const asset of poolConfig.poolAssetsConfig) {
|
|
15
|
+
const assetId = asset.assetId;
|
|
16
|
+
if (!principalsDict.has(assetId)) {
|
|
17
|
+
continue;
|
|
18
|
+
}
|
|
19
|
+
const assetPrincipal = principalsDict.get(assetId);
|
|
20
|
+
if (!pricesDict.has(assetId)) {
|
|
21
|
+
console.warn(`No price for asset ${asset.name}`);
|
|
22
|
+
continue;
|
|
23
|
+
}
|
|
24
|
+
const assetPrice = pricesDict.get(assetId);
|
|
25
|
+
if (!assetsDataDict.has(assetId)) {
|
|
26
|
+
console.warn(`Dynamics for assetId ${assetId} is not defined, skipping`);
|
|
27
|
+
continue;
|
|
28
|
+
}
|
|
29
|
+
const assetData = assetsDataDict.get(assetId);
|
|
30
|
+
if (!assetsConfigDict.has(assetId)) {
|
|
31
|
+
console.warn(`Config for assetId ${assetId} is not defined, skipping`);
|
|
32
|
+
continue;
|
|
33
|
+
}
|
|
34
|
+
const assetConfig = assetsConfigDict.get(assetId);
|
|
35
|
+
const assetScale = 10n ** assetConfig.decimals;
|
|
36
|
+
const { sRate, bRate } = assetData;
|
|
37
|
+
const assetPresent = (0, math_1.presentValue)(sRate, bRate, assetPrincipal, poolConfig.masterConstants);
|
|
38
|
+
const assetValue = assetPresent.amount * assetPrice / assetScale;
|
|
39
|
+
if (assetPresent.type === User_1.BalanceType.borrow) {
|
|
40
|
+
loanAssets.set(assetId, assetValue);
|
|
41
|
+
}
|
|
42
|
+
else {
|
|
43
|
+
collateralAssets.set(assetId, assetValue);
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
return { loanAssets, collateralAssets };
|
|
47
|
+
}
|
|
48
|
+
exports.calculateAssetsValues = calculateAssetsValues;
|
|
49
|
+
function selectGreatestAssets(principalsDict, pricesDict, assetsConfigDict, assetsDataDict, poolConfig) {
|
|
50
|
+
let maxLoanId = 0n;
|
|
51
|
+
let maxLoanValue = 0n;
|
|
52
|
+
let maxCollateralId = 0n;
|
|
53
|
+
let maxCollateralValue = 0n;
|
|
54
|
+
const assetsValues = calculateAssetsValues(principalsDict, pricesDict, assetsConfigDict, assetsDataDict, poolConfig);
|
|
55
|
+
for (const [loanId, loanValue] of assetsValues.loanAssets) {
|
|
56
|
+
if (loanValue > maxLoanValue) {
|
|
57
|
+
maxLoanId = loanId;
|
|
58
|
+
maxLoanValue = loanValue;
|
|
59
|
+
}
|
|
60
|
+
}
|
|
61
|
+
for (const [collateralId, collateralValue] of assetsValues.collateralAssets) {
|
|
62
|
+
if (collateralValue > maxCollateralValue) {
|
|
63
|
+
maxCollateralValue = collateralValue;
|
|
64
|
+
maxCollateralId = collateralId;
|
|
65
|
+
}
|
|
66
|
+
}
|
|
67
|
+
return {
|
|
68
|
+
selectedLoanId: maxLoanId,
|
|
69
|
+
selectedLoanValue: maxLoanValue,
|
|
70
|
+
selectedCollateralId: maxCollateralId,
|
|
71
|
+
selectedCollateralValue: maxCollateralValue
|
|
72
|
+
};
|
|
73
|
+
}
|
|
74
|
+
exports.selectGreatestAssets = selectGreatestAssets;
|
|
75
|
+
/**
|
|
76
|
+
* This function shows how to calculate min collateral amount value.
|
|
77
|
+
* when liquidator has not enough of loan asset to cover the full loan.
|
|
78
|
+
* @param transferredAmount amount of loan asset liquidator want to transfer.
|
|
79
|
+
* @param maxLiquidationAmount max liquidation amount value calculated.
|
|
80
|
+
* @param maxCollateralReward max collateral reward amount calculated.
|
|
81
|
+
* @returns minCollateralAmount value for safe liquidation.
|
|
82
|
+
*/
|
|
83
|
+
function calculateMinCollateralByTransferredAmount(transferredAmount, maxLiquidationAmount, maxCollateralReward) {
|
|
84
|
+
if (maxLiquidationAmount === 0n) {
|
|
85
|
+
return 0n;
|
|
86
|
+
}
|
|
87
|
+
return maxCollateralReward * transferredAmount / maxLiquidationAmount;
|
|
88
|
+
}
|
|
89
|
+
exports.calculateMinCollateralByTransferredAmount = calculateMinCollateralByTransferredAmount;
|
|
90
|
+
/**
|
|
91
|
+
* Calculates liquidation amount and corresponding collateral amount
|
|
92
|
+
* @param supplyAmount user total supply worth amount
|
|
93
|
+
* @param borrowAmount user total borrow worth amount
|
|
94
|
+
* @param masterConstants evaa master contract constants
|
|
95
|
+
* @param loanAsset loan asset pool config
|
|
96
|
+
* @param collateralAsset collateral asset pool config
|
|
97
|
+
* @param principalsDict user principals
|
|
98
|
+
* @param assetsDataDict assets data collection
|
|
99
|
+
* @param assetsConfigDict assets config collection
|
|
100
|
+
* @param pricesDict assets prices
|
|
101
|
+
* @returns maxLiquidationAmount max loan asset amount to transfer
|
|
102
|
+
* @returns maxCollateralRewardAmount max collateral reward amount, which can be obtained
|
|
103
|
+
*/
|
|
104
|
+
function calculateLiquidationAmounts(loanAsset, collateralAsset, supplyAmount, borrowAmount, // from calculate health params
|
|
105
|
+
principalsDict, pricesDict, assetsDataDict, assetsConfigDict, masterConstants) {
|
|
106
|
+
const loanInfo = prepareAssetInfo(loanAsset.assetId, assetsConfigDict, assetsDataDict, pricesDict, principalsDict, masterConstants);
|
|
107
|
+
const collateralInfo = prepareAssetInfo(collateralAsset.assetId, assetsConfigDict, assetsDataDict, pricesDict, principalsDict, masterConstants);
|
|
108
|
+
if (!loanInfo.ok || !collateralInfo.ok ||
|
|
109
|
+
loanInfo.present.type !== User_1.BalanceType.borrow ||
|
|
110
|
+
collateralInfo.present.type !== User_1.BalanceType.supply) {
|
|
111
|
+
return { maxLiquidationAmount: 0n, maxCollateralRewardAmount: 0n };
|
|
112
|
+
}
|
|
113
|
+
const liquidationBonusScale = masterConstants.ASSET_LIQUIDATION_BONUS_SCALE;
|
|
114
|
+
const collateralThreshold = masterConstants.COLLATERAL_WORTH_THRESHOLD; // basically 100$ worth (100*10^9)
|
|
115
|
+
const reserveFactorScale = masterConstants.ASSET_RESERVE_FACTOR_SCALE;
|
|
116
|
+
const reserveFactor = loanInfo.liquidationReserveFactor;
|
|
117
|
+
const liquidationBonus = collateralInfo.liquidationBonus;
|
|
118
|
+
let allowedCollateralValue = toAssetWorth(collateralInfo.balance, collateralInfo.scale, collateralInfo.price);
|
|
119
|
+
const _isBadDebt = isBadDebt(supplyAmount, borrowAmount, liquidationBonus, masterConstants);
|
|
120
|
+
if (!_isBadDebt) {
|
|
121
|
+
allowedCollateralValue = math_1.BigMath.min(allowedCollateralValue, math_1.BigMath.max(allowedCollateralValue / 2n, collateralThreshold));
|
|
122
|
+
}
|
|
123
|
+
const loanValue = toAssetWorth(loanInfo.balance, loanInfo.scale, loanInfo.price);
|
|
124
|
+
const baseLiquidationValue = math_1.BigMath.min(
|
|
125
|
+
// deductReserve(loanValue, reserveFactor, reserveFactorScale),
|
|
126
|
+
loanValue, deductLiquidationBonus(allowedCollateralValue, liquidationBonus, liquidationBonusScale));
|
|
127
|
+
// calculate collateral amount
|
|
128
|
+
let collateralAmount = addLiquidationBonus(baseLiquidationValue, liquidationBonus, liquidationBonusScale);
|
|
129
|
+
collateralAmount = toAssetAmount(collateralAmount, collateralInfo.scale, collateralInfo.price);
|
|
130
|
+
// calculate loan amount
|
|
131
|
+
let liquidationAmount = addReserve(baseLiquidationValue, reserveFactor, reserveFactorScale);
|
|
132
|
+
liquidationAmount = toAssetAmount(liquidationAmount, loanInfo.scale, loanInfo.price);
|
|
133
|
+
return {
|
|
134
|
+
maxLiquidationAmount: liquidationAmount,
|
|
135
|
+
maxCollateralRewardAmount: collateralAmount
|
|
136
|
+
};
|
|
137
|
+
}
|
|
138
|
+
exports.calculateLiquidationAmounts = calculateLiquidationAmounts;
|
|
139
|
+
/**
|
|
140
|
+
* Check if the user is subject to liquidation
|
|
141
|
+
* @param args
|
|
142
|
+
*/
|
|
143
|
+
function isLiquidatable(args) {
|
|
144
|
+
const { isLiquidatable: res } = (0, math_1.calculateHealthParams)(args);
|
|
145
|
+
return res;
|
|
146
|
+
}
|
|
147
|
+
exports.isLiquidatable = isLiquidatable;
|
|
148
|
+
/**
|
|
149
|
+
* Determines if the provided pair of assets forms a bad debt.
|
|
150
|
+
* @param totalSupply total supply worth amount
|
|
151
|
+
* @param totalBorrow total borrow worth amount
|
|
152
|
+
* @param liquidationBonus collateral liquidation bonus value
|
|
153
|
+
* @param masterConstants pool constants
|
|
154
|
+
*/
|
|
155
|
+
function isBadDebt(totalSupply, totalBorrow, liquidationBonus, masterConstants) {
|
|
156
|
+
return totalSupply * masterConstants.ASSET_LIQUIDATION_BONUS_SCALE < totalBorrow * liquidationBonus;
|
|
157
|
+
}
|
|
158
|
+
exports.isBadDebt = isBadDebt;
|
|
159
|
+
/**
|
|
160
|
+
* Adds reserve to liquidation amount
|
|
161
|
+
* @param amount raw liquidation amount
|
|
162
|
+
* @param reserveFactor asset reserve factor
|
|
163
|
+
* @param factorScale asset reserve factor scale
|
|
164
|
+
* @returns liquidation amount with reserve
|
|
165
|
+
*/
|
|
166
|
+
function addReserve(amount, reserveFactor, factorScale) {
|
|
167
|
+
return amount * factorScale / (factorScale - reserveFactor);
|
|
168
|
+
}
|
|
169
|
+
exports.addReserve = addReserve;
|
|
170
|
+
/**
|
|
171
|
+
* Deducts reserve from liquidation amount
|
|
172
|
+
* @param amount liquidation amount with reserve
|
|
173
|
+
* @param reserveFactor asset reserve factor
|
|
174
|
+
* @param reserveFactorScale asset reserve factor scale
|
|
175
|
+
* @returns liquidation amount without reserve
|
|
176
|
+
*/
|
|
177
|
+
function deductReserve(amount, reserveFactor, reserveFactorScale) {
|
|
178
|
+
return amount * (reserveFactorScale - reserveFactor) / reserveFactorScale;
|
|
179
|
+
}
|
|
180
|
+
exports.deductReserve = deductReserve;
|
|
181
|
+
/**
|
|
182
|
+
* Converts worth value to specified asset amount
|
|
183
|
+
* @param worth worth value (decimals = 9)
|
|
184
|
+
* @param scale asset scale (10^asset_decimals)
|
|
185
|
+
* @param price asset price
|
|
186
|
+
*/
|
|
187
|
+
function toAssetAmount(worth, scale, price) {
|
|
188
|
+
return worth * scale / price;
|
|
189
|
+
}
|
|
190
|
+
exports.toAssetAmount = toAssetAmount;
|
|
191
|
+
/**
|
|
192
|
+
* Converts calculates worth value of specified asset amount
|
|
193
|
+
* @param amount worth value (decimals = 9)
|
|
194
|
+
* @param scale asset scale (10^asset_decimals)
|
|
195
|
+
* @param price asset price
|
|
196
|
+
*/
|
|
197
|
+
function toAssetWorth(amount, scale, price) {
|
|
198
|
+
return amount * price / scale;
|
|
199
|
+
}
|
|
200
|
+
exports.toAssetWorth = toAssetWorth;
|
|
201
|
+
/**
|
|
202
|
+
* Adds liquidation bonus to reward value.
|
|
203
|
+
* @param value reward asset value
|
|
204
|
+
* @param bonus liquidation bonus factor
|
|
205
|
+
* @param scale liquidation bonus scale
|
|
206
|
+
*/
|
|
207
|
+
function addLiquidationBonus(value, bonus, scale) {
|
|
208
|
+
return value * bonus / scale;
|
|
209
|
+
}
|
|
210
|
+
exports.addLiquidationBonus = addLiquidationBonus;
|
|
211
|
+
/**
|
|
212
|
+
* Deducts liquidation bonus from reward value.
|
|
213
|
+
* @param value reward asset value
|
|
214
|
+
* @param bonus liquidation bonus factor
|
|
215
|
+
* @param scale liquidation bonus scale
|
|
216
|
+
*/
|
|
217
|
+
function deductLiquidationBonus(value, bonus, scale) {
|
|
218
|
+
return value * scale / bonus;
|
|
219
|
+
}
|
|
220
|
+
exports.deductLiquidationBonus = deductLiquidationBonus;
|
|
221
|
+
/**
|
|
222
|
+
* Prepares extended asset info
|
|
223
|
+
* @param assetId
|
|
224
|
+
* @param assetsConfigDict
|
|
225
|
+
* @param assetsDataDict
|
|
226
|
+
* @param pricesDict
|
|
227
|
+
* @param principalsDict
|
|
228
|
+
* @param masterConstants
|
|
229
|
+
*/
|
|
230
|
+
function prepareAssetInfo(assetId, assetsConfigDict, assetsDataDict, pricesDict, principalsDict, masterConstants) {
|
|
231
|
+
if (!assetsConfigDict.has(assetId) ||
|
|
232
|
+
!assetsDataDict.has(assetId) ||
|
|
233
|
+
!pricesDict.has(assetId) ||
|
|
234
|
+
!principalsDict.has(assetId)) {
|
|
235
|
+
return { ok: false };
|
|
236
|
+
}
|
|
237
|
+
const config = assetsConfigDict.get(assetId);
|
|
238
|
+
const data = assetsDataDict.get(assetId);
|
|
239
|
+
const base = {
|
|
240
|
+
ok: true,
|
|
241
|
+
config, data,
|
|
242
|
+
price: pricesDict.get(assetId),
|
|
243
|
+
principal: principalsDict.get(assetId),
|
|
244
|
+
scale: 10n ** config.decimals,
|
|
245
|
+
liquidationReserveFactor: config.liquidationReserveFactor,
|
|
246
|
+
liquidationBonus: config.liquidationBonus
|
|
247
|
+
};
|
|
248
|
+
const assetPresent = (0, math_1.presentValue)(data.sRate, data.bRate, base.principal, masterConstants);
|
|
249
|
+
const dustPresent = (0, math_1.presentValue)(data.sRate, data.bRate, config.dust, masterConstants);
|
|
250
|
+
return { ...base, present: assetPresent, balance: assetPresent.amount, dust: dustPresent.amount };
|
|
251
|
+
}
|
|
252
|
+
exports.prepareAssetInfo = prepareAssetInfo;
|
package/dist/api/math.d.ts
CHANGED
|
@@ -1,11 +1,20 @@
|
|
|
1
1
|
import { AgregatedBalances, AssetConfig, AssetData, AssetInterest, ExtendedAssetData, ExtendedAssetsConfig, ExtendedAssetsData, MasterConstants, PoolConfig } from '../types/Master';
|
|
2
2
|
import { Dictionary } from '@ton/core';
|
|
3
|
-
import { LiquidationData, PredictHealthFactorArgs, UserBalance } from '../types/User';
|
|
3
|
+
import { HealthParamsArgs, LiquidationData, PredictHealthFactorArgs, UserBalance } from '../types/User';
|
|
4
4
|
export declare function mulFactor(decimal: bigint, a: bigint, b: bigint): bigint;
|
|
5
5
|
export declare function mulDiv(x: bigint, y: bigint, z: bigint): bigint;
|
|
6
6
|
export declare function mulDivC(x: bigint, y: bigint, z: bigint): bigint;
|
|
7
|
+
export declare function bigAbs(value: bigint): bigint;
|
|
7
8
|
export declare function bigIntMax(...args: bigint[]): bigint;
|
|
8
9
|
export declare function bigIntMin(...args: bigint[]): bigint;
|
|
10
|
+
export declare const BigMath: {
|
|
11
|
+
mulFactor: typeof mulFactor;
|
|
12
|
+
mulDiv: typeof mulDiv;
|
|
13
|
+
mulDivC: typeof mulDivC;
|
|
14
|
+
abs: typeof bigAbs;
|
|
15
|
+
min: typeof bigIntMin;
|
|
16
|
+
max: typeof bigIntMax;
|
|
17
|
+
};
|
|
9
18
|
export declare function calculatePresentValue(index: bigint, principalValue: bigint, masterConstants: MasterConstants): bigint;
|
|
10
19
|
export declare function calculateCurrentRates(assetConfig: AssetConfig, assetData: AssetData, masterConstants: MasterConstants): {
|
|
11
20
|
sRate: bigint;
|
|
@@ -20,14 +29,32 @@ export declare function checkNotInDebtAtAll(principals: Dictionary<bigint, bigin
|
|
|
20
29
|
export declare function getAgregatedBalances(assetsData: ExtendedAssetsData, assetsConfig: ExtendedAssetsConfig, principals: Dictionary<bigint, bigint>, prices: Dictionary<bigint, bigint>, masterConstants: MasterConstants): AgregatedBalances;
|
|
21
30
|
export declare function calculateMaximumWithdrawAmount(assetsConfig: ExtendedAssetsConfig, assetsData: ExtendedAssetsData, principals: Dictionary<bigint, bigint>, prices: Dictionary<bigint, bigint>, masterConstants: MasterConstants, assetId: bigint): bigint;
|
|
22
31
|
export declare function getAvailableToBorrow(assetsConfig: ExtendedAssetsConfig, assetsData: ExtendedAssetsData, principals: Dictionary<bigint, bigint>, prices: Dictionary<bigint, bigint>, masterConstants: MasterConstants): bigint;
|
|
32
|
+
/**
|
|
33
|
+
* Calculates balance value for asset principal.
|
|
34
|
+
* @param sRate asset supply rate
|
|
35
|
+
* @param bRate asset borrow rate
|
|
36
|
+
* @param principalValue asset principal value
|
|
37
|
+
* @param masterConstants pool constants
|
|
38
|
+
*/
|
|
23
39
|
export declare function presentValue(sRate: bigint, bRate: bigint, principalValue: bigint, masterConstants: MasterConstants): UserBalance;
|
|
24
40
|
/**
|
|
25
|
-
*
|
|
26
|
-
* @param
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
41
|
+
* Calculates health parameters of the specified user account based on its parameters
|
|
42
|
+
* @param parameters
|
|
43
|
+
*/
|
|
44
|
+
export declare function calculateHealthParams(parameters: HealthParamsArgs): {
|
|
45
|
+
totalDebt: bigint;
|
|
46
|
+
totalLimit: bigint;
|
|
47
|
+
totalSupply: bigint;
|
|
48
|
+
isLiquidatable: boolean;
|
|
49
|
+
isBadDebt: (liquidationBonus: bigint) => boolean;
|
|
50
|
+
};
|
|
51
|
+
/**
|
|
52
|
+
* Calculates liquidation data for greatest loan and collateral assets
|
|
53
|
+
* @param assetsConfig assets config dictionary
|
|
54
|
+
* @param assetsData assets data dictionary
|
|
55
|
+
* @param principals principals dictionary
|
|
56
|
+
* @param prices prices dictionary
|
|
57
|
+
* @param poolConfig pool config
|
|
31
58
|
* @returns can return UNDEFINED_ASSET if there are no assets
|
|
32
59
|
*/
|
|
33
60
|
export declare function calculateLiquidationData(assetsConfig: ExtendedAssetsConfig, assetsData: ExtendedAssetsData, principals: Dictionary<bigint, bigint>, prices: Dictionary<bigint, bigint>, poolConfig: PoolConfig): LiquidationData;
|