@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
|
@@ -1,8 +1,6 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.convertToMerkleProof =
|
|
4
|
-
exports.generateMerkleProofDirect = generateMerkleProofDirect;
|
|
5
|
-
exports.generateMerkleProof = generateMerkleProof;
|
|
3
|
+
exports.generateMerkleProof = exports.generateMerkleProofDirect = exports.convertToMerkleProof = void 0;
|
|
6
4
|
const core_1 = require("@ton/core");
|
|
7
5
|
function readUnaryLength(slice) {
|
|
8
6
|
let res = 0;
|
|
@@ -22,6 +20,7 @@ function convertToMerkleProof(c) {
|
|
|
22
20
|
.storeUint(c.depth(0), 16)
|
|
23
21
|
.storeRef(c));
|
|
24
22
|
}
|
|
23
|
+
exports.convertToMerkleProof = convertToMerkleProof;
|
|
25
24
|
function convertToPrunedBranch(c) {
|
|
26
25
|
return endExoticCell((0, core_1.beginCell)()
|
|
27
26
|
.storeUint(1, 8)
|
|
@@ -102,6 +101,8 @@ function generateMerkleProofDirect(dict, keys, keyObject) {
|
|
|
102
101
|
const s = (0, core_1.beginCell)().storeDictDirect(dict).asSlice();
|
|
103
102
|
return doGenerateMerkleProof('', s, keyObject.bits, keys.map((key) => keyObject.serialize(key).toString(2).padStart(keyObject.bits, '0')));
|
|
104
103
|
}
|
|
104
|
+
exports.generateMerkleProofDirect = generateMerkleProofDirect;
|
|
105
105
|
function generateMerkleProof(dict, keys, keyObject) {
|
|
106
106
|
return convertToMerkleProof(generateMerkleProofDirect(dict, keys, keyObject));
|
|
107
107
|
}
|
|
108
|
+
exports.generateMerkleProof = generateMerkleProof;
|
|
@@ -1,7 +1,7 @@
|
|
|
1
|
+
/// <reference types="node" />
|
|
1
2
|
import { Cell, Dictionary, Slice } from "@ton/core";
|
|
2
3
|
import { PriceData, RawPriceData } from "../types/Common";
|
|
3
4
|
import { Oracle, PoolAssetsConfig } from "../types/Master";
|
|
4
|
-
import 'promise.any';
|
|
5
5
|
type OutputData = {
|
|
6
6
|
metadata: {
|
|
7
7
|
blockId: string;
|
|
@@ -36,7 +36,7 @@ export type OraclePricesData = {
|
|
|
36
36
|
timestamp: number;
|
|
37
37
|
prices: Dictionary<bigint, bigint>;
|
|
38
38
|
};
|
|
39
|
-
export declare function loadPrices(oracleNftId: String, endpoints: String[]
|
|
39
|
+
export declare function loadPrices(oracleNftId: String, endpoints: String[]): Promise<OutputData>;
|
|
40
40
|
export declare function parsePrices(outputData: OutputData, oracleId: number): Promise<RawPriceData>;
|
|
41
41
|
export declare function verifyPrices(assets: PoolAssetsConfig): (priceData: RawPriceData) => boolean;
|
|
42
42
|
export declare function getMedianPrice(pricesData: PriceData[], asset: bigint): bigint;
|
package/dist/utils/priceUtils.js
CHANGED
|
@@ -1,30 +1,22 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.loadPrices =
|
|
4
|
-
exports.parsePrices = parsePrices;
|
|
5
|
-
exports.verifyPrices = verifyPrices;
|
|
6
|
-
exports.getMedianPrice = getMedianPrice;
|
|
7
|
-
exports.packAssetsData = packAssetsData;
|
|
8
|
-
exports.packPrices = packPrices;
|
|
9
|
-
exports.createOracleDataProof = createOracleDataProof;
|
|
10
|
-
exports.packOraclesData = packOraclesData;
|
|
11
|
-
exports.sumDicts = sumDicts;
|
|
3
|
+
exports.sumDicts = exports.packOraclesData = exports.createOracleDataProof = exports.packPrices = exports.packAssetsData = exports.getMedianPrice = exports.verifyPrices = exports.parsePrices = exports.loadPrices = void 0;
|
|
12
4
|
const core_1 = require("@ton/core");
|
|
13
5
|
const config_1 = require("../config");
|
|
14
6
|
const merkleProof_1 = require("./merkleProof");
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
return await Promise.any(endpoints.map(x => loadOracleData(oracleNftId, x, timeout)));
|
|
7
|
+
async function loadPrices(oracleNftId, endpoints) {
|
|
8
|
+
return await Promise.any(endpoints.map(x => loadOracleData(oracleNftId, x)));
|
|
18
9
|
}
|
|
19
|
-
|
|
10
|
+
exports.loadPrices = loadPrices;
|
|
11
|
+
async function loadOracleData(oracleNftId, endpoint) {
|
|
20
12
|
let result = await fetch(`https://${endpoint}/api/indexer/v1/outputs/nft/${oracleNftId}`, {
|
|
21
13
|
headers: { accept: 'application/json' },
|
|
22
|
-
signal: AbortSignal.timeout(
|
|
14
|
+
signal: AbortSignal.timeout(5000)
|
|
23
15
|
});
|
|
24
16
|
let outputId = (await result.json());
|
|
25
17
|
result = await fetch(`https://${endpoint}/api/core/v2/outputs/${outputId.items[0]}`, {
|
|
26
18
|
headers: { accept: 'application/json' },
|
|
27
|
-
signal: AbortSignal.timeout(
|
|
19
|
+
signal: AbortSignal.timeout(5000)
|
|
28
20
|
});
|
|
29
21
|
return await result.json();
|
|
30
22
|
}
|
|
@@ -49,11 +41,12 @@ async function parsePrices(outputData, oracleId) {
|
|
|
49
41
|
throw Error();
|
|
50
42
|
}
|
|
51
43
|
}
|
|
44
|
+
exports.parsePrices = parsePrices;
|
|
52
45
|
function verifyPrices(assets) {
|
|
53
46
|
return function (priceData) {
|
|
54
47
|
const timestamp = Date.now() / 1000;
|
|
55
48
|
const pricesTime = priceData.timestamp;
|
|
56
|
-
for (const
|
|
49
|
+
for (const asset of assets) {
|
|
57
50
|
if (!priceData.dict.has(asset.assetId)) {
|
|
58
51
|
return false;
|
|
59
52
|
}
|
|
@@ -62,6 +55,7 @@ function verifyPrices(assets) {
|
|
|
62
55
|
return timestamp - pricesTime < config_1.TTL_ORACLE_DATA_SEC;
|
|
63
56
|
};
|
|
64
57
|
}
|
|
58
|
+
exports.verifyPrices = verifyPrices;
|
|
65
59
|
function getMedianPrice(pricesData, asset) {
|
|
66
60
|
const sorted = pricesData.map(x => x.dict.get(asset)).sort((a, b) => Number(a) - Number(b));
|
|
67
61
|
const mid = Math.floor(sorted.length / 2);
|
|
@@ -72,6 +66,7 @@ function getMedianPrice(pricesData, asset) {
|
|
|
72
66
|
return sorted[mid];
|
|
73
67
|
}
|
|
74
68
|
}
|
|
69
|
+
exports.getMedianPrice = getMedianPrice;
|
|
75
70
|
function packAssetsData(assetsData) {
|
|
76
71
|
if (assetsData.length == 0) {
|
|
77
72
|
throw new Error("No assets data to pack");
|
|
@@ -82,6 +77,7 @@ function packAssetsData(assetsData) {
|
|
|
82
77
|
.storeMaybeRef(acc)
|
|
83
78
|
.endCell(), null);
|
|
84
79
|
}
|
|
80
|
+
exports.packAssetsData = packAssetsData;
|
|
85
81
|
function packPrices(assetsDataCell, oraclesDataCell) {
|
|
86
82
|
let pricesCell = (0, core_1.beginCell)()
|
|
87
83
|
.storeRef(assetsDataCell)
|
|
@@ -89,6 +85,7 @@ function packPrices(assetsDataCell, oraclesDataCell) {
|
|
|
89
85
|
.endCell();
|
|
90
86
|
return pricesCell;
|
|
91
87
|
}
|
|
88
|
+
exports.packPrices = packPrices;
|
|
92
89
|
function createOracleDataProof(oracle, data, signature, assets) {
|
|
93
90
|
let prunedDict = (0, merkleProof_1.generateMerkleProofDirect)(data.prices, assets, core_1.Dictionary.Keys.BigUint(256));
|
|
94
91
|
let prunedData = (0, core_1.beginCell)().storeUint(data.timestamp, 32).storeMaybeRef(prunedDict).endCell();
|
|
@@ -96,6 +93,7 @@ function createOracleDataProof(oracle, data, signature, assets) {
|
|
|
96
93
|
let oracleDataProof = (0, core_1.beginCell)().storeUint(oracle.id, 32).storeRef(merkleProof).storeBuffer(signature).asSlice();
|
|
97
94
|
return oracleDataProof;
|
|
98
95
|
}
|
|
96
|
+
exports.createOracleDataProof = createOracleDataProof;
|
|
99
97
|
function packOraclesData(oraclesData, assets) {
|
|
100
98
|
if (oraclesData.length == 0) {
|
|
101
99
|
throw new Error("no oracles data to pack");
|
|
@@ -103,6 +101,7 @@ function packOraclesData(oraclesData, assets) {
|
|
|
103
101
|
let proofs = oraclesData.sort((d1, d2) => d1.oracle.id - d2.oracle.id).map(({ oracle, data, signature }) => createOracleDataProof(oracle, data, signature, assets));
|
|
104
102
|
return proofs.reduceRight((acc, val) => (0, core_1.beginCell)().storeSlice(val).storeMaybeRef(acc).endCell(), null);
|
|
105
103
|
}
|
|
104
|
+
exports.packOraclesData = packOraclesData;
|
|
106
105
|
// : String = "api.stardust-mainnet.iotaledger.net"
|
|
107
106
|
function sumDicts(result, addendum) {
|
|
108
107
|
for (const key of addendum.keys()) {
|
|
@@ -115,3 +114,4 @@ function sumDicts(result, addendum) {
|
|
|
115
114
|
result.set(key, current + value);
|
|
116
115
|
}
|
|
117
116
|
}
|
|
117
|
+
exports.sumDicts = sumDicts;
|
|
@@ -3,10 +3,11 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
|
3
3
|
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
4
|
};
|
|
5
5
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
-
exports.sha256Hash =
|
|
6
|
+
exports.sha256Hash = void 0;
|
|
7
7
|
const sha256_1 = __importDefault(require("crypto-js/sha256"));
|
|
8
8
|
function sha256Hash(input) {
|
|
9
9
|
const hash = (0, sha256_1.default)(input);
|
|
10
10
|
const hashHex = hash.toString();
|
|
11
11
|
return BigInt('0x' + hashHex);
|
|
12
12
|
}
|
|
13
|
+
exports.sha256Hash = sha256Hash;
|
|
@@ -1,7 +1,6 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.getLastSentBoc =
|
|
4
|
-
exports.getTonConnectSender = getTonConnectSender;
|
|
3
|
+
exports.getTonConnectSender = exports.getLastSentBoc = void 0;
|
|
5
4
|
const core_1 = require("@ton/core");
|
|
6
5
|
/*
|
|
7
6
|
This is not the best solution to get the BOC of the sent external message, however the Sender
|
|
@@ -12,6 +11,7 @@ let lastSentBoc;
|
|
|
12
11
|
function getLastSentBoc() {
|
|
13
12
|
return lastSentBoc;
|
|
14
13
|
}
|
|
14
|
+
exports.getLastSentBoc = getLastSentBoc;
|
|
15
15
|
function getTonConnectSender(connector) {
|
|
16
16
|
return {
|
|
17
17
|
get address() {
|
|
@@ -34,3 +34,4 @@ function getTonConnectSender(connector) {
|
|
|
34
34
|
},
|
|
35
35
|
};
|
|
36
36
|
}
|
|
37
|
+
exports.getTonConnectSender = getTonConnectSender;
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.getUserJettonWallet =
|
|
3
|
+
exports.getUserJettonWallet = void 0;
|
|
4
4
|
const core_1 = require("@ton/core");
|
|
5
5
|
const assets_1 = require("../constants/assets");
|
|
6
6
|
function getUserJettonWallet(ownerAddress, poolAssetConfig) {
|
|
@@ -59,3 +59,4 @@ function getUserJettonWallet(ownerAddress, poolAssetConfig) {
|
|
|
59
59
|
.endCell();
|
|
60
60
|
return new core_1.Address(0, stateInit.hash());
|
|
61
61
|
}
|
|
62
|
+
exports.getUserJettonWallet = getUserJettonWallet;
|
package/dist/utils/utils.js
CHANGED
package/package.json
CHANGED
package/src/api/math.ts
CHANGED
|
@@ -159,73 +159,7 @@ export function getAgregatedBalances (
|
|
|
159
159
|
return {totalSupply: user_total_supply, totalBorrow: user_total_borrow};
|
|
160
160
|
}
|
|
161
161
|
|
|
162
|
-
|
|
163
|
-
* @deprecated The method should be used only for main contract v5
|
|
164
|
-
*/
|
|
165
|
-
export function isV5MainPoolContract(poolConfig: PoolConfig): boolean {
|
|
166
|
-
if ((poolConfig.masterAddress == MAINNET_POOL_CONFIG.masterAddress && poolConfig.masterVersion == 5) ||
|
|
167
|
-
(poolConfig.masterAddress == TESTNET_POOL_CONFIG.masterAddress && poolConfig.masterVersion == 5)) {
|
|
168
|
-
return true;
|
|
169
|
-
} else {
|
|
170
|
-
return false;
|
|
171
|
-
}
|
|
172
|
-
}
|
|
173
|
-
|
|
174
|
-
/**
|
|
175
|
-
* @deprecated The method should be used only for main contract v5
|
|
176
|
-
*/
|
|
177
|
-
export function calculateMaximumWithdrawAmountOld(
|
|
178
|
-
assetsConfig: ExtendedAssetsConfig,
|
|
179
|
-
assetsData: ExtendedAssetsData,
|
|
180
|
-
principals: Dictionary<bigint, bigint>,
|
|
181
|
-
prices: Dictionary<bigint, bigint>,
|
|
182
|
-
masterConstants: MasterConstants,
|
|
183
|
-
assetId: bigint,
|
|
184
|
-
): bigint {
|
|
185
|
-
let withdrawAmountMax = 0n;
|
|
186
|
-
|
|
187
|
-
const assetConfig = assetsConfig.get(assetId) as AssetConfig;
|
|
188
|
-
const assetData = assetsData.get(assetId) as ExtendedAssetData;
|
|
189
|
-
const oldPrincipal = principals.get(assetId) as bigint;
|
|
190
|
-
const oldPresentValue = presentValue(assetData.sRate, assetData.bRate, oldPrincipal, masterConstants);
|
|
191
|
-
|
|
192
|
-
if (oldPresentValue.amount > assetConfig.dust) {
|
|
193
|
-
if(checkNotInDebtAtAll(principals)) {
|
|
194
|
-
withdrawAmountMax = oldPresentValue.amount;
|
|
195
|
-
} else {
|
|
196
|
-
if (!prices.has(assetId)) {
|
|
197
|
-
return 0n;
|
|
198
|
-
}
|
|
199
|
-
|
|
200
|
-
//const borrowable = getAvailableToBorrow(assetsConfig, assetsData, principals, prices, masterConstants);
|
|
201
|
-
const price = prices.get(assetId) as bigint;
|
|
202
|
-
const agregatedBalances = getAgregatedBalances(assetsData, assetsConfig, principals, principals, masterConstants);
|
|
203
|
-
let maxAmountToReclaim =
|
|
204
|
-
mulDiv(
|
|
205
|
-
agregatedBalances.totalSupply - mulDivC(agregatedBalances.totalBorrow, masterConstants.ASSET_COEFFICIENT_SCALE, assetConfig.collateralFactor),
|
|
206
|
-
10n**assetConfig.decimals,
|
|
207
|
-
price
|
|
208
|
-
);
|
|
209
|
-
|
|
210
|
-
withdrawAmountMax = bigIntMin(
|
|
211
|
-
maxAmountToReclaim,
|
|
212
|
-
oldPresentValue.amount
|
|
213
|
-
);
|
|
214
|
-
}
|
|
215
|
-
} else {
|
|
216
|
-
if (!prices.has(assetId)) {
|
|
217
|
-
return 0n;
|
|
218
|
-
}
|
|
219
|
-
|
|
220
|
-
const price = prices.get(assetId) as bigint;
|
|
221
|
-
|
|
222
|
-
return getAvailableToBorrow(assetsConfig, assetsData, principals, prices, masterConstants) * (10n ** assetConfig.decimals) / price;
|
|
223
|
-
}
|
|
224
|
-
|
|
225
|
-
return withdrawAmountMax;
|
|
226
|
-
}
|
|
227
|
-
|
|
228
|
-
export function calculateMaximumWithdrawAmount( // todo v6 ifelse dust not debt at all is fixed?
|
|
162
|
+
export function calculateMaximumWithdrawAmount(
|
|
229
163
|
assetsConfig: ExtendedAssetsConfig,
|
|
230
164
|
assetsData: ExtendedAssetsData,
|
|
231
165
|
principals: Dictionary<bigint, bigint>,
|
|
@@ -258,10 +192,12 @@ export function calculateMaximumWithdrawAmount( // todo v6 ifelse dust not deb
|
|
|
258
192
|
}
|
|
259
193
|
else if (price > 0) {
|
|
260
194
|
maxAmountToReclaim =
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
|
|
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
|
+
);
|
|
265
201
|
}
|
|
266
202
|
|
|
267
203
|
withdrawAmountMax = bigIntMin(
|
|
@@ -299,13 +235,13 @@ export function getAvailableToBorrow(
|
|
|
299
235
|
const principal = principals.get(assetID) as bigint;
|
|
300
236
|
|
|
301
237
|
if (principal < 0) {
|
|
302
|
-
borrowAmount += (calculatePresentValue(assetData.bRate, -principal, masterConstants)
|
|
238
|
+
borrowAmount += mulDiv(calculatePresentValue(assetData.bRate, -principal, masterConstants), price, 10n ** assetConfig.decimals);
|
|
303
239
|
} else if (principal > 0) {
|
|
304
240
|
borrowLimit +=
|
|
305
|
-
|
|
306
|
-
|
|
307
|
-
|
|
308
|
-
|
|
241
|
+
mulDiv(
|
|
242
|
+
mulDiv(calculatePresentValue(assetData.sRate, principal, masterConstants), price, 10n ** assetConfig.decimals),
|
|
243
|
+
assetConfig.collateralFactor,
|
|
244
|
+
masterConstants.ASSET_COEFFICIENT_SCALE);
|
|
309
245
|
}
|
|
310
246
|
}
|
|
311
247
|
|
|
@@ -405,7 +341,7 @@ export function calculateLiquidationData(
|
|
|
405
341
|
loanScale -
|
|
406
342
|
10n;
|
|
407
343
|
minCollateralAmount = (minCollateralAmount * 97n) / 100n;
|
|
408
|
-
if (minCollateralAmount / collateralDecimal >=
|
|
344
|
+
if (minCollateralAmount / collateralDecimal >= 0n) { // todo back to 1
|
|
409
345
|
return {
|
|
410
346
|
greatestCollateralAsset: collateralAsset,
|
|
411
347
|
greatestCollateralValue: collateralValue,
|
|
@@ -433,10 +369,10 @@ export function calculateLiquidationData(
|
|
|
433
369
|
|
|
434
370
|
export function predictHealthFactor(args: PredictHealthFactorArgs): number {
|
|
435
371
|
const liquidationData = calculateLiquidationData(args.assetsConfig, args.assetsData, args.principals, args.prices, args.poolConfig);
|
|
436
|
-
const
|
|
372
|
+
const assetId = args.asset.assetId;
|
|
437
373
|
|
|
438
|
-
const assetConfig = args.assetsConfig.get(
|
|
439
|
-
const assetPrice = Number(args.prices.get(
|
|
374
|
+
const assetConfig = args.assetsConfig.get(assetId)!;
|
|
375
|
+
const assetPrice = Number(args.prices.get(assetId)!);
|
|
440
376
|
|
|
441
377
|
let totalLimit = Number(liquidationData.totalLimit);
|
|
442
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
|
|