@evaafi/sdk 0.5.0 → 0.5.1

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.
@@ -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,34 @@ 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 && currentAmount != 0n) {
226
+ if (changeType == User_1.BalanceChangeType.Borrow) {
227
+ totalBorrow += currentBalance * (1 + Number(assetConfig.originationFee) / Number(constants_1.MASTER_CONSTANTS.ASSET_ORIGINATION_FEE_SCALE));
228
+ }
229
+ else if (changeType == User_1.BalanceChangeType.Repay) {
230
+ totalBorrow -= currentBalance;
231
+ }
232
+ else if (changeType == User_1.BalanceChangeType.Withdraw) {
233
+ totalLimit -= currentBalance * Number(assetConfig.liquidationThreshold) / Number(constants_1.MASTER_CONSTANTS.ASSET_COEFFICIENT_SCALE);
234
+ }
235
+ else if (changeType == User_1.BalanceChangeType.Supply) {
236
+ totalLimit += currentBalance * Number(assetConfig.liquidationThreshold) / Number(constants_1.MASTER_CONSTANTS.ASSET_COEFFICIENT_SCALE);
237
+ }
238
+ }
239
+ if (Number(totalLimit) == 0) {
240
+ return 1;
241
+ }
242
+ return Math.min(Math.max(1 - totalBorrow / totalLimit, 0), 1); // let's limit a result to zero below and one above
243
+ }
244
+ exports.predictHealthFactor = predictHealthFactor;
@@ -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 = {
@@ -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
+ };
@@ -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
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@evaafi/sdk",
3
- "version": "0.5.0",
3
+ "version": "0.5.1",
4
4
  "description": "SDK for EVAA contracts",
5
5
  "main": "dist/index.js",
6
6
  "files": [
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,38 @@ 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 && currentAmount != 0n) {
271
+ if (changeType == BalanceChangeType.Borrow) {
272
+ totalBorrow += currentBalance * (1 + Number(assetConfig.originationFee) / Number(MASTER_CONSTANTS.ASSET_ORIGINATION_FEE_SCALE));
273
+ } else if (changeType == BalanceChangeType.Repay) {
274
+ totalBorrow -= currentBalance;
275
+ } else if (changeType == BalanceChangeType.Withdraw) {
276
+ totalLimit -= currentBalance * Number(assetConfig.liquidationThreshold) / Number(MASTER_CONSTANTS.ASSET_COEFFICIENT_SCALE);
277
+ } else if (changeType == BalanceChangeType.Supply) {
278
+ totalLimit += currentBalance * Number(assetConfig.liquidationThreshold) / Number(MASTER_CONSTANTS.ASSET_COEFFICIENT_SCALE);
279
+ }
280
+ }
281
+ if (Number(totalLimit) == 0) {
282
+ return 1;
283
+ }
284
+
285
+ return Math.min(Math.max(1 - totalBorrow / totalLimit, 0), 1); // let's limit a result to zero below and one above
286
+ }
package/src/constants.ts CHANGED
@@ -45,6 +45,7 @@ export const MASTER_CONSTANTS = {
45
45
  ASSET_PRICE_SCALE: BigInt(1e8),
46
46
  ASSET_RESERVE_FACTOR_SCALE: 10000n,
47
47
  ASSET_LIQUIDATION_RESERVE_FACTOR_SCALE: 10000n,
48
+ ASSET_ORIGINATION_FEE_SCALE: BigInt(1e9)
48
49
  };
49
50
 
50
51
  export const LENDING_CODE = Cell.fromBoc(
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
+ };