Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Wire up Closed LP table #262

Merged
merged 4 commits into from
Jul 22, 2023
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
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import { ClosedLpShares, getClosedLpSharesQuery } from "@hyperdrive/core";
import { QueryStatus, useQuery } from "@tanstack/react-query";
import { Address, usePublicClient } from "wagmi";

interface UseClosedLpSharesOptions {
account: Address | undefined;
hyperdriveAddress: Address | undefined;
}
export function useClosedLpShares({
account,
hyperdriveAddress,
}: UseClosedLpSharesOptions): {
lpShares: ClosedLpShares[] | undefined;
lpSharesStatus: QueryStatus;
} {
const publicClient = usePublicClient();
const { data: lpShares, status: lpSharesStatus } = useQuery(
getClosedLpSharesQuery({
providerAddress: account,
hyperdriveAddress,
publicClient: publicClient as any,
}),
);
return { lpShares, lpSharesStatus };
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
import { ReactElement } from "react";
import { Hyperdrive } from "src/appconfig/types";
import { SortableGridTable } from "src/ui/base/components/tables/SortableGridTable";
import { formatBalance } from "src/ui/base/formatting/formatBalance";
import { useClosedLpShares } from "src/ui/hyperdrive/lp/hooks/useClosedLpShares";
import { useWithdrawalShares } from "src/ui/hyperdrive/lp/hooks/useWithdrawalShares";
import { formatUnits } from "viem";
import { useAccount } from "wagmi";

interface ClosedLpTablePRops {
hyperdrive: Hyperdrive;
}

export function ClosedLpTable({
hyperdrive,
}: ClosedLpTablePRops): ReactElement {
const { address: account } = useAccount();

const { lpShares } = useClosedLpShares({
hyperdriveAddress: hyperdrive.address,
account,
});

// TODO: make a useClosedWithdrawalShares hook
const { withdrawalShares } = useWithdrawalShares({
hyperdriveAddress: hyperdrive.address,
account,
});

return (
<SortableGridTable
headingRowClassName="grid-cols-5 text-start text-neutral-content"
bodyRowClassName="grid-cols-5 text-base-content items-center text-sm md:text-h6 even:bg-secondary/5 h-16"
cols={[
"Position",
"Shares",
"Value Received",
"Withdrawal shares",
"Closed on",
]}
rows={
lpShares
? lpShares.map(
({
lpAmount,
baseAmount,
closedTimestamp,
withdrawalShareAmount,
}) => [
<span
key="type"
className="font-semibold uppercase italic text-primary"
>
LP
</span>,
<span key="shares" className="italic">
{formatBalance(
formatUnits(
lpAmount,
(hyperdrive as Hyperdrive).baseToken.decimals,
),
)}
</span>,
<span key="value" className="italic">
{`${formatBalance(
formatUnits(baseAmount, hyperdrive.baseToken.decimals),
)} ${hyperdrive.baseToken.symbol}`}
</span>,
<span key="withdrawalShares" className="italic">
{`${formatBalance(
formatUnits(
withdrawalShareAmount,
hyperdrive.baseToken.decimals,
),
)}`}
</span>,
<span key="closed-on" className="italic">
{new Date(
Number(closedTimestamp * 1000n),
).toLocaleDateString()}
</span>,
],
)
: []
}
/>
);
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { ReactElement, useEffect } from "react";
import { useSearchParams } from "react-router-dom";
import { Hyperdrive } from "src/appconfig/types";
import { ClosedLongsTable } from "src/ui/portfolio/ClosedLongsTable/ClosedLongsTable";
import { ClosedLpTable } from "src/ui/portfolio/ClosedLpTable/ClosedLpTable";
import { ClosedShortsTable } from "src/ui/portfolio/ClosedShortsTable/ClosedShortsTable";
import { OpenLongsTable } from "src/ui/portfolio/OpenLongsTable/OpenLongsTable";
import { OpenLpPosition } from "src/ui/portfolio/OpenLpPosition/OpenLpPosition";
Expand Down Expand Up @@ -98,8 +99,10 @@ export function PositionsSection({
return <ClosedShortsTable hyperdrive={hyperdrive} />;
}
case "LP":
// return <Well>Under construction</Well>;
return <OpenLpPosition hyperdrive={hyperdrive} />;
if (activeOpenOrClosedTab === "Open") {
return <OpenLpPosition hyperdrive={hyperdrive} />;
}
return <ClosedLpTable hyperdrive={hyperdrive} />;
default:
assertNever(activePositionTab);
}
Expand Down
76 changes: 76 additions & 0 deletions packages/hyperdrive/src/amm/lp/getClosedLpShares.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
import { QueryObserverOptions } from "@tanstack/query-core";
import { PublicClient, Address, Transport, Chain } from "viem";
import { makeQueryKey } from "src/makeQueryKey";
import { getRemoveLiquidityEvents } from "src/amm/lp/getRemoveLiquidityEvents";

export interface GetClosedLpSharesOptions {
providerAddress: Address;
hyperdriveAddress: Address;
publicClient: PublicClient<Transport, Chain>;
}

export interface ClosedLpShares {
hyperdriveAddress: `0x${string}`;
lpAmount: bigint;
baseAmount: bigint;
withdrawalShareAmount: bigint;
closedTimestamp: bigint;
}

export async function getClosedLpShares({
providerAddress,
hyperdriveAddress,
publicClient,
}: GetClosedLpSharesOptions): Promise<ClosedLpShares[]> {
const removeLiquidityEvents = await getRemoveLiquidityEvents({
args: { providerAddress },
hyperdriveAddress,
publicClient,
});

return Promise.all(
removeLiquidityEvents.map(async (event) => {
const {
eventData: { lpAmount, baseAmount, withdrawalShareAmount },
} = event;
return {
hyperdriveAddress,
lpAmount,
baseAmount,
withdrawalShareAmount,
closedTimestamp: (
await publicClient.getBlock({
blockNumber: event.eventLog.blockNumber as bigint,
})
).timestamp,
};
}),
);
}

export function getClosedLpSharesQuery({
hyperdriveAddress,
publicClient,
providerAddress,
}: Partial<GetClosedLpSharesOptions>): QueryObserverOptions<
Awaited<ReturnType<typeof getClosedLpShares>>
> {
const queryEnabled =
!!providerAddress && !!hyperdriveAddress && !!publicClient;

return {
enabled: queryEnabled,
queryKey: makeQueryKey("remove-liquidity", {
hyperdriveAddress,
providerAddress,
}),
queryFn: queryEnabled
? () =>
getClosedLpShares({
providerAddress: providerAddress,
hyperdriveAddress,
publicClient,
})
: undefined,
};
}
59 changes: 59 additions & 0 deletions packages/hyperdrive/src/amm/lp/getRemoveLiquidityEvents.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
import { HyperdriveABI } from "src/abis/Hyperdrive";
import {
PublicClient,
Address,
decodeEventLog,
Transport,
Chain,
DecodeEventLogReturnType,
GetFilterLogsReturnType,
BlockTag,
} from "viem";
interface CloseShortEvent {
eventData: DecodeEventLogReturnType<
typeof HyperdriveABI,
"RemoveLiquidity"
>["args"];
eventLog: GetFilterLogsReturnType<
typeof HyperdriveABI,
"RemoveLiquidity"
>[number];
}
interface GetRemoveLiquidityEventsOptions {
args: { providerAddress?: Address };
fromBlock?: bigint;
toBlock?: bigint | BlockTag;
hyperdriveAddress: Address;
publicClient: PublicClient<Transport, Chain>;
}

export async function getRemoveLiquidityEvents({
args: { providerAddress },
fromBlock = 0n,
toBlock = "latest",
hyperdriveAddress,
publicClient,
}: GetRemoveLiquidityEventsOptions): Promise<CloseShortEvent[]> {
const closeLPLogs = await publicClient.getFilterLogs({
filter: await publicClient.createContractEventFilter({
abi: HyperdriveABI,
address: hyperdriveAddress,
eventName: "RemoveLiquidity",
args: { provider: providerAddress },
fromBlock,
toBlock,
}),
});

return closeShortLogs.map((log) => ({
// This is a typesafe copy of eventLog.args
eventData: decodeEventLog({
abi: HyperdriveABI,
data: log.data,
topics: log.topics,
eventName: "RemoveLiquidity",
}).args,

eventLog: log,
}));
}
11 changes: 8 additions & 3 deletions packages/hyperdrive/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,14 +34,14 @@ export {
getOpenShortsQuery,
} from "src/amm/shorts/getOpenShorts";
export { getShorts } from "src/amm/shorts/getShortsOld";

// Liquidity
export { getLiquidity, getLiquidityQuery } from "src/amm/getLiquidity";
export {
getClosedShorts,
getClosedShortsQuery,
} from "src/amm/shorts/getClosedShorts";

// Liquidity
export { getLiquidity, getLiquidityQuery } from "src/amm/getLiquidity";

// Trading Volume
export {
getTradingVolume,
Expand All @@ -51,6 +51,11 @@ export {
// LP Shares
export { LP_ASSET_ID, WITHDRAW_SHARES_ASSET_ID } from "src/amm/lp/constants";
export { getLpShares, getLpSharesQuery } from "src/amm/lp/getLpShares";
export {
getClosedLpShares,
getClosedLpSharesQuery,
} from "src/amm/lp/getClosedLpShares";
export type { ClosedLpShares } from "src/amm/lp/getClosedLpShares";
export type { GetLpSharesOptions } from "src/amm/lp/getLpShares";

// Withdraw Shares
Expand Down