Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix underlyingBalance computing #114

Closed
wants to merge 4 commits into from
Closed
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
6 changes: 0 additions & 6 deletions .eslintrc.json

This file was deleted.

11 changes: 11 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,17 @@
"singleQuote": true,
"trailingComma": "es5"
},
"eslint": {
"parser": "@typescript-eslint/parser",
"extends": [
"eslint:recommended",
"plugin:@typescript-eslint/recommended",
"plugin:import/errors",
"plugin:import/warnings",
"plugin:import/typescript",
"plugin:prettier/recommended"
]
},
"resolutions": {
"**/typescript": "^4.0.5",
"**/@typescript-eslint/eslint-plugin": "^4.6.1",
Expand Down
56 changes: 55 additions & 1 deletion src/helpers/pool-math.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,12 @@ import BigNumber from 'bignumber.js';

import {
BigNumberValue,
pow10,
valueToBigNumber,
valueToZDBigNumber,
pow10,
} from './bignumber';
import * as RayMath from './ray-math';
import { RAY } from './ray-math';
import { SECONDS_PER_YEAR } from './constants';

export const LTV_PRECISION = 4;
Expand Down Expand Up @@ -127,3 +128,56 @@ export function calculateAverageRate(
.multipliedBy(SECONDS_PER_YEAR)
.toString();
}

export function currentATokenBalance(
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this is essentially an exact implementation of calculateCumulatedBalance from v1, let's ping @kyzia551 and ask what was the rational behind changing it in first place.

It's essentially the question, why the change from:
https://github.com/aave/aave-js/blob/master/src/v1/computations-and-formatting.ts#L207 to
https://github.com/aave/aave-js/blob/master/src/v2/computations-and-formatting.ts#L78

part of the changes seem to be the removal of redirection, but I have problems following the rational behind the other changes

scaledATokenBalance: BigNumberValue,
liquidityIndex: BigNumberValue,
currentLiquidityRate: BigNumberValue,
lastUpdateTimestamp: number,
currentTimestamp: number
): BigNumber {
const principalBalanceRay = RayMath.wadToRay(scaledATokenBalance);
const normalizedIncome = getNormalizedIncome(
liquidityIndex,
currentLiquidityRate,
lastUpdateTimestamp,
currentTimestamp
);
const scaledATokenBalanceRay = RayMath.rayMul(
principalBalanceRay,
normalizedIncome
);
return RayMath.rayToWad(scaledATokenBalanceRay);
}

function getNormalizedIncome(
liquidityIndex: BigNumberValue,
currentLiquidityRate: BigNumberValue,
lastUpdateTimestamp: number,
currentTimestamp: number
): BigNumber {
liquidityIndex = valueToZDBigNumber(liquidityIndex);
currentLiquidityRate = valueToZDBigNumber(currentLiquidityRate);
if (currentTimestamp === lastUpdateTimestamp) {
return liquidityIndex;
}
const linearInterest = calculateLinearInterest(
currentLiquidityRate,
lastUpdateTimestamp,
currentTimestamp
);
return RayMath.rayMul(linearInterest, liquidityIndex);
}

function calculateLinearInterest(
currentLiquidityRate: BigNumberValue,
lastUpdateTimestamp: number,
currentTimestamp: number
): BigNumber {
currentLiquidityRate = valueToZDBigNumber(currentLiquidityRate);
const timeDifference = currentTimestamp - lastUpdateTimestamp;
return currentLiquidityRate
.multipliedBy(timeDifference)
.div(SECONDS_PER_YEAR)
.plus(RAY);
}
52 changes: 52 additions & 0 deletions src/test/v2/computation-and-formatting.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import {
formatUserSummaryData,
} from '../../v2/computations-and-formatting';
import BigNumber from 'bignumber.js';
import { currentATokenBalance } from '../../helpers/pool-math';
import * as RayMath from '../../helpers/ray-math';

const mockReserve: ReserveData = {
underlyingAsset: '0xff795577d9ac8bd7d90ee22b6c1703490b6512fd',
Expand Down Expand Up @@ -151,5 +153,55 @@ describe('computations and formattings', () => {

expect(new BigNumber(second.totalDebt).gte(first.totalDebt)).toBe(true);
});

it('should compute collateral balance from blockchain data', () => {
// data exported from user 0xa5a69107816c5e3dfa5561e6b621dfe6294f6e5b
// at block number: 11581421
// reserve: YFI
const scaledATokenBalance = '161316503206059870';
const liquidityIndex = '1001723339432542553527150680';
const currentLiquidityRate = '22461916953455574582370088';
const lastUpdateTimestamp = 1609673617;
// at a later time, but on the same block
// expected balance computed with hardhat
const currentTimestamp = 1609675535;
const expectedATokenBalance = '161594727054623229';
const underlyingBalance = currentATokenBalance(
scaledATokenBalance,
liquidityIndex,
currentLiquidityRate,
lastUpdateTimestamp,
currentTimestamp
).toString();
expect(underlyingBalance).toBe(expectedATokenBalance);
});

it('should compute collateral balance from subgraph data', () => {
// id: 0xa5a69107816c5e3dfa5561e6b621dfe6294f6e5b0x0bc529c00c6401aef6d220be8c6ea1667f6ad93e0xb53c1a33016b2dc2ff3653530bff1848a515c8c5
const mockUserReserve = {
scaledATokenBalance: '161316503206059870',
currentATokenBalance: '161422626192524099',
lastUpdateTimestamp: 1609361267,
liquidityRate: '356057734567773693054943410',
};
const scaledATokenBalanceRay = RayMath.wadToRay(
mockUserReserve.scaledATokenBalance
);
const currentATokenBalanceRay = RayMath.wadToRay(
mockUserReserve.currentATokenBalance
);
const liquidityIndex = RayMath.rayDiv(
currentATokenBalanceRay,
scaledATokenBalanceRay
);
const underlyingBalance = currentATokenBalance(
mockUserReserve.scaledATokenBalance,
liquidityIndex,
mockUserReserve.liquidityRate,
mockUserReserve.lastUpdateTimestamp,
mockUserReserve.lastUpdateTimestamp
).toString();
expect(underlyingBalance).toBe(mockUserReserve.currentATokenBalance);
});
});
});
2 changes: 1 addition & 1 deletion src/v1/computations-and-formatting.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ export function getCompoundedBorrowBalance(

let cumulatedInterest;
if (userReserve.borrowRateMode === BorrowRateMode.Variable) {
let compoundedInterest = calculateCompoundedInterest(
const compoundedInterest = calculateCompoundedInterest(
reserve.variableBorrowRate,
currentTimestamp,
reserve.lastUpdateTimestamp
Expand Down
15 changes: 8 additions & 7 deletions src/v2/computations-and-formatting.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,28 +2,29 @@ import BigNumber from 'bignumber.js';

import {
BigNumberValue,
valueToBigNumber,
valueToZDBigNumber,
normalize,
pow10,
valueToBigNumber,
valueToZDBigNumber,
} from '../helpers/bignumber';
import {
calculateAvailableBorrowsETH,
calculateAverageRate,
calculateCompoundedInterest,
calculateHealthFactorFromBalances,
currentATokenBalance,
getCompoundedBalance,
getCompoundedStableBalance,
calculateAverageRate,
LTV_PRECISION,
calculateCompoundedInterest,
} from '../helpers/pool-math';
import { rayMul } from '../helpers/ray-math';
import {
ComputedReserveData,
ComputedUserReserve,
ReserveData,
ReserveRatesData,
UserReserveData,
UserSummaryData,
ReserveRatesData,
ComputedReserveData,
} from './types';
import { ETH_DECIMALS, RAY_DECIMALS, USD_DECIMALS } from '../helpers/constants';

Expand Down Expand Up @@ -75,7 +76,7 @@ export function computeUserReserveData(
price: { priceInEth },
decimals,
} = poolReserve;
const underlyingBalance = getCompoundedBalance(
const underlyingBalance = currentATokenBalance(
userReserve.scaledATokenBalance,
poolReserve.liquidityIndex,
poolReserve.liquidityRate,
Expand Down