Skip to content

Commit

Permalink
fix: incorrect conversion from APR to APY (#1755)
Browse files Browse the repository at this point in the history
  • Loading branch information
therealemjy authored Nov 10, 2023
1 parent 26ca688 commit c4f1a9c
Show file tree
Hide file tree
Showing 9 changed files with 58 additions and 25 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,8 @@ describe('getHypotheticalPrimeApys', () => {

expect(response).toMatchInlineSnapshot(`
{
"borrowApyPercentage": "3.4582968502661515",
"supplyApyPercentage": "2.633999809916232",
"borrowApyPercentage": "0.3405770666813801",
"supplyApyPercentage": "0.2603373646915319",
}
`);
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ exports[`getMainPool > does not fetch Prime distributions if user is not Prime 1
"type": "rewardDistributor",
},
{
"apyPercentage": "2.0200781032923",
"apyPercentage": "0.20019958435848473",
"referenceValues": {
"userBorrowBalanceTokens": "66012867068.78838627155172413793",
"userSupplyBalanceTokens": "15951129109546755.24488088106947945753",
Expand Down Expand Up @@ -64,7 +64,7 @@ exports[`getMainPool > does not fetch Prime distributions if user is not Prime 1
"type": "rewardDistributor",
},
{
"apyPercentage": "2.3265798060834175",
"apyPercentage": "0.23026397657694986",
"referenceValues": {
"userBorrowBalanceTokens": "66012867068.78838627155172413793",
"userSupplyBalanceTokens": "15951129109546755.24488088106947945753",
Expand Down Expand Up @@ -118,7 +118,7 @@ exports[`getMainPool > does not fetch Prime distributions if user is not Prime 1
"type": "rewardDistributor",
},
{
"apyPercentage": "2.0200781032923",
"apyPercentage": "0.20019958435848473",
"referenceValues": {
"userBorrowBalanceTokens": "110643.61749019365079365079",
"userSupplyBalanceTokens": "1.358795738297682434014619091383216952499549585717162e+31",
Expand Down Expand Up @@ -160,7 +160,7 @@ exports[`getMainPool > does not fetch Prime distributions if user is not Prime 1
"type": "rewardDistributor",
},
{
"apyPercentage": "2.3265798060834175",
"apyPercentage": "0.23026397657694986",
"referenceValues": {
"userBorrowBalanceTokens": "110643.61749019365079365079",
"userSupplyBalanceTokens": "1.358795738297682434014619091383216952499549585717162e+31",
Expand Down Expand Up @@ -214,7 +214,7 @@ exports[`getMainPool > does not fetch Prime distributions if user is not Prime 1
"type": "rewardDistributor",
},
{
"apyPercentage": "2.0200781032923",
"apyPercentage": "0.20019958435848473",
"referenceValues": {
"userBorrowBalanceTokens": "453577420.21510010545510979029",
"userSupplyBalanceTokens": "447427293535420150.537063455989777353",
Expand Down Expand Up @@ -256,7 +256,7 @@ exports[`getMainPool > does not fetch Prime distributions if user is not Prime 1
"type": "rewardDistributor",
},
{
"apyPercentage": "2.3265798060834175",
"apyPercentage": "0.23026397657694986",
"referenceValues": {
"userBorrowBalanceTokens": "453577420.21510010545510979029",
"userSupplyBalanceTokens": "447427293535420150.537063455989777353",
Expand Down Expand Up @@ -657,7 +657,7 @@ exports[`getMainPool > fetches and formats Prime distributions and Prime distrib
"type": "rewardDistributor",
},
{
"apyPercentage": "3.2516057037486057",
"apyPercentage": "0.32051113933926345",
"token": {
"address": "0x16227D60f7a0e586C66B005219dfc887D13C9531",
"asset": "/src/packages/tokens/img/usdc.svg",
Expand All @@ -667,7 +667,7 @@ exports[`getMainPool > fetches and formats Prime distributions and Prime distrib
"type": "prime",
},
{
"apyPercentage": "2.0200781032923",
"apyPercentage": "0.20019958435848473",
"referenceValues": {
"userBorrowBalanceTokens": "66012867068.78838627155172413793",
"userSupplyBalanceTokens": "15951129109546755.24488088106947945753",
Expand Down Expand Up @@ -709,7 +709,7 @@ exports[`getMainPool > fetches and formats Prime distributions and Prime distrib
"type": "rewardDistributor",
},
{
"apyPercentage": "2.942340858505288",
"apyPercentage": "0.2904197513866391",
"token": {
"address": "0x16227D60f7a0e586C66B005219dfc887D13C9531",
"asset": "/src/packages/tokens/img/usdc.svg",
Expand All @@ -719,7 +719,7 @@ exports[`getMainPool > fetches and formats Prime distributions and Prime distrib
"type": "prime",
},
{
"apyPercentage": "2.3265798060834175",
"apyPercentage": "0.23026397657694986",
"referenceValues": {
"userBorrowBalanceTokens": "66012867068.78838627155172413793",
"userSupplyBalanceTokens": "15951129109546755.24488088106947945753",
Expand Down Expand Up @@ -773,7 +773,7 @@ exports[`getMainPool > fetches and formats Prime distributions and Prime distrib
"type": "rewardDistributor",
},
{
"apyPercentage": "3.2516057037486057",
"apyPercentage": "0.32051113933926345",
"token": {
"address": "0xA11c8D9DC9b66E209Ef60F0C8D969D3CD988782c",
"asset": "/src/packages/tokens/img/usdt.svg",
Expand All @@ -783,7 +783,7 @@ exports[`getMainPool > fetches and formats Prime distributions and Prime distrib
"type": "prime",
},
{
"apyPercentage": "2.0200781032923",
"apyPercentage": "0.20019958435848473",
"referenceValues": {
"userBorrowBalanceTokens": "110643.61749019365079365079",
"userSupplyBalanceTokens": "1.358795738297682434014619091383216952499549585717162e+31",
Expand Down Expand Up @@ -825,7 +825,7 @@ exports[`getMainPool > fetches and formats Prime distributions and Prime distrib
"type": "rewardDistributor",
},
{
"apyPercentage": "2.942340858505288",
"apyPercentage": "0.2904197513866391",
"token": {
"address": "0xA11c8D9DC9b66E209Ef60F0C8D969D3CD988782c",
"asset": "/src/packages/tokens/img/usdt.svg",
Expand All @@ -835,7 +835,7 @@ exports[`getMainPool > fetches and formats Prime distributions and Prime distrib
"type": "prime",
},
{
"apyPercentage": "2.3265798060834175",
"apyPercentage": "0.23026397657694986",
"referenceValues": {
"userBorrowBalanceTokens": "110643.61749019365079365079",
"userSupplyBalanceTokens": "1.358795738297682434014619091383216952499549585717162e+31",
Expand Down Expand Up @@ -889,7 +889,7 @@ exports[`getMainPool > fetches and formats Prime distributions and Prime distrib
"type": "rewardDistributor",
},
{
"apyPercentage": "3.2516057037486057",
"apyPercentage": "0.32051113933926345",
"token": {
"address": "0x8301F2213c0eeD49a7E28Ae4c3e91722919B8B47",
"asset": "/src/packages/tokens/img/busd.svg",
Expand All @@ -899,7 +899,7 @@ exports[`getMainPool > fetches and formats Prime distributions and Prime distrib
"type": "prime",
},
{
"apyPercentage": "2.0200781032923",
"apyPercentage": "0.20019958435848473",
"referenceValues": {
"userBorrowBalanceTokens": "453577420.21510010545510979029",
"userSupplyBalanceTokens": "447427293535420150.537063455989777353",
Expand Down Expand Up @@ -941,7 +941,7 @@ exports[`getMainPool > fetches and formats Prime distributions and Prime distrib
"type": "rewardDistributor",
},
{
"apyPercentage": "2.942340858505288",
"apyPercentage": "0.2904197513866391",
"token": {
"address": "0x8301F2213c0eeD49a7E28Ae4c3e91722919B8B47",
"asset": "/src/packages/tokens/img/busd.svg",
Expand All @@ -951,7 +951,7 @@ exports[`getMainPool > fetches and formats Prime distributions and Prime distrib
"type": "prime",
},
{
"apyPercentage": "2.3265798060834175",
"apyPercentage": "0.23026397657694986",
"referenceValues": {
"userBorrowBalanceTokens": "453577420.21510010545510979029",
"userSupplyBalanceTokens": "447427293535420150.537063455989777353",
Expand Down
1 change: 1 addition & 0 deletions src/containers/PrimeStatusBanner/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@ export const PrimeStatusBannerUi: React.FC<PrimeStatusBannerUiProps> = ({
const readableStakeDeltaTokens = useConvertWeiToReadableTokenString({
value: stakeDeltaTokens,
token: xvs,
roundingMode: BigNumber.ROUND_UP,
});

const readableApyBoostPercentage = useFormatPercentageToReadableValue({
Expand Down
2 changes: 1 addition & 1 deletion src/utilities/convertAprToApy/__tests__/index.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,6 @@ describe('convertAprToApy', () => {
aprBips: '23',
});

expect(res).toMatchInlineSnapshot('"2.3265798060834175"');
expect(res).toMatchInlineSnapshot('"0.23026397657694986"');
});
});
2 changes: 1 addition & 1 deletion src/utilities/convertAprToApy/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import calculateApy from '../calculateApy';

export const convertAprToApy = ({ aprBips }: { aprBips: string }) => {
// Convert bips to daily rate
const dailyRate = new BigNumber(aprBips).div(1000).div(DAYS_PER_YEAR);
const dailyRate = new BigNumber(aprBips).div(10000).div(DAYS_PER_YEAR);
// Convert daily rate to APY
return calculateApy({ dailyRate });
};
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import PLACEHOLDER_KEY from 'constants/placeholderKey';

import formatTokensToReadableValue, { FormatTokensToReadableValueInput } from '..';

describe('utilities/formatTokensToReadableValue', () => {
describe('formatTokensToReadableValue', () => {
test('should return placeholder when value is undefined', () => {
const result = formatTokensToReadableValue({
value: undefined,
Expand Down Expand Up @@ -79,4 +79,21 @@ describe('utilities/formatTokensToReadableValue', () => {
});
expect(negativeResult).toEqual('-1.23K BUSD');
});

test('should return a formatted value rounded using the passed rounding mode', () => {
const input: FormatTokensToReadableValueInput = {
value: new BigNumber(12.5678),
token: busd,
roundingMode: BigNumber.ROUND_DOWN,
};

const result = formatTokensToReadableValue(input);
expect(result).toEqual('12.56 BUSD');

const negativeResult = formatTokensToReadableValue({
...input,
value: new BigNumber(-12.5678),
});
expect(negativeResult).toEqual('-12.56 BUSD');
});
});
4 changes: 4 additions & 0 deletions src/utilities/formatTokensToReadableValue/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,14 @@ export interface FormatTokensToReadableValueInput {
value: BigNumber | undefined;
token: Token | VToken | undefined;
addSymbol?: boolean;
roundingMode?: BigNumber.RoundingMode;
}

export const formatTokensToReadableValue = ({
value,
token,
addSymbol = true,
roundingMode,
}: FormatTokensToReadableValueInput) => {
if (!token || !value) {
return PLACEHOLDER_KEY;
Expand All @@ -35,6 +37,7 @@ export const formatTokensToReadableValue = ({
const formattedReadableValue = shortenValueWithSuffix({
minDecimalPlaces: MIN_DECIMALS,
value: new BigNumber(MAX_VALUE),
roundingMode,
});
readableValue = `${isNegative ? '< -' : '> '}${formattedReadableValue}`;
} else if (absoluteValue.isLessThan(MIN_VALUE)) {
Expand All @@ -44,6 +47,7 @@ export const formatTokensToReadableValue = ({
value: absoluteValue,
minDecimalPlaces: MIN_DECIMALS,
maxDecimalPlaces: token.decimals,
roundingMode,
});

readableValue = `${isNegative ? '-' : ''}${formattedReadableValue}`;
Expand Down
11 changes: 10 additions & 1 deletion src/utilities/shortenValueWithSuffix/__tests__/index.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import BigNumber from 'bignumber.js';

import shortenValueWithSuffix from '..';

describe('utilities/shortenValueWithSuffix', () => {
describe('shortenValueWithSuffix', () => {
it('should return a formatted value in billions with "T" suffix when value is greater or equal to one trillion', () => {
const value = new BigNumber(1500000000000);
const result = shortenValueWithSuffix({
Expand Down Expand Up @@ -68,4 +68,13 @@ describe('utilities/shortenValueWithSuffix', () => {
});
expect(result).toEqual('100.001');
});

it('should return a formatted value rounded using the passed rounding mode', () => {
const value = new BigNumber('100.0013921');
const result = shortenValueWithSuffix({
value,
roundingMode: BigNumber.ROUND_DOWN,
});
expect(result).toEqual('100.0013');
});
});
4 changes: 3 additions & 1 deletion src/utilities/shortenValueWithSuffix/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,14 @@ export interface ShortenValueWithSuffix {
value: BigNumber;
minDecimalPlaces?: number;
maxDecimalPlaces?: number;
roundingMode?: BigNumber.RoundingMode;
}

const shortenValueWithSuffix = ({
value,
minDecimalPlaces,
maxDecimalPlaces,
roundingMode,
}: ShortenValueWithSuffix) => {
if (value.isEqualTo(0)) {
return '0';
Expand Down Expand Up @@ -44,7 +46,7 @@ const shortenValueWithSuffix = ({
maxDecimalPlaces,
});

return `${new BigNumber(formattedValue).toFormat(decimalPlaces)}${suffix}`;
return `${new BigNumber(formattedValue).toFormat(decimalPlaces, roundingMode)}${suffix}`;
};

export default shortenValueWithSuffix;

0 comments on commit c4f1a9c

Please sign in to comment.