@evaafi/sdk 0.5.6 → 0.6.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/api/helpers.js +3 -2
- package/dist/api/math.d.ts +1 -10
- package/dist/api/math.js +26 -74
- package/dist/api/parser.d.ts +2 -1
- package/dist/api/parser.js +42 -56
- package/dist/api/prices.d.ts +2 -1
- package/dist/api/prices.js +29 -19
- package/dist/config.d.ts +1 -2
- package/dist/config.js +2 -3
- package/dist/constants/general.d.ts +7 -5
- package/dist/constants/general.js +29 -10
- package/dist/constants/pools.js +6 -3
- package/dist/constants.d.ts +35 -6
- package/dist/constants.js +47 -92
- package/dist/contracts/MasterContract.d.ts +8 -3
- package/dist/contracts/MasterContract.js +13 -8
- package/dist/index.d.ts +3 -3
- package/dist/index.js +3 -1
- package/dist/types/Common.d.ts +9 -0
- package/dist/types/Master.d.ts +23 -4
- package/dist/types/User.d.ts +4 -1
- package/dist/utils/merkleProof.js +4 -3
- package/dist/utils/priceUtils.d.ts +2 -2
- package/dist/utils/priceUtils.js +16 -16
- package/dist/utils/sha256BigInt.js +2 -1
- package/dist/utils/tonConnectSender.js +3 -2
- package/dist/utils/userJettonWallet.js +2 -1
- package/dist/utils/utils.js +2 -1
- package/package.json +2 -2
- package/src/api/math.ts +16 -80
- package/src/api/parser.ts +33 -51
- package/src/api/prices.ts +33 -57
- package/src/config.ts +1 -0
- package/src/constants/general.ts +32 -9
- package/src/constants/pools.ts +7 -4
- package/src/contracts/MasterContract.ts +22 -17
- package/src/index.ts +4 -0
- package/src/types/Common.ts +10 -0
- package/src/types/Master.ts +23 -9
- package/src/types/User.ts +2 -3
- package/src/utils/merkleProof.ts +141 -0
- package/src/utils/priceUtils.ts +177 -0
package/dist/api/helpers.js
CHANGED
|
@@ -1,7 +1,6 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.loadMyRef =
|
|
4
|
-
exports.loadMaybeMyRef = loadMaybeMyRef;
|
|
3
|
+
exports.loadMaybeMyRef = exports.loadMyRef = void 0;
|
|
5
4
|
const core_1 = require("@ton/core");
|
|
6
5
|
class MyCell extends core_1.Cell {
|
|
7
6
|
toString() {
|
|
@@ -19,6 +18,7 @@ function loadMyRef(slice) {
|
|
|
19
18
|
refs: cell.refs,
|
|
20
19
|
});
|
|
21
20
|
}
|
|
21
|
+
exports.loadMyRef = loadMyRef;
|
|
22
22
|
function loadMaybeMyRef(slice) {
|
|
23
23
|
const cell = slice.loadMaybeRef();
|
|
24
24
|
if (cell === null) {
|
|
@@ -30,3 +30,4 @@ function loadMaybeMyRef(slice) {
|
|
|
30
30
|
refs: cell.refs,
|
|
31
31
|
});
|
|
32
32
|
}
|
|
33
|
+
exports.loadMaybeMyRef = loadMaybeMyRef;
|
package/dist/api/math.d.ts
CHANGED
|
@@ -18,16 +18,7 @@ export declare function calculateAssetData(assetsConfigDict: ExtendedAssetsConfi
|
|
|
18
18
|
export declare function calculateAssetInterest(assetConfig: AssetConfig, assetData: AssetData, masterConstants: MasterConstants): AssetInterest;
|
|
19
19
|
export declare function checkNotInDebtAtAll(principals: Dictionary<bigint, bigint>): boolean;
|
|
20
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;
|
|
21
|
+
export declare function calculateMaximumWithdrawAmount(assetsConfig: ExtendedAssetsConfig, assetsData: ExtendedAssetsData, principals: Dictionary<bigint, bigint>, prices: Dictionary<bigint, bigint>, masterConstants: MasterConstants, assetId: bigint): bigint;
|
|
31
22
|
export declare function getAvailableToBorrow(assetsConfig: ExtendedAssetsConfig, assetsData: ExtendedAssetsData, principals: Dictionary<bigint, bigint>, prices: Dictionary<bigint, bigint>, masterConstants: MasterConstants): bigint;
|
|
32
23
|
export declare function presentValue(sRate: bigint, bRate: bigint, principalValue: bigint, masterConstants: MasterConstants): UserBalance;
|
|
33
24
|
/**
|
package/dist/api/math.js
CHANGED
|
@@ -1,46 +1,33 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.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;
|
|
3
|
+
exports.predictHealthFactor = exports.calculateLiquidationData = exports.presentValue = exports.getAvailableToBorrow = exports.calculateMaximumWithdrawAmount = exports.getAgregatedBalances = exports.checkNotInDebtAtAll = exports.calculateAssetInterest = exports.calculateAssetData = exports.calculateCurrentRates = exports.calculatePresentValue = exports.bigIntMin = exports.bigIntMax = exports.mulDivC = exports.mulDiv = exports.mulFactor = void 0;
|
|
21
4
|
const User_1 = require("../types/User");
|
|
22
|
-
const sha256BigInt_1 = require("../utils/sha256BigInt");
|
|
23
5
|
const assets_1 = require("../constants/assets");
|
|
24
|
-
const __1 = require("..");
|
|
25
6
|
function mulFactor(decimal, a, b) {
|
|
26
7
|
return (a * b) / decimal;
|
|
27
8
|
}
|
|
9
|
+
exports.mulFactor = mulFactor;
|
|
28
10
|
function mulDiv(x, y, z) {
|
|
29
11
|
return (x * y) / z;
|
|
30
12
|
}
|
|
13
|
+
exports.mulDiv = mulDiv;
|
|
31
14
|
function mulDivC(x, y, z) {
|
|
32
15
|
const mul = x * y;
|
|
33
16
|
return mul / z + (mul % z ? 1n : 0n);
|
|
34
17
|
}
|
|
18
|
+
exports.mulDivC = mulDivC;
|
|
35
19
|
function bigIntMax(...args) {
|
|
36
20
|
return args.reduce((m, e) => (e > m ? e : m));
|
|
37
21
|
}
|
|
22
|
+
exports.bigIntMax = bigIntMax;
|
|
38
23
|
function bigIntMin(...args) {
|
|
39
24
|
return args.reduce((m, e) => (e < m ? e : m));
|
|
40
25
|
}
|
|
26
|
+
exports.bigIntMin = bigIntMin;
|
|
41
27
|
function calculatePresentValue(index, principalValue, masterConstants) {
|
|
42
28
|
return (principalValue * index) / masterConstants.FACTOR_SCALE;
|
|
43
29
|
}
|
|
30
|
+
exports.calculatePresentValue = calculatePresentValue;
|
|
44
31
|
function calculateCurrentRates(assetConfig, assetData, masterConstants) {
|
|
45
32
|
const now = BigInt(Math.floor(Date.now() / 1000));
|
|
46
33
|
const timeElapsed = now - assetData.lastAccural;
|
|
@@ -64,6 +51,7 @@ function calculateCurrentRates(assetConfig, assetData, masterConstants) {
|
|
|
64
51
|
now,
|
|
65
52
|
};
|
|
66
53
|
}
|
|
54
|
+
exports.calculateCurrentRates = calculateCurrentRates;
|
|
67
55
|
function calculateAssetData(assetsConfigDict, assetsDataDict, assetId, masterConstants) {
|
|
68
56
|
const config = assetsConfigDict.get(assetId);
|
|
69
57
|
const data = assetsDataDict.get(assetId);
|
|
@@ -82,6 +70,7 @@ function calculateAssetData(assetsConfigDict, assetsDataDict, assetId, masterCon
|
|
|
82
70
|
...{ supplyApy, borrowApy },
|
|
83
71
|
};
|
|
84
72
|
}
|
|
73
|
+
exports.calculateAssetData = calculateAssetData;
|
|
85
74
|
function calculateAssetInterest(assetConfig, assetData, masterConstants) {
|
|
86
75
|
const totalSupply = calculatePresentValue(assetData.sRate, assetData.totalSupply, masterConstants);
|
|
87
76
|
const totalBorrow = calculatePresentValue(assetData.bRate, assetData.totalBorrow, masterConstants);
|
|
@@ -108,9 +97,11 @@ function calculateAssetInterest(assetConfig, assetData, masterConstants) {
|
|
|
108
97
|
borrowInterest,
|
|
109
98
|
};
|
|
110
99
|
}
|
|
100
|
+
exports.calculateAssetInterest = calculateAssetInterest;
|
|
111
101
|
function checkNotInDebtAtAll(principals) {
|
|
112
102
|
return principals.values().every(x => x >= 0n);
|
|
113
103
|
}
|
|
104
|
+
exports.checkNotInDebtAtAll = checkNotInDebtAtAll;
|
|
114
105
|
function getAgregatedBalances(assetsData, assetsConfig, principals, prices, masterConstants) {
|
|
115
106
|
let user_total_supply = 0n;
|
|
116
107
|
let user_total_borrow = 0n;
|
|
@@ -132,53 +123,8 @@ function getAgregatedBalances(assetsData, assetsConfig, principals, prices, mast
|
|
|
132
123
|
}
|
|
133
124
|
return { totalSupply: user_total_supply, totalBorrow: user_total_borrow };
|
|
134
125
|
}
|
|
135
|
-
|
|
136
|
-
|
|
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) {
|
|
126
|
+
exports.getAgregatedBalances = getAgregatedBalances;
|
|
127
|
+
function calculateMaximumWithdrawAmount(assetsConfig, assetsData, principals, prices, masterConstants, assetId) {
|
|
182
128
|
let withdrawAmountMax = 0n;
|
|
183
129
|
const assetConfig = assetsConfig.get(assetId);
|
|
184
130
|
const assetData = assetsData.get(assetId);
|
|
@@ -200,7 +146,8 @@ assetsConfig, assetsData, principals, prices, masterConstants, assetId) {
|
|
|
200
146
|
}
|
|
201
147
|
else if (price > 0) {
|
|
202
148
|
maxAmountToReclaim =
|
|
203
|
-
mulDiv(
|
|
149
|
+
bigIntMax(0n, mulDiv(mulDiv(borrowable, masterConstants.ASSET_COEFFICIENT_SCALE, assetConfig.collateralFactor), 10n ** assetConfig.decimals, price)
|
|
150
|
+
- calculatePresentValue(assetData.sRate, assetConfig.dust, masterConstants) / 2n);
|
|
204
151
|
}
|
|
205
152
|
withdrawAmountMax = bigIntMin(maxAmountToReclaim, oldPresentValue.amount);
|
|
206
153
|
}
|
|
@@ -214,6 +161,7 @@ assetsConfig, assetsData, principals, prices, masterConstants, assetId) {
|
|
|
214
161
|
}
|
|
215
162
|
return withdrawAmountMax;
|
|
216
163
|
}
|
|
164
|
+
exports.calculateMaximumWithdrawAmount = calculateMaximumWithdrawAmount;
|
|
217
165
|
function getAvailableToBorrow(assetsConfig, assetsData, principals, prices, masterConstants) {
|
|
218
166
|
let borrowLimit = 0n;
|
|
219
167
|
let borrowAmount = 0n;
|
|
@@ -223,15 +171,16 @@ function getAvailableToBorrow(assetsConfig, assetsData, principals, prices, mast
|
|
|
223
171
|
const price = prices.get(assetID);
|
|
224
172
|
const principal = principals.get(assetID);
|
|
225
173
|
if (principal < 0) {
|
|
226
|
-
borrowAmount += (calculatePresentValue(assetData.bRate, -principal, masterConstants)
|
|
174
|
+
borrowAmount += mulDiv(calculatePresentValue(assetData.bRate, -principal, masterConstants), price, 10n ** assetConfig.decimals);
|
|
227
175
|
}
|
|
228
176
|
else if (principal > 0) {
|
|
229
177
|
borrowLimit +=
|
|
230
|
-
|
|
178
|
+
mulDiv(mulDiv(calculatePresentValue(assetData.sRate, principal, masterConstants), price, 10n ** assetConfig.decimals), assetConfig.collateralFactor, masterConstants.ASSET_COEFFICIENT_SCALE);
|
|
231
179
|
}
|
|
232
180
|
}
|
|
233
181
|
return borrowLimit - borrowAmount;
|
|
234
182
|
}
|
|
183
|
+
exports.getAvailableToBorrow = getAvailableToBorrow;
|
|
235
184
|
function presentValue(sRate, bRate, principalValue, masterConstants) {
|
|
236
185
|
if (principalValue > 0) {
|
|
237
186
|
return {
|
|
@@ -252,6 +201,7 @@ function presentValue(sRate, bRate, principalValue, masterConstants) {
|
|
|
252
201
|
};
|
|
253
202
|
}
|
|
254
203
|
}
|
|
204
|
+
exports.presentValue = presentValue;
|
|
255
205
|
/**
|
|
256
206
|
*
|
|
257
207
|
* @param assetsConfig
|
|
@@ -314,7 +264,7 @@ function calculateLiquidationData(assetsConfig, assetsData, principals, prices,
|
|
|
314
264
|
loanScale -
|
|
315
265
|
10n;
|
|
316
266
|
minCollateralAmount = (minCollateralAmount * 97n) / 100n;
|
|
317
|
-
if (minCollateralAmount / collateralDecimal >=
|
|
267
|
+
if (minCollateralAmount / collateralDecimal >= 0n) { // todo back to 1
|
|
318
268
|
return {
|
|
319
269
|
greatestCollateralAsset: collateralAsset,
|
|
320
270
|
greatestCollateralValue: collateralValue,
|
|
@@ -338,11 +288,12 @@ function calculateLiquidationData(assetsConfig, assetsData, principals, prices,
|
|
|
338
288
|
liquidable: false,
|
|
339
289
|
};
|
|
340
290
|
}
|
|
291
|
+
exports.calculateLiquidationData = calculateLiquidationData;
|
|
341
292
|
function predictHealthFactor(args) {
|
|
342
293
|
const liquidationData = calculateLiquidationData(args.assetsConfig, args.assetsData, args.principals, args.prices, args.poolConfig);
|
|
343
|
-
const
|
|
344
|
-
const assetConfig = args.assetsConfig.get(
|
|
345
|
-
const assetPrice = Number(args.prices.get(
|
|
294
|
+
const assetId = args.asset.assetId;
|
|
295
|
+
const assetConfig = args.assetsConfig.get(assetId);
|
|
296
|
+
const assetPrice = Number(args.prices.get(assetId));
|
|
346
297
|
let totalLimit = Number(liquidationData.totalLimit);
|
|
347
298
|
let totalBorrow = Number(liquidationData.totalDebt);
|
|
348
299
|
const currentAmount = args.amount;
|
|
@@ -368,3 +319,4 @@ function predictHealthFactor(args) {
|
|
|
368
319
|
}
|
|
369
320
|
return Math.min(Math.max(1 - totalBorrow / totalLimit, 0), 1); // let's limit a result to zero below and one above
|
|
370
321
|
}
|
|
322
|
+
exports.predictHealthFactor = predictHealthFactor;
|
package/dist/api/parser.d.ts
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import { Dictionary, DictionaryValue } from '@ton/core';
|
|
2
2
|
import { AssetConfig, AssetData, ExtendedAssetsConfig, ExtendedAssetsData, MasterConstants, MasterData, PoolAssetsConfig, PoolConfig } from '../types/Master';
|
|
3
|
-
import { UserData, UserLiteData } from '../types/User';
|
|
3
|
+
import { UserData, UserLiteData, UserRewards } from '../types/User';
|
|
4
|
+
export declare function createUserRewards(): DictionaryValue<UserRewards>;
|
|
4
5
|
export declare function createAssetData(): DictionaryValue<AssetData>;
|
|
5
6
|
export declare function createAssetConfig(): DictionaryValue<AssetConfig>;
|
|
6
7
|
export declare function parseMasterData(masterDataBOC: string, poolAssetsConfig: PoolAssetsConfig, masterConstants: MasterConstants): MasterData;
|
package/dist/api/parser.js
CHANGED
|
@@ -1,28 +1,24 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.createAssetData =
|
|
4
|
-
exports.createAssetConfig = createAssetConfig;
|
|
5
|
-
exports.parseMasterData = parseMasterData;
|
|
6
|
-
exports.parseUserLiteData = parseUserLiteData;
|
|
7
|
-
exports.parseUserData = parseUserData;
|
|
3
|
+
exports.parseUserData = exports.parseUserLiteData = exports.parseMasterData = exports.createAssetConfig = exports.createAssetData = exports.createUserRewards = void 0;
|
|
8
4
|
const core_1 = require("@ton/core");
|
|
9
5
|
const math_1 = require("./math");
|
|
10
6
|
const helpers_1 = require("./helpers");
|
|
11
7
|
const User_1 = require("../types/User");
|
|
12
|
-
|
|
13
|
-
/* export function createUserRewards(): DictionaryValue<UserRewards> {
|
|
8
|
+
function createUserRewards() {
|
|
14
9
|
return {
|
|
15
|
-
serialize: (src
|
|
10
|
+
serialize: (src, buidler) => {
|
|
16
11
|
buidler.storeUint(src.trackingIndex, 64);
|
|
17
12
|
buidler.storeUint(src.trackingAccured, 64);
|
|
18
|
-
|
|
19
|
-
parse: (src
|
|
13
|
+
},
|
|
14
|
+
parse: (src) => {
|
|
20
15
|
const trackingIndex = BigInt(src.loadUint(64));
|
|
21
16
|
const trackingAccured = BigInt(src.loadUint(64));
|
|
22
17
|
return { trackingIndex, trackingAccured };
|
|
23
18
|
},
|
|
24
19
|
};
|
|
25
|
-
}
|
|
20
|
+
}
|
|
21
|
+
exports.createUserRewards = createUserRewards;
|
|
26
22
|
function createAssetData() {
|
|
27
23
|
return {
|
|
28
24
|
serialize: (src, buidler) => {
|
|
@@ -32,10 +28,11 @@ function createAssetData() {
|
|
|
32
28
|
buidler.storeUint(src.totalBorrow, 64);
|
|
33
29
|
buidler.storeUint(src.lastAccural, 32);
|
|
34
30
|
buidler.storeUint(src.balance, 64);
|
|
35
|
-
/* Will be in v6
|
|
36
31
|
buidler.storeUint(src.trackingSupplyIndex, 64);
|
|
37
32
|
buidler.storeUint(src.trackingBorrowIndex, 64);
|
|
38
|
-
|
|
33
|
+
if (src.awaitedSupply) {
|
|
34
|
+
buidler.storeUint(src.awaitedSupply, 64);
|
|
35
|
+
}
|
|
39
36
|
},
|
|
40
37
|
parse: (src) => {
|
|
41
38
|
const sRate = BigInt(src.loadInt(64));
|
|
@@ -44,16 +41,17 @@ function createAssetData() {
|
|
|
44
41
|
const totalBorrow = BigInt(src.loadInt(64));
|
|
45
42
|
const lastAccural = BigInt(src.loadInt(32));
|
|
46
43
|
const balance = BigInt(src.loadInt(64));
|
|
47
|
-
/* Will be in v6
|
|
48
44
|
const trackingSupplyIndex = BigInt(src.loadUint(64));
|
|
49
45
|
const trackingBorrowIndex = BigInt(src.loadUint(64));
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
46
|
+
let awaitedSupply = undefined;
|
|
47
|
+
if (src.remainingBits == 64) {
|
|
48
|
+
awaitedSupply = BigInt(src.loadUint(64));
|
|
49
|
+
}
|
|
50
|
+
return { sRate, bRate, totalSupply, totalBorrow, lastAccural, balance, trackingSupplyIndex, trackingBorrowIndex, awaitedSupply };
|
|
54
51
|
},
|
|
55
52
|
};
|
|
56
53
|
}
|
|
54
|
+
exports.createAssetData = createAssetData;
|
|
57
55
|
function createAssetConfig() {
|
|
58
56
|
return {
|
|
59
57
|
serialize: (src, builder) => {
|
|
@@ -74,10 +72,9 @@ function createAssetConfig() {
|
|
|
74
72
|
refBuild.storeUint(src.maxTotalSupply, 64);
|
|
75
73
|
refBuild.storeUint(src.reserveFactor, 16);
|
|
76
74
|
refBuild.storeUint(src.liquidationReserveFactor, 16);
|
|
77
|
-
/* Will be in v6
|
|
78
75
|
refBuild.storeUint(src.minPrincipalForRewards, 64);
|
|
79
76
|
refBuild.storeUint(src.baseTrackingSupplySpeed, 64);
|
|
80
|
-
refBuild.storeUint(src.baseTrackingBorrowSpeed, 64);
|
|
77
|
+
refBuild.storeUint(src.baseTrackingBorrowSpeed, 64);
|
|
81
78
|
builder.storeRef(refBuild.endCell());
|
|
82
79
|
},
|
|
83
80
|
parse: (src) => {
|
|
@@ -98,10 +95,9 @@ function createAssetConfig() {
|
|
|
98
95
|
const maxTotalSupply = ref.loadUintBig(64);
|
|
99
96
|
const reserveFactor = ref.loadUintBig(16);
|
|
100
97
|
const liquidationReserveFactor = ref.loadUintBig(16);
|
|
101
|
-
/* Will be in v6
|
|
102
98
|
const minPrincipalForRewards = ref.loadUintBig(64);
|
|
103
99
|
const baseTrackingSupplySpeed = ref.loadUintBig(64);
|
|
104
|
-
const baseTrackingBorrowSpeed = ref.loadUintBig(64);
|
|
100
|
+
const baseTrackingBorrowSpeed = ref.loadUintBig(64);
|
|
105
101
|
return {
|
|
106
102
|
oracle,
|
|
107
103
|
decimals,
|
|
@@ -119,14 +115,14 @@ function createAssetConfig() {
|
|
|
119
115
|
maxTotalSupply,
|
|
120
116
|
reserveFactor,
|
|
121
117
|
liquidationReserveFactor,
|
|
122
|
-
/* Will be in v6
|
|
123
118
|
minPrincipalForRewards,
|
|
124
119
|
baseTrackingSupplySpeed,
|
|
125
|
-
baseTrackingBorrowSpeed
|
|
120
|
+
baseTrackingBorrowSpeed
|
|
126
121
|
};
|
|
127
122
|
},
|
|
128
123
|
};
|
|
129
124
|
}
|
|
125
|
+
exports.createAssetConfig = createAssetConfig;
|
|
130
126
|
function parseMasterData(masterDataBOC, poolAssetsConfig, masterConstants) {
|
|
131
127
|
const masterSlice = core_1.Cell.fromBase64(masterDataBOC).beginParse();
|
|
132
128
|
const meta = masterSlice.loadRef().beginParse().loadStringTail();
|
|
@@ -138,11 +134,10 @@ function parseMasterData(masterDataBOC, poolAssetsConfig, masterConstants) {
|
|
|
138
134
|
updateTime: upgradeConfigParser.loadUint(64),
|
|
139
135
|
freezeTime: upgradeConfigParser.loadUint(64),
|
|
140
136
|
userCode: (0, helpers_1.loadMyRef)(upgradeConfigParser),
|
|
141
|
-
blankCode: (0, helpers_1.loadMyRef)(upgradeConfigParser),
|
|
142
137
|
newMasterCode: (0, helpers_1.loadMaybeMyRef)(upgradeConfigParser),
|
|
143
138
|
newUserCode: (0, helpers_1.loadMaybeMyRef)(upgradeConfigParser),
|
|
144
139
|
};
|
|
145
|
-
upgradeConfigParser.endParse();
|
|
140
|
+
// upgradeConfigParser.endParse(); todo fix with new testnet contract
|
|
146
141
|
const masterConfigSlice = masterSlice.loadRef().beginParse();
|
|
147
142
|
const assetsConfigDict = masterConfigSlice.loadDict(core_1.Dictionary.Keys.BigUint(256), createAssetConfig());
|
|
148
143
|
const assetsDataDict = masterSlice.loadDict(core_1.Dictionary.Keys.BigUint(256), createAssetData());
|
|
@@ -159,9 +154,12 @@ function parseMasterData(masterDataBOC, poolAssetsConfig, masterConstants) {
|
|
|
159
154
|
const masterConfig = {
|
|
160
155
|
ifActive: masterConfigSlice.loadInt(8),
|
|
161
156
|
admin: masterConfigSlice.loadAddress(),
|
|
162
|
-
|
|
157
|
+
oraclesInfo: {
|
|
158
|
+
numOracles: masterConfigSlice.loadUint(16),
|
|
159
|
+
threshold: masterConfigSlice.loadUint(16),
|
|
160
|
+
oracles: (0, helpers_1.loadMaybeMyRef)(masterConfigSlice)
|
|
161
|
+
},
|
|
163
162
|
tokenKeys: (0, helpers_1.loadMaybeMyRef)(masterConfigSlice),
|
|
164
|
-
walletToMaster: (0, helpers_1.loadMaybeMyRef)(masterConfigSlice),
|
|
165
163
|
};
|
|
166
164
|
masterConfigSlice.endParse();
|
|
167
165
|
for (const [_, asset] of Object.entries(poolAssetsConfig)) {
|
|
@@ -182,6 +180,7 @@ function parseMasterData(masterDataBOC, poolAssetsConfig, masterConstants) {
|
|
|
182
180
|
apy: apy,
|
|
183
181
|
};
|
|
184
182
|
}
|
|
183
|
+
exports.parseMasterData = parseMasterData;
|
|
185
184
|
function parseUserLiteData(userDataBOC, assetsData, assetsConfig, poolConfig, applyDust = true) {
|
|
186
185
|
const poolAssetsConfig = poolConfig.poolAssetsConfig;
|
|
187
186
|
const masterConstants = poolConfig.masterConstants;
|
|
@@ -191,40 +190,33 @@ function parseUserLiteData(userDataBOC, assetsData, assetsConfig, poolConfig, ap
|
|
|
191
190
|
const userAddress = userSlice.loadAddress();
|
|
192
191
|
const principalsDict = userSlice.loadDict(core_1.Dictionary.Keys.BigUint(256), core_1.Dictionary.Values.BigInt(64));
|
|
193
192
|
const userState = userSlice.loadInt(64);
|
|
194
|
-
const trackingSupplyIndex = userSlice.loadUintBig(64);
|
|
195
|
-
const trackingBorrowIndex = userSlice.loadUintBig(64);
|
|
196
|
-
const dutchAuctionStart = userSlice.loadUint(32);
|
|
197
|
-
const backupCell = (0, helpers_1.loadMyRef)(userSlice);
|
|
198
|
-
/* Will be in v6
|
|
199
193
|
let trackingSupplyIndex = 0n;
|
|
200
194
|
let trackingBorrowIndex = 0n;
|
|
201
195
|
let dutchAuctionStart = 0;
|
|
202
|
-
let backupCell = Cell.EMPTY;
|
|
203
|
-
let rewards = Dictionary.empty(Dictionary.Keys.BigUint(256), createUserRewards());
|
|
204
|
-
let backupCell1
|
|
205
|
-
let backupCell2
|
|
196
|
+
let backupCell = core_1.Cell.EMPTY;
|
|
197
|
+
let rewards = core_1.Dictionary.empty(core_1.Dictionary.Keys.BigUint(256), createUserRewards());
|
|
198
|
+
let backupCell1 = null;
|
|
199
|
+
let backupCell2 = null;
|
|
206
200
|
const bitsLeft = userSlice.remainingBits;
|
|
207
201
|
if (bitsLeft > 32) {
|
|
208
202
|
trackingSupplyIndex = userSlice.loadUintBig(64);
|
|
209
203
|
trackingBorrowIndex = userSlice.loadUintBig(64);
|
|
210
204
|
dutchAuctionStart = userSlice.loadUint(32);
|
|
211
|
-
backupCell = loadMyRef(userSlice);
|
|
212
|
-
}
|
|
213
|
-
|
|
205
|
+
backupCell = (0, helpers_1.loadMyRef)(userSlice);
|
|
206
|
+
}
|
|
207
|
+
else {
|
|
208
|
+
rewards = userSlice.loadDict(core_1.Dictionary.Keys.BigUint(256), createUserRewards());
|
|
214
209
|
backupCell1 = userSlice.loadMaybeRef();
|
|
215
210
|
backupCell2 = userSlice.loadMaybeRef();
|
|
216
211
|
}
|
|
217
|
-
*/
|
|
218
212
|
userSlice.endParse();
|
|
219
|
-
const isV5Main = (0, math_1.isV5MainPoolContract)(poolConfig);
|
|
220
213
|
const userBalances = core_1.Dictionary.empty();
|
|
221
214
|
for (const [_, asset] of Object.entries(poolAssetsConfig)) {
|
|
222
215
|
const assetData = assetsData.get(asset.assetId);
|
|
223
216
|
const assetConfig = assetsConfig.get(asset.assetId);
|
|
224
217
|
let principal = principalsDict.get(asset.assetId) || 0n;
|
|
225
218
|
let balance = (0, math_1.presentValue)(assetData.sRate, assetData.bRate, principal, masterConstants);
|
|
226
|
-
|
|
227
|
-
if (applyDust && (principal > 0 && (leftBorder < assetConfig.dust))) { // v6 will be abs(principals) < dust
|
|
219
|
+
if (applyDust && (principal > 0 && (principal < assetConfig.dust))) {
|
|
228
220
|
principal = 0n;
|
|
229
221
|
balance = {
|
|
230
222
|
amount: 0n,
|
|
@@ -246,12 +238,12 @@ function parseUserLiteData(userDataBOC, assetsData, assetsConfig, poolConfig, ap
|
|
|
246
238
|
trackingBorrowIndex: trackingBorrowIndex,
|
|
247
239
|
dutchAuctionStart: dutchAuctionStart,
|
|
248
240
|
backupCell: backupCell,
|
|
249
|
-
/* Will be in v6
|
|
250
241
|
rewards: rewards,
|
|
251
242
|
backupCell1: backupCell1,
|
|
252
|
-
backupCell2: backupCell2,
|
|
243
|
+
backupCell2: backupCell2,
|
|
253
244
|
};
|
|
254
245
|
}
|
|
246
|
+
exports.parseUserLiteData = parseUserLiteData;
|
|
255
247
|
function parseUserData(userLiteData, assetsData, assetsConfig, prices, poolConfig, applyDust = true) {
|
|
256
248
|
const poolAssetsConfig = poolConfig.poolAssetsConfig;
|
|
257
249
|
const masterConstants = poolConfig.masterConstants;
|
|
@@ -259,14 +251,12 @@ function parseUserData(userLiteData, assetsData, assetsConfig, prices, poolConfi
|
|
|
259
251
|
const borrowLimits = core_1.Dictionary.empty();
|
|
260
252
|
let supplyBalance = 0n;
|
|
261
253
|
let borrowBalance = 0n;
|
|
262
|
-
const isV5Main = (0, math_1.isV5MainPoolContract)(poolConfig);
|
|
263
254
|
for (const [_, asset] of Object.entries(poolAssetsConfig)) {
|
|
264
255
|
const assetData = assetsData.get(asset.assetId);
|
|
265
256
|
const assetConfig = assetsConfig.get(asset.assetId);
|
|
266
257
|
let principal = userLiteData.principals.get(asset.assetId) || 0n;
|
|
267
258
|
const balance = (0, math_1.presentValue)(assetData.sRate, assetData.bRate, principal, masterConstants);
|
|
268
|
-
|
|
269
|
-
if (applyDust && (principal > 0 && (leftBorder < assetConfig.dust))) { // v6 will be abs(principals) < dust
|
|
259
|
+
if (applyDust && (principal > 0 && (principal < assetConfig.dust))) {
|
|
270
260
|
principal = 0n;
|
|
271
261
|
userLiteData.principals.set(asset.assetId, 0n);
|
|
272
262
|
}
|
|
@@ -288,14 +278,9 @@ function parseUserData(userLiteData, assetsData, assetsConfig, prices, poolConfi
|
|
|
288
278
|
const assetData = assetsData.get(asset.assetId);
|
|
289
279
|
const balance = userLiteData.balances.get(asset.assetId);
|
|
290
280
|
if (balance.type === User_1.BalanceType.supply) {
|
|
291
|
-
|
|
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
|
-
}
|
|
281
|
+
withdrawalLimits.set(asset.assetId, (0, math_1.bigIntMin)((0, math_1.calculateMaximumWithdrawAmount)(assetsConfig, assetsData, userLiteData.principals, prices, masterConstants, asset.assetId), assetData.balance));
|
|
297
282
|
}
|
|
298
|
-
borrowLimits.set(asset.assetId, (0, math_1.bigIntMin)((availableToBorrow * 10n ** assetConfig.decimals) / prices.get(asset.assetId), assetData.balance));
|
|
283
|
+
borrowLimits.set(asset.assetId, (0, math_1.bigIntMax)(0n, (0, math_1.bigIntMin)((availableToBorrow * 10n ** assetConfig.decimals) / prices.get(asset.assetId), assetData.balance, assetData.totalSupply - assetData.totalBorrow)));
|
|
299
284
|
}
|
|
300
285
|
const limitUsed = borrowBalance + availableToBorrow;
|
|
301
286
|
const limitUsedPercent = limitUsed === 0n
|
|
@@ -316,3 +301,4 @@ function parseUserData(userLiteData, assetsData, assetsConfig, prices, poolConfi
|
|
|
316
301
|
healthFactor: healthFactor,
|
|
317
302
|
};
|
|
318
303
|
}
|
|
304
|
+
exports.parseUserData = parseUserData;
|
package/dist/api/prices.d.ts
CHANGED
|
@@ -1,2 +1,3 @@
|
|
|
1
1
|
import { PriceData } from '../types/Common';
|
|
2
|
-
|
|
2
|
+
import { PoolConfig } from '../types/Master';
|
|
3
|
+
export declare function getPrices(endpoints?: String[], poolConfig?: PoolConfig): Promise<PriceData>;
|
package/dist/api/prices.js
CHANGED
|
@@ -1,25 +1,35 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.getPrices =
|
|
3
|
+
exports.getPrices = void 0;
|
|
4
4
|
const core_1 = require("@ton/core");
|
|
5
|
-
const
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
5
|
+
const priceUtils_1 = require("../utils/priceUtils");
|
|
6
|
+
const pools_1 = require("../constants/pools");
|
|
7
|
+
async function getPrices(endpoints = ["api.stardust-mainnet.iotaledger.net", "iota.evaa.finance"], poolConfig = pools_1.MAINNET_POOL_CONFIG) {
|
|
8
|
+
if (endpoints.length == 0) {
|
|
9
|
+
throw new Error("Empty endpoint list");
|
|
10
|
+
}
|
|
11
|
+
const prices = await Promise.all(poolConfig.oracles.map(async (x) => await (0, priceUtils_1.parsePrices)(await (0, priceUtils_1.loadPrices)(x.address, endpoints), x.id)));
|
|
12
|
+
let acceptedPrices = prices.filter((0, priceUtils_1.verifyPrices)(poolConfig.poolAssetsConfig));
|
|
13
|
+
if (acceptedPrices.length < poolConfig.minimalOracles) {
|
|
14
|
+
throw new Error("Prices are outdated");
|
|
15
|
+
}
|
|
16
|
+
if (acceptedPrices.length > poolConfig.minimalOracles && acceptedPrices.length % 2 == 0) {
|
|
17
|
+
acceptedPrices = acceptedPrices.slice(0, acceptedPrices.length - 1); // to reduce fees, MINIMAL_ORACLES_NUMBER is odd
|
|
18
|
+
}
|
|
19
|
+
if (acceptedPrices.length != poolConfig.minimalOracles) {
|
|
20
|
+
const sortedByTimestamp = acceptedPrices.slice().sort((a, b) => b.timestamp - a.timestamp);
|
|
21
|
+
const newerPrices = sortedByTimestamp.slice(0, poolConfig.minimalOracles);
|
|
22
|
+
acceptedPrices = newerPrices.sort((a, b) => a.oracleId - b.oracleId);
|
|
23
|
+
}
|
|
24
|
+
const medianData = poolConfig.poolAssetsConfig.map(asset => ({ assetId: asset.assetId, medianPrice: (0, priceUtils_1.getMedianPrice)(acceptedPrices, asset.assetId) }));
|
|
25
|
+
const packedMedianData = (0, priceUtils_1.packAssetsData)(medianData);
|
|
26
|
+
const oraclesData = acceptedPrices.map(x => ({ oracle: { id: x.oracleId, pubkey: x.pubkey }, data: { timestamp: x.timestamp, prices: x.dict }, signature: x.signature }));
|
|
27
|
+
const packedOracleData = (0, priceUtils_1.packOraclesData)(oraclesData, poolConfig.poolAssetsConfig.map(x => x.assetId));
|
|
28
|
+
const dict = core_1.Dictionary.empty();
|
|
29
|
+
medianData.forEach(x => dict.set(x.assetId, x.medianPrice));
|
|
21
30
|
return {
|
|
22
|
-
dict:
|
|
23
|
-
dataCell: (0,
|
|
31
|
+
dict: dict,
|
|
32
|
+
dataCell: (0, priceUtils_1.packPrices)(packedMedianData, packedOracleData)
|
|
24
33
|
};
|
|
25
34
|
}
|
|
35
|
+
exports.getPrices = getPrices;
|
package/dist/config.d.ts
CHANGED
|
@@ -1,2 +1 @@
|
|
|
1
|
-
export declare const
|
|
2
|
-
export declare const TTL_ORACLE_DATA_SEC = 170;
|
|
1
|
+
export declare const TTL_ORACLE_DATA_SEC = 120;
|
package/dist/config.js
CHANGED
|
@@ -1,5 +1,4 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.TTL_ORACLE_DATA_SEC =
|
|
4
|
-
exports.
|
|
5
|
-
exports.TTL_ORACLE_DATA_SEC = 170; // todo back to 120
|
|
3
|
+
exports.TTL_ORACLE_DATA_SEC = void 0;
|
|
4
|
+
exports.TTL_ORACLE_DATA_SEC = 120; // todo back to 120
|
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import { Address, Cell } from '@ton/core';
|
|
2
|
+
import { OracleNFT } from '../types/Master';
|
|
2
3
|
export declare const MASTER_CONSTANTS: {
|
|
3
4
|
FACTOR_SCALE: bigint;
|
|
4
5
|
ASSET_COEFFICIENT_SCALE: bigint;
|
|
@@ -11,13 +12,14 @@ export declare const MASTER_CONSTANTS: {
|
|
|
11
12
|
};
|
|
12
13
|
export declare const NULL_ADDRESS: Address;
|
|
13
14
|
export declare const EVAA_MASTER_MAINNET: Address;
|
|
14
|
-
export declare const MAINNET_VERSION =
|
|
15
|
+
export declare const MAINNET_VERSION = 6;
|
|
15
16
|
export declare const EVAA_MASTER_TESTNET: Address;
|
|
16
|
-
export declare const TESTNET_VERSION =
|
|
17
|
+
export declare const TESTNET_VERSION = 0;
|
|
17
18
|
export declare const EVAA_LP_MAINNET: Address;
|
|
18
|
-
export declare const EVAA_LP_MAINNET_VERSION =
|
|
19
|
-
export declare const
|
|
20
|
-
export declare const
|
|
19
|
+
export declare const EVAA_LP_MAINNET_VERSION = 2;
|
|
20
|
+
export declare const ORACLES_MAINNET: OracleNFT[];
|
|
21
|
+
export declare const ORACLES_TESTNET: OracleNFT[];
|
|
22
|
+
export declare const ORACLES_LP: OracleNFT[];
|
|
21
23
|
export declare const LENDING_CODE: Cell;
|
|
22
24
|
export declare const JETTON_WALLET_STANDART_CODE: Cell;
|
|
23
25
|
export declare const JETTON_WALLET_STANDART_CODE_TESTNET: Cell;
|