@evaafi/sdk 0.5.4 → 0.5.5

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.
@@ -3,6 +3,7 @@ import { Dictionary } from '@ton/core';
3
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
+ export declare function mulDivC(x: bigint, y: bigint, z: bigint): bigint;
6
7
  export declare function bigIntMax(...args: bigint[]): bigint;
7
8
  export declare function bigIntMin(...args: bigint[]): bigint;
8
9
  export declare function calculatePresentValue(index: bigint, principalValue: bigint, masterConstants: MasterConstants): bigint;
@@ -15,6 +16,8 @@ export declare function calculateCurrentRates(assetConfig: AssetConfig, assetDat
15
16
  };
16
17
  export declare function calculateAssetData(assetsConfigDict: ExtendedAssetsConfig, assetsDataDict: Dictionary<bigint, AssetData>, assetId: bigint, masterConstants: MasterConstants): ExtendedAssetData;
17
18
  export declare function calculateAssetInterest(assetConfig: AssetConfig, assetData: AssetData, masterConstants: MasterConstants): AssetInterest;
19
+ export declare function checkNotInDebtAtAll(principals: Dictionary<bigint, bigint>): boolean;
20
+ export declare function calculateMaximumWithdrawAmount(assetsConfig: ExtendedAssetsConfig, assetsData: ExtendedAssetsData, principals: Dictionary<bigint, bigint>, prices: Dictionary<bigint, bigint>, masterConstants: MasterConstants, assetId: bigint): bigint;
18
21
  export declare function getAvailableToBorrow(assetsConfig: ExtendedAssetsConfig, assetsData: ExtendedAssetsData, principals: Dictionary<bigint, bigint>, prices: Dictionary<bigint, bigint>, masterConstants: MasterConstants): bigint;
19
22
  export declare function presentValue(sRate: bigint, bRate: bigint, principalValue: bigint, masterConstants: MasterConstants): UserBalance;
20
23
  export declare function calculateLiquidationData(assetsConfig: ExtendedAssetsConfig, assetsData: ExtendedAssetsData, principals: Dictionary<bigint, bigint>, prices: Dictionary<bigint, bigint>, masterConstants: MasterConstants): LiquidationData;
package/dist/api/math.js CHANGED
@@ -1,6 +1,6 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
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;
3
+ exports.predictHealthFactor = exports.calculateLiquidationData = exports.presentValue = exports.getAvailableToBorrow = exports.calculateMaximumWithdrawAmount = exports.checkNotInDebtAtAll = exports.calculateAssetInterest = exports.calculateAssetData = exports.calculateCurrentRates = exports.calculatePresentValue = exports.bigIntMin = exports.bigIntMax = exports.mulDivC = exports.mulDiv = exports.mulFactor = void 0;
4
4
  const User_1 = require("../types/User");
5
5
  const sha256BigInt_1 = require("../utils/sha256BigInt");
6
6
  function mulFactor(decimal, a, b) {
@@ -11,6 +11,11 @@ function mulDiv(x, y, z) {
11
11
  return (x * y) / z;
12
12
  }
13
13
  exports.mulDiv = mulDiv;
14
+ function mulDivC(x, y, z) {
15
+ const mul = x * y;
16
+ return mul / z + (mul % z ? 1n : 0n);
17
+ }
18
+ exports.mulDivC = mulDivC;
14
19
  function bigIntMax(...args) {
15
20
  return args.reduce((m, e) => (e > m ? e : m));
16
21
  }
@@ -93,6 +98,47 @@ function calculateAssetInterest(assetConfig, assetData, masterConstants) {
93
98
  };
94
99
  }
95
100
  exports.calculateAssetInterest = calculateAssetInterest;
