Skip to content

Commit

Permalink
fix compute underlyingBalance
Browse files Browse the repository at this point in the history
  • Loading branch information
alexandre-abrioux committed Mar 3, 2021
1 parent cd01845 commit 3e2edb6
Show file tree
Hide file tree
Showing 3 changed files with 94 additions and 10 deletions.
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(
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);
}
33 changes: 31 additions & 2 deletions src/test/v2/computation-and-formatting.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@ import {
formatUserSummaryData,
} from '../../v2/computations-and-formatting';
import BigNumber from 'bignumber.js';
import { getCompoundedBalance } from '../../helpers/pool-math';
import { currentATokenBalance } from '../../helpers/pool-math';
import * as RayMath from '../../helpers/ray-math';

const mockReserve: ReserveData = {
underlyingAsset: '0xff795577d9ac8bd7d90ee22b6c1703490b6512fd',
Expand Down Expand Up @@ -165,7 +166,7 @@ describe('computations and formattings', () => {
// expected balance computed with hardhat
const currentTimestamp = 1609675535;
const expectedATokenBalance = '161594727054623229';
const underlyingBalance = getCompoundedBalance(
const underlyingBalance = currentATokenBalance(
scaledATokenBalance,
liquidityIndex,
currentLiquidityRate,
Expand All @@ -174,5 +175,33 @@ describe('computations and formattings', () => {
).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);
});
});
});
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

0 comments on commit 3e2edb6

Please sign in to comment.