Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 11 additions & 8 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@blend-capital/blend-sdk",
"version": "3.2.0-beta",
"version": "3.2.0-beta.2",
"description": "Javascript SDK for the Blend Protocol",
"type": "module",
"scripts": {
Expand Down
8 changes: 5 additions & 3 deletions src/pool/user_positions_est.ts
Original file line number Diff line number Diff line change
Expand Up @@ -120,9 +120,11 @@ export class PositionsEstimate {
const borrowLimit =
totalEffectiveCollateral == 0 ? 0 : totalEffectiveLiabilities / totalEffectiveCollateral;
const netApy =
totalBorrowed + totalSupplied == 0
? 0
: (supplyApy - borrowApy) / (totalBorrowed + totalSupplied);
totalSupplied == 0
? // if user has no supplied funds and has borrowed funds (e.g. a bad debt position), the debt will
// be forgiven as bad debt so the net APY is still 0
0
: (supplyApy - borrowApy) / totalSupplied;
supplyApy = totalSupplied == 0 ? 0 : supplyApy / totalSupplied;
borrowApy = totalBorrowed == 0 ? 0 : borrowApy / totalBorrowed;

Expand Down
154 changes: 154 additions & 0 deletions test/pool/user_position_est.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,154 @@
import { PoolOracle, PoolV2, Positions, PositionsEstimate } from '../../src/index.js';
import { toFixed } from '../../src/math.js';
import { ReserveV2 } from '../../src/pool/reserve.js';

describe('user position estimation', () => {
const timestamp = Math.floor(Date.now() / 1000);
const reserve_0 = new ReserveV2(
'pool_id',
'asset_0',
{
index: 0,
c_factor: 9500000,
l_factor: 9000000,
decimals: 7,
} as any,
{
dRate: BigInt(1_400_000_000_000), // d_rate
bRate: BigInt(1_250_000_000_000), // b_rate
lastTime: timestamp,
} as any,
undefined, // borrow emissions
undefined, // supply emissions
0.08, // borrow APR
0.09, // estBorrowApy
0.05, // supply APR
0.06, // estSupplyApy
0 // latestLedger
);

const reserve_1 = new ReserveV2(
'pool_id',
'asset_1',
{
index: 1,
c_factor: 0,
l_factor: 5000000,
decimals: 7,
} as any,
{
dRate: BigInt(1_200_000_000_000), // d_rate
bRate: BigInt(1_100_000_000_000), // b_rate
lastTime: timestamp,
} as any,
undefined, // borrow emissions
undefined, // supply emissions
0.02, // borrow APR
0.03, // estBorrowApy
0.01, // supply APR
0.02, // estSupplyApy
0 // latestLedger
);

const reserve_2 = new ReserveV2(
'pool_id',
'asset_2',
{
index: 2,
c_factor: 9000000,
l_factor: 8500000,
decimals: 7,
} as any,
{
dRate: BigInt(1_150_000_000_000), // d_rate
bRate: BigInt(1_050_000_000_000), // b_rate
lastTime: timestamp,
} as any,
undefined, // borrow emissions
undefined, // supply emissions
0.12, // borrow APR
0.15, // estBorrowApy
0.09, // supply APR
0.11, // estSupplyApy
0 // latestLedger
);

const pool_oracle = new PoolOracle(
'oracle_id',
new Map([
['asset_0', { price: toFixed(1, 7), timestamp }],
['asset_1', { price: toFixed(0.3, 7), timestamp }],
['asset_2', { price: toFixed(200, 7), timestamp }],
]),
7,
0
);

const pool = new PoolV2(
{} as any,
'pool_id',
{} as any,
new Map([
['asset_0', reserve_0],
['asset_1', reserve_1],
['asset_2', reserve_2],
]),
timestamp
);

it('calculates positions estimate correctly', () => {
const user_positions = new Positions(
// liabilities
new Map([[2, toFixed(10, 7)]]),
// collateral
new Map([[0, toFixed(4_000, 7)]]),
// supply
new Map([[1, toFixed(10_000, 7)]])
);

const estimate = PositionsEstimate.build(pool, pool_oracle, user_positions);

expect(estimate.totalBorrowed).toBeCloseTo(10 * 200 * 1.15);
expect(estimate.totalSupplied).toBeCloseTo(4000 * 1 * 1.25 + 10_000 * 0.3 * 1.1);
expect(estimate.totalEffectiveLiabilities).toBeCloseTo((10 * 200 * 1.15) / 0.85);
expect(estimate.totalEffectiveCollateral).toBeCloseTo(4000 * 1 * 1.25 * 0.95);
expect(estimate.borrowCap).toBeCloseTo(
estimate.totalEffectiveCollateral - estimate.totalEffectiveLiabilities
);
expect(estimate.borrowLimit).toBeCloseTo(
estimate.totalEffectiveLiabilities / estimate.totalEffectiveCollateral
);
expect(estimate.netApy).toBeCloseTo(
(estimate.supplyApy - estimate.borrowApy) / estimate.totalSupplied
);
expect(estimate.supplyApy).toBeCloseTo(
(4000 * 1 * 1.25 * 0.06 + 10_000 * 0.3 * 1.1 * 0.02) / estimate.totalSupplied
);
expect(estimate.borrowApy).toBeCloseTo((10 * 200 * 1.15 * 0.15) / estimate.totalBorrowed);
});

it('calculates positions estimate correctly for bad debt user', () => {
const user_positions = new Positions(
// liabilities
new Map([[2, toFixed(10, 7)]]),
// collateral
new Map(),
// supply
new Map()
);

const estimate = PositionsEstimate.build(pool, pool_oracle, user_positions);

expect(estimate.totalBorrowed).toBeCloseTo(10 * 200 * 1.15);
expect(estimate.totalSupplied).toBeCloseTo(0);
expect(estimate.totalEffectiveLiabilities).toBeCloseTo((10 * 200 * 1.15) / 0.85);
expect(estimate.totalEffectiveCollateral).toBeCloseTo(0);
expect(estimate.borrowCap).toBeCloseTo(
estimate.totalEffectiveCollateral - estimate.totalEffectiveLiabilities
);
expect(estimate.borrowLimit).toBeCloseTo(0);
expect(estimate.netApy).toBeCloseTo(0);
expect(estimate.supplyApy).toBeCloseTo(0);
expect(estimate.borrowApy).toBeCloseTo((10 * 200 * 1.15 * 0.15) / estimate.totalBorrowed);
});
});