Skip to content

Commit 1c0658c

Browse files
authored
Merge pull request #23 from evaafi/sdk-v062c
APP-109 Sdk v062c
2 parents 2558f00 + 12a82eb commit 1c0658c

8 files changed

+55
-15
lines changed

CHANGELOG.md

+6
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,12 @@ All notable changes to this project will be documented in this file.
44
The format is based on [Keep a Changelog](https://keepachangelog.com/)
55
and this project adheres to [Semantic Versioning](https://semver.org/).
66

7+
## 0.6.2-c — 2025-01-07
8+
### Added
9+
- ```getAssetLiquidityMinusReserves``` function needed to count the amount of free amount for withdrawal
10+
### Fixed
11+
- ```borrowLimits``` function now take into account the amount available for withdrawal
12+
713
## 0.6.2-b — 2024-12-17
814
### Added
915
- new field in ```UserLiteData``` . ```fullyParsed``` and ```havePrincipalWithoutPrice```

package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@evaafi/sdk",
3-
"version": "0.6.2-b",
3+
"version": "0.6.2-c",
44
"description": "The EVAA SDK is designed to easily integrate with the EVAA lending protocol on TON blockchain.",
55
"main": "dist/index.js",
66
"files": [

src/api/math.ts

+15-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import {
22
AgregatedBalances,
3+
AssetApy,
34
AssetConfig,
45
AssetData,
56
AssetInterest,
@@ -54,6 +55,12 @@ export function calculatePresentValue(index: bigint, principalValue: bigint, mas
5455
return (principalValue * index) / masterConstants.FACTOR_SCALE;
5556
}
5657

58+
export function getAssetLiquidityMinusReserves(assetData: AssetData, masterConstants: MasterConstants) {
59+
const total_supply = calculatePresentValue(assetData.sRate, assetData.totalSupply, masterConstants);
60+
const total_borrow = calculatePresentValue(assetData.bRate, assetData.totalBorrow, masterConstants);
61+
return bigIntMin(total_supply - total_borrow, assetData.balance);
62+
}
63+
5764
export function calculateCurrentRates(assetConfig: AssetConfig, assetData: AssetData, masterConstants: MasterConstants) {
5865
const now = BigInt(Math.floor(Date.now() / 1000));
5966
const timeElapsed = now - assetData.lastAccural;
@@ -530,7 +537,7 @@ export function predictHealthFactor(args: PredictHealthFactorArgs): number {
530537
*
531538
* @returns Estimated APYs for Supply and Borrow
532539
*/
533-
export function predictAPY(args: PredictAPYArgs): AssetInterest {
540+
export function predictAPY(args: PredictAPYArgs): AssetInterest & AssetApy {
534541
const assetConfig = args.assetConfig;
535542
const assetData = args.assetData;
536543
const masterConstants = args.masterConstants;
@@ -553,5 +560,11 @@ export function predictAPY(args: PredictAPYArgs): AssetInterest {
553560
}
554561
}
555562

556-
return calculateInterestWithSupplyBorrow(totalSupply, totalBorrow, assetConfig, masterConstants);
563+
const interest = calculateInterestWithSupplyBorrow(totalSupply, totalBorrow, assetConfig, masterConstants);
564+
565+
return {
566+
...interest,
567+
supplyApy: (1 + (Number(interest.supplyInterest) / 1e12) * 24 * 3600) ** 365 - 1,
568+
borrowApy: (1 + (Number(interest.borrowInterest) / 1e12) * 24 * 3600) ** 365 - 1
569+
}
557570
}

src/api/parser.ts

+4-1
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import {
77
calculateLiquidationData,
88
calculateMaximumWithdrawAmount,
99
calculatePresentValue,
10+
getAssetLiquidityMinusReserves,
1011
getAvailableToBorrow,
1112
presentValue,
1213
} from './math';
@@ -344,6 +345,8 @@ export function parseUserData(
344345
const assetConfig = assetsConfig.get(asset.assetId) as AssetConfig;
345346
const assetData = assetsData.get(asset.assetId) as ExtendedAssetData;
346347

348+
const assetLiquidityMinusReserves = getAssetLiquidityMinusReserves(assetData, masterConstants);
349+
347350
if (balance.type === BalanceType.supply) {
348351
withdrawalLimits.set(
349352
asset.assetId,
@@ -358,7 +361,7 @@ export function parseUserData(
358361

359362
borrowLimits.set(
360363
asset.assetId,
361-
bigIntMax(0n, bigIntMin((availableToBorrow * 10n ** assetConfig.decimals) / prices.get(asset.assetId)!, assetData.balance, assetData.totalSupply - assetData.totalBorrow)),
364+
bigIntMax(0n, bigIntMin((availableToBorrow * 10n ** assetConfig.decimals) / prices.get(asset.assetId)!, assetLiquidityMinusReserves)),
362365
);
363366
}
364367

src/index.ts

+1
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ export {
1717
calculateInterestWithSupplyBorrow,
1818
predictAPY,
1919
BigMath,
20+
getAssetLiquidityMinusReserves,
2021
} from './api/math';
2122

2223
export {

tests/dust_test.ts

+9-3
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import {BalanceChangeType, createAssetConfig, Evaa, EVAA_MASTER_MAINNET, getPrices, MAINNET_LP_POOL_CONFIG, MAINNET_POOL_CONFIG, PricesCollector, TESTNET_POOL_CONFIG, USDT_MAINNET, UserDataActive} from '../src';
1+
import {BalanceChangeType, createAssetConfig, Evaa, EVAA_MASTER_MAINNET, getPrices, JUSDC_MAINNET, JUSDT_MAINNET, MAINNET_LP_POOL_CONFIG, MAINNET_POOL_CONFIG, PricesCollector, TESTNET_POOL_CONFIG, TON_MAINNET, USDT_MAINNET, UserDataActive} from '../src';
22
import {Address, beginCell, Dictionary, TonClient} from '@ton/ton';
33
import dotenv from 'dotenv';
44
import { predictHealthFactor } from '../src/api/math';
@@ -16,16 +16,22 @@ beforeAll(async () => {
1616
test('Manual dust check', async () => {
1717
const evaa = client.open(new Evaa({poolConfig: MAINNET_POOL_CONFIG}));
1818
await evaa.getSync();
19-
const user = client.open(await evaa.openUserContract(Address.parseFriendly("UQD0x5tVfMGTgUJpOvn6as58kKJEremAaXq8_rP3rA2bW3D9").address));
19+
const user = client.open(await evaa.openUserContract(Address.parseFriendly("UQDN5CpSs8HT2GO4IymOXPS5zTDzHtY-s8VTuUVAsCTwWJzM").address));
2020

2121
const collector = new PricesCollector(MAINNET_POOL_CONFIG);
2222
//console.log('priceData', priceData);
2323
await user.getSync(evaa.data!.assetsData, evaa.data!.assetsConfig, (await collector.getPrices()).dict, true);
24-
24+
2525
if (user.data?.type != 'active') {
2626
console.log('User is inactive');
2727
return;
2828
}
29+
console.log(evaa.data?.assetsData.get(JUSDC_MAINNET.assetId)?.totalSupply);
30+
console.log(evaa.data?.assetsReserves.get(JUSDC_MAINNET.assetId));
31+
console.log(evaa.data?.assetsConfig.get(JUSDC_MAINNET.assetId));
32+
console.log('withdrawalLimits', user.data.withdrawalLimits);
33+
console.log('borrowLimits', user.data.borrowLimits);
34+
2935
console.log('User principals');
3036
console.log('realPrincipals', user.data.realPrincipals);
3137
console.log('userPrincipal', user.data.principals);

tests/math/predictAPY.test.ts

+14-3
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { AssetConfig, AssetData, BalanceChangeType, calculatePresentValue, Evaa, EvaaUser, MAINNET_POOL_CONFIG, MASTER_CONSTANTS, MasterConfig, MasterConstants, mulFactor, predictAPY, Prices, PricesCollector, RawPriceData, TON_MAINNET, UserDataActive, verifyPricesSign, verifyRawPriceDataSign } from "../../src";
1+
import { AssetConfig, AssetData, BalanceChangeType, calculatePresentValue, Evaa, EvaaUser, ExtendedAssetData, MAINNET_POOL_CONFIG, MASTER_CONSTANTS, MasterConfig, MasterConstants, mulFactor, predictAPY, Prices, PricesCollector, RawPriceData, TON_MAINNET, UserDataActive, verifyPricesSign, verifyRawPriceDataSign } from "../../src";
22
import { Dictionary, OpenedContract, TonClient } from "@ton/ton";
33

44
import dotenv from 'dotenv';
@@ -7,10 +7,10 @@ describe('parseUserData test', () => {
77
dotenv.config();
88
let clientMainNet;
99
let evaaMainNet: OpenedContract<Evaa>;
10-
let assetsData: Dictionary<bigint, AssetData>;
10+
let assetsData: Dictionary<bigint, ExtendedAssetData>;
1111
let assetsConfig: Dictionary<bigint, AssetConfig>;
1212
let masterConstants: MasterConstants;
13-
let tonData: AssetData;
13+
let tonData: ExtendedAssetData;
1414
let tonConfig: AssetConfig;
1515
let totalSupply: bigint;
1616
let totalBorrow: bigint;
@@ -63,4 +63,15 @@ describe('parseUserData test', () => {
6363

6464
expect(predicted.borrowInterest).toEqual(tonConfig.baseBorrowRate);
6565
});
66+
67+
test('test 0 value', () => {
68+
const predicted = predictAPY({
69+
amount: 0n,
70+
balanceChangeType: BalanceChangeType.Repay,
71+
assetData: tonData,
72+
assetConfig: tonConfig,
73+
masterConstants: masterConstants
74+
});
75+
expect(predicted.borrowInterest).toEqual(tonData.borrowInterest);
76+
});
6677
});

tests/supply_withdraw_test.ts

+5-5
Original file line numberDiff line numberDiff line change
@@ -207,8 +207,8 @@ test('Just supply mainnet', async () => {
207207
console.log(e);
208208
}
209209
})*/
210-
/*
211-
test('Just withdraw', async () => {
210+
211+
/*test('Just withdraw', async () => {
212212
console.log(priceData.dict);
213213
await evaaMainNet.getSync();
214214
@@ -222,14 +222,14 @@ test('Just withdraw', async () => {
222222
await evaaMainNet.sendWithdraw(sender_mainnet, toNano(0.7), {
223223
queryID: 0n,
224224
includeUserCode: true,
225-
amount: 100_000_000n,
225+
amount: 200_000n,
226226
userAddress: address,
227-
asset: TON_MAINNET,
227+
asset: USDT_MAINNET,
228228
priceData: currentWithdrawPrices.dataCell,
229229
amountToTransfer: toNano(0),
230230
payload: Cell.EMPTY
231231
});
232-
})
232+
})*/
233233

234234
/*test('Liquidate test', async () => {
235235
await evaa.getSync();

0 commit comments

Comments
 (0)