Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 31 additions & 0 deletions apps/hyperdrive-trading/src/ui/hyperdrive/hooks/useCheckpoint.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import { Checkpoint } from "@hyperdrive/sdk";
import { QueryStatus, useQuery } from "@tanstack/react-query";
import { makeQueryKey } from "src/base/makeQueryKey";
import { useReadHyperdrive } from "src/ui/hyperdrive/hooks/useReadHyperdrive";
import { Address } from "wagmi";

export function useCheckpoint({
checkpointId,
hyperdriveAddress,
}: {
hyperdriveAddress: Address;
checkpointId: bigint;
}): {
checkpoint: Checkpoint | undefined;
checkpointStatus: QueryStatus;
} {
const readHyperdrive = useReadHyperdrive(hyperdriveAddress);
const queryEnabled = !!readHyperdrive;
const { data, status } = useQuery({
queryKey: makeQueryKey("getCheckpoint", {
hyperdriveAddress,
checkpointId: checkpointId.toString(),
}),
queryFn: queryEnabled
? () => readHyperdrive.getCheckpoint({ checkpointId })
: undefined,
enabled: queryEnabled,
});

return { checkpoint: data, checkpointStatus: status };
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,15 @@ import {
useReactTable,
} from "@tanstack/react-table";
import classNames from "classnames";
import * as dnum from "dnum";
import { ReactElement } from "react";
import { Hyperdrive } from "src/appconfig/types";
import { makeQueryKey } from "src/base/makeQueryKey";
import { parseUnits } from "src/base/parseUnits";
import { NonIdealState } from "src/ui/base/components/NonIdealState";
import { formatBalance } from "src/ui/base/formatting/formatBalance";
import { useCheckpoint } from "src/ui/hyperdrive/hooks/useCheckpoint";
import { usePoolInfo } from "src/ui/hyperdrive/hooks/usePoolInfo";
import { useReadHyperdrive } from "src/ui/hyperdrive/hooks/useReadHyperdrive";
import { getProfitLossText } from "src/ui/hyperdrive/shorts/CloseShortForm/getProfitLossText";
import { CloseShortModalButton } from "src/ui/hyperdrive/shorts/CloseShortModalButton/CloseShortModalButton";
Expand Down Expand Up @@ -57,13 +60,62 @@ const getColumns = (hyperdrive: Hyperdrive) => [
},
}),
columnHelper.display({
header: `Current value (WETH)`,
header: `Accrued yield (${hyperdrive.baseToken.symbol})`,
cell: ({ row }) => {
return <AccruedYieldCell hyperdrive={hyperdrive} row={row} />;
},
}),
columnHelper.display({
header: `Current value (${hyperdrive.baseToken.symbol})`,
cell: ({ row }) => {
return <CurrentValueCell hyperdrive={hyperdrive} row={row} />;
},
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think the better pattern here is to use the accessor, and then pass the value to the component from getValue() instead of grabbing it from the row.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💯 I'll tackle this in a followup when we add sorting

}),
];

function AccruedYieldCell({
row,
hyperdrive,
}: {
row: Row<OpenShort>;
hyperdrive: Hyperdrive;
}) {
const { poolInfo } = usePoolInfo(hyperdrive.address);
const { checkpoint } = useCheckpoint({
checkpointId: row.original.checkpointId,
hyperdriveAddress: hyperdrive.address,
});

// Accrued yield = (current share price - checkpoint share price) x number of bonds
const accruedYield = dnum.mul(
dnum.sub(
[poolInfo?.sharePrice || 0n, hyperdrive.baseToken.decimals],
[checkpoint?.sharePrice || 0n, 18],
),
[row.original.bondAmount, hyperdrive.baseToken.decimals],
hyperdrive.baseToken.decimals,
);

const currentValue =
accruedYield &&
formatBalance({
balance: accruedYield[0],
decimals: hyperdrive.baseToken.decimals,
places: 6,
});

return (
<div className="flex flex-col gap-1">
<span
className="daisy-tooltip"
data-tip={`Yield from shorted hy${hyperdrive.baseToken.symbol} deposited into the yield source`}
>
{currentValue?.toString()}
</span>
</div>
);
}

function CurrentValueCell({
row,
hyperdrive,
Expand Down
Loading