-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Co-authored-by: Ryan Goree <[email protected]>
- Loading branch information
1 parent
013c1bc
commit 3167184
Showing
6 changed files
with
261 additions
and
5 deletions.
There are no files selected for viewing
25 changes: 25 additions & 0 deletions
25
apps/hyperdrive-trading/src/ui/hyperdrive/lp/hooks/useClosedLpShares.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 }; | ||
} |
88 changes: 88 additions & 0 deletions
88
apps/hyperdrive-trading/src/ui/portfolio/ClosedLpTable/ClosedLpTable.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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>, | ||
], | ||
) | ||
: [] | ||
} | ||
/> | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
59
packages/hyperdrive/src/amm/lp/getRemoveLiquidityEvents.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, | ||
})); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
3167184
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Successfully deployed to the following URLs:
hyperdrive-monorepo-hyperdrive-trading – ./apps/hyperdrive-trading
hyperdrive-monorepo-hyperdrive-trading-git-main-delvtech.vercel.app
hyperdrive-monorepo-hyperdrive-trading.vercel.app
hyperdrive-monorepo-hyperdrive-trading-delvtech.vercel.app
3167184
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Successfully deployed to the following URLs:
hyperdrive-fixed-borrow – ./apps/fixed-borrow
hyperdrive-fixed-borrow.vercel.app
hyperdrive-fixed-borrow-git-main-delvtech.vercel.app
hyperdrive-fixed-borrow-delvtech.vercel.app