@evaafi/sdk 0.5.6-a → 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/math.d.ts +1 -10
- package/dist/api/math.js +12 -68
- package/dist/api/parser.d.ts +2 -1
- package/dist/api/parser.js +37 -52
- package/dist/api/prices.d.ts +2 -1
- package/dist/api/prices.js +28 -19
- package/dist/config.d.ts +1 -1
- package/dist/config.js +1 -1
- package/dist/constants/general.d.ts +7 -5
- package/dist/constants/general.js +29 -10
- package/dist/constants/pools.js +6 -3
- 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/package.json +2 -2
- package/src/api/math.ts +20 -89
- 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
|
@@ -41,9 +41,8 @@ class Evaa {
|
|
|
41
41
|
.storeUint(general_1.OPCODES.SUPPLY, 32)
|
|
42
42
|
.storeInt(parameters.includeUserCode ? -1 : 0, 2)
|
|
43
43
|
.storeAddress(parameters.userAddress)
|
|
44
|
-
/* Will be in v6
|
|
45
44
|
.storeUint(parameters.amountToTransfer, 64)
|
|
46
|
-
.storeRef(parameters.payload)
|
|
45
|
+
.storeRef(parameters.payload)
|
|
47
46
|
.endCell())
|
|
48
47
|
.endCell();
|
|
49
48
|
}
|
|
@@ -54,9 +53,8 @@ class Evaa {
|
|
|
54
53
|
.storeInt(parameters.includeUserCode ? -1 : 0, 2)
|
|
55
54
|
.storeUint(parameters.amount, 64)
|
|
56
55
|
.storeAddress(parameters.userAddress)
|
|
57
|
-
/* Will be in v6
|
|
58
56
|
.storeUint(parameters.amountToTransfer, 64)
|
|
59
|
-
.storeRef(parameters.payload)
|
|
57
|
+
.storeRef(parameters.payload)
|
|
60
58
|
.endCell();
|
|
61
59
|
}
|
|
62
60
|
}
|
|
@@ -72,9 +70,8 @@ class Evaa {
|
|
|
72
70
|
.storeUint(parameters.amount, 64)
|
|
73
71
|
.storeAddress(parameters.userAddress)
|
|
74
72
|
.storeInt(parameters.includeUserCode ? -1 : 0, 2)
|
|
75
|
-
/* Will be in v6
|
|
76
73
|
.storeUint(parameters.amountToTransfer, 64)
|
|
77
|
-
.storeRef(parameters.payload)
|
|
74
|
+
.storeRef(parameters.payload)
|
|
78
75
|
.storeRef(parameters.priceData)
|
|
79
76
|
.endCell();
|
|
80
77
|
}
|
|
@@ -103,6 +100,10 @@ class Evaa {
|
|
|
103
100
|
// do not need liquidationAmount in case of jetton liquidation because
|
|
104
101
|
// the exact amount of transferred jettons for liquidation is known
|
|
105
102
|
.storeUint(0, 64)
|
|
103
|
+
.storeRef((0, core_1.beginCell)()
|
|
104
|
+
.storeUint(parameters.forwardAmount ?? 0, 64) // idk .. ) todo check
|
|
105
|
+
.storeRef(parameters.payload)
|
|
106
|
+
.endCell())
|
|
106
107
|
.storeRef(parameters.priceData)
|
|
107
108
|
.endCell())
|
|
108
109
|
.endCell();
|
|
@@ -117,6 +118,10 @@ class Evaa {
|
|
|
117
118
|
.storeUint(parameters.minCollateralAmount, 64)
|
|
118
119
|
.storeInt(parameters.includeUserCode ? -1 : 0, 2)
|
|
119
120
|
.storeUint(parameters.liquidationAmount, 64)
|
|
121
|
+
.storeRef((0, core_1.beginCell)()
|
|
122
|
+
.storeUint(parameters.forwardAmount ?? 0, 64) // idk .. ) todo check
|
|
123
|
+
.storeRef(parameters.payload)
|
|
124
|
+
.endCell())
|
|
120
125
|
.storeRef(parameters.priceData)
|
|
121
126
|
.endCell();
|
|
122
127
|
}
|
|
@@ -233,10 +238,10 @@ class Evaa {
|
|
|
233
238
|
}
|
|
234
239
|
async getPrices(provider, endpoints) {
|
|
235
240
|
if ((endpoints?.length ?? 0) > 0) {
|
|
236
|
-
return await (0, __1.getPrices)(endpoints, this.poolConfig
|
|
241
|
+
return await (0, __1.getPrices)(endpoints, this.poolConfig);
|
|
237
242
|
}
|
|
238
243
|
else {
|
|
239
|
-
return await (0, __1.getPrices)(undefined, this.poolConfig
|
|
244
|
+
return await (0, __1.getPrices)(undefined, this.poolConfig);
|
|
240
245
|
}
|
|
241
246
|
}
|
|
242
247
|
}
|
package/dist/index.d.ts
CHANGED
|
@@ -1,11 +1,11 @@
|
|
|
1
|
-
export { mulFactor, mulDiv, bigIntMin, bigIntMax, calculatePresentValue, calculateCurrentRates, calculateAssetData, calculateAssetInterest, getAvailableToBorrow, calculateMaximumWithdrawAmount, presentValue, calculateLiquidationData, } from './api/math';
|
|
1
|
+
export { mulFactor, mulDiv, bigIntMin, bigIntMax, calculatePresentValue, calculateCurrentRates, calculateAssetData, calculateAssetInterest, getAvailableToBorrow, calculateMaximumWithdrawAmount, presentValue, calculateLiquidationData, predictHealthFactor } from './api/math';
|
|
2
2
|
export { createAssetData, createAssetConfig, parseMasterData, parseUserData, parseUserLiteData } from './api/parser';
|
|
3
3
|
export { getPrices } from './api/prices';
|
|
4
4
|
export { JettonWallet } from './contracts/JettonWallet';
|
|
5
|
-
export { EvaaParameters, WithdrawParameters, LiquidationBaseData, Evaa, } from './contracts/MasterContract';
|
|
5
|
+
export { EvaaParameters, WithdrawParameters, LiquidationBaseData, LiquidationParameters, Evaa, } from './contracts/MasterContract';
|
|
6
6
|
export { EvaaUser } from './contracts/UserContract';
|
|
7
7
|
export { PriceData } from './types/Common';
|
|
8
|
-
export { UpgradeConfig, AssetConfig, MasterConfig, AssetData, AssetInterest, AssetApy, ExtendedAssetData, MasterData, PoolConfig, ExtendedAssetsData, ExtendedAssetsConfig, PoolAssetConfig, PoolAssetsConfig, } from './types/Master';
|
|
8
|
+
export { UpgradeConfig, AssetConfig, MasterConfig, AssetData, AssetInterest, AssetApy, ExtendedAssetData, MasterData, PoolConfig, ExtendedAssetsData, ExtendedAssetsConfig, PoolAssetConfig, PoolAssetsConfig, MasterConstants } from './types/Master';
|
|
9
9
|
export { BalanceType, UserBalance, UserLiqudationData, LiquidableData, NonLiquidableData, LiquidationData, UserDataInactive, UserDataActive, UserData, BalanceChangeType } from './types/User';
|
|
10
10
|
export { EVAA_MASTER_MAINNET, MAINNET_VERSION, EVAA_MASTER_TESTNET, TESTNET_VERSION, LENDING_CODE, OPCODES, FEES, MASTER_CONSTANTS } from './constants/general';
|
|
11
11
|
export { MAINNET_POOL_CONFIG, TESTNET_POOL_CONFIG, MAINNET_LP_POOL_CONFIG, } from './constants/pools';
|
package/dist/index.js
CHANGED
|
@@ -14,7 +14,8 @@ var __exportStar = (this && this.__exportStar) || function(m, exports) {
|
|
|
14
14
|
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
|
|
15
15
|
};
|
|
16
16
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
17
|
-
exports.
|
|
17
|
+
exports.getTonConnectSender = exports.getLastSentBoc = exports.STTON_TESTNET = exports.JUSDC_TESTNET = exports.JUSDT_TESTNET = exports.TSTON_MAINNET = exports.STTON_MAINNET = exports.JUSDC_MAINNET = exports.JUSDT_MAINNET = exports.USDT_STORM_MAINNET = exports.TON_STORM_MAINNET = exports.TONUSDT_DEDUST_MAINNET = exports.USDT_MAINNET = exports.TON_MAINNET = exports.UNDEFINED_ASSET = exports.MAINNET_LP_POOL_CONFIG = exports.TESTNET_POOL_CONFIG = exports.MAINNET_POOL_CONFIG = exports.MASTER_CONSTANTS = exports.FEES = exports.OPCODES = exports.LENDING_CODE = exports.TESTNET_VERSION = exports.EVAA_MASTER_TESTNET = exports.MAINNET_VERSION = exports.EVAA_MASTER_MAINNET = exports.BalanceChangeType = exports.BalanceType = exports.EvaaUser = exports.Evaa = exports.JettonWallet = exports.getPrices = exports.parseUserLiteData = exports.parseUserData = exports.parseMasterData = exports.createAssetConfig = exports.createAssetData = exports.predictHealthFactor = exports.calculateLiquidationData = exports.presentValue = exports.calculateMaximumWithdrawAmount = exports.getAvailableToBorrow = exports.calculateAssetInterest = exports.calculateAssetData = exports.calculateCurrentRates = exports.calculatePresentValue = exports.bigIntMax = exports.bigIntMin = exports.mulDiv = exports.mulFactor = void 0;
|
|
18
|
+
exports.getUserJettonWallet = void 0;
|
|
18
19
|
// Math
|
|
19
20
|
var math_1 = require("./api/math");
|
|
20
21
|
Object.defineProperty(exports, "mulFactor", { enumerable: true, get: function () { return math_1.mulFactor; } });
|
|
@@ -29,6 +30,7 @@ Object.defineProperty(exports, "getAvailableToBorrow", { enumerable: true, get:
|
|
|
29
30
|
Object.defineProperty(exports, "calculateMaximumWithdrawAmount", { enumerable: true, get: function () { return math_1.calculateMaximumWithdrawAmount; } });
|
|
30
31
|
Object.defineProperty(exports, "presentValue", { enumerable: true, get: function () { return math_1.presentValue; } });
|
|
31
32
|
Object.defineProperty(exports, "calculateLiquidationData", { enumerable: true, get: function () { return math_1.calculateLiquidationData; } });
|
|
33
|
+
Object.defineProperty(exports, "predictHealthFactor", { enumerable: true, get: function () { return math_1.predictHealthFactor; } });
|
|
32
34
|
// Parser
|
|
33
35
|
var parser_1 = require("./api/parser");
|
|
34
36
|
Object.defineProperty(exports, "createAssetData", { enumerable: true, get: function () { return parser_1.createAssetData; } });
|
package/dist/types/Common.d.ts
CHANGED
|
@@ -1,4 +1,13 @@
|
|
|
1
|
+
/// <reference types="node" />
|
|
1
2
|
import { Cell, Dictionary } from '@ton/core';
|
|
3
|
+
export type RawPriceData = {
|
|
4
|
+
dict: Dictionary<bigint, bigint>;
|
|
5
|
+
dataCell: Cell;
|
|
6
|
+
oracleId: number;
|
|
7
|
+
signature: Buffer;
|
|
8
|
+
pubkey: Buffer;
|
|
9
|
+
timestamp: number;
|
|
10
|
+
};
|
|
2
11
|
export type PriceData = {
|
|
3
12
|
dict: Dictionary<bigint, bigint>;
|
|
4
13
|
dataCell: Cell;
|
package/dist/types/Master.d.ts
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
/// <reference types="node" />
|
|
1
2
|
import { Address, Cell, Dictionary } from '@ton/core';
|
|
2
3
|
export type MasterConstants = {
|
|
3
4
|
FACTOR_SCALE: bigint;
|
|
@@ -20,7 +21,8 @@ export type PoolConfig = {
|
|
|
20
21
|
masterAddress: Address;
|
|
21
22
|
masterVersion: number;
|
|
22
23
|
masterConstants: MasterConstants;
|
|
23
|
-
|
|
24
|
+
oracles: OracleNFT[];
|
|
25
|
+
minimalOracles: number;
|
|
24
26
|
poolAssetsConfig: PoolAssetsConfig;
|
|
25
27
|
lendingCode: Cell;
|
|
26
28
|
};
|
|
@@ -31,7 +33,6 @@ export type UpgradeConfig = {
|
|
|
31
33
|
updateTime: number;
|
|
32
34
|
freezeTime: number;
|
|
33
35
|
userCode: Cell;
|
|
34
|
-
blankCode: Cell;
|
|
35
36
|
newMasterCode: Cell | null;
|
|
36
37
|
newUserCode: Cell | null;
|
|
37
38
|
};
|
|
@@ -52,13 +53,20 @@ export type AssetConfig = {
|
|
|
52
53
|
maxTotalSupply: bigint;
|
|
53
54
|
reserveFactor: bigint;
|
|
54
55
|
liquidationReserveFactor: bigint;
|
|
56
|
+
minPrincipalForRewards: bigint;
|
|
57
|
+
baseTrackingSupplySpeed: bigint;
|
|
58
|
+
baseTrackingBorrowSpeed: bigint;
|
|
55
59
|
};
|
|
56
60
|
export type MasterConfig = {
|
|
57
61
|
ifActive: number;
|
|
58
62
|
admin: Address;
|
|
59
|
-
|
|
63
|
+
oraclesInfo: OraclesInfo;
|
|
60
64
|
tokenKeys: Cell | null;
|
|
61
|
-
|
|
65
|
+
};
|
|
66
|
+
export type OraclesInfo = {
|
|
67
|
+
numOracles: number;
|
|
68
|
+
threshold: number;
|
|
69
|
+
oracles: Cell | null;
|
|
62
70
|
};
|
|
63
71
|
export type AssetData = {
|
|
64
72
|
sRate: bigint;
|
|
@@ -67,6 +75,9 @@ export type AssetData = {
|
|
|
67
75
|
totalBorrow: bigint;
|
|
68
76
|
lastAccural: bigint;
|
|
69
77
|
balance: bigint;
|
|
78
|
+
trackingSupplyIndex: bigint;
|
|
79
|
+
trackingBorrowIndex: bigint;
|
|
80
|
+
awaitedSupply?: bigint;
|
|
70
81
|
};
|
|
71
82
|
export type AssetInterest = {
|
|
72
83
|
supplyInterest: bigint;
|
|
@@ -95,3 +106,11 @@ export type AgregatedBalances = {
|
|
|
95
106
|
totalBorrow: bigint;
|
|
96
107
|
totalSupply: bigint;
|
|
97
108
|
};
|
|
109
|
+
export type OracleNFT = {
|
|
110
|
+
id: number;
|
|
111
|
+
address: string;
|
|
112
|
+
};
|
|
113
|
+
export type Oracle = {
|
|
114
|
+
id: number;
|
|
115
|
+
pubkey: Buffer;
|
|
116
|
+
};
|
package/dist/types/User.d.ts
CHANGED
|
@@ -37,6 +37,9 @@ export type UserLiteData = {
|
|
|
37
37
|
trackingBorrowIndex: bigint;
|
|
38
38
|
dutchAuctionStart: number;
|
|
39
39
|
backupCell: Cell;
|
|
40
|
+
rewards: Dictionary<bigint, UserRewards>;
|
|
41
|
+
backupCell1: Cell | null;
|
|
42
|
+
backupCell2: Cell | null;
|
|
40
43
|
};
|
|
41
44
|
export type UserDataActive = UserLiteData & {
|
|
42
45
|
withdrawalLimits: Dictionary<bigint, bigint>;
|
|
@@ -67,7 +70,7 @@ export declare enum BalanceChangeType {
|
|
|
67
70
|
export type PredictHealthFactorArgs = {
|
|
68
71
|
balanceChangeType: BalanceChangeType;
|
|
69
72
|
amount: bigint;
|
|
70
|
-
|
|
73
|
+
asset: PoolAssetConfig;
|
|
71
74
|
principals: Dictionary<bigint, bigint>;
|
|
72
75
|
prices: Dictionary<bigint, bigint>;
|
|
73
76
|
assetsData: ExtendedAssetsData;
|
package/package.json
CHANGED
package/src/api/math.ts
CHANGED
|
@@ -14,9 +14,8 @@ export function mulDiv(x: bigint, y: bigint, z: bigint): bigint {
|
|
|
14
14
|
}
|
|
15
15
|
|
|
16
16
|
export function mulDivC(x: bigint, y: bigint, z: bigint): bigint {
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
return BigInt(Math.ceil(Number(x * y) / Number(z)));
|
|
17
|
+
const mul = x * y;
|
|
18
|
+
return mul / z + (mul % z ? 1n : 0n);
|
|
20
19
|
}
|
|
21
20
|
|
|
22
21
|
export function bigIntMax(...args: bigint[]): bigint {
|
|
@@ -148,89 +147,19 @@ export function getAgregatedBalances (
|
|
|
148
147
|
const price = prices.get(assetId)!;
|
|
149
148
|
const assetData = assetsData.get(assetId)!;
|
|
150
149
|
const assetConfig = assetsConfig.get(assetId)!;
|
|
151
|
-
|
|
150
|
+
|
|
152
151
|
if (principal < 0) {
|
|
153
152
|
user_total_borrow += presentValue(assetData.sRate, assetData.bRate, principal, masterConstants).amount * price / 10n ** assetConfig.decimals;
|
|
154
153
|
} else {
|
|
155
154
|
user_total_supply += presentValue(assetData.sRate, assetData.bRate, principal, masterConstants).amount * price / 10n ** assetConfig.decimals;
|
|
156
155
|
}
|
|
157
|
-
|
|
156
|
+
|
|
158
157
|
}
|
|
159
158
|
}
|
|
160
159
|
return {totalSupply: user_total_supply, totalBorrow: user_total_borrow};
|
|
161
160
|
}
|
|
162
161
|
|
|
163
|
-
|
|
164
|
-
* @deprecated The method should be used only for main contract v5
|
|
165
|
-
*/
|
|
166
|
-
export function isV5MainPoolContract(poolConfig: PoolConfig): boolean {
|
|
167
|
-
if ((poolConfig.masterAddress == MAINNET_POOL_CONFIG.masterAddress && poolConfig.masterVersion == 5) ||
|
|
168
|
-
(poolConfig.masterAddress == TESTNET_POOL_CONFIG.masterAddress && poolConfig.masterVersion == 5)) {
|
|
169
|
-
return true;
|
|
170
|
-
} else {
|
|
171
|
-
return false;
|
|
172
|
-
}
|
|
173
|
-
}
|
|
174
|
-
|
|
175
|
-
/**
|
|
176
|
-
* @deprecated The method should be used only for main contract v5
|
|
177
|
-
*/
|
|
178
|
-
export function calculateMaximumWithdrawAmountOld(
|
|
179
|
-
assetsConfig: ExtendedAssetsConfig,
|
|
180
|
-
assetsData: ExtendedAssetsData,
|
|
181
|
-
principals: Dictionary<bigint, bigint>,
|
|
182
|
-
prices: Dictionary<bigint, bigint>,
|
|
183
|
-
masterConstants: MasterConstants,
|
|
184
|
-
assetId: bigint,
|
|
185
|
-
): bigint {
|
|
186
|
-
let withdrawAmountMax = 0n;
|
|
187
|
-
|
|
188
|
-
const assetConfig = assetsConfig.get(assetId) as AssetConfig;
|
|
189
|
-
const assetData = assetsData.get(assetId) as ExtendedAssetData;
|
|
190
|
-
const oldPrincipal = principals.get(assetId) as bigint;
|
|
191
|
-
const oldPresentValue = presentValue(assetData.sRate, assetData.bRate, oldPrincipal, masterConstants);
|
|
192
|
-
|
|
193
|
-
if (oldPresentValue.amount > assetConfig.dust) {
|
|
194
|
-
if(checkNotInDebtAtAll(principals)) {
|
|
195
|
-
withdrawAmountMax = oldPresentValue.amount;
|
|
196
|
-
} else {
|
|
197
|
-
if (!prices.has(assetId)) {
|
|
198
|
-
return 0n;
|
|
199
|
-
}
|
|
200
|
-
|
|
201
|
-
//const borrowable = getAvailableToBorrow(assetsConfig, assetsData, principals, prices, masterConstants);
|
|
202
|
-
const price = prices.get(assetId) as bigint;
|
|
203
|
-
const agregatedBalances = getAgregatedBalances(assetsData, assetsConfig, principals, prices, masterConstants);
|
|
204
|
-
let maxAmountToReclaim =
|
|
205
|
-
mulDiv(
|
|
206
|
-
agregatedBalances.totalSupply - mulDivC(agregatedBalances.totalBorrow, masterConstants.ASSET_COEFFICIENT_SCALE, assetConfig.collateralFactor),
|
|
207
|
-
10n**assetConfig.decimals,
|
|
208
|
-
price
|
|
209
|
-
);
|
|
210
|
-
|
|
211
|
-
withdrawAmountMax = bigIntMin(
|
|
212
|
-
maxAmountToReclaim,
|
|
213
|
-
oldPresentValue.amount
|
|
214
|
-
);
|
|
215
|
-
//console.log('agregatedBalances', agregatedBalances);
|
|
216
|
-
//console.log('masterConstants.ASSET_COEFFICIENT_SCALE', masterConstants.ASSET_COEFFICIENT_SCALE);
|
|
217
|
-
//console.log('assetConfig.collateralFactor', assetConfig.collateralFactor);
|
|
218
|
-
//console.log('sekay', maxAmountToReclaim, oldPresentValue.amount);
|
|
219
|
-
}
|
|
220
|
-
} else {
|
|
221
|
-
if (!prices.has(assetId)) {
|
|
222
|
-
return 0n;
|
|
223
|
-
}
|
|
224
|
-
|
|
225
|
-
const price = prices.get(assetId) as bigint;
|
|
226
|
-
|
|
227
|
-
return getAvailableToBorrow(assetsConfig, assetsData, principals, prices, masterConstants) * (10n ** assetConfig.decimals) / price;
|
|
228
|
-
}
|
|
229
|
-
|
|
230
|
-
return withdrawAmountMax;
|
|
231
|
-
}
|
|
232
|
-
|
|
233
|
-
export function calculateMaximumWithdrawAmount( // todo v6 ifelse dust not debt at all is fixed?
|
|
162
|
+
export function calculateMaximumWithdrawAmount(
|
|
234
163
|
assetsConfig: ExtendedAssetsConfig,
|
|
235
164
|
assetsData: ExtendedAssetsData,
|
|
236
165
|
principals: Dictionary<bigint, bigint>,
|
|
@@ -263,10 +192,12 @@ export function calculateMaximumWithdrawAmount( // todo v6 ifelse dust not deb
|
|
|
263
192
|
}
|
|
264
193
|
else if (price > 0) {
|
|
265
194
|
maxAmountToReclaim =
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
|
|
195
|
+
bigIntMax(0n,
|
|
196
|
+
mulDiv(
|
|
197
|
+
mulDiv(borrowable, masterConstants.ASSET_COEFFICIENT_SCALE, assetConfig.collateralFactor),
|
|
198
|
+
10n ** assetConfig.decimals, price)
|
|
199
|
+
- calculatePresentValue(assetData.sRate, assetConfig.dust, masterConstants) / 2n
|
|
200
|
+
);
|
|
270
201
|
}
|
|
271
202
|
|
|
272
203
|
withdrawAmountMax = bigIntMin(
|
|
@@ -304,13 +235,13 @@ export function getAvailableToBorrow(
|
|
|
304
235
|
const principal = principals.get(assetID) as bigint;
|
|
305
236
|
|
|
306
237
|
if (principal < 0) {
|
|
307
|
-
borrowAmount += (calculatePresentValue(assetData.bRate, -principal, masterConstants)
|
|
238
|
+
borrowAmount += mulDiv(calculatePresentValue(assetData.bRate, -principal, masterConstants), price, 10n ** assetConfig.decimals);
|
|
308
239
|
} else if (principal > 0) {
|
|
309
240
|
borrowLimit +=
|
|
310
|
-
|
|
311
|
-
|
|
312
|
-
|
|
313
|
-
|
|
241
|
+
mulDiv(
|
|
242
|
+
mulDiv(calculatePresentValue(assetData.sRate, principal, masterConstants), price, 10n ** assetConfig.decimals),
|
|
243
|
+
assetConfig.collateralFactor,
|
|
244
|
+
masterConstants.ASSET_COEFFICIENT_SCALE);
|
|
314
245
|
}
|
|
315
246
|
}
|
|
316
247
|
|
|
@@ -410,7 +341,7 @@ export function calculateLiquidationData(
|
|
|
410
341
|
loanScale -
|
|
411
342
|
10n;
|
|
412
343
|
minCollateralAmount = (minCollateralAmount * 97n) / 100n;
|
|
413
|
-
if (minCollateralAmount / collateralDecimal >=
|
|
344
|
+
if (minCollateralAmount / collateralDecimal >= 0n) { // todo back to 1
|
|
414
345
|
return {
|
|
415
346
|
greatestCollateralAsset: collateralAsset,
|
|
416
347
|
greatestCollateralValue: collateralValue,
|
|
@@ -438,10 +369,10 @@ export function calculateLiquidationData(
|
|
|
438
369
|
|
|
439
370
|
export function predictHealthFactor(args: PredictHealthFactorArgs): number {
|
|
440
371
|
const liquidationData = calculateLiquidationData(args.assetsConfig, args.assetsData, args.principals, args.prices, args.poolConfig);
|
|
441
|
-
const
|
|
372
|
+
const assetId = args.asset.assetId;
|
|
442
373
|
|
|
443
|
-
const assetConfig = args.assetsConfig.get(
|
|
444
|
-
const assetPrice = Number(args.prices.get(
|
|
374
|
+
const assetConfig = args.assetsConfig.get(assetId)!;
|
|
375
|
+
const assetPrice = Number(args.prices.get(assetId)!);
|
|
445
376
|
|
|
446
377
|
let totalLimit = Number(liquidationData.totalLimit);
|
|
447
378
|
let totalBorrow = Number(liquidationData.totalDebt);
|
package/src/api/parser.ts
CHANGED
|
@@ -6,10 +6,8 @@ import {
|
|
|
6
6
|
calculateAssetData,
|
|
7
7
|
calculateLiquidationData,
|
|
8
8
|
calculateMaximumWithdrawAmount,
|
|
9
|
-
calculateMaximumWithdrawAmountOld,
|
|
10
9
|
calculatePresentValue,
|
|
11
10
|
getAvailableToBorrow,
|
|
12
|
-
isV5MainPoolContract,
|
|
13
11
|
presentValue,
|
|
14
12
|
} from './math';
|
|
15
13
|
import { loadMaybeMyRef, loadMyRef } from './helpers';
|
|
@@ -17,8 +15,7 @@ import { BalanceType, UserBalance, UserData, UserLiteData, UserRewards } from '.
|
|
|
17
15
|
import { MAINNET_POOL_CONFIG, TESTNET_POOL_CONFIG } from '../constants/pools';
|
|
18
16
|
import { basename } from 'path';
|
|
19
17
|
|
|
20
|
-
|
|
21
|
-
/* export function createUserRewards(): DictionaryValue<UserRewards> {
|
|
18
|
+
export function createUserRewards(): DictionaryValue<UserRewards> {
|
|
22
19
|
return {
|
|
23
20
|
serialize: (src: any, buidler: any) => {
|
|
24
21
|
buidler.storeUint(src.trackingIndex, 64);
|
|
@@ -30,7 +27,7 @@ import { basename } from 'path';
|
|
|
30
27
|
return { trackingIndex, trackingAccured };
|
|
31
28
|
},
|
|
32
29
|
};
|
|
33
|
-
}
|
|
30
|
+
}
|
|
34
31
|
|
|
35
32
|
export function createAssetData(): DictionaryValue<AssetData> {
|
|
36
33
|
return {
|
|
@@ -41,10 +38,11 @@ export function createAssetData(): DictionaryValue<AssetData> {
|
|
|
41
38
|
buidler.storeUint(src.totalBorrow, 64);
|
|
42
39
|
buidler.storeUint(src.lastAccural, 32);
|
|
43
40
|
buidler.storeUint(src.balance, 64);
|
|
44
|
-
/* Will be in v6
|
|
45
41
|
buidler.storeUint(src.trackingSupplyIndex, 64);
|
|
46
42
|
buidler.storeUint(src.trackingBorrowIndex, 64);
|
|
47
|
-
|
|
43
|
+
if (src.awaitedSupply) {
|
|
44
|
+
buidler.storeUint(src.awaitedSupply, 64);
|
|
45
|
+
}
|
|
48
46
|
},
|
|
49
47
|
parse: (src: Slice) => {
|
|
50
48
|
const sRate = BigInt(src.loadInt(64));
|
|
@@ -53,13 +51,15 @@ export function createAssetData(): DictionaryValue<AssetData> {
|
|
|
53
51
|
const totalBorrow = BigInt(src.loadInt(64));
|
|
54
52
|
const lastAccural = BigInt(src.loadInt(32));
|
|
55
53
|
const balance = BigInt(src.loadInt(64));
|
|
56
|
-
/* Will be in v6
|
|
57
54
|
const trackingSupplyIndex = BigInt(src.loadUint(64));
|
|
58
55
|
const trackingBorrowIndex = BigInt(src.loadUint(64));
|
|
59
|
-
|
|
56
|
+
let awaitedSupply: bigint | undefined = undefined;
|
|
57
|
+
if (src.remainingBits == 64) {
|
|
58
|
+
awaitedSupply = BigInt(src.loadUint(64));
|
|
59
|
+
}
|
|
60
60
|
|
|
61
|
-
return { sRate, bRate, totalSupply, totalBorrow, lastAccural, balance, trackingSupplyIndex, trackingBorrowIndex,
|
|
62
|
-
|
|
61
|
+
return { sRate, bRate, totalSupply, totalBorrow, lastAccural, balance, trackingSupplyIndex, trackingBorrowIndex, awaitedSupply};
|
|
62
|
+
},
|
|
63
63
|
};
|
|
64
64
|
}
|
|
65
65
|
|
|
@@ -83,10 +83,9 @@ export function createAssetConfig(): DictionaryValue<AssetConfig> {
|
|
|
83
83
|
refBuild.storeUint(src.maxTotalSupply, 64);
|
|
84
84
|
refBuild.storeUint(src.reserveFactor, 16);
|
|
85
85
|
refBuild.storeUint(src.liquidationReserveFactor, 16);
|
|
86
|
-
/* Will be in v6
|
|
87
86
|
refBuild.storeUint(src.minPrincipalForRewards, 64);
|
|
88
87
|
refBuild.storeUint(src.baseTrackingSupplySpeed, 64);
|
|
89
|
-
refBuild.storeUint(src.baseTrackingBorrowSpeed, 64);
|
|
88
|
+
refBuild.storeUint(src.baseTrackingBorrowSpeed, 64);
|
|
90
89
|
builder.storeRef(refBuild.endCell());
|
|
91
90
|
},
|
|
92
91
|
parse: (src: Slice) => {
|
|
@@ -107,10 +106,9 @@ export function createAssetConfig(): DictionaryValue<AssetConfig> {
|
|
|
107
106
|
const maxTotalSupply = ref.loadUintBig(64);
|
|
108
107
|
const reserveFactor = ref.loadUintBig(16);
|
|
109
108
|
const liquidationReserveFactor = ref.loadUintBig(16);
|
|
110
|
-
/* Will be in v6
|
|
111
109
|
const minPrincipalForRewards = ref.loadUintBig(64);
|
|
112
110
|
const baseTrackingSupplySpeed = ref.loadUintBig(64);
|
|
113
|
-
const baseTrackingBorrowSpeed = ref.loadUintBig(64);
|
|
111
|
+
const baseTrackingBorrowSpeed = ref.loadUintBig(64);
|
|
114
112
|
|
|
115
113
|
return {
|
|
116
114
|
oracle,
|
|
@@ -129,10 +127,9 @@ export function createAssetConfig(): DictionaryValue<AssetConfig> {
|
|
|
129
127
|
maxTotalSupply,
|
|
130
128
|
reserveFactor,
|
|
131
129
|
liquidationReserveFactor,
|
|
132
|
-
/* Will be in v6
|
|
133
130
|
minPrincipalForRewards,
|
|
134
131
|
baseTrackingSupplySpeed,
|
|
135
|
-
baseTrackingBorrowSpeed
|
|
132
|
+
baseTrackingBorrowSpeed
|
|
136
133
|
};
|
|
137
134
|
},
|
|
138
135
|
};
|
|
@@ -150,16 +147,15 @@ export function parseMasterData(masterDataBOC: string, poolAssetsConfig: PoolAss
|
|
|
150
147
|
updateTime: upgradeConfigParser.loadUint(64),
|
|
151
148
|
freezeTime: upgradeConfigParser.loadUint(64),
|
|
152
149
|
userCode: loadMyRef(upgradeConfigParser),
|
|
153
|
-
blankCode: loadMyRef(upgradeConfigParser),
|
|
154
150
|
newMasterCode: loadMaybeMyRef(upgradeConfigParser),
|
|
155
151
|
newUserCode: loadMaybeMyRef(upgradeConfigParser),
|
|
156
152
|
};
|
|
157
|
-
upgradeConfigParser.endParse();
|
|
153
|
+
// upgradeConfigParser.endParse(); todo fix with new testnet contract
|
|
158
154
|
|
|
159
155
|
const masterConfigSlice = masterSlice.loadRef().beginParse();
|
|
160
|
-
|
|
161
156
|
const assetsConfigDict = masterConfigSlice.loadDict(Dictionary.Keys.BigUint(256), createAssetConfig());
|
|
162
157
|
const assetsDataDict = masterSlice.loadDict(Dictionary.Keys.BigUint(256), createAssetData());
|
|
158
|
+
|
|
163
159
|
const assetsExtendedData = Dictionary.empty<bigint, ExtendedAssetData>();
|
|
164
160
|
const assetsReserves = Dictionary.empty<bigint, bigint>();
|
|
165
161
|
const apy = {
|
|
@@ -171,15 +167,16 @@ export function parseMasterData(masterDataBOC: string, poolAssetsConfig: PoolAss
|
|
|
171
167
|
const assetData = calculateAssetData(assetsConfigDict, assetsDataDict, asset.assetId, masterConstants);
|
|
172
168
|
assetsExtendedData.set(asset.assetId, assetData);
|
|
173
169
|
}
|
|
174
|
-
|
|
175
170
|
const masterConfig = {
|
|
176
171
|
ifActive: masterConfigSlice.loadInt(8),
|
|
177
172
|
admin: masterConfigSlice.loadAddress(),
|
|
178
|
-
|
|
173
|
+
oraclesInfo: {
|
|
174
|
+
numOracles: masterConfigSlice.loadUint(16),
|
|
175
|
+
threshold: masterConfigSlice.loadUint(16),
|
|
176
|
+
oracles: loadMaybeMyRef(masterConfigSlice)
|
|
177
|
+
},
|
|
179
178
|
tokenKeys: loadMaybeMyRef(masterConfigSlice),
|
|
180
|
-
walletToMaster: loadMaybeMyRef(masterConfigSlice),
|
|
181
179
|
};
|
|
182
|
-
|
|
183
180
|
masterConfigSlice.endParse();
|
|
184
181
|
|
|
185
182
|
for (const [_, asset] of Object.entries(poolAssetsConfig)) {
|
|
@@ -220,11 +217,7 @@ export function parseUserLiteData(
|
|
|
220
217
|
const userAddress = userSlice.loadAddress();
|
|
221
218
|
const principalsDict = userSlice.loadDict(Dictionary.Keys.BigUint(256), Dictionary.Values.BigInt(64));
|
|
222
219
|
const userState = userSlice.loadInt(64);
|
|
223
|
-
|
|
224
|
-
const trackingBorrowIndex = userSlice.loadUintBig(64);
|
|
225
|
-
const dutchAuctionStart = userSlice.loadUint(32);
|
|
226
|
-
const backupCell = loadMyRef(userSlice);
|
|
227
|
-
/* Will be in v6
|
|
220
|
+
|
|
228
221
|
let trackingSupplyIndex = 0n;
|
|
229
222
|
let trackingBorrowIndex = 0n;
|
|
230
223
|
let dutchAuctionStart = 0;
|
|
@@ -243,9 +236,8 @@ export function parseUserLiteData(
|
|
|
243
236
|
backupCell1 = userSlice.loadMaybeRef();
|
|
244
237
|
backupCell2 = userSlice.loadMaybeRef();
|
|
245
238
|
}
|
|
246
|
-
|
|
239
|
+
|
|
247
240
|
userSlice.endParse();
|
|
248
|
-
const isV5Main = isV5MainPoolContract(poolConfig);
|
|
249
241
|
const userBalances = Dictionary.empty<bigint, UserBalance>();
|
|
250
242
|
|
|
251
243
|
for (const [_, asset] of Object.entries(poolAssetsConfig)) {
|
|
@@ -254,9 +246,8 @@ export function parseUserLiteData(
|
|
|
254
246
|
|
|
255
247
|
let principal = principalsDict.get(asset.assetId) || 0n;
|
|
256
248
|
let balance = presentValue(assetData.sRate, assetData.bRate, principal, masterConstants);
|
|
257
|
-
let leftBorder = isV5Main ? balance.amount : principal;
|
|
258
249
|
|
|
259
|
-
if (applyDust && (principal > 0 && (
|
|
250
|
+
if (applyDust && (principal > 0 && (principal < assetConfig.dust))) {
|
|
260
251
|
principal = 0n;
|
|
261
252
|
balance = {
|
|
262
253
|
amount: 0n,
|
|
@@ -279,10 +270,10 @@ export function parseUserLiteData(
|
|
|
279
270
|
trackingBorrowIndex: trackingBorrowIndex,
|
|
280
271
|
dutchAuctionStart: dutchAuctionStart,
|
|
281
272
|
backupCell: backupCell,
|
|
282
|
-
|
|
273
|
+
|
|
283
274
|
rewards: rewards,
|
|
284
275
|
backupCell1: backupCell1,
|
|
285
|
-
backupCell2: backupCell2,
|
|
276
|
+
backupCell2: backupCell2,
|
|
286
277
|
};
|
|
287
278
|
}
|
|
288
279
|
|
|
@@ -302,7 +293,6 @@ export function parseUserData(
|
|
|
302
293
|
|
|
303
294
|
let supplyBalance = 0n;
|
|
304
295
|
let borrowBalance = 0n;
|
|
305
|
-
const isV5Main = isV5MainPoolContract(poolConfig);
|
|
306
296
|
|
|
307
297
|
for (const [_, asset] of Object.entries(poolAssetsConfig)) {
|
|
308
298
|
const assetData = assetsData.get(asset.assetId) as ExtendedAssetData;
|
|
@@ -310,9 +300,8 @@ export function parseUserData(
|
|
|
310
300
|
|
|
311
301
|
let principal = userLiteData.principals.get(asset.assetId) || 0n;
|
|
312
302
|
const balance = presentValue(assetData.sRate, assetData.bRate, principal, masterConstants);
|
|
313
|
-
let leftBorder = isV5Main ? balance.amount : principal;
|
|
314
303
|
|
|
315
|
-
if (applyDust && (principal > 0 && (
|
|
304
|
+
if (applyDust && (principal > 0 && (principal < assetConfig.dust))) {
|
|
316
305
|
principal = 0n;
|
|
317
306
|
userLiteData.principals.set(asset.assetId, 0n);
|
|
318
307
|
}
|
|
@@ -339,22 +328,15 @@ export function parseUserData(
|
|
|
339
328
|
const balance = userLiteData.balances.get(asset.assetId) as UserBalance;
|
|
340
329
|
|
|
341
330
|
if (balance.type === BalanceType.supply) {
|
|
342
|
-
|
|
343
|
-
|
|
344
|
-
|
|
345
|
-
|
|
346
|
-
);
|
|
347
|
-
}
|
|
348
|
-
else {
|
|
349
|
-
withdrawalLimits.set(
|
|
350
|
-
asset.assetId,
|
|
351
|
-
calculateMaximumWithdrawAmount(assetsConfig, assetsData, userLiteData.principals, prices, masterConstants, asset.assetId)
|
|
352
|
-
);
|
|
353
|
-
}
|
|
331
|
+
withdrawalLimits.set(
|
|
332
|
+
asset.assetId,
|
|
333
|
+
bigIntMin(calculateMaximumWithdrawAmount(assetsConfig, assetsData, userLiteData.principals, prices, masterConstants, asset.assetId), assetData.balance)
|
|
334
|
+
);
|
|
354
335
|
}
|
|
336
|
+
|
|
355
337
|
borrowLimits.set(
|
|
356
338
|
asset.assetId,
|
|
357
|
-
bigIntMin((availableToBorrow * 10n ** assetConfig.decimals) / prices.get(asset.assetId)!, assetData.balance),
|
|
339
|
+
bigIntMax(0n, bigIntMin((availableToBorrow * 10n ** assetConfig.decimals) / prices.get(asset.assetId)!, assetData.balance, assetData.totalSupply - assetData.totalBorrow)),
|
|
358
340
|
);
|
|
359
341
|
}
|
|
360
342
|
|