@evaafi/sdk 0.5.4 → 0.5.6
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/helpers.js +2 -3
- package/dist/api/math.d.ts +24 -2
- package/dist/api/math.js +189 -62
- package/dist/api/parser.d.ts +3 -3
- package/dist/api/parser.js +37 -25
- package/dist/api/prices.d.ts +1 -1
- package/dist/api/prices.js +3 -3
- package/dist/config.d.ts +2 -0
- package/dist/config.js +5 -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 +5 -34
- package/dist/constants.js +90 -37
- 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 +107 -0
- package/dist/utils/priceUtils.d.ts +55 -0
- package/dist/utils/priceUtils.js +117 -0
- package/dist/utils/sha256BigInt.js +1 -2
- package/dist/utils/tonConnectSender.js +2 -3
- package/dist/utils/userJettonWallet.d.ts +2 -2
- package/dist/utils/userJettonWallet.js +5 -2
- package/dist/utils/utils.d.ts +2 -0
- package/dist/utils/utils.js +6 -0
- package/package.json +2 -2
- package/src/api/math.ts +229 -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/dist/api/helpers.js
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.
|
|
3
|
+
exports.loadMyRef = loadMyRef;
|
|
4
|
+
exports.loadMaybeMyRef = loadMaybeMyRef;
|
|
4
5
|
const core_1 = require("@ton/core");
|
|
5
6
|
class MyCell extends core_1.Cell {
|
|
6
7
|
toString() {
|
|
@@ -18,7 +19,6 @@ function loadMyRef(slice) {
|
|
|
18
19
|
refs: cell.refs,
|
|
19
20
|
});
|
|
20
21
|
}
|
|
21
|
-
exports.loadMyRef = loadMyRef;
|
|
22
22
|
function loadMaybeMyRef(slice) {
|
|
23
23
|
const cell = slice.loadMaybeRef();
|
|
24
24
|
if (cell === null) {
|
|
@@ -30,4 +30,3 @@ function loadMaybeMyRef(slice) {
|
|
|
30
30
|
refs: cell.refs,
|
|
31
31
|
});
|
|
32
32
|
}
|
|
33
|
-
exports.loadMaybeMyRef = loadMaybeMyRef;
|
package/dist/api/math.d.ts
CHANGED
|
@@ -1,8 +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 { 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
|
+
export declare function mulDivC(x: bigint, y: bigint, z: bigint): bigint;
|
|
6
7
|
export declare function bigIntMax(...args: bigint[]): bigint;
|
|
7
8
|
export declare function bigIntMin(...args: bigint[]): bigint;
|
|
8
9
|
export declare function calculatePresentValue(index: bigint, principalValue: bigint, masterConstants: MasterConstants): bigint;
|
|
@@ -15,7 +16,28 @@ export declare function calculateCurrentRates(assetConfig: AssetConfig, assetDat
|
|
|
15
16
|
};
|
|
16
17
|
export declare function calculateAssetData(assetsConfigDict: ExtendedAssetsConfig, assetsDataDict: Dictionary<bigint, AssetData>, assetId: bigint, masterConstants: MasterConstants): ExtendedAssetData;
|
|
17
18
|
export declare function calculateAssetInterest(assetConfig: AssetConfig, assetData: AssetData, masterConstants: MasterConstants): AssetInterest;
|
|
19
|
+
export declare function checkNotInDebtAtAll(principals: Dictionary<bigint, bigint>): boolean;
|
|
20
|
+
export declare function getAgregatedBalances(assetsData: ExtendedAssetsData, assetsConfig: ExtendedAssetsConfig, principals: Dictionary<bigint, bigint>, prices: Dictionary<bigint, bigint>, masterConstants: MasterConstants): AgregatedBalances;
|
|
21
|
+
/**
|
|
22
|
+
* @deprecated The method should be used only for main contract v5
|
|
23
|
+
*/
|
|
24
|
+
export declare function isV5MainPoolContract(poolConfig: PoolConfig): boolean;
|
|
25
|
+
/**
|
|
26
|
+
* @deprecated The method should be used only for main contract v5
|
|
27
|
+
*/
|
|
28
|
+
export declare function calculateMaximumWithdrawAmountOld(assetsConfig: ExtendedAssetsConfig, assetsData: ExtendedAssetsData, principals: Dictionary<bigint, bigint>, prices: Dictionary<bigint, bigint>, masterConstants: MasterConstants, assetId: bigint): bigint;
|
|
29
|
+
export declare function calculateMaximumWithdrawAmount(// todo v6 ifelse dust not debt at all is fixed?
|
|
30
|
+
assetsConfig: ExtendedAssetsConfig, assetsData: ExtendedAssetsData, principals: Dictionary<bigint, bigint>, prices: Dictionary<bigint, bigint>, masterConstants: MasterConstants, assetId: bigint): bigint;
|
|
18
31
|
export declare function getAvailableToBorrow(assetsConfig: ExtendedAssetsConfig, assetsData: ExtendedAssetsData, principals: Dictionary<bigint, bigint>, prices: Dictionary<bigint, bigint>, masterConstants: MasterConstants): bigint;
|
|
19
32
|
export declare function presentValue(sRate: bigint, bRate: bigint, principalValue: bigint, masterConstants: MasterConstants): UserBalance;
|
|
20
|
-
|
|
33
|
+
/**
|
|
34
|
+
*
|
|
35
|
+
* @param assetsConfig
|
|
36
|
+
* @param assetsData
|
|
37
|
+
* @param principals
|
|
38
|
+
* @param prices
|
|
39
|
+
* @param poolConfig
|
|
40
|
+
* @returns can return UNDEFINED_ASSET if there are no assets
|
|
41
|
+
*/
|
|
42
|
+
export declare function calculateLiquidationData(assetsConfig: ExtendedAssetsConfig, assetsData: ExtendedAssetsData, principals: Dictionary<bigint, bigint>, prices: Dictionary<bigint, bigint>, poolConfig: PoolConfig): LiquidationData;
|
|
21
43
|
export declare function predictHealthFactor(args: PredictHealthFactorArgs): number;
|
package/dist/api/math.js
CHANGED
|
@@ -1,28 +1,46 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.
|
|
3
|
+
exports.mulFactor = mulFactor;
|
|
4
|
+
exports.mulDiv = mulDiv;
|
|
5
|
+
exports.mulDivC = mulDivC;
|
|
6
|
+
exports.bigIntMax = bigIntMax;
|
|
7
|
+
exports.bigIntMin = bigIntMin;
|
|
8
|
+
exports.calculatePresentValue = calculatePresentValue;
|
|
9
|
+
exports.calculateCurrentRates = calculateCurrentRates;
|
|
10
|
+
exports.calculateAssetData = calculateAssetData;
|
|
11
|
+
exports.calculateAssetInterest = calculateAssetInterest;
|
|
12
|
+
exports.checkNotInDebtAtAll = checkNotInDebtAtAll;
|
|
13
|
+
exports.getAgregatedBalances = getAgregatedBalances;
|
|
14
|
+
exports.isV5MainPoolContract = isV5MainPoolContract;
|
|
15
|
+
exports.calculateMaximumWithdrawAmountOld = calculateMaximumWithdrawAmountOld;
|
|
16
|
+
exports.calculateMaximumWithdrawAmount = calculateMaximumWithdrawAmount;
|
|
17
|
+
exports.getAvailableToBorrow = getAvailableToBorrow;
|
|
18
|
+
exports.presentValue = presentValue;
|
|
19
|
+
exports.calculateLiquidationData = calculateLiquidationData;
|
|
20
|
+
exports.predictHealthFactor = predictHealthFactor;
|
|
4
21
|
const User_1 = require("../types/User");
|
|
5
22
|
const sha256BigInt_1 = require("../utils/sha256BigInt");
|
|
23
|
+
const assets_1 = require("../constants/assets");
|
|
24
|
+
const __1 = require("..");
|
|
6
25
|
function mulFactor(decimal, a, b) {
|
|
7
26
|
return (a * b) / decimal;
|
|
8
27
|
}
|
|
9
|
-
exports.mulFactor = mulFactor;
|
|
10
28
|
function mulDiv(x, y, z) {
|
|
11
29
|
return (x * y) / z;
|
|
12
30
|
}
|
|
13
|
-
|
|
31
|
+
function mulDivC(x, y, z) {
|
|
32
|
+
const mul = x * y;
|
|
33
|
+
return mul / z + (mul % z ? 1n : 0n);
|
|
34
|
+
}
|
|
14
35
|
function bigIntMax(...args) {
|
|
15
36
|
return args.reduce((m, e) => (e > m ? e : m));
|
|
16
37
|
}
|
|
17
|
-
exports.bigIntMax = bigIntMax;
|
|
18
38
|
function bigIntMin(...args) {
|
|
19
39
|
return args.reduce((m, e) => (e < m ? e : m));
|
|
20
40
|
}
|
|
21
|
-
exports.bigIntMin = bigIntMin;
|
|
22
41
|
function calculatePresentValue(index, principalValue, masterConstants) {
|
|
23
42
|
return (principalValue * index) / masterConstants.FACTOR_SCALE;
|
|
24
43
|
}
|
|
25
|
-
exports.calculatePresentValue = calculatePresentValue;
|
|
26
44
|
function calculateCurrentRates(assetConfig, assetData, masterConstants) {
|
|
27
45
|
const now = BigInt(Math.floor(Date.now() / 1000));
|
|
28
46
|
const timeElapsed = now - assetData.lastAccural;
|
|
@@ -46,7 +64,6 @@ function calculateCurrentRates(assetConfig, assetData, masterConstants) {
|
|
|
46
64
|
now,
|
|
47
65
|
};
|
|
48
66
|
}
|
|
49
|
-
exports.calculateCurrentRates = calculateCurrentRates;
|
|
50
67
|
function calculateAssetData(assetsConfigDict, assetsDataDict, assetId, masterConstants) {
|
|
51
68
|
const config = assetsConfigDict.get(assetId);
|
|
52
69
|
const data = assetsDataDict.get(assetId);
|
|
@@ -65,7 +82,6 @@ function calculateAssetData(assetsConfigDict, assetsDataDict, assetId, masterCon
|
|
|
65
82
|
...{ supplyApy, borrowApy },
|
|
66
83
|
};
|
|
67
84
|
}
|
|
68
|
-
exports.calculateAssetData = calculateAssetData;
|
|
69
85
|
function calculateAssetInterest(assetConfig, assetData, masterConstants) {
|
|
70
86
|
const totalSupply = calculatePresentValue(assetData.sRate, assetData.totalSupply, masterConstants);
|
|
71
87
|
const totalBorrow = calculatePresentValue(assetData.bRate, assetData.totalBorrow, masterConstants);
|
|
@@ -92,7 +108,112 @@ function calculateAssetInterest(assetConfig, assetData, masterConstants) {
|
|
|
92
108
|
borrowInterest,
|
|
93
109
|
};
|
|
94
110
|
}
|
|
95
|
-
|
|
111
|
+
function checkNotInDebtAtAll(principals) {
|
|
112
|
+
return principals.values().every(x => x >= 0n);
|
|
113
|
+
}
|
|
114
|
+
function getAgregatedBalances(assetsData, assetsConfig, principals, prices, masterConstants) {
|
|
115
|
+
let user_total_supply = 0n;
|
|
116
|
+
let user_total_borrow = 0n;
|
|
117
|
+
for (const [assetId, principal] of principals) {
|
|
118
|
+
if (principal) {
|
|
119
|
+
if (!prices.has(assetId)) {
|
|
120
|
+
return { totalSupply: 0n, totalBorrow: 0n };
|
|
121
|
+
}
|
|
122
|
+
const price = prices.get(assetId);
|
|
123
|
+
const assetData = assetsData.get(assetId);
|
|
124
|
+
const assetConfig = assetsConfig.get(assetId);
|
|
125
|
+
if (principal < 0) {
|
|
126
|
+
user_total_borrow += presentValue(assetData.sRate, assetData.bRate, principal, masterConstants).amount * price / 10n ** assetConfig.decimals;
|
|
127
|
+
}
|
|
128
|
+
else {
|
|
129
|
+
user_total_supply += presentValue(assetData.sRate, assetData.bRate, principal, masterConstants).amount * price / 10n ** assetConfig.decimals;
|
|
130
|
+
}
|
|
131
|
+
}
|
|
132
|
+
}
|
|
133
|
+
return { totalSupply: user_total_supply, totalBorrow: user_total_borrow };
|
|
134
|
+
}
|
|
135
|
+
/**
|
|
136
|
+
* @deprecated The method should be used only for main contract v5
|
|
137
|
+
*/
|
|
138
|
+
function isV5MainPoolContract(poolConfig) {
|
|
139
|
+
if ((poolConfig.masterAddress == __1.MAINNET_POOL_CONFIG.masterAddress && poolConfig.masterVersion == 5) ||
|
|
140
|
+
(poolConfig.masterAddress == __1.TESTNET_POOL_CONFIG.masterAddress && poolConfig.masterVersion == 5)) {
|
|
141
|
+
return true;
|
|
142
|
+
}
|
|
143
|
+
else {
|
|
144
|
+
return false;
|
|
145
|
+
}
|
|
146
|
+
}
|
|
147
|
+
/**
|
|
148
|
+
* @deprecated The method should be used only for main contract v5
|
|
149
|
+
*/
|
|
150
|
+
function calculateMaximumWithdrawAmountOld(assetsConfig, assetsData, principals, prices, masterConstants, assetId) {
|
|
151
|
+
let withdrawAmountMax = 0n;
|
|
152
|
+
const assetConfig = assetsConfig.get(assetId);
|
|
153
|
+
const assetData = assetsData.get(assetId);
|
|
154
|
+
const oldPrincipal = principals.get(assetId);
|
|
155
|
+
const oldPresentValue = presentValue(assetData.sRate, assetData.bRate, oldPrincipal, masterConstants);
|
|
156
|
+
if (oldPresentValue.amount > assetConfig.dust) {
|
|
157
|
+
if (checkNotInDebtAtAll(principals)) {
|
|
158
|
+
withdrawAmountMax = oldPresentValue.amount;
|
|
159
|
+
}
|
|
160
|
+
else {
|
|
161
|
+
if (!prices.has(assetId)) {
|
|
162
|
+
return 0n;
|
|
163
|
+
}
|
|
164
|
+
//const borrowable = getAvailableToBorrow(assetsConfig, assetsData, principals, prices, masterConstants);
|
|
165
|
+
const price = prices.get(assetId);
|
|
166
|
+
const agregatedBalances = getAgregatedBalances(assetsData, assetsConfig, principals, principals, masterConstants);
|
|
167
|
+
let maxAmountToReclaim = mulDiv(agregatedBalances.totalSupply - mulDivC(agregatedBalances.totalBorrow, masterConstants.ASSET_COEFFICIENT_SCALE, assetConfig.collateralFactor), 10n ** assetConfig.decimals, price);
|
|
168
|
+
withdrawAmountMax = bigIntMin(maxAmountToReclaim, oldPresentValue.amount);
|
|
169
|
+
}
|
|
170
|
+
}
|
|
171
|
+
else {
|
|
172
|
+
if (!prices.has(assetId)) {
|
|
173
|
+
return 0n;
|
|
174
|
+
}
|
|
175
|
+
const price = prices.get(assetId);
|
|
176
|
+
return getAvailableToBorrow(assetsConfig, assetsData, principals, prices, masterConstants) * (10n ** assetConfig.decimals) / price;
|
|
177
|
+
}
|
|
178
|
+
return withdrawAmountMax;
|
|
179
|
+
}
|
|
180
|
+
function calculateMaximumWithdrawAmount(// todo v6 ifelse dust not debt at all is fixed?
|
|
181
|
+
assetsConfig, assetsData, principals, prices, masterConstants, assetId) {
|
|
182
|
+
let withdrawAmountMax = 0n;
|
|
183
|
+
const assetConfig = assetsConfig.get(assetId);
|
|
184
|
+
const assetData = assetsData.get(assetId);
|
|
185
|
+
const oldPrincipal = principals.get(assetId);
|
|
186
|
+
if (oldPrincipal > assetConfig.dust) {
|
|
187
|
+
const oldPresentValue = presentValue(assetData.sRate, assetData.bRate, oldPrincipal, masterConstants);
|
|
188
|
+
if (checkNotInDebtAtAll(principals)) {
|
|
189
|
+
withdrawAmountMax = oldPresentValue.amount;
|
|
190
|
+
}
|
|
191
|
+
else {
|
|
192
|
+
if (!prices.has(assetId)) {
|
|
193
|
+
return 0n;
|
|
194
|
+
}
|
|
195
|
+
const borrowable = getAvailableToBorrow(assetsConfig, assetsData, principals, prices, masterConstants);
|
|
196
|
+
const price = prices.get(assetId);
|
|
197
|
+
let maxAmountToReclaim = 0n;
|
|
198
|
+
if (assetConfig.collateralFactor == 0n) {
|
|
199
|
+
maxAmountToReclaim = oldPresentValue.amount;
|
|
200
|
+
}
|
|
201
|
+
else if (price > 0) {
|
|
202
|
+
maxAmountToReclaim =
|
|
203
|
+
mulDiv(mulDivC(borrowable, masterConstants.ASSET_COEFFICIENT_SCALE, assetConfig.collateralFactor), 10n ** assetConfig.decimals, price);
|
|
204
|
+
}
|
|
205
|
+
withdrawAmountMax = bigIntMin(maxAmountToReclaim, oldPresentValue.amount);
|
|
206
|
+
}
|
|
207
|
+
}
|
|
208
|
+
else {
|
|
209
|
+
if (!prices.has(assetId)) {
|
|
210
|
+
return 0n;
|
|
211
|
+
}
|
|
212
|
+
const price = prices.get(assetId);
|
|
213
|
+
return getAvailableToBorrow(assetsConfig, assetsData, principals, prices, masterConstants) * (10n ** assetConfig.decimals) / price;
|
|
214
|
+
}
|
|
215
|
+
return withdrawAmountMax;
|
|
216
|
+
}
|
|
96
217
|
function getAvailableToBorrow(assetsConfig, assetsData, principals, prices, masterConstants) {
|
|
97
218
|
let borrowLimit = 0n;
|
|
98
219
|
let borrowAmount = 0n;
|
|
@@ -106,14 +227,11 @@ function getAvailableToBorrow(assetsConfig, assetsData, principals, prices, mast
|
|
|
106
227
|
}
|
|
107
228
|
else if (principal > 0) {
|
|
108
229
|
borrowLimit +=
|
|
109
|
-
(calculatePresentValue(assetData.sRate, principal, masterConstants)
|
|
110
|
-
10n ** assetConfig.decimals /
|
|
111
|
-
masterConstants.ASSET_COEFFICIENT_SCALE;
|
|
230
|
+
mulDivC(mulDivC(calculatePresentValue(assetData.sRate, principal, masterConstants), price, 10n ** assetConfig.decimals), assetConfig.collateralFactor, masterConstants.ASSET_COEFFICIENT_SCALE);
|
|
112
231
|
}
|
|
113
232
|
}
|
|
114
233
|
return borrowLimit - borrowAmount;
|
|
115
234
|
}
|
|
116
|
-
exports.getAvailableToBorrow = getAvailableToBorrow;
|
|
117
235
|
function presentValue(sRate, bRate, principalValue, masterConstants) {
|
|
118
236
|
if (principalValue > 0) {
|
|
119
237
|
return {
|
|
@@ -134,63 +252,74 @@ function presentValue(sRate, bRate, principalValue, masterConstants) {
|
|
|
134
252
|
};
|
|
135
253
|
}
|
|
136
254
|
}
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
255
|
+
/**
|
|
256
|
+
*
|
|
257
|
+
* @param assetsConfig
|
|
258
|
+
* @param assetsData
|
|
259
|
+
* @param principals
|
|
260
|
+
* @param prices
|
|
261
|
+
* @param poolConfig
|
|
262
|
+
* @returns can return UNDEFINED_ASSET if there are no assets
|
|
263
|
+
*/
|
|
264
|
+
function calculateLiquidationData(assetsConfig, assetsData, principals, prices, poolConfig) {
|
|
265
|
+
let collateralValue = 0n;
|
|
266
|
+
let collateralAsset = assets_1.UNDEFINED_ASSET;
|
|
267
|
+
let loanValue = 0n;
|
|
268
|
+
let loanAsset = assets_1.UNDEFINED_ASSET;
|
|
143
269
|
let totalDebt = 0n;
|
|
144
270
|
let totalLimit = 0n;
|
|
145
|
-
for (const
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
271
|
+
for (const asset of poolConfig.poolAssetsConfig) {
|
|
272
|
+
if (!principals.has(asset.assetId)) {
|
|
273
|
+
continue;
|
|
274
|
+
}
|
|
275
|
+
const principal = principals.get(asset.assetId);
|
|
276
|
+
const assetConfig = assetsConfig.get(asset.assetId);
|
|
277
|
+
const assetData = assetsData.get(asset.assetId);
|
|
149
278
|
const balance = principal > 0 ? (principal * assetData.sRate) / BigInt(1e12) : (principal * assetData.bRate) / BigInt(1e12);
|
|
150
279
|
if (balance > 0) {
|
|
151
|
-
const assetWorth = (balance * prices.get(
|
|
152
|
-
totalLimit += (assetWorth * assetConfig.liquidationThreshold) / masterConstants.ASSET_COEFFICIENT_SCALE;
|
|
153
|
-
if (assetWorth >
|
|
154
|
-
|
|
155
|
-
|
|
280
|
+
const assetWorth = (balance * prices.get(asset.assetId)) / 10n ** assetConfig.decimals;
|
|
281
|
+
totalLimit += (assetWorth * assetConfig.liquidationThreshold) / poolConfig.masterConstants.ASSET_COEFFICIENT_SCALE;
|
|
282
|
+
if (assetWorth > collateralValue) {
|
|
283
|
+
collateralValue = assetWorth;
|
|
284
|
+
collateralAsset = asset;
|
|
156
285
|
}
|
|
157
286
|
}
|
|
158
287
|
else if (balance < 0) {
|
|
159
|
-
const assetWorth = (-balance * prices.get(
|
|
288
|
+
const assetWorth = (-balance * prices.get(asset.assetId)) / 10n ** assetConfig.decimals;
|
|
160
289
|
totalDebt += assetWorth;
|
|
161
|
-
if (assetWorth >
|
|
162
|
-
|
|
163
|
-
|
|
290
|
+
if (assetWorth > loanValue) {
|
|
291
|
+
loanValue = assetWorth;
|
|
292
|
+
loanAsset = asset;
|
|
164
293
|
}
|
|
165
294
|
}
|
|
166
295
|
}
|
|
167
|
-
if (totalLimit < totalDebt) {
|
|
168
|
-
const
|
|
296
|
+
if (collateralAsset.assetId !== assets_1.UNDEFINED_ASSET.assetId && totalLimit < totalDebt) {
|
|
297
|
+
const loanAssetPrice = prices.get(loanAsset.assetId);
|
|
169
298
|
const values = [];
|
|
170
|
-
const
|
|
171
|
-
const
|
|
172
|
-
const liquidationBonus =
|
|
173
|
-
const
|
|
174
|
-
values.push((bigIntMax(
|
|
175
|
-
|
|
176
|
-
masterConstants.ASSET_COEFFICIENT_SCALE) /
|
|
299
|
+
const collateralAssetConfig = assetsConfig.get(collateralAsset.assetId);
|
|
300
|
+
const loanAssetConfig = assetsConfig.get(loanAsset.assetId);
|
|
301
|
+
const liquidationBonus = collateralAssetConfig.liquidationBonus;
|
|
302
|
+
const loanScale = 10n ** loanAssetConfig.decimals;
|
|
303
|
+
values.push((bigIntMax(collateralValue / 2n, bigIntMin(collateralValue, 100000000000n)) *
|
|
304
|
+
loanScale *
|
|
305
|
+
poolConfig.masterConstants.ASSET_COEFFICIENT_SCALE) /
|
|
177
306
|
liquidationBonus /
|
|
178
|
-
|
|
179
|
-
values.push((
|
|
307
|
+
loanAssetPrice);
|
|
308
|
+
values.push((loanValue * loanScale) / loanAssetPrice);
|
|
180
309
|
const liquidationAmount = bigIntMin(...values) - 5n;
|
|
181
|
-
const
|
|
182
|
-
const collateralDecimal = 10n **
|
|
183
|
-
let minCollateralAmount = (((liquidationAmount *
|
|
184
|
-
|
|
185
|
-
|
|
310
|
+
const collateralAssetPrice = prices.get(collateralAsset.assetId);
|
|
311
|
+
const collateralDecimal = 10n ** collateralAssetConfig.decimals;
|
|
312
|
+
let minCollateralAmount = (((liquidationAmount * loanAssetPrice * liquidationBonus) / poolConfig.masterConstants.ASSET_LIQUIDATION_BONUS_SCALE) * collateralDecimal) /
|
|
313
|
+
collateralAssetPrice /
|
|
314
|
+
loanScale -
|
|
186
315
|
10n;
|
|
187
316
|
minCollateralAmount = (minCollateralAmount * 97n) / 100n;
|
|
188
317
|
if (minCollateralAmount / collateralDecimal >= 1n) {
|
|
189
318
|
return {
|
|
190
|
-
greatestCollateralAsset:
|
|
191
|
-
greatestCollateralValue:
|
|
192
|
-
greatestLoanAsset:
|
|
193
|
-
greatestLoanValue:
|
|
319
|
+
greatestCollateralAsset: collateralAsset,
|
|
320
|
+
greatestCollateralValue: collateralValue,
|
|
321
|
+
greatestLoanAsset: loanAsset,
|
|
322
|
+
greatestLoanValue: loanValue,
|
|
194
323
|
totalDebt,
|
|
195
324
|
totalLimit,
|
|
196
325
|
liquidable: true,
|
|
@@ -200,18 +329,17 @@ function calculateLiquidationData(assetsConfig, assetsData, principals, prices,
|
|
|
200
329
|
}
|
|
201
330
|
}
|
|
202
331
|
return {
|
|
203
|
-
greatestCollateralAsset:
|
|
204
|
-
greatestCollateralValue:
|
|
205
|
-
greatestLoanAsset:
|
|
206
|
-
greatestLoanValue:
|
|
332
|
+
greatestCollateralAsset: collateralAsset,
|
|
333
|
+
greatestCollateralValue: collateralValue,
|
|
334
|
+
greatestLoanAsset: loanAsset,
|
|
335
|
+
greatestLoanValue: loanValue,
|
|
207
336
|
totalDebt,
|
|
208
337
|
totalLimit,
|
|
209
338
|
liquidable: false,
|
|
210
339
|
};
|
|
211
340
|
}
|
|
212
|
-
exports.calculateLiquidationData = calculateLiquidationData;
|
|
213
341
|
function predictHealthFactor(args) {
|
|
214
|
-
const liquidationData = calculateLiquidationData(args.assetsConfig, args.assetsData, args.
|
|
342
|
+
const liquidationData = calculateLiquidationData(args.assetsConfig, args.assetsData, args.principals, args.prices, args.poolConfig);
|
|
215
343
|
const tokenHash = (0, sha256BigInt_1.sha256Hash)(args.tokenSymbol);
|
|
216
344
|
const assetConfig = args.assetsConfig.get(tokenHash);
|
|
217
345
|
const assetPrice = Number(args.prices.get(tokenHash));
|
|
@@ -223,16 +351,16 @@ function predictHealthFactor(args) {
|
|
|
223
351
|
const changeType = args.balanceChangeType;
|
|
224
352
|
if (currentAmount != null && currentAmount != 0n) {
|
|
225
353
|
if (changeType == User_1.BalanceChangeType.Borrow) {
|
|
226
|
-
totalBorrow += currentBalance * (1 + Number(assetConfig.originationFee) / Number(args.masterConstants.ASSET_ORIGINATION_FEE_SCALE));
|
|
354
|
+
totalBorrow += currentBalance * (1 + Number(assetConfig.originationFee) / Number(args.poolConfig.masterConstants.ASSET_ORIGINATION_FEE_SCALE));
|
|
227
355
|
}
|
|
228
356
|
else if (changeType == User_1.BalanceChangeType.Repay) {
|
|
229
357
|
totalBorrow -= currentBalance;
|
|
230
358
|
}
|
|
231
359
|
else if (changeType == User_1.BalanceChangeType.Withdraw) {
|
|
232
|
-
totalLimit -= currentBalance * Number(assetConfig.liquidationThreshold) / Number(args.masterConstants.ASSET_COEFFICIENT_SCALE);
|
|
360
|
+
totalLimit -= currentBalance * Number(assetConfig.liquidationThreshold) / Number(args.poolConfig.masterConstants.ASSET_COEFFICIENT_SCALE);
|
|
233
361
|
}
|
|
234
362
|
else if (changeType == User_1.BalanceChangeType.Supply) {
|
|
235
|
-
totalLimit += currentBalance * Number(assetConfig.liquidationThreshold) / Number(args.masterConstants.ASSET_COEFFICIENT_SCALE);
|
|
363
|
+
totalLimit += currentBalance * Number(assetConfig.liquidationThreshold) / Number(args.poolConfig.masterConstants.ASSET_COEFFICIENT_SCALE);
|
|
236
364
|
}
|
|
237
365
|
}
|
|
238
366
|
if (Number(totalLimit) == 0) {
|
|
@@ -240,4 +368,3 @@ function predictHealthFactor(args) {
|
|
|
240
368
|
}
|
|
241
369
|
return Math.min(Math.max(1 - totalBorrow / totalLimit, 0), 1); // let's limit a result to zero below and one above
|
|
242
370
|
}
|
|
243
|
-
exports.predictHealthFactor = predictHealthFactor;
|
package/dist/api/parser.d.ts
CHANGED
|
@@ -1,8 +1,8 @@
|
|
|
1
1
|
import { Dictionary, DictionaryValue } from '@ton/core';
|
|
2
|
-
import { AssetConfig, AssetData, ExtendedAssetsConfig, ExtendedAssetsData, MasterConstants, MasterData, PoolAssetsConfig } from '../types/Master';
|
|
2
|
+
import { AssetConfig, AssetData, ExtendedAssetsConfig, ExtendedAssetsData, MasterConstants, MasterData, PoolAssetsConfig, PoolConfig } from '../types/Master';
|
|
3
3
|
import { UserData, UserLiteData } from '../types/User';
|
|
4
4
|
export declare function createAssetData(): DictionaryValue<AssetData>;
|
|
5
5
|
export declare function createAssetConfig(): DictionaryValue<AssetConfig>;
|
|
6
6
|
export declare function parseMasterData(masterDataBOC: string, poolAssetsConfig: PoolAssetsConfig, masterConstants: MasterConstants): MasterData;
|
|
7
|
-
export declare function parseUserLiteData(userDataBOC: string, assetsData: ExtendedAssetsData, assetsConfig: ExtendedAssetsConfig,
|
|
8
|
-
export declare function parseUserData(userLiteData: UserLiteData, assetsData: ExtendedAssetsData, assetsConfig: ExtendedAssetsConfig, prices: Dictionary<bigint, bigint>,
|
|
7
|
+
export declare function parseUserLiteData(userDataBOC: string, assetsData: ExtendedAssetsData, assetsConfig: ExtendedAssetsConfig, poolConfig: PoolConfig, applyDust?: boolean): UserLiteData;
|
|
8
|
+
export declare function parseUserData(userLiteData: UserLiteData, assetsData: ExtendedAssetsData, assetsConfig: ExtendedAssetsConfig, prices: Dictionary<bigint, bigint>, poolConfig: PoolConfig, applyDust?: boolean): UserData;
|
package/dist/api/parser.js
CHANGED
|
@@ -1,6 +1,10 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.
|
|
3
|
+
exports.createAssetData = createAssetData;
|
|
4
|
+
exports.createAssetConfig = createAssetConfig;
|
|
5
|
+
exports.parseMasterData = parseMasterData;
|
|
6
|
+
exports.parseUserLiteData = parseUserLiteData;
|
|
7
|
+
exports.parseUserData = parseUserData;
|
|
4
8
|
const core_1 = require("@ton/core");
|
|
5
9
|
const math_1 = require("./math");
|
|
6
10
|
const helpers_1 = require("./helpers");
|
|
@@ -50,7 +54,6 @@ function createAssetData() {
|
|
|
50
54
|
},
|
|
51
55
|
};
|
|
52
56
|
}
|
|
53
|
-
exports.createAssetData = createAssetData;
|
|
54
57
|
function createAssetConfig() {
|
|
55
58
|
return {
|
|
56
59
|
serialize: (src, builder) => {
|
|
@@ -124,7 +127,6 @@ function createAssetConfig() {
|
|
|
124
127
|
},
|
|
125
128
|
};
|
|
126
129
|
}
|
|
127
|
-
exports.createAssetConfig = createAssetConfig;
|
|
128
130
|
function parseMasterData(masterDataBOC, poolAssetsConfig, masterConstants) {
|
|
129
131
|
const masterSlice = core_1.Cell.fromBase64(masterDataBOC).beginParse();
|
|
130
132
|
const meta = masterSlice.loadRef().beginParse().loadStringTail();
|
|
@@ -180,8 +182,9 @@ function parseMasterData(masterDataBOC, poolAssetsConfig, masterConstants) {
|
|
|
180
182
|
apy: apy,
|
|
181
183
|
};
|
|
182
184
|
}
|
|
183
|
-
|
|
184
|
-
|
|
185
|
+
function parseUserLiteData(userDataBOC, assetsData, assetsConfig, poolConfig, applyDust = true) {
|
|
186
|
+
const poolAssetsConfig = poolConfig.poolAssetsConfig;
|
|
187
|
+
const masterConstants = poolConfig.masterConstants;
|
|
185
188
|
const userSlice = core_1.Cell.fromBase64(userDataBOC).beginParse();
|
|
186
189
|
const codeVersion = userSlice.loadCoins();
|
|
187
190
|
const masterAddress = userSlice.loadAddress();
|
|
@@ -213,16 +216,22 @@ function parseUserLiteData(userDataBOC, assetsData, assetsConfig, poolAssetsConf
|
|
|
213
216
|
}
|
|
214
217
|
*/
|
|
215
218
|
userSlice.endParse();
|
|
219
|
+
const isV5Main = (0, math_1.isV5MainPoolContract)(poolConfig);
|
|
216
220
|
const userBalances = core_1.Dictionary.empty();
|
|
217
221
|
for (const [_, asset] of Object.entries(poolAssetsConfig)) {
|
|
218
222
|
const assetData = assetsData.get(asset.assetId);
|
|
219
223
|
const assetConfig = assetsConfig.get(asset.assetId);
|
|
220
|
-
let
|
|
221
|
-
|
|
222
|
-
|
|
224
|
+
let principal = principalsDict.get(asset.assetId) || 0n;
|
|
225
|
+
let balance = (0, math_1.presentValue)(assetData.sRate, assetData.bRate, principal, masterConstants);
|
|
226
|
+
let leftBorder = isV5Main ? balance.amount : principal;
|
|
227
|
+
if (applyDust && (principal > 0 && (leftBorder < assetConfig.dust))) { // v6 will be abs(principals) < dust
|
|
228
|
+
principal = 0n;
|
|
229
|
+
balance = {
|
|
230
|
+
amount: 0n,
|
|
231
|
+
type: User_1.BalanceType.supply,
|
|
232
|
+
};
|
|
223
233
|
principalsDict.set(asset.assetId, 0n);
|
|
224
234
|
}
|
|
225
|
-
const balance = (0, math_1.presentValue)(assetData.sRate, assetData.bRate, principals, masterConstants);
|
|
226
235
|
userBalances.set(asset.assetId, balance);
|
|
227
236
|
}
|
|
228
237
|
return {
|
|
@@ -243,24 +252,27 @@ function parseUserLiteData(userDataBOC, assetsData, assetsConfig, poolAssetsConf
|
|
|
243
252
|
backupCell2: backupCell2, */
|
|
244
253
|
};
|
|
245
254
|
}
|
|
246
|
-
|
|
247
|
-
|
|
255
|
+
function parseUserData(userLiteData, assetsData, assetsConfig, prices, poolConfig, applyDust = true) {
|
|
256
|
+
const poolAssetsConfig = poolConfig.poolAssetsConfig;
|
|
257
|
+
const masterConstants = poolConfig.masterConstants;
|
|
248
258
|
const withdrawalLimits = core_1.Dictionary.empty();
|
|
249
259
|
const borrowLimits = core_1.Dictionary.empty();
|
|
250
260
|
let supplyBalance = 0n;
|
|
251
261
|
let borrowBalance = 0n;
|
|
252
|
-
|
|
262
|
+
const isV5Main = (0, math_1.isV5MainPoolContract)(poolConfig);
|
|
263
|
+
for (const [_, asset] of Object.entries(poolAssetsConfig)) {
|
|
253
264
|
const assetData = assetsData.get(asset.assetId);
|
|
254
265
|
const assetConfig = assetsConfig.get(asset.assetId);
|
|
255
|
-
let
|
|
256
|
-
|
|
257
|
-
|
|
266
|
+
let principal = userLiteData.principals.get(asset.assetId) || 0n;
|
|
267
|
+
const balance = (0, math_1.presentValue)(assetData.sRate, assetData.bRate, principal, masterConstants);
|
|
268
|
+
let leftBorder = isV5Main ? balance.amount : principal;
|
|
269
|
+
if (applyDust && (principal > 0 && (leftBorder < assetConfig.dust))) { // v6 will be abs(principals) < dust
|
|
270
|
+
principal = 0n;
|
|
258
271
|
userLiteData.principals.set(asset.assetId, 0n);
|
|
259
272
|
}
|
|
260
|
-
const balance = (0, math_1.presentValue)(assetData.sRate, assetData.bRate, principals, masterConstants);
|
|
261
273
|
userLiteData.balances.set(asset.assetId, balance);
|
|
262
274
|
}
|
|
263
|
-
for (const [_, asset] of Object.entries(
|
|
275
|
+
for (const [_, asset] of Object.entries(poolAssetsConfig)) {
|
|
264
276
|
const assetConfig = assetsConfig.get(asset.assetId);
|
|
265
277
|
const balance = userLiteData.balances.get(asset.assetId);
|
|
266
278
|
if (balance.type === User_1.BalanceType.supply) {
|
|
@@ -271,16 +283,17 @@ function parseUserData(userLiteData, assetsData, assetsConfig, prices, poolAsset
|
|
|
271
283
|
}
|
|
272
284
|
}
|
|
273
285
|
const availableToBorrow = (0, math_1.getAvailableToBorrow)(assetsConfig, assetsData, userLiteData.principals, prices, masterConstants);
|
|
274
|
-
for (const [_, asset] of Object.entries(
|
|
286
|
+
for (const [_, asset] of Object.entries(poolAssetsConfig)) {
|
|
275
287
|
const assetConfig = assetsConfig.get(asset.assetId);
|
|
276
288
|
const assetData = assetsData.get(asset.assetId);
|
|
277
289
|
const balance = userLiteData.balances.get(asset.assetId);
|
|
278
290
|
if (balance.type === User_1.BalanceType.supply) {
|
|
279
|
-
|
|
280
|
-
(
|
|
281
|
-
|
|
282
|
-
|
|
283
|
-
|
|
291
|
+
if ((0, math_1.isV5MainPoolContract)(poolConfig)) {
|
|
292
|
+
withdrawalLimits.set(asset.assetId, (0, math_1.calculateMaximumWithdrawAmountOld)(assetsConfig, assetsData, userLiteData.principals, prices, masterConstants, asset.assetId));
|
|
293
|
+
}
|
|
294
|
+
else {
|
|
295
|
+
withdrawalLimits.set(asset.assetId, (0, math_1.calculateMaximumWithdrawAmount)(assetsConfig, assetsData, userLiteData.principals, prices, masterConstants, asset.assetId));
|
|
296
|
+
}
|
|
284
297
|
}
|
|
285
298
|
borrowLimits.set(asset.assetId, (0, math_1.bigIntMin)((availableToBorrow * 10n ** assetConfig.decimals) / prices.get(asset.assetId), assetData.balance));
|
|
286
299
|
}
|
|
@@ -288,7 +301,7 @@ function parseUserData(userLiteData, assetsData, assetsConfig, prices, poolAsset
|
|
|
288
301
|
const limitUsedPercent = limitUsed === 0n
|
|
289
302
|
? 0
|
|
290
303
|
: Number(BigInt(1e9) - (availableToBorrow * BigInt(1e9)) / (borrowBalance + availableToBorrow)) / 1e7;
|
|
291
|
-
const liquidationData = (0, math_1.calculateLiquidationData)(assetsConfig, assetsData, userLiteData.principals, prices,
|
|
304
|
+
const liquidationData = (0, math_1.calculateLiquidationData)(assetsConfig, assetsData, userLiteData.principals, prices, poolConfig);
|
|
292
305
|
const healthFactor = 1 - Number(liquidationData.totalDebt) / Number(liquidationData.totalLimit);
|
|
293
306
|
return {
|
|
294
307
|
...userLiteData,
|
|
@@ -303,4 +316,3 @@ function parseUserData(userLiteData, assetsData, assetsConfig, prices, poolAsset
|
|
|
303
316
|
healthFactor: healthFactor,
|
|
304
317
|
};
|
|
305
318
|
}
|
|
306
|
-
exports.parseUserData = parseUserData;
|
package/dist/api/prices.d.ts
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
1
|
import { PriceData } from '../types/Common';
|
|
2
|
-
export declare function
|
|
2
|
+
export declare function getPrices(endpoints?: string[], nftId?: string): Promise<PriceData>;
|
package/dist/api/prices.js
CHANGED
|
@@ -1,11 +1,11 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.
|
|
3
|
+
exports.getPrices = getPrices;
|
|
4
4
|
const core_1 = require("@ton/core");
|
|
5
|
-
|
|
5
|
+
const general_1 = require("../constants/general");
|
|
6
|
+
async function getPrices(endpoints = ["api.stardust-mainnet.iotaledger.net"], nftId = general_1.MAIN_POOL_NFT_ID) {
|
|
6
7
|
return await Promise.any(endpoints.map(x => loadPrices(nftId, x)));
|
|
7
8
|
}
|
|
8
|
-
exports.getPricesByNft = getPricesByNft;
|
|
9
9
|
async function loadPrices(nftId, endpoint = "api.stardust-mainnet.iotaledger.net") {
|
|
10
10
|
let result = await fetch(`https://${endpoint}/api/indexer/v1/outputs/nft/${nftId}`, {
|
|
11
11
|
headers: { accept: 'application/json' },
|
package/dist/config.d.ts
ADDED
package/dist/config.js
ADDED
|
@@ -1,8 +1,7 @@
|
|
|
1
1
|
import { PoolAssetConfig } from "../types/Master";
|
|
2
|
+
export declare const UNDEFINED_ASSET: PoolAssetConfig;
|
|
2
3
|
export declare const TON_MAINNET: PoolAssetConfig;
|
|
3
|
-
export declare const TON_TESTNET:
|
|
4
|
-
name: string;
|
|
5
|
-
};
|
|
4
|
+
export declare const TON_TESTNET: PoolAssetConfig;
|
|
6
5
|
export declare const JUSDT_MAINNET: PoolAssetConfig;
|
|
7
6
|
export declare const JUSDC_MAINNET: PoolAssetConfig;
|
|
8
7
|
export declare const STTON_MAINNET: PoolAssetConfig;
|
package/dist/constants/assets.js
CHANGED
|
@@ -1,12 +1,20 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.USDT_STORM_MAINNET = exports.TON_STORM_MAINNET = exports.TONUSDT_DEDUST_MAINNET = exports.STTON_TESTNET = exports.JUSDC_TESTNET = exports.JUSDT_TESTNET = exports.USDT_MAINNET = exports.TSTON_MAINNET = exports.STTON_MAINNET = exports.JUSDC_MAINNET = exports.JUSDT_MAINNET = exports.TON_TESTNET = exports.TON_MAINNET = void 0;
|
|
3
|
+
exports.USDT_STORM_MAINNET = exports.TON_STORM_MAINNET = exports.TONUSDT_DEDUST_MAINNET = exports.STTON_TESTNET = exports.JUSDC_TESTNET = exports.JUSDT_TESTNET = exports.USDT_MAINNET = exports.TSTON_MAINNET = exports.STTON_MAINNET = exports.JUSDC_MAINNET = exports.JUSDT_MAINNET = exports.TON_TESTNET = exports.TON_MAINNET = exports.UNDEFINED_ASSET = void 0;
|
|
4
4
|
const core_1 = require("@ton/core");
|
|
5
5
|
const sha256BigInt_1 = require("../utils/sha256BigInt");
|
|
6
6
|
const general_1 = require("./general");
|
|
7
|
+
exports.UNDEFINED_ASSET = {
|
|
8
|
+
name: 'undefined_asset',
|
|
9
|
+
assetId: -1n,
|
|
10
|
+
jettonMasterAddress: general_1.NULL_ADDRESS, // fake
|
|
11
|
+
jettonWalletCode: core_1.Cell.EMPTY
|
|
12
|
+
};
|
|
7
13
|
exports.TON_MAINNET = {
|
|
8
14
|
name: 'TON',
|
|
9
|
-
assetId: (0, sha256BigInt_1.sha256Hash)('TON')
|
|
15
|
+
assetId: (0, sha256BigInt_1.sha256Hash)('TON'),
|
|
16
|
+
jettonMasterAddress: general_1.NULL_ADDRESS, // fake
|
|
17
|
+
jettonWalletCode: core_1.Cell.EMPTY
|
|
10
18
|
};
|
|
11
19
|
exports.TON_TESTNET = exports.TON_MAINNET;
|
|
12
20
|
exports.JUSDT_MAINNET = {
|