101
+ function checkNotInDebtAtAll(principals) {
102
+ return principals.values().every(x => x >= 0n);
103
+ }
104
+ exports.checkNotInDebtAtAll = checkNotInDebtAtAll;
105
+ function calculateMaximumWithdrawAmount(assetsConfig, assetsData, principals, prices, masterConstants, assetId) {
106
+ let withdrawAmountMax = 0n;
107
+ const assetConfig = assetsConfig.get(assetId);
108
+ const assetData = assetsData.get(assetId);
109
+ const oldPrincipal = principals.get(assetId);
110
+ if (oldPrincipal > assetConfig.dust) {
111
+ const oldPresentValue = presentValue(assetData.sRate, assetData.bRate, oldPrincipal, masterConstants);
112
+ if (checkNotInDebtAtAll(principals)) {
113
+ withdrawAmountMax = oldPresentValue.amount;
114
+ }
115
+ else {
116
+ if (!prices.has(assetId)) {
117
+ return 0n;
118
+ }
119
+ const borrowable = getAvailableToBorrow(assetsConfig, assetsData, principals, prices, masterConstants);
120
+ const price = prices.get(assetId);
121
+ let maxAmountToReclaim = 0n;
122
+ if (assetConfig.collateralFactor == 0n) {
123
+ maxAmountToReclaim = oldPresentValue.amount;
124
+ }
125
+ else if (price > 0) {
126
+ maxAmountToReclaim =
127
+ mulDiv(mulDivC(borrowable, masterConstants.ASSET_COEFFICIENT_SCALE, assetConfig.collateralFactor), 10n ** assetConfig.decimals, price);
128
+ }
129
+ withdrawAmountMax = bigIntMin(maxAmountToReclaim, oldPresentValue.amount);
130
+ }
131
+ }
132
+ else {
133
+ if (!prices.has(assetId)) {
134
+ return 0n;
135
+ }
136
+ const price = prices.get(assetId);
137
+ return getAvailableToBorrow(assetsConfig, assetsData, principals, prices, masterConstants) * (10n ** assetConfig.decimals) / price;
138
+ }
139
+ return withdrawAmountMax;
140
+ }
141
+ exports.calculateMaximumWithdrawAmount = calculateMaximumWithdrawAmount;
96
142
  function getAvailableToBorrow(assetsConfig, assetsData, principals, prices, masterConstants) {
97
143
  let borrowLimit = 0n;
98
144
  let borrowAmount = 0n;
@@ -106,9 +152,7 @@ function getAvailableToBorrow(assetsConfig, assetsData, principals, prices, mast
106
152
  }
107
153
  else if (principal > 0) {
108
154
  borrowLimit +=
109
- (calculatePresentValue(assetData.sRate, principal, masterConstants) * price * assetConfig.collateralFactor) /
110
- 10n ** assetConfig.decimals /
111
- masterConstants.ASSET_COEFFICIENT_SCALE;
155
+ mulDivC(mulDivC(calculatePresentValue(assetData.sRate, principal, masterConstants), price, 10n ** assetConfig.decimals), assetConfig.collateralFactor, masterConstants.ASSET_COEFFICIENT_SCALE);
112
156
  }
113
157
  }
114
158
  return borrowLimit - borrowAmount;
@@ -218,11 +218,11 @@ function parseUserLiteData(userDataBOC, assetsData, assetsConfig, poolAssetsConf
218
218
  const assetData = assetsData.get(asset.assetId);
219
219
  const assetConfig = assetsConfig.get(asset.assetId);
220
220
  let principals = principalsDict.get(asset.assetId) || 0n;
221
- if (applyDust && (principals > -assetConfig.dust && principals < assetConfig.dust)) { // abs(principals) < dust
221
+ const balance = (0, math_1.presentValue)(assetData.sRate, assetData.bRate, principals, masterConstants);
222
+ if (applyDust && (principals > -assetConfig.dust && balance.amount < assetConfig.dust)) { // v6 will be abs(principals) < dust
222
223
  principals = 0n;
223
224
  principalsDict.set(asset.assetId, 0n);
224
225
  }
225
- const balance = (0, math_1.presentValue)(assetData.sRate, assetData.bRate, principals, masterConstants);
226
226
  userBalances.set(asset.assetId, balance);
227
227
  }
228
228
  return {
@@ -253,11 +253,11 @@ function parseUserData(userLiteData, assetsData, assetsConfig, prices, poolAsset
253
253
  const assetData = assetsData.get(asset.assetId);
254
254
  const assetConfig = assetsConfig.get(asset.assetId);
255
255
  let principals = userLiteData.principals.get(asset.assetId) || 0n;
256
- if (applyDust && (principals > -assetConfig.dust && principals < assetConfig.dust)) { // abs(principals) < dust
256
+ const balance = (0, math_1.presentValue)(assetData.sRate, assetData.bRate, principals, masterConstants);
257
+ if (applyDust && (principals > -assetConfig.dust && balance.amount < assetConfig.dust)) { // v6 will be abs(principals) < dust
257
258
  principals = 0n;
258
259
  userLiteData.principals.set(asset.assetId, 0n);
259
260
  }
260
- const balance = (0, math_1.presentValue)(assetData.sRate, assetData.bRate, principals, masterConstants);
261
261
  userLiteData.balances.set(asset.assetId, balance);
262
262
  }
263
263
  for (const [_, asset] of Object.entries(poolAssetConfig)) {
@@ -276,11 +276,7 @@ function parseUserData(userLiteData, assetsData, assetsConfig, prices, poolAsset
276
276
  const assetData = assetsData.get(asset.assetId);
277
277
  const balance = userLiteData.balances.get(asset.assetId);
278
278
  if (balance.type === User_1.BalanceType.supply) {
279
- withdrawalLimits.set(asset.assetId, (0, math_1.bigIntMax)((0, math_1.bigIntMin)(assetData.balance, ((supplyBalance -
280
- (borrowBalance * masterConstants.ASSET_COEFFICIENT_SCALE) / assetConfig.collateralFactor) *
281
- 10n ** assetConfig.decimals) /
282
- prices.get(asset.assetId) -
283
- 5n, balance.amount), 0n));
279
+ withdrawalLimits.set(asset.assetId, (0, math_1.calculateMaximumWithdrawAmount)(assetsConfig, assetsData, userLiteData.principals, prices, masterConstants, asset.assetId));
284
280
  }
285
281
  borrowLimits.set(asset.assetId, (0, math_1.bigIntMin)((availableToBorrow * 10n ** assetConfig.decimals) / prices.get(asset.assetId), assetData.balance));
286
282
  }
@@ -1,2 +1,2 @@
1
1
  import { PriceData } from '../types/Common';
2
- export declare function getPricesByNft(nftId: string, endpoints?: string[]): Promise<PriceData>;
2
+ export declare function getPrices(endpoints?: string[], nftId?: string): Promise<PriceData>;
@@ -1,11 +1,12 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.getPricesByNft = void 0;
3
+ exports.getPrices = void 0;
4
4
  const core_1 = require("@ton/core");
5
- async function getPricesByNft(nftId, endpoints = ["api.stardust-mainnet.iotaledger.net"]) {
5
+ const general_1 = require("../constants/general");
6
+ async function getPrices(endpoints = ["api.stardust-mainnet.iotaledger.net"], nftId = general_1.MAIN_POOL_NFT_ID) {
6
7
  return await Promise.any(endpoints.map(x => loadPrices(nftId, x)));
7
8
  }
8
- exports.getPricesByNft = getPricesByNft;
9
+ exports.getPrices = getPrices;
9
10
  async function loadPrices(nftId, endpoint = "api.stardust-mainnet.iotaledger.net") {
10
11
  let result = await fetch(`https://${endpoint}/api/indexer/v1/outputs/nft/${nftId}`, {
11
12
  headers: { accept: 'application/json' },
@@ -235,10 +235,10 @@ class Evaa {
235
235
  }
236
236
  async getPrices(provider, endpoints) {
237
237
  if ((endpoints?.length ?? 0) > 0) {
238
- return await (0, __1.getPricesByNft)(this.poolConfig.nftId, endpoints);
238
+ return await (0, __1.getPrices)(endpoints, this.poolConfig.nftId);
239
239
  }
240
240
  else {
241
- return await (0, __1.getPricesByNft)(this.poolConfig.nftId);
241
+ return await (0, __1.getPrices)(undefined, this.poolConfig.nftId);
242
242
  }
243
243
  }
244
244
  }
package/dist/index.d.ts CHANGED
@@ -1,6 +1,6 @@
1
- export { mulFactor, mulDiv, bigIntMin, bigIntMax, calculatePresentValue, calculateCurrentRates, calculateAssetData, calculateAssetInterest, getAvailableToBorrow, presentValue, calculateLiquidationData, } from './api/math';
1
+ export { mulFactor, mulDiv, bigIntMin, bigIntMax, calculatePresentValue, calculateCurrentRates, calculateAssetData, calculateAssetInterest, getAvailableToBorrow, calculateMaximumWithdrawAmount, presentValue, calculateLiquidationData, } from './api/math';
2
2
  export { createAssetData, createAssetConfig, parseMasterData, parseUserData, parseUserLiteData } from './api/parser';
3
- export { getPricesByNft } from './api/prices';
3
+ export { getPrices } from './api/prices';
4
4
  export { JettonWallet } from './contracts/JettonWallet';
5
5
  export { EvaaParameters, JettonMessageParameters, TonSupplyParameters, JettonSupplyParameters, WithdrawParameters, LiquidationBaseData, TonLiquidationParameters, JettonLiquidationParameters, Evaa, } from './contracts/MasterContract';
6
6
  export { EvaaUser } from './contracts/UserContract';
package/dist/index.js CHANGED
@@ -14,7 +14,7 @@ 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.getUserJettonWallet = 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.TESTNET_LP_POOL_CONFIG = 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.getPricesByNft = 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;
17
+ exports.getUserJettonWallet = 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.TESTNET_LP_POOL_CONFIG = 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.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
18
  // Math
19
19
  var math_1 = require("./api/math");
20
20
  Object.defineProperty(exports, "mulFactor", { enumerable: true, get: function () { return math_1.mulFactor; } });
@@ -26,6 +26,7 @@ Object.defineProperty(exports, "calculateCurrentRates", { enumerable: true, get:
26
26
  Object.defineProperty(exports, "calculateAssetData", { enumerable: true, get: function () { return math_1.calculateAssetData; } });
27
27
  Object.defineProperty(exports, "calculateAssetInterest", { enumerable: true, get: function () { return math_1.calculateAssetInterest; } });
28
28
  Object.defineProperty(exports, "getAvailableToBorrow", { enumerable: true, get: function () { return math_1.getAvailableToBorrow; } });
29
+ Object.defineProperty(exports, "calculateMaximumWithdrawAmount", { enumerable: true, get: function () { return math_1.calculateMaximumWithdrawAmount; } });
29
30
  Object.defineProperty(exports, "presentValue", { enumerable: true, get: function () { return math_1.presentValue; } });
30
31
  Object.defineProperty(exports, "calculateLiquidationData", { enumerable: true, get: function () { return math_1.calculateLiquidationData; } });
31
32
  // Parser
@@ -37,7 +38,7 @@ Object.defineProperty(exports, "parseUserData", { enumerable: true, get: functio
37
38
  Object.defineProperty(exports, "parseUserLiteData", { enumerable: true, get: function () { return parser_1.parseUserLiteData; } });
38
39
  // Prices
39
40
  var prices_1 = require("./api/prices");
40
- Object.defineProperty(exports, "getPricesByNft", { enumerable: true, get: function () { return prices_1.getPricesByNft; } });
41
+ Object.defineProperty(exports, "getPrices", { enumerable: true, get: function () { return prices_1.getPrices; } });
41
42
  // Contracts' wrappers
42
43
  var JettonWallet_1 = require("./contracts/JettonWallet");
43
44
  Object.defineProperty(exports, "JettonWallet", { enumerable: true, get: function () { return JettonWallet_1.JettonWallet; } });
package/package.json CHANGED
@@ -1,8 +1,9 @@
1
1
  {
2
2
  "name": "@evaafi/sdk",
3
- "version": "0.5.4",
3
+ "version": "0.5.5",
4
4
  "description": "SDK for EVAA contracts",
5
5
  "main": "dist/index.js",
6
+ "types": "src/index.ts",
6
7
  "files": [
7
8
  "dist",
8
9
  "src"
package/src/api/math.ts CHANGED
@@ -11,6 +11,11 @@ export function mulDiv(x: bigint, y: bigint, z: bigint): bigint {
11
11
  return (x * y) / z;
12
12
  }
13
13
 
14
+ export function mulDivC(x: bigint, y: bigint, z: bigint): bigint {
15
+ const mul = x * y;
16
+ return mul / z + (mul % z ? 1n : 0n);
17
+ }
18
+
14
19
  export function bigIntMax(...args: bigint[]): bigint {
15
20
  return args.reduce((m, e) => (e > m ? e : m));
16
21
  }
@@ -116,6 +121,66 @@ export function calculateAssetInterest(assetConfig: AssetConfig, assetData: Asse
116
121
  };
117
122
  }
118
123
 
124
+ export function checkNotInDebtAtAll(principals: Dictionary<bigint, bigint>): boolean {
125
+ return principals.values().every(x => x >= 0n);
126
+ }
127
+
128
+ export function calculateMaximumWithdrawAmount(
129
+ assetsConfig: ExtendedAssetsConfig,
130
+ assetsData: ExtendedAssetsData,
131
+ principals: Dictionary<bigint, bigint>,
132
+ prices: Dictionary<bigint, bigint>,
133
+ masterConstants: MasterConstants,
134
+ assetId: bigint
135
+ ): bigint {
136
+ let withdrawAmountMax = 0n;
137
+
138
+ const assetConfig = assetsConfig.get(assetId) as AssetConfig;
139
+ const assetData = assetsData.get(assetId) as ExtendedAssetData;
140
+ const oldPrincipal = principals.get(assetId) as bigint;
141
+
142
+ if (oldPrincipal > assetConfig.dust) {
143
+ const oldPresentValue = presentValue(assetData.sRate, assetData.bRate, oldPrincipal, masterConstants);
144
+ if(checkNotInDebtAtAll(principals)) {
145
+ withdrawAmountMax = oldPresentValue.amount;
146
+ } else {
147
+ if (!prices.has(assetId)) {
148
+ return 0n;
149
+ }
150
+
151
+ const borrowable = getAvailableToBorrow(assetsConfig, assetsData, principals, prices, masterConstants);
152
+ const price = prices.get(assetId) as bigint;
153
+
154
+ let maxAmountToReclaim = 0n;
155
+ if (assetConfig.collateralFactor == 0n) {
156
+ maxAmountToReclaim = oldPresentValue.amount;
157
+ }
158
+ else if (price > 0) {
159
+ maxAmountToReclaim =
160
+ mulDiv(
161
+ mulDivC(borrowable, masterConstants.ASSET_COEFFICIENT_SCALE, assetConfig.collateralFactor),
162
+ 10n ** assetConfig.decimals, price
163
+ );
164
+ }
165
+
166
+ withdrawAmountMax = bigIntMin(
167
+ maxAmountToReclaim,
168
+ oldPresentValue.amount
169
+ );
170
+ }
171
+ } else {
172
+ if (!prices.has(assetId)) {
173
+ return 0n;
174
+ }
175
+
176
+ const price = prices.get(assetId) as bigint;
177
+
178
+ return getAvailableToBorrow(assetsConfig, assetsData, principals, prices, masterConstants) * (10n ** assetConfig.decimals) / price;
179
+ }
180
+
181
+ return withdrawAmountMax;
182
+ }
183
+
119
184
  export function getAvailableToBorrow(
120
185
  assetsConfig: ExtendedAssetsConfig,
121
186
  assetsData: ExtendedAssetsData,
@@ -136,9 +201,10 @@ export function getAvailableToBorrow(
136
201
  borrowAmount += (calculatePresentValue(assetData.bRate, -principal, masterConstants) * price) / 10n ** assetConfig.decimals;
137
202
  } else if (principal > 0) {
138
203
  borrowLimit +=
139
- (calculatePresentValue(assetData.sRate, principal, masterConstants) * price * assetConfig.collateralFactor) /
140
- 10n ** assetConfig.decimals /
141
- masterConstants.ASSET_COEFFICIENT_SCALE;
204
+ mulDivC(
205
+ mulDivC(calculatePresentValue(assetData.sRate, principal, masterConstants), price, 10n ** assetConfig.decimals),
206
+ assetConfig.collateralFactor,
207
+ masterConstants.ASSET_COEFFICIENT_SCALE);
142
208
  }
143
209
  }
144
210
 
package/src/api/parser.ts CHANGED
@@ -5,6 +5,7 @@ import {
5
5
  bigIntMin,
6
6
  calculateAssetData,
7
7
  calculateLiquidationData,
8
+ calculateMaximumWithdrawAmount,
8
9
  calculatePresentValue,
9
10
  getAvailableToBorrow,
10
11
  presentValue,
@@ -246,12 +247,12 @@ export function parseUserLiteData(
246
247
  const assetData = assetsData.get(asset.assetId) as ExtendedAssetData;
247
248
  const assetConfig = assetsConfig.get(asset.assetId) as AssetConfig;
248
249
  let principals = principalsDict.get(asset.assetId) || 0n;
250
+ const balance = presentValue(assetData.sRate, assetData.bRate, principals, masterConstants);
249
251
 
250
- if (applyDust && (principals > -assetConfig.dust && principals < assetConfig.dust)) { // abs(principals) < dust
252
+ if (applyDust && (principals > -assetConfig.dust && balance.amount < assetConfig.dust)) { // v6 will be abs(principals) < dust
251
253
  principals = 0n;
252
254
  principalsDict.set(asset.assetId, 0n);
253
255
  }
254
- const balance = presentValue(assetData.sRate, assetData.bRate, principals, masterConstants);
255
256
  userBalances.set(asset.assetId, balance);
256
257
  }
257
258
 
@@ -292,13 +293,13 @@ export function parseUserData(
292
293
  const assetData = assetsData.get(asset.assetId) as ExtendedAssetData;
293
294
  const assetConfig = assetsConfig.get(asset.assetId) as AssetConfig;
294
295
  let principals = userLiteData.principals.get(asset.assetId) || 0n;
296
+ const balance = presentValue(assetData.sRate, assetData.bRate, principals, masterConstants);
295
297
 
296
- if (applyDust && (principals > -assetConfig.dust && principals < assetConfig.dust )) { // abs(principals) < dust
298
+ if (applyDust && (principals > -assetConfig.dust && balance.amount < assetConfig.dust )) { // v6 will be abs(principals) < dust
297
299
  principals = 0n;
298
300
  userLiteData.principals.set(asset.assetId, 0n);
299
301
  }
300
302
 
301
- const balance = presentValue(assetData.sRate, assetData.bRate, principals, masterConstants);
302
303
  userLiteData.balances.set(asset.assetId, balance);
303
304
  }
304
305
 
@@ -319,22 +320,11 @@ export function parseUserData(
319
320
  const assetConfig = assetsConfig.get(asset.assetId) as AssetConfig;
320
321
  const assetData = assetsData.get(asset.assetId) as ExtendedAssetData;
321
322
  const balance = userLiteData.balances.get(asset.assetId) as UserBalance;
322
-
323
+
323
324
  if (balance.type === BalanceType.supply) {
324
325
  withdrawalLimits.set(
325
326
  asset.assetId,
326
- bigIntMax(
327
- bigIntMin(
328
- assetData.balance,
329
- ((supplyBalance -
330
- (borrowBalance * masterConstants.ASSET_COEFFICIENT_SCALE) / assetConfig.collateralFactor) *
331
- 10n ** assetConfig.decimals) /
332
- prices.get(asset.assetId)! -
333
- 5n,
334
- balance.amount,
335
- ),
336
- 0n,
337
- ),
327
+ calculateMaximumWithdrawAmount(assetsConfig, assetsData, userLiteData.principals, prices, masterConstants, asset.assetId)
338
328
  );
339
329
  }
340
330
  borrowLimits.set(
package/src/api/prices.ts CHANGED
@@ -1,5 +1,6 @@
1
1
  import { beginCell, Cell, Dictionary } from '@ton/core';
2
2
  import { PriceData } from '../types/Common';
3
+ import { MAIN_POOL_NFT_ID } from '../constants/general';
3
4
 
4
5
  type NftData = {
5
6
  ledgerIndex: number;
@@ -38,7 +39,7 @@ type OutputData = {
38
39
  };
39
40
  }
40
41
 
41
- export async function getPricesByNft(nftId: string, endpoints: string[] = ["api.stardust-mainnet.iotaledger.net"]) {
42
+ export async function getPrices(endpoints: string[] = ["api.stardust-mainnet.iotaledger.net"], nftId: string = MAIN_POOL_NFT_ID) {
42
43
  return await Promise.any(endpoints.map(x => loadPrices(nftId, x)));
43
44
  }
44
45
 
@@ -19,7 +19,7 @@ import { parseMasterData } from '../api/parser';
19
19
  import { MasterData, PoolAssetConfig, PoolConfig, PoolJettonAssetConfig, PoolTonAssetConfig } from '../types/Master';
20
20
  import { JettonWallet } from './JettonWallet';
21
21
  import { getUserJettonWallet } from '../utils/userJettonWallet';
22
- import { getPricesByNft, MAINNET_POOL_CONFIG } from '..';
22
+ import { getPrices, MAINNET_POOL_CONFIG } from '..';
23
23
 
24
24
  /**
25
25
  * Parameters for the Evaa contract
@@ -424,9 +424,9 @@ export class Evaa implements Contract {
424
424
 
425
425
  async getPrices(provider: ContractProvider, endpoints?: string[]) {
426
426
  if ((endpoints?.length ?? 0) > 0) {
427
- return await getPricesByNft(this.poolConfig.nftId, endpoints);
427
+ return await getPrices(endpoints, this.poolConfig.nftId);
428
428
  } else {
429
- return await getPricesByNft(this.poolConfig.nftId);
429
+ return await getPrices(undefined, this.poolConfig.nftId);
430
430
  }
431
431
  }
432
432
  }
package/src/index.ts CHANGED
@@ -9,6 +9,7 @@ export {
9
9
  calculateAssetData,
10
10
  calculateAssetInterest,
11
11
  getAvailableToBorrow,
12
+ calculateMaximumWithdrawAmount,
12
13
  presentValue,
13
14
  calculateLiquidationData,
14
15
  } from './api/math';
@@ -17,7 +18,7 @@ export {
17
18
  export { createAssetData, createAssetConfig, parseMasterData, parseUserData, parseUserLiteData } from './api/parser';
18
19
 
19
20
  // Prices
20
- export { getPricesByNft } from './api/prices';
21
+ export { getPrices } from './api/prices';
21
22
 
22
23
  // Contracts' wrappers
23
24
  export { JettonWallet } from './contracts/JettonWallet';