Skip to content

Commit

Permalink
apr fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
0xshiba1 committed Jul 19, 2024
1 parent 7003b00 commit 4e4ad6f
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -74,10 +74,7 @@ function TooltipContent({ side, fields, d, viewBox, x }: TooltipContentProps) {

const aprPercent = new BigNumber(d[interestField] as number);
const totalAprPercent = rewardFields.reduce(
(acc, field) =>
acc.plus(
new BigNumber(d[field] as number).times(side === Side.DEPOSIT ? 1 : -1),
),
(acc, field) => acc.plus(new BigNumber(d[field] as number)),
new BigNumber(aprPercent),
);

Expand Down Expand Up @@ -110,11 +107,7 @@ function TooltipContent({ side, fields, d, viewBox, x }: TooltipContentProps) {
value={
<span style={{ color }}>
{formatPercent(
!coinType
? aprPercent
: new BigNumber(d[field] as number).times(
side === Side.DEPOSIT ? 1 : -1,
),
!coinType ? aprPercent : new BigNumber(d[field] as number),
{ useAccountingSign: true },
)}
</span>
Expand Down Expand Up @@ -180,12 +173,37 @@ function Chart({ side, data }: ChartProps) {
const minX = Math.min(...data.map((d) => d.timestampS));
const maxX = Math.max(...data.map((d) => d.timestampS));

const minY = 0;
const maxY = Math.max(
let minY = Math.min(
0,
...data.map(
(d) =>
d[
side === Side.DEPOSIT
? "depositInterestAprPercent"
: "borrowInterestAprPercent"
] ?? 0,
),
...data.map((d) =>
fields.reduce((acc: number, field) => acc + (d[field] ?? 0), 0),
),
);
if (minY < 0) minY -= 1;

let maxY = Math.max(
0,
...data.map(
(d) =>
d[
side === Side.DEPOSIT
? "depositInterestAprPercent"
: "borrowInterestAprPercent"
] ?? 0,
),
...data.map((d) =>
fields.reduce((acc: number, field) => acc + (d[field] ?? 0), 0),
),
);
if (maxY > 0) maxY += 1;

// Ticks
const ticksX = data
Expand All @@ -200,15 +218,15 @@ function Chart({ side, data }: ChartProps) {
return d.timestampS + new Date().getTimezoneOffset() * 60;
});
const ticksY = Array.from({ length: 4 }).map(
(_, index, array) => Math.ceil(maxY / (array.length - 1)) * index,
(_, index, array) => minY + ((maxY - minY) / (array.length - 1)) * index,
);

const tickFormatterX = (timestampS: number) => {
if (days === 1) return format(new Date(timestampS * 1000), "HH:mm");
return format(new Date(timestampS * 1000), "MM/dd");
};
const tickFormatterY = (value: number) =>
formatPercent(new BigNumber(value), { dp: 0 });
formatPercent(new BigNumber(value), { dp: 1 });

// Domain
const domainX = [minX, maxX];
Expand Down
3 changes: 2 additions & 1 deletion frontend/src/lib/events.ts
Original file line number Diff line number Diff line change
Expand Up @@ -200,7 +200,8 @@ export const calculateRewardAprPercent = (
? event.depositedAmountUsd
: event.borrowedAmountUsd,
)
.times(100),
.times(100)
.times(side === Side.DEPOSIT ? 1 : -1),
),
new BigNumber(0),
);
Expand Down
4 changes: 2 additions & 2 deletions frontend/src/lib/format.ts
Original file line number Diff line number Diff line change
Expand Up @@ -165,11 +165,11 @@ export const formatPercent = (
style: "percent",
minimumFractionDigits: dp,
maximumFractionDigits: dp,
}).format(value.abs().div(100).toNumber());
}).format(+value.div(100));

return !useAccountingSign || value.gte(0)
? formattedValue
: `(${formattedValue})`;
: `(${formattedValue[0] === "-" ? formattedValue.slice(1) : formattedValue})`;
};

export const formatDuration = (seconds: BigNumber) => {
Expand Down
12 changes: 10 additions & 2 deletions frontend/src/lib/liquidityMining.ts
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,11 @@ export function formatRewards(
poolReward.endTimeMs - poolReward.startTimeMs,
),
)
.div(reserve.depositedAmountUsd)
.div(
side === Side.DEPOSIT
? reserve.depositedAmountUsd
: reserve.borrowedAmountUsd,
)
.times(100)
: undefined;
const perDay = rewardReserve
Expand All @@ -93,7 +97,11 @@ export function formatRewards(
),
)
.div(365)
.div(reserve.depositedAmount);
.div(
side === Side.DEPOSIT
? reserve.depositedAmount
: reserve.borrowedAmount,
);

return {
stats: {
Expand Down

0 comments on commit 4e4ad6f

Please sign in to comment.