Skip to content

Commit

Permalink
fix: account for approximation error in deposit and borrow rates (#270)
Browse files Browse the repository at this point in the history
* fix: account for approximation error in deposit and borrow rates

* fix: add minus one to rate and update test snapshot
  • Loading branch information
defispartan authored Oct 21, 2021
1 parent 50b3a88 commit de6a678
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ Object {
"isFrozen": false,
"lastUpdateTimestamp": 1606992400,
"liquidityIndex": "1.00016444737961059057",
"liquidityRate": "0.02677620073531209306",
"liquidityRate": "0.02713790431950061223",
"name": "",
"optimalUtilisationRate": "109284236984257451326752610",
"price": Object {
Expand All @@ -100,7 +100,7 @@ Object {
"sIncentivesAPY": "0",
"sIncentivesLastUpdateTimestamp": 1606992400,
"sTokenIncentivesIndex": "0",
"stableBorrowRate": "0.10928437169401419784",
"stableBorrowRate": "0.11547951583122878182",
"stableBorrowRateEnabled": true,
"stableDebtLastUpdateTimestamp": 1606992400,
"stableDebtTokenAddress": "0x3B91257Fe5CA63b4114ac41A0d467D25E2F747F3",
Expand All @@ -121,7 +121,7 @@ Object {
"vIncentivesLastUpdateTimestamp": 1606992400,
"vTokenIncentivesIndex": "0",
"variableBorrowIndex": "1.00023285443371120965",
"variableBorrowRate": "0.03856874338802839568",
"variableBorrowRate": "0.03932217240517564451",
"variableDebtTokenAddress": "0xEAbBDBe7aaD7d5A278da40967E62C8c8Fe5fAec8",
"variableRateSlope1": "40000000000000000000000000",
"variableRateSlope2": "750000000000000000000000000",
Expand All @@ -148,7 +148,7 @@ Object {
"isFrozen": false,
"lastUpdateTimestamp": 1606992400,
"liquidityIndex": "1.00016444737961059057",
"liquidityRate": "0.02677620073531209306",
"liquidityRate": "0.02713790431950061223",
"name": "",
"optimalUtilisationRate": "109284236984257451326752610",
"price": Object {
Expand All @@ -161,7 +161,7 @@ Object {
"sIncentivesAPY": "0",
"sIncentivesLastUpdateTimestamp": 1606992400,
"sTokenIncentivesIndex": "0",
"stableBorrowRate": "0.10928437169401419784",
"stableBorrowRate": "0.11547951583122878182",
"stableBorrowRateEnabled": true,
"stableDebtLastUpdateTimestamp": 1606992400,
"stableDebtTokenAddress": "0x3B91257Fe5CA63b4114ac41A0d467D25E2F747F3",
Expand All @@ -182,7 +182,7 @@ Object {
"vIncentivesLastUpdateTimestamp": 1606992400,
"vTokenIncentivesIndex": "0",
"variableBorrowIndex": "1.00023285443371120965",
"variableBorrowRate": "0.03856874338802839568",
"variableBorrowRate": "0.03932217240517564451",
"variableDebtTokenAddress": "0xEAbBDBe7aaD7d5A278da40967E62C8c8Fe5fAec8",
"variableRateSlope1": "40000000000000000000000000",
"variableRateSlope2": "750000000000000000000000000",
Expand Down
29 changes: 25 additions & 4 deletions src/v2/computations-and-formatting.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import {
calculateCompoundedInterest,
getLinearBalance,
} from '../helpers/pool-math';
import { rayDiv, rayMul } from '../helpers/ray-math';
import { RAY, rayDiv, rayMul, rayPow } from '../helpers/ray-math';
import {
ComputedUserReserve,
ReserveData,
Expand Down Expand Up @@ -586,6 +586,27 @@ export function formatReserves(
)
: '0';

const exactLiquidityRate = rayPow(
valueToZDBigNumber(reserve.liquidityRate)
.dividedBy(SECONDS_PER_YEAR)
.plus(RAY),
SECONDS_PER_YEAR
).minus(RAY);

const exactVariableBorrowRate = rayPow(
valueToZDBigNumber(reserve.variableBorrowRate)
.dividedBy(SECONDS_PER_YEAR)
.plus(RAY),
SECONDS_PER_YEAR
).minus(RAY);

const exactStableBorrowRate = rayPow(
valueToZDBigNumber(reserve.stableBorrowRate)
.dividedBy(SECONDS_PER_YEAR)
.plus(RAY),
SECONDS_PER_YEAR
).minus(RAY);

return {
...reserve,
totalVariableDebt,
Expand All @@ -606,7 +627,7 @@ export function formatReserves(
LTV_PRECISION
),
reserveFactor: normalize(reserve.reserveFactor, LTV_PRECISION),
variableBorrowRate: normalize(reserve.variableBorrowRate, RAY_DECIMALS),
variableBorrowRate: normalize(exactVariableBorrowRate, RAY_DECIMALS),
avg30DaysVariableBorrowRate: reserve30DaysAgo
? calculateAverageRate(
reserve30DaysAgo.variableBorrowIndex,
Expand All @@ -624,8 +645,8 @@ export function formatReserves(
)
: undefined,

stableBorrowRate: normalize(reserve.stableBorrowRate, RAY_DECIMALS),
liquidityRate: normalize(reserve.liquidityRate, RAY_DECIMALS),
stableBorrowRate: normalize(exactStableBorrowRate, RAY_DECIMALS),
liquidityRate: normalize(exactLiquidityRate, RAY_DECIMALS),
liquidityIndex: normalize(reserve.liquidityIndex, RAY_DECIMALS),
reserveLiquidationThreshold: normalize(
reserve.reserveLiquidationThreshold,
Expand Down

0 comments on commit de6a678

Please sign in to comment.