@evaafi/sdk 0.5.0 → 0.5.3
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 +2 -1
- package/dist/api/math.js +34 -1
- package/dist/api/prices.d.ts +1 -1
- package/dist/api/prices.js +19 -22
- package/dist/constants.d.ts +1 -0
- package/dist/constants.js +1 -0
- package/dist/contracts/MasterContract.js +1 -1
- package/dist/contracts/UserContract.js +2 -2
- package/dist/index.d.ts +1 -1
- package/dist/index.js +2 -1
- package/dist/types/User.d.ts +16 -0
- package/dist/types/User.js +8 -1
- package/package.json +1 -1
- package/src/api/math.ts +38 -1
- package/src/api/prices.ts +23 -24
- package/src/constants.ts +1 -0
- package/src/contracts/MasterContract.ts +1 -1
- package/src/contracts/UserContract.ts +2 -2
- package/src/index.ts +1 -0
- package/src/types/User.ts +18 -0
package/dist/api/math.d.ts
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { AssetConfig, AssetData, AssetInterest, ExtendedAssetData } from '../types/Master';
|
|
2
2
|
import { Dictionary } from '@ton/core';
|
|
3
|
-
import { LiquidationData, UserBalance } from '../types/User';
|
|
3
|
+
import { LiquidationData, PredictHealthFactorArgs, UserBalance } from '../types/User';
|
|
4
4
|
export declare function mulFactor(decimal: bigint, a: bigint, b: bigint): bigint;
|
|
5
5
|
export declare function mulDiv(x: bigint, y: bigint, z: bigint): bigint;
|
|
6
6
|
export declare function bigIntMax(...args: bigint[]): bigint;
|
|
@@ -18,3 +18,4 @@ export declare function calculateAssetInterest(assetConfig: AssetConfig, assetDa
|
|
|
18
18
|
export declare function getAvailableToBorrow(assetsConfig: Dictionary<bigint, AssetConfig>, assetsData: Dictionary<bigint, ExtendedAssetData>, principals: Dictionary<bigint, bigint>, prices: Dictionary<bigint, bigint>): bigint;
|
|
19
19
|
export declare function presentValue(sRate: bigint, bRate: bigint, principalValue: bigint): UserBalance;
|
|
20
20
|
export declare function calculateLiquidationData(assetsConfig: Dictionary<bigint, AssetConfig>, assetsData: Dictionary<bigint, ExtendedAssetData>, principals: Dictionary<bigint, bigint>, prices: Dictionary<bigint, bigint>): LiquidationData;
|
|
21
|
+
export declare function predictHealthFactor(args: PredictHealthFactorArgs): number;
|
package/dist/api/math.js
CHANGED
|
@@ -1,8 +1,9 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.calculateLiquidationData = exports.presentValue = exports.getAvailableToBorrow = exports.calculateAssetInterest = exports.calculateAssetData = exports.calculateCurrentRates = exports.calculatePresentValue = exports.bigIntMin = exports.bigIntMax = exports.mulDiv = exports.mulFactor = void 0;
|
|
3
|
+
exports.predictHealthFactor = exports.calculateLiquidationData = exports.presentValue = exports.getAvailableToBorrow = exports.calculateAssetInterest = exports.calculateAssetData = exports.calculateCurrentRates = exports.calculatePresentValue = exports.bigIntMin = exports.bigIntMax = exports.mulDiv = exports.mulFactor = void 0;
|
|
4
4
|
const constants_1 = require("../constants");
|
|
5
5
|
const User_1 = require("../types/User");
|
|
6
|
+
const sha256BigInt_1 = require("../utils/sha256BigInt");
|
|
6
7
|
function mulFactor(decimal, a, b) {
|
|
7
8
|
return (a * b) / decimal;
|
|
8
9
|
}
|
|
@@ -210,3 +211,35 @@ function calculateLiquidationData(assetsConfig, assetsData, principals, prices)
|
|
|
210
211
|
};
|
|
211
212
|
}
|
|
212
213
|
exports.calculateLiquidationData = calculateLiquidationData;
|
|
214
|
+
function predictHealthFactor(args) {
|
|
215
|
+
const liquidationData = calculateLiquidationData(args.assetsConfig, args.assetsData, args.balances, args.prices);
|
|
216
|
+
const tokenHash = (0, sha256BigInt_1.sha256Hash)(args.tokenSymbol);
|
|
217
|
+
const assetConfig = args.assetsConfig.get(tokenHash);
|
|
218
|
+
const assetPrice = Number(args.prices.get(tokenHash));
|
|
219
|
+
let totalLimit = Number(liquidationData.totalLimit);
|
|
220
|
+
let totalBorrow = Number(liquidationData.totalDebt);
|
|
221
|
+
const currentAmount = args.amount;
|
|
222
|
+
const decimals = Number(assetConfig.decimals);
|
|
223
|
+
const currentBalance = assetPrice * Number(currentAmount) / Math.pow(10, decimals);
|
|
224
|
+
const changeType = args.balanceChangeType;
|
|
225
|
+
if (currentAmount != null && !Number.isNaN(currentAmount) &&
|
|
226
|
+
Number.isFinite(currentAmount) && currentAmount != 0n) {
|
|
227
|
+
if (changeType == User_1.BalanceChangeType.Borrow) {
|
|
228
|
+
totalBorrow += currentBalance * (1 + Number(assetConfig.originationFee) / Number(constants_1.MASTER_CONSTANTS.ASSET_ORIGINATION_FEE_SCALE));
|
|
229
|
+
}
|
|
230
|
+
else if (changeType == User_1.BalanceChangeType.Repay) {
|
|
231
|
+
totalBorrow -= currentBalance;
|
|
232
|
+
}
|
|
233
|
+
else if (changeType == User_1.BalanceChangeType.Withdraw) {
|
|
234
|
+
totalLimit -= currentBalance * Number(assetConfig.liquidationThreshold) / Number(constants_1.MASTER_CONSTANTS.ASSET_COEFFICIENT_SCALE);
|
|
235
|
+
}
|
|
236
|
+
else if (changeType == User_1.BalanceChangeType.Supply) {
|
|
237
|
+
totalLimit += currentBalance * Number(assetConfig.liquidationThreshold) / Number(constants_1.MASTER_CONSTANTS.ASSET_COEFFICIENT_SCALE);
|
|
238
|
+
}
|
|
239
|
+
}
|
|
240
|
+
if (Number(totalLimit) == 0) {
|
|
241
|
+
return 1;
|
|
242
|
+
}
|
|
243
|
+
return Math.min(Math.max(1 - totalBorrow / totalLimit, 0), 1); // let's limit a result to zero below and one above
|
|
244
|
+
}
|
|
245
|
+
exports.predictHealthFactor = predictHealthFactor;
|
package/dist/api/prices.d.ts
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
1
|
import { PriceData } from '../types/Common';
|
|
2
|
-
export declare function getPrices(
|
|
2
|
+
export declare function getPrices(endpoints?: string[]): Promise<PriceData>;
|
package/dist/api/prices.js
CHANGED
|
@@ -3,27 +3,24 @@ Object.defineProperty(exports, "__esModule", { value: true });
|
|
|
3
3
|
exports.getPrices = void 0;
|
|
4
4
|
const core_1 = require("@ton/core");
|
|
5
5
|
const constants_1 = require("../constants");
|
|
6
|
-
async function getPrices(
|
|
7
|
-
|
|
8
|
-
let result = await fetch(`https://${endpoint}/api/indexer/v1/outputs/nft/${constants_1.NFT_ID}`, {
|
|
9
|
-
headers: { accept: 'application/json' },
|
|
10
|
-
});
|
|
11
|
-
let outputId = (await result.json());
|
|
12
|
-
result = await fetch(`https://${endpoint}/api/core/v2/outputs/${outputId.items[0]}`, {
|
|
13
|
-
headers: { accept: 'application/json' },
|
|
14
|
-
});
|
|
15
|
-
let resData = (await result.json());
|
|
16
|
-
const data = JSON.parse(decodeURIComponent(resData.output.features[0].data.replace('0x', '').replace(/[0-9a-f]{2}/g, '%$&')));
|
|
17
|
-
const pricesCell = core_1.Cell.fromBoc(Buffer.from(data['packedPrices'], 'hex'))[0];
|
|
18
|
-
const signature = Buffer.from(data['signature'], 'hex');
|
|
19
|
-
return {
|
|
20
|
-
dict: pricesCell.beginParse().loadDictDirect(core_1.Dictionary.Keys.BigUint(256), core_1.Dictionary.Values.BigUint(64)),
|
|
21
|
-
dataCell: (0, core_1.beginCell)().storeRef(pricesCell).storeBuffer(signature).endCell(),
|
|
22
|
-
};
|
|
23
|
-
}
|
|
24
|
-
catch (error) {
|
|
25
|
-
console.error(error);
|
|
26
|
-
return undefined;
|
|
27
|
-
}
|
|
6
|
+
async function getPrices(endpoints = ["api.stardust-mainnet.iotaledger.net"]) {
|
|
7
|
+
return await Promise.any(endpoints.map(x => loadPrices(x)));
|
|
28
8
|
}
|
|
29
9
|
exports.getPrices = getPrices;
|
|
10
|
+
async function loadPrices(endpoint = "api.stardust-mainnet.iotaledger.net") {
|
|
11
|
+
let result = await fetch(`https://${endpoint}/api/indexer/v1/outputs/nft/${constants_1.NFT_ID}`, {
|
|
12
|
+
headers: { accept: 'application/json' },
|
|
13
|
+
});
|
|
14
|
+
let outputId = (await result.json());
|
|
15
|
+
result = await fetch(`https://${endpoint}/api/core/v2/outputs/${outputId.items[0]}`, {
|
|
16
|
+
headers: { accept: 'application/json' },
|
|
17
|
+
});
|
|
18
|
+
let resData = (await result.json());
|
|
19
|
+
const data = JSON.parse(decodeURIComponent(resData.output.features[0].data.replace('0x', '').replace(/[0-9a-f]{2}/g, '%$&')));
|
|
20
|
+
const pricesCell = core_1.Cell.fromBoc(Buffer.from(data['packedPrices'], 'hex'))[0];
|
|
21
|
+
const signature = Buffer.from(data['signature'], 'hex');
|
|
22
|
+
return {
|
|
23
|
+
dict: pricesCell.beginParse().loadDictDirect(core_1.Dictionary.Keys.BigUint(256), core_1.Dictionary.Values.BigUint(64)),
|
|
24
|
+
dataCell: (0, core_1.beginCell)().storeRef(pricesCell).storeBuffer(signature).endCell(),
|
|
25
|
+
};
|
|
26
|
+
}
|
package/dist/constants.d.ts
CHANGED
|
@@ -36,6 +36,7 @@ export declare const MASTER_CONSTANTS: {
|
|
|
36
36
|
ASSET_PRICE_SCALE: bigint;
|
|
37
37
|
ASSET_RESERVE_FACTOR_SCALE: bigint;
|
|
38
38
|
ASSET_LIQUIDATION_RESERVE_FACTOR_SCALE: bigint;
|
|
39
|
+
ASSET_ORIGINATION_FEE_SCALE: bigint;
|
|
39
40
|
};
|
|
40
41
|
export declare const LENDING_CODE: Cell;
|
|
41
42
|
export declare const JETTON_WALLETS_CODE: {
|
package/dist/constants.js
CHANGED
|
@@ -42,6 +42,7 @@ exports.MASTER_CONSTANTS = {
|
|
|
42
42
|
ASSET_PRICE_SCALE: BigInt(1e8),
|
|
43
43
|
ASSET_RESERVE_FACTOR_SCALE: 10000n,
|
|
44
44
|
ASSET_LIQUIDATION_RESERVE_FACTOR_SCALE: 10000n,
|
|
45
|
+
ASSET_ORIGINATION_FEE_SCALE: BigInt(1e9)
|
|
45
46
|
};
|
|
46
47
|
exports.LENDING_CODE = core_1.Cell.fromBoc(Buffer.from('b5ee9c72c1010e0100fd000d12182a555a6065717691969efd0114ff00f4a413f4bcf2c80b010202c8050202039f740403001ff2f8276a2687d2018fd201800f883b840051d38642c678b64e4400780e58fc10802faf07f80e59fa801e78b096664c02078067c07c100627a7978402014807060007a0ddb0c60201c709080013a0fd007a026900aa90400201200b0a0031b8e1002191960aa00b9e2ca007f4042796d225e8019203f6010201200d0c000bf7c147d2218400b9d10e86981fd201840b07f8138d809797976a2687d2029116382f970fd9178089910374daf81b619fd20182c7883b8701981684100627910eba56001797a6a6ba610fd8200e8768f76a9f6aa00cc2a32a8292878809bef2f1889f883bbcdeb86f01', 'hex'))[0];
|
|
47
48
|
exports.JETTON_WALLETS_CODE = {
|
|
@@ -224,7 +224,7 @@ class Evaa {
|
|
|
224
224
|
async getSync(provider) {
|
|
225
225
|
const state = (await provider.getState()).state;
|
|
226
226
|
if (state.type === 'active') {
|
|
227
|
-
this._data = (0, parser_1.parseMasterData)(state.data.toString('
|
|
227
|
+
this._data = (0, parser_1.parseMasterData)(state.data.toString('base64'), this.network === 'testnet');
|
|
228
228
|
if (this.network === 'testnet' && this._data.upgradeConfig.masterCodeVersion !== constants_1.TESTNET_VERSION) {
|
|
229
229
|
throw Error(`Outdated SDK version. It supports only master code version ${constants_1.TESTNET_VERSION} on testnet, but the current master code version is ${this._data.upgradeConfig.masterCodeVersion}`);
|
|
230
230
|
}
|
|
@@ -25,7 +25,7 @@ class EvaaUser {
|
|
|
25
25
|
async getSyncLite(provider, assetsData, assetsConfig) {
|
|
26
26
|
const state = (await provider.getState()).state;
|
|
27
27
|
if (state.type === 'active') {
|
|
28
|
-
this._liteData = (0, parser_1.parseUserLiteData)(state.data.toString('
|
|
28
|
+
this._liteData = (0, parser_1.parseUserLiteData)(state.data.toString('base64'), assetsData, assetsConfig, this.testnet);
|
|
29
29
|
this.lastSync = Math.floor(Date.now() / 1000);
|
|
30
30
|
}
|
|
31
31
|
else {
|
|
@@ -65,7 +65,7 @@ class EvaaUser {
|
|
|
65
65
|
async getSync(provider, assetsData, assetsConfig, prices) {
|
|
66
66
|
const state = (await provider.getState()).state;
|
|
67
67
|
if (state.type === 'active') {
|
|
68
|
-
this._liteData = (0, parser_1.parseUserLiteData)(state.data.toString('
|
|
68
|
+
this._liteData = (0, parser_1.parseUserLiteData)(state.data.toString('base64'), assetsData, assetsConfig, this.testnet);
|
|
69
69
|
this._data = (0, parser_1.parseUserData)(this._liteData, assetsData, assetsConfig, prices, this.testnet);
|
|
70
70
|
this.lastSync = Math.floor(Date.now() / 1000);
|
|
71
71
|
}
|
package/dist/index.d.ts
CHANGED
|
@@ -6,7 +6,7 @@ export { EvaaParameters, JettonMessageParameters, TonSupplyParameters, JettonSup
|
|
|
6
6
|
export { EvaaUser } from './contracts/UserContract';
|
|
7
7
|
export { PriceData } from './types/Common';
|
|
8
8
|
export { UpgradeConfig, AssetConfig, MasterConfig, AssetData, AssetInterest, AssetApy, ExtendedAssetData, MasterData, } from './types/Master';
|
|
9
|
-
export { BalanceType, UserBalance, UserLiqudationData, LiquidableData, NonLiquidableData, LiquidationData, UserDataInactive, UserDataActive, UserData, } from './types/User';
|
|
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, JETTON_MASTER_ADDRESSES, MASTER_CONSTANTS, LENDING_CODE, OPCODES, FEES, } from './constants';
|
|
11
11
|
export { getLastSentBoc, getTonConnectSender } from './utils/tonConnectSender';
|
|
12
12
|
export { getUserJettonWallet } from './utils/userJettonWallet';
|
package/dist/index.js
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.getUserJettonWallet = exports.getTonConnectSender = exports.getLastSentBoc = exports.FEES = exports.OPCODES = exports.LENDING_CODE = exports.MASTER_CONSTANTS = exports.JETTON_MASTER_ADDRESSES = exports.TESTNET_VERSION = exports.EVAA_MASTER_TESTNET = exports.MAINNET_VERSION = exports.EVAA_MASTER_MAINNET = exports.BalanceType = exports.EvaaUser = exports.Evaa = exports.JettonWallet = exports.getPrices = exports.parseUserLiteData = exports.parseUserData = exports.parseMasterData = exports.createAssetConfig = exports.createAssetData = exports.calculateLiquidationData = exports.presentValue = exports.getAvailableToBorrow = exports.calculateAssetInterest = exports.calculateAssetData = exports.calculateCurrentRates = exports.calculatePresentValue = exports.bigIntMax = exports.bigIntMin = exports.mulDiv = exports.mulFactor = void 0;
|
|
3
|
+
exports.getUserJettonWallet = exports.getTonConnectSender = exports.getLastSentBoc = exports.FEES = exports.OPCODES = exports.LENDING_CODE = exports.MASTER_CONSTANTS = exports.JETTON_MASTER_ADDRESSES = 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.calculateLiquidationData = exports.presentValue = exports.getAvailableToBorrow = exports.calculateAssetInterest = exports.calculateAssetData = exports.calculateCurrentRates = exports.calculatePresentValue = exports.bigIntMax = exports.bigIntMin = exports.mulDiv = exports.mulFactor = void 0;
|
|
4
4
|
// Math
|
|
5
5
|
var math_1 = require("./api/math");
|
|
6
6
|
Object.defineProperty(exports, "mulFactor", { enumerable: true, get: function () { return math_1.mulFactor; } });
|
|
@@ -33,6 +33,7 @@ var UserContract_1 = require("./contracts/UserContract");
|
|
|
33
33
|
Object.defineProperty(exports, "EvaaUser", { enumerable: true, get: function () { return UserContract_1.EvaaUser; } });
|
|
34
34
|
var User_1 = require("./types/User");
|
|
35
35
|
Object.defineProperty(exports, "BalanceType", { enumerable: true, get: function () { return User_1.BalanceType; } });
|
|
36
|
+
Object.defineProperty(exports, "BalanceChangeType", { enumerable: true, get: function () { return User_1.BalanceChangeType; } });
|
|
36
37
|
// Constants
|
|
37
38
|
var constants_1 = require("./constants");
|
|
38
39
|
Object.defineProperty(exports, "EVAA_MASTER_MAINNET", { enumerable: true, get: function () { return constants_1.EVAA_MASTER_MAINNET; } });
|
package/dist/types/User.d.ts
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import { Address, Cell, Dictionary } from '@ton/core';
|
|
2
|
+
import { AssetConfig, ExtendedAssetData } from './Master';
|
|
2
3
|
export declare enum BalanceType {
|
|
3
4
|
supply = "supply",
|
|
4
5
|
borrow = "borrow"
|
|
@@ -57,3 +58,18 @@ export type UserRewards = {
|
|
|
57
58
|
trackingIndex: bigint;
|
|
58
59
|
trackingAccured: bigint;
|
|
59
60
|
};
|
|
61
|
+
export declare enum BalanceChangeType {
|
|
62
|
+
Borrow = 0,
|
|
63
|
+
Repay = 1,
|
|
64
|
+
Supply = 2,
|
|
65
|
+
Withdraw = 3
|
|
66
|
+
}
|
|
67
|
+
export type PredictHealthFactorArgs = {
|
|
68
|
+
balanceChangeType: BalanceChangeType;
|
|
69
|
+
amount: bigint;
|
|
70
|
+
tokenSymbol: string;
|
|
71
|
+
balances: Dictionary<bigint, bigint>;
|
|
72
|
+
prices: Dictionary<bigint, bigint>;
|
|
73
|
+
assetsData: Dictionary<bigint, ExtendedAssetData>;
|
|
74
|
+
assetsConfig: Dictionary<bigint, AssetConfig>;
|
|
75
|
+
};
|
package/dist/types/User.js
CHANGED
|
@@ -1,8 +1,15 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.BalanceType = void 0;
|
|
3
|
+
exports.BalanceChangeType = exports.BalanceType = void 0;
|
|
4
4
|
var BalanceType;
|
|
5
5
|
(function (BalanceType) {
|
|
6
6
|
BalanceType["supply"] = "supply";
|
|
7
7
|
BalanceType["borrow"] = "borrow";
|
|
8
8
|
})(BalanceType || (exports.BalanceType = BalanceType = {}));
|
|
9
|
+
var BalanceChangeType;
|
|
10
|
+
(function (BalanceChangeType) {
|
|
11
|
+
BalanceChangeType[BalanceChangeType["Borrow"] = 0] = "Borrow";
|
|
12
|
+
BalanceChangeType[BalanceChangeType["Repay"] = 1] = "Repay";
|
|
13
|
+
BalanceChangeType[BalanceChangeType["Supply"] = 2] = "Supply";
|
|
14
|
+
BalanceChangeType[BalanceChangeType["Withdraw"] = 3] = "Withdraw";
|
|
15
|
+
})(BalanceChangeType || (exports.BalanceChangeType = BalanceChangeType = {}));
|
package/package.json
CHANGED
package/src/api/math.ts
CHANGED
|
@@ -1,7 +1,8 @@
|
|
|
1
1
|
import { AssetConfig, AssetData, AssetInterest, ExtendedAssetData } from '../types/Master';
|
|
2
2
|
import { MASTER_CONSTANTS } from '../constants';
|
|
3
3
|
import { Dictionary } from '@ton/core';
|
|
4
|
-
import { BalanceType, LiquidationData, UserBalance } from '../types/User';
|
|
4
|
+
import { BalanceChangeType, BalanceType, LiquidationData, PredictHealthFactorArgs, UserBalance } from '../types/User';
|
|
5
|
+
import { sha256Hash } from '../utils/sha256BigInt';
|
|
5
6
|
|
|
6
7
|
export function mulFactor(decimal: bigint, a: bigint, b: bigint): bigint {
|
|
7
8
|
return (a * b) / decimal;
|
|
@@ -248,3 +249,39 @@ export function calculateLiquidationData(
|
|
|
248
249
|
liquidable: false,
|
|
249
250
|
};
|
|
250
251
|
}
|
|
252
|
+
|
|
253
|
+
export function predictHealthFactor(args: PredictHealthFactorArgs): number {
|
|
254
|
+
const liquidationData = calculateLiquidationData(args.assetsConfig, args.assetsData, args.balances, args.prices);
|
|
255
|
+
const tokenHash = sha256Hash(args.tokenSymbol);
|
|
256
|
+
|
|
257
|
+
const assetConfig = args.assetsConfig.get(tokenHash)!;
|
|
258
|
+
const assetPrice = Number(args.prices.get(tokenHash)!);
|
|
259
|
+
|
|
260
|
+
let totalLimit = Number(liquidationData.totalLimit);
|
|
261
|
+
let totalBorrow = Number(liquidationData.totalDebt);
|
|
262
|
+
|
|
263
|
+
const currentAmount = args.amount;
|
|
264
|
+
|
|
265
|
+
const decimals = Number(assetConfig.decimals)
|
|
266
|
+
|
|
267
|
+
const currentBalance = assetPrice * Number(currentAmount) / Math.pow(10, decimals);
|
|
268
|
+
const changeType = args.balanceChangeType;
|
|
269
|
+
|
|
270
|
+
if (currentAmount != null && !Number.isNaN(currentAmount) &&
|
|
271
|
+
Number.isFinite(currentAmount) && currentAmount != 0n) {
|
|
272
|
+
if (changeType == BalanceChangeType.Borrow) {
|
|
273
|
+
totalBorrow += currentBalance * (1 + Number(assetConfig.originationFee) / Number(MASTER_CONSTANTS.ASSET_ORIGINATION_FEE_SCALE));
|
|
274
|
+
} else if (changeType == BalanceChangeType.Repay) {
|
|
275
|
+
totalBorrow -= currentBalance;
|
|
276
|
+
} else if (changeType == BalanceChangeType.Withdraw) {
|
|
277
|
+
totalLimit -= currentBalance * Number(assetConfig.liquidationThreshold) / Number(MASTER_CONSTANTS.ASSET_COEFFICIENT_SCALE);
|
|
278
|
+
} else if (changeType == BalanceChangeType.Supply) {
|
|
279
|
+
totalLimit += currentBalance * Number(assetConfig.liquidationThreshold) / Number(MASTER_CONSTANTS.ASSET_COEFFICIENT_SCALE);
|
|
280
|
+
}
|
|
281
|
+
}
|
|
282
|
+
if (Number(totalLimit) == 0) {
|
|
283
|
+
return 1;
|
|
284
|
+
}
|
|
285
|
+
|
|
286
|
+
return Math.min(Math.max(1 - totalBorrow / totalLimit, 0), 1); // let's limit a result to zero below and one above
|
|
287
|
+
}
|
package/src/api/prices.ts
CHANGED
|
@@ -37,34 +37,33 @@ type OutputData = {
|
|
|
37
37
|
data: string;
|
|
38
38
|
}[];
|
|
39
39
|
};
|
|
40
|
-
}
|
|
40
|
+
}
|
|
41
41
|
|
|
42
|
-
export async function getPrices(
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
headers: { accept: 'application/json' },
|
|
46
|
-
});
|
|
47
|
-
let outputId = (await result.json()) as NftData;
|
|
42
|
+
export async function getPrices(endpoints: string[] = ["api.stardust-mainnet.iotaledger.net"]) {
|
|
43
|
+
return await Promise.any(endpoints.map(x => loadPrices(x)));
|
|
44
|
+
}
|
|
48
45
|
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
}
|
|
46
|
+
async function loadPrices(endpoint: String = "api.stardust-mainnet.iotaledger.net"): Promise<PriceData> {
|
|
47
|
+
let result = await fetch(`https://${endpoint}/api/indexer/v1/outputs/nft/${NFT_ID}`, {
|
|
48
|
+
headers: { accept: 'application/json' },
|
|
49
|
+
});
|
|
50
|
+
let outputId = (await result.json()) as NftData;
|
|
52
51
|
|
|
53
|
-
|
|
52
|
+
result = await fetch(`https://${endpoint}/api/core/v2/outputs/${outputId.items[0]}`, {
|
|
53
|
+
headers: { accept: 'application/json' },
|
|
54
|
+
});
|
|
54
55
|
|
|
55
|
-
|
|
56
|
-
decodeURIComponent(resData.output.features[0].data.replace('0x', '').replace(/[0-9a-f]{2}/g, '%$&')),
|
|
57
|
-
);
|
|
56
|
+
let resData = (await result.json()) as OutputData;
|
|
58
57
|
|
|
59
|
-
|
|
60
|
-
|
|
58
|
+
const data = JSON.parse(
|
|
59
|
+
decodeURIComponent(resData.output.features[0].data.replace('0x', '').replace(/[0-9a-f]{2}/g, '%$&')),
|
|
60
|
+
);
|
|
61
61
|
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
}
|
|
62
|
+
const pricesCell = Cell.fromBoc(Buffer.from(data['packedPrices'], 'hex'))[0];
|
|
63
|
+
const signature = Buffer.from(data['signature'], 'hex');
|
|
64
|
+
|
|
65
|
+
return {
|
|
66
|
+
dict: pricesCell.beginParse().loadDictDirect(Dictionary.Keys.BigUint(256), Dictionary.Values.BigUint(64)),
|
|
67
|
+
dataCell: beginCell().storeRef(pricesCell).storeBuffer(signature).endCell(),
|
|
68
|
+
};
|
|
70
69
|
}
|
package/src/constants.ts
CHANGED
|
@@ -405,7 +405,7 @@ export class Evaa implements Contract {
|
|
|
405
405
|
async getSync(provider: ContractProvider) {
|
|
406
406
|
const state = (await provider.getState()).state;
|
|
407
407
|
if (state.type === 'active') {
|
|
408
|
-
this._data = parseMasterData(state.data!.toString('
|
|
408
|
+
this._data = parseMasterData(state.data!.toString('base64'), this.network === 'testnet');
|
|
409
409
|
if (this.network === 'testnet' && this._data.upgradeConfig.masterCodeVersion !== TESTNET_VERSION) {
|
|
410
410
|
throw Error(
|
|
411
411
|
`Outdated SDK version. It supports only master code version ${TESTNET_VERSION} on testnet, but the current master code version is ${this._data.upgradeConfig.masterCodeVersion}`,
|
|
@@ -37,7 +37,7 @@ export class EvaaUser implements Contract {
|
|
|
37
37
|
const state = (await provider.getState()).state;
|
|
38
38
|
if (state.type === 'active') {
|
|
39
39
|
this._liteData = parseUserLiteData(
|
|
40
|
-
state.data!.toString('
|
|
40
|
+
state.data!.toString('base64'),
|
|
41
41
|
assetsData,
|
|
42
42
|
assetsConfig,
|
|
43
43
|
this.testnet,
|
|
@@ -99,7 +99,7 @@ export class EvaaUser implements Contract {
|
|
|
99
99
|
const state = (await provider.getState()).state;
|
|
100
100
|
if (state.type === 'active') {
|
|
101
101
|
this._liteData = parseUserLiteData(
|
|
102
|
-
state.data!.toString('
|
|
102
|
+
state.data!.toString('base64'),
|
|
103
103
|
assetsData,
|
|
104
104
|
assetsConfig,
|
|
105
105
|
this.testnet,
|
package/src/index.ts
CHANGED
package/src/types/User.ts
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import { Address, Cell, Dictionary } from '@ton/core';
|
|
2
|
+
import { AssetConfig, ExtendedAssetData } from './Master';
|
|
2
3
|
|
|
3
4
|
export enum BalanceType {
|
|
4
5
|
supply = 'supply',
|
|
@@ -73,3 +74,20 @@ export type UserRewards = {
|
|
|
73
74
|
trackingIndex: bigint;
|
|
74
75
|
trackingAccured: bigint;
|
|
75
76
|
};
|
|
77
|
+
|
|
78
|
+
export enum BalanceChangeType {
|
|
79
|
+
Borrow = 0,
|
|
80
|
+
Repay = 1,
|
|
81
|
+
Supply = 2,
|
|
82
|
+
Withdraw = 3
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
export type PredictHealthFactorArgs = {
|
|
86
|
+
balanceChangeType: BalanceChangeType;
|
|
87
|
+
amount: bigint; // always positive
|
|
88
|
+
tokenSymbol: string;
|
|
89
|
+
balances: Dictionary<bigint, bigint>;
|
|
90
|
+
prices: Dictionary<bigint, bigint>;
|
|
91
|
+
assetsData: Dictionary<bigint, ExtendedAssetData>;
|
|
92
|
+
assetsConfig: Dictionary<bigint, AssetConfig>;
|
|
93
|
+
};
|