Skip to content

Commit 24f3a18

Browse files
authored
Cleanup formatRate util (#1631)
* Refactor to options object * Fix tests
1 parent c4cc49b commit 24f3a18

File tree

16 files changed

+57
-41
lines changed

16 files changed

+57
-41
lines changed

apps/hyperdrive-trading/src/base/formatRate.test.ts

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,33 +3,37 @@ import { parseUnits } from "viem";
33
import { expect, test } from "vitest";
44

55
test("formatRate should return positive bigints formatted as a percent", async () => {
6-
const value = formatRate(BigInt(1e18), 18);
6+
const value = formatRate({ rate: BigInt(1e18) });
77
expect(value).toEqual("100.00");
88

9-
const valueWithCommas = formatRate(BigInt(10e18), 18);
9+
const valueWithCommas = formatRate({ rate: BigInt(10e18) });
1010
expect(valueWithCommas).toEqual("1,000.00");
1111

1212
// rounds down
13-
const value2 = formatRate(parseUnits("0.056721", 18), 18);
13+
const value2 = formatRate({ rate: parseUnits("0.056721", 18) });
1414
expect(value2).toEqual("5.67");
1515

1616
// rounds up
17-
const value3 = formatRate(parseUnits("0.056781", 18), 18);
17+
const value3 = formatRate({ rate: parseUnits("0.056781", 18) });
1818
expect(value3).toEqual("5.68");
1919
});
2020

2121
test("formatRate should return negative bigints formatted as a percent", async () => {
22-
const value = formatRate(BigInt(-1e18), 18);
22+
const value = formatRate({ rate: BigInt(-1e18) });
2323
expect(value).toEqual("-100.00");
2424

25-
const valueWithCommas = formatRate(BigInt(-10e18), 18);
25+
const valueWithCommas = formatRate({ rate: BigInt(-10e18) });
2626
expect(valueWithCommas).toEqual("-1,000.00");
2727

2828
// rounds down
29-
const value2 = formatRate(parseUnits("-1.0281219", 18), 18);
29+
const value2 = formatRate({
30+
rate: parseUnits("-1.0281219", 18),
31+
});
3032
expect(value2).toEqual("-102.81");
3133

3234
// rounds up
33-
const value3 = formatRate(parseUnits("-0.0297902", 18), 18);
35+
const value3 = formatRate({
36+
rate: parseUnits("-0.0297902", 18),
37+
});
3438
expect(value3).toEqual("-2.98");
3539
});

apps/hyperdrive-trading/src/base/formatRate.ts

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,16 @@
11
import { fixed } from "@delvtech/fixed-point-wasm";
22

3-
export function formatRate(
4-
rate: bigint,
5-
decimals = 18,
3+
/**
4+
* Formats a rate represented as an 18 decimal bigint as a percentage string.
5+
*/
6+
export function formatRate({
7+
rate,
68
includePercentSign = true,
7-
): string {
8-
let formatted = fixed(rate, decimals).format({
9+
}: {
10+
rate: bigint;
11+
includePercentSign?: boolean;
12+
}): string {
13+
let formatted = fixed(rate).format({
914
percent: true,
1015
decimals: 2,
1116
});

apps/hyperdrive-trading/src/ui/hyperdrive/longs/OpenLongPreview/OpenLongPreview.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -140,7 +140,7 @@ export function OpenLongPreview({
140140
<span className="flex gap-2">
141141
<span className="text-base-content/80">{`${fixedApr?.formatted}`}</span>
142142
<ArrowRightIcon className="h-4 text-base-content/80" />
143-
{formatRate(spotRateAfterOpen)}
143+
{formatRate({ rate: spotRateAfterOpen })}
144144
</span>
145145
) : (
146146
"-"
@@ -220,5 +220,5 @@ function getMarketImpactLabel(
220220
if (isChangeInFixedAprLessThanOneBasisPoint) {
221221
return "-<0.01%";
222222
}
223-
return `-${formatRate(changeInFixedApr)}`;
223+
return `-${formatRate({ rate: changeInFixedApr })}`;
224224
}

apps/hyperdrive-trading/src/ui/hyperdrive/longs/OpenLongPreview/OpenLongStats.tsx

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -73,14 +73,14 @@ export function OpenLongStats({
7373
) : (
7474
<span className="text-h3 font-bold">
7575
{bondAmount > 0 ? (
76-
`${formatRate(
77-
calculateAprFromPrice({
76+
`${formatRate({
77+
rate: calculateAprFromPrice({
7878
positionDuration:
7979
hyperdrive.poolConfig.positionDuration || 0n,
8080
baseAmount: amountPaidInBase,
8181
bondAmount: bondAmount,
8282
}),
83-
)}`
83+
})}`
8484
) : fixedApr?.formatted ? (
8585
`${fixedApr.formatted}`
8686
) : (

apps/hyperdrive-trading/src/ui/hyperdrive/longs/OpenLongsTable/OpenLongsTableDesktop.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -348,7 +348,7 @@ function getColumns({
348348

349349
return (
350350
<div className="flex flex-col">
351-
<div>{formatRate(fixedRate)} APR</div>
351+
<div>{formatRate({ rate: fixedRate })} APR</div>
352352
<span className="flex font-dmMono text-neutral-content">
353353
{formatBalance({
354354
balance: row.original.details?.bondAmount || 0n,

apps/hyperdrive-trading/src/ui/hyperdrive/longs/hooks/useFixedRate.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,11 +48,11 @@ export function useFixedRate({
4848
return {
4949
fixedApr: {
5050
apr: fixedApr,
51-
formatted: formatRate(fixedApr),
51+
formatted: formatRate({ rate: fixedApr }),
5252
},
5353
fixedRoi: {
5454
roi: fixedRoi,
55-
formatted: formatRate(fixedRoi),
55+
formatted: formatRate({ rate: fixedRoi }),
5656
},
5757
};
5858
}

apps/hyperdrive-trading/src/ui/hyperdrive/lp/AddLiquidityForm/AddLiquidityForm.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -396,7 +396,7 @@ export function AddLiquidityForm({
396396
lpApy === undefined || lpApy.isNew ? (
397397
<div className="flex gap-2">✨New✨</div>
398398
) : (
399-
`${formatRate(lpApy.lpApy)}`
399+
`${formatRate({ rate: lpApy.lpApy })}`
400400
)
401401
}
402402
tooltipContent="The annual percentage yield projection for providing liquidity."

apps/hyperdrive-trading/src/ui/hyperdrive/lp/AddLiquidityForm/AddLiquidityForm2.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -547,7 +547,7 @@ function LpApyStat({ hyperdrive }: { hyperdrive: HyperdriveConfig }) {
547547

548548
return (
549549
<span className="text-h3 font-bold">
550-
{formatRate(lpApy?.netLpApy)}
550+
{formatRate({ rate: lpApy?.netLpApy })}
551551
</span>
552552
);
553553
})()}
@@ -563,7 +563,7 @@ function LpApyStat({ hyperdrive }: { hyperdrive: HyperdriveConfig }) {
563563
{appConfig.yieldSources[hyperdrive.yieldSource].shortName} @{" "}
564564
{isNewPool
565565
? "✨New✨"
566-
: `${formatRate(vaultRate.netVaultRate)} APY`}
566+
: `${formatRate({ rate: vaultRate.netVaultRate })} APY`}
567567
</div>
568568
) : (
569569
<Skeleton className="w-42 h-8" />

apps/hyperdrive-trading/src/ui/hyperdrive/lp/LpAndWithdrawalSharesTable/LpCurrentValueCell.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,7 @@ export function LpCurrentValueCell({
111111
</>
112112
</span>
113113
<span className="text-neutral-content">
114-
{`${formatRate(withdrawablePercent.div(parseFixed("100")).bigint)} withdrawable`}
114+
{`${formatRate({ rate: withdrawablePercent.div(parseFixed("100")).bigint })} withdrawable`}
115115
</span>
116116
</div>
117117
);

apps/hyperdrive-trading/src/ui/hyperdrive/shorts/OpenShortForm/OpenShortForm.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -372,7 +372,7 @@ export function OpenShortForm({
372372
{appConfig.yieldSources[hyperdrive.yieldSource].shortName} @{" "}
373373
{isNewPool
374374
? "✨New✨"
375-
: `${formatRate(vaultRate.netVaultRate)} APY`}
375+
: `${formatRate({ rate: vaultRate.netVaultRate })} APY`}
376376
</>
377377
) : null
378378
}
@@ -476,7 +476,7 @@ export function OpenShortForm({
476476
tooltipContent={`The fixed rate you pay upfront that determines the cost-basis of this short.`}
477477
value={
478478
<span className="text-h3 font-bold">
479-
{formatRate(fixedRatePaid || 0n)}
479+
{formatRate({ rate: fixedRatePaid || 0n })}
480480
</span>
481481
}
482482
valueContainerClassName="flex items-end"

0 commit comments

Comments
 (0)