@evaafi/sdk 0.5.6-a → 0.6.0-a
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/api/math.d.ts +1 -10
- package/dist/api/math.js +11 -63
- package/dist/api/parser.d.ts +2 -1
- package/dist/api/parser.js +41 -53
- 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 +9 -3
- package/dist/contracts/MasterContract.js +13 -8
- package/dist/contracts/UserContract.d.ts +2 -2
- package/dist/contracts/UserContract.js +5 -5
- 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 +17 -84
- package/src/api/parser.ts +37 -52
- 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 +23 -17
- package/src/contracts/UserContract.ts +7 -3
- 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.payloadForwardAmount ?? 0, 64)
|
|
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.payloadForwardAmount ?? 0, 64)
|
|
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
|
}
|
|
@@ -17,7 +17,7 @@ export declare class EvaaUser implements Contract {
|
|
|
17
17
|
*/
|
|
18
18
|
static createFromAddress(address: Address, poolConfig?: PoolConfig): EvaaUser;
|
|
19
19
|
private constructor();
|
|
20
|
-
getSyncLite(provider: ContractProvider, assetsData: ExtendedAssetsData, assetsConfig: ExtendedAssetsConfig): Promise<void>;
|
|
20
|
+
getSyncLite(provider: ContractProvider, assetsData: ExtendedAssetsData, assetsConfig: ExtendedAssetsConfig, applyDust?: boolean): Promise<void>;
|
|
21
21
|
/**
|
|
22
22
|
* Calculate full user data from lite data and prices
|
|
23
23
|
* @param assetsData assets data
|
|
@@ -31,7 +31,7 @@ export declare class EvaaUser implements Contract {
|
|
|
31
31
|
* @param forwardPayload - payload that will be forwarded to the address which requested the data
|
|
32
32
|
*/
|
|
33
33
|
sendOnchainGetter(provider: ContractProvider, via: Sender, value: bigint, queryID: bigint, forwardPayload: Cell): Promise<void>;
|
|
34
|
-
getSync(provider: ContractProvider, assetsData: ExtendedAssetsData, assetsConfig: ExtendedAssetsConfig, prices: Dictionary<bigint, bigint
|
|
34
|
+
getSync(provider: ContractProvider, assetsData: ExtendedAssetsData, assetsConfig: ExtendedAssetsConfig, prices: Dictionary<bigint, bigint>, applyDust?: boolean): Promise<void>;
|
|
35
35
|
/**
|
|
36
36
|
* Get user contract lite data
|
|
37
37
|
* @returns user lite data if available, otherwise undefined
|
|
@@ -22,10 +22,10 @@ class EvaaUser {
|
|
|
22
22
|
this.address = address;
|
|
23
23
|
this.poolConfig = poolConfig;
|
|
24
24
|
}
|
|
25
|
-
async getSyncLite(provider, assetsData, assetsConfig) {
|
|
25
|
+
async getSyncLite(provider, assetsData, assetsConfig, applyDust = false) {
|
|
26
26
|
const state = (await provider.getState()).state;
|
|
27
27
|
if (state.type === 'active') {
|
|
28
|
-
this._liteData = (0, parser_1.parseUserLiteData)(state.data.toString('base64'), assetsData, assetsConfig, this.poolConfig);
|
|
28
|
+
this._liteData = (0, parser_1.parseUserLiteData)(state.data.toString('base64'), assetsData, assetsConfig, this.poolConfig, applyDust);
|
|
29
29
|
this.lastSync = Math.floor(Date.now() / 1000);
|
|
30
30
|
}
|
|
31
31
|
else {
|
|
@@ -62,11 +62,11 @@ class EvaaUser {
|
|
|
62
62
|
.endCell(),
|
|
63
63
|
});
|
|
64
64
|
}
|
|
65
|
-
async getSync(provider, assetsData, assetsConfig, prices) {
|
|
65
|
+
async getSync(provider, assetsData, assetsConfig, prices, applyDust = false) {
|
|
66
66
|
const state = (await provider.getState()).state;
|
|
67
67
|
if (state.type === 'active') {
|
|
68
|
-
this._liteData = (0, parser_1.parseUserLiteData)(state.data.toString('base64'), assetsData, assetsConfig, this.poolConfig);
|
|
69
|
-
this._data = (0, parser_1.parseUserData)(this._liteData, assetsData, assetsConfig, prices, this.poolConfig);
|
|
68
|
+
this._liteData = (0, parser_1.parseUserLiteData)(state.data.toString('base64'), assetsData, assetsConfig, this.poolConfig, applyDust);
|
|
69
|
+
this._data = (0, parser_1.parseUserData)(this._liteData, assetsData, assetsConfig, prices, this.poolConfig, applyDust);
|
|
70
70
|
this.lastSync = Math.floor(Date.now() / 1000);
|
|
71
71
|
}
|
|
72
72
|
else {
|
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
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@evaafi/sdk",
|
|
3
|
-
"version": "0.
|
|
4
|
-
"description": "SDK
|
|
3
|
+
"version": "0.6.0a",
|
|
4
|
+
"description": "The EVAA SDK is designed to easily integrate with the EVAA lending protocol on TON blockchain.",
|
|
5
5
|
"main": "dist/index.js",
|
|
6
6
|
"files": [
|
|
7
7
|
"dist",
|
package/src/api/math.ts
CHANGED
|
@@ -160,77 +160,7 @@ export function getAgregatedBalances (
|
|
|
160
160
|
return {totalSupply: user_total_supply, totalBorrow: user_total_borrow};
|
|
161
161
|
}
|
|
162
162
|
|
|
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?
|
|
163
|
+
export function calculateMaximumWithdrawAmount(
|
|
234
164
|
assetsConfig: ExtendedAssetsConfig,
|
|
235
165
|
assetsData: ExtendedAssetsData,
|
|
236
166
|
principals: Dictionary<bigint, bigint>,
|
|
@@ -263,10 +193,12 @@ export function calculateMaximumWithdrawAmount( // todo v6 ifelse dust not deb
|
|
|
263
193
|
}
|
|
264
194
|
else if (price > 0) {
|
|
265
195
|
maxAmountToReclaim =
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
|
|
196
|
+
bigIntMax(0n,
|
|
197
|
+
mulDiv(
|
|
198
|
+
mulDiv(borrowable, masterConstants.ASSET_COEFFICIENT_SCALE, assetConfig.collateralFactor),
|
|
199
|
+
10n ** assetConfig.decimals, price)
|
|
200
|
+
- calculatePresentValue(assetData.sRate, assetConfig.dust, masterConstants) / 2n
|
|
201
|
+
);
|
|
270
202
|
}
|
|
271
203
|
|
|
272
204
|
withdrawAmountMax = bigIntMin(
|
|
@@ -304,13 +236,13 @@ export function getAvailableToBorrow(
|
|
|
304
236
|
const principal = principals.get(assetID) as bigint;
|
|
305
237
|
|
|
306
238
|
if (principal < 0) {
|
|
307
|
-
borrowAmount += (calculatePresentValue(assetData.bRate, -principal, masterConstants)
|
|
239
|
+
borrowAmount += mulDiv(calculatePresentValue(assetData.bRate, -principal, masterConstants), price, 10n ** assetConfig.decimals);
|
|
308
240
|
} else if (principal > 0) {
|
|
309
241
|
borrowLimit +=
|
|
310
|
-
|
|
311
|
-
|
|
312
|
-
|
|
313
|
-
|
|
242
|
+
mulDiv(
|
|
243
|
+
mulDiv(calculatePresentValue(assetData.sRate, principal, masterConstants), price, 10n ** assetConfig.decimals),
|
|
244
|
+
assetConfig.collateralFactor,
|
|
245
|
+
masterConstants.ASSET_COEFFICIENT_SCALE);
|
|
314
246
|
}
|
|
315
247
|
}
|
|
316
248
|
|
|
@@ -410,7 +342,7 @@ export function calculateLiquidationData(
|
|
|
410
342
|
loanScale -
|
|
411
343
|
10n;
|
|
412
344
|
minCollateralAmount = (minCollateralAmount * 97n) / 100n;
|
|
413
|
-
if (minCollateralAmount / collateralDecimal >=
|
|
345
|
+
if (minCollateralAmount / collateralDecimal >= 0n) { // todo back to 1
|
|
414
346
|
return {
|
|
415
347
|
greatestCollateralAsset: collateralAsset,
|
|
416
348
|
greatestCollateralValue: collateralValue,
|
|
@@ -438,10 +370,11 @@ export function calculateLiquidationData(
|
|
|
438
370
|
|
|
439
371
|
export function predictHealthFactor(args: PredictHealthFactorArgs): number {
|
|
440
372
|
const liquidationData = calculateLiquidationData(args.assetsConfig, args.assetsData, args.principals, args.prices, args.poolConfig);
|
|
441
|
-
const
|
|
373
|
+
const assetId = args.asset.assetId;
|
|
442
374
|
|
|
443
|
-
const assetConfig = args.assetsConfig.get(
|
|
444
|
-
const assetPrice = Number(args.prices.get(
|
|
375
|
+
const assetConfig = args.assetsConfig.get(assetId)!;
|
|
376
|
+
const assetPrice = Number(args.prices.get(assetId)!);
|
|
377
|
+
const assetData = args.assetsData.get(assetId)!;
|
|
445
378
|
|
|
446
379
|
let totalLimit = Number(liquidationData.totalLimit);
|
|
447
380
|
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
|
|
|
@@ -365,7 +347,10 @@ export function parseUserData(
|
|
|
365
347
|
: Number(BigInt(1e9) - (availableToBorrow * BigInt(1e9)) / (borrowBalance + availableToBorrow)) / 1e7;
|
|
366
348
|
|
|
367
349
|
const liquidationData = calculateLiquidationData(assetsConfig, assetsData, userLiteData.principals, prices, poolConfig);
|
|
368
|
-
|
|
350
|
+
let healthFactor = 1;
|
|
351
|
+
if (liquidationData.totalLimit != 0n) {
|
|
352
|
+
healthFactor = 1 - Number(liquidationData.totalDebt) / Number(liquidationData.totalLimit);
|
|
353
|
+
}
|
|
369
354
|
|
|
370
355
|
return {
|
|
371
356
|
...userLiteData,
|