-
Notifications
You must be signed in to change notification settings - Fork 1
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
Group Open LP Positions by Yield Source and Chain Id #1670
Merged
Merged
Changes from 11 commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
724a6d0
basics of new table scaffold laid out
jackburrus 64c19d7
valid lp shares showing correctly in one table
jackburrus f259ae9
Adds position table heading
jackburrus f4b2e1a
total lp positions calculated, need to render
jackburrus 451dadb
total values rendering
jackburrus 968690e
use table skeleton
jackburrus 51ac279
remove duplicate component
jackburrus 57430cd
rename hooks
jackburrus d24b249
comments
jackburrus ff078bd
use table data length
jackburrus 56bebe1
cleanup variable names
jackburrus 7603a7d
fixes per comments
jackburrus File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
84 changes: 84 additions & 0 deletions
84
apps/hyperdrive-trading/src/ui/hyperdrive/lp/hooks/useTotalOpenLpPositions.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,84 @@ | ||
import { | ||
appConfig, | ||
getHyperdriveConfig, | ||
HyperdriveConfig, | ||
} from "@delvtech/hyperdrive-appconfig"; | ||
import { getHyperdrive } from "@delvtech/hyperdrive-js"; | ||
import { useQuery } from "@tanstack/react-query"; | ||
import { makeQueryKey } from "src/base/makeQueryKey"; | ||
import { getDrift } from "src/drift/getDrift"; | ||
import { Address } from "viem"; | ||
export function useTotalOpenLpPositions({ | ||
account, | ||
openLpPositions, | ||
enabled, | ||
}: { | ||
account: Address | undefined; | ||
openLpPositions: | ||
| { | ||
hyperdrive: HyperdriveConfig; | ||
lpShares: bigint; | ||
withdrawalShares: bigint; | ||
}[] | ||
| undefined; | ||
enabled: boolean; | ||
}): { | ||
totalOpenLpPositions: bigint | undefined; | ||
isLoading: boolean; | ||
totalOpenLpPositionsError: Error; | ||
} { | ||
const queryEnabled = !!account && !!openLpPositions && enabled; | ||
|
||
const { | ||
data: totalOpenLpPositions, | ||
isLoading, | ||
error: totalOpenLpPositionsError, | ||
} = useQuery({ | ||
queryKey: makeQueryKey("totalLpPositions", { | ||
account, | ||
openLpPositions: openLpPositions?.map((p) => ({ | ||
hyperdrive: p.hyperdrive.address, | ||
lpShares: p.lpShares.toString(), | ||
withdrawalShares: p.withdrawalShares.toString(), | ||
})), | ||
}), | ||
enabled: queryEnabled, | ||
queryFn: queryEnabled | ||
? async () => { | ||
const openLpPositionValues = await Promise.all( | ||
openLpPositions.map(async (position) => { | ||
const readHyperdrive = await getHyperdrive({ | ||
address: position.hyperdrive.address, | ||
drift: getDrift({ chainId: position.hyperdrive.chainId }), | ||
earliestBlock: position.hyperdrive.initializationBlock, | ||
}); | ||
const openLpPosition = await readHyperdrive.getOpenLpPosition({ | ||
account, | ||
asBase: getHyperdriveConfig({ | ||
hyperdriveChainId: position.hyperdrive.chainId, | ||
hyperdriveAddress: position.hyperdrive.address, | ||
appConfig, | ||
}).withdrawOptions.isBaseTokenWithdrawalEnabled, | ||
}); | ||
return { | ||
baseValue: openLpPosition.baseValue, | ||
}; | ||
}), | ||
); | ||
|
||
let total = 0n; | ||
openLpPositionValues.forEach((openLpPositionValue) => { | ||
total += openLpPositionValue.baseValue || 0n; | ||
}); | ||
|
||
return total; | ||
} | ||
: undefined, | ||
}); | ||
|
||
return { | ||
totalOpenLpPositions, | ||
isLoading, | ||
totalOpenLpPositionsError: totalOpenLpPositionsError as Error, | ||
}; | ||
} |
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 |
---|---|---|
@@ -1,20 +1,31 @@ | ||
import { appConfig } from "@delvtech/hyperdrive-appconfig"; | ||
import { appConfig, HyperdriveConfig } from "@delvtech/hyperdrive-appconfig"; | ||
import { Link } from "@tanstack/react-router"; | ||
import { ReactElement } from "react"; | ||
import { ExternalLink } from "src/ui/analytics/ExternalLink"; | ||
import LoadingState from "src/ui/base/components/LoadingState"; | ||
import { NonIdealState } from "src/ui/base/components/NonIdealState"; | ||
import { OpenLpTableDesktop } from "src/ui/portfolio/lp/LpAndWithdrawalSharesTable/LpAndWithdrawalSharesTable"; | ||
import { TotalLpValue } from "src/ui/portfolio/lp/LpAndWithdrawalSharesTable/TotalLpValue"; | ||
import { usePortfolioLpData } from "src/ui/portfolio/lp/usePortfolioLpData"; | ||
import { NoWalletConnected } from "src/ui/portfolio/NoWalletConnected"; | ||
import { PositionContainer } from "src/ui/portfolio/PositionContainer"; | ||
import { PositionTableHeading } from "src/ui/portfolio/PositionTableHeading"; | ||
import { useAccount } from "wagmi"; | ||
|
||
export function LpAndWithdrawalSharesContainer(): ReactElement { | ||
const { openLpPositions, openLpPositionStatus } = usePortfolioLpData(); | ||
const { address: account } = useAccount(); | ||
|
||
// Initialize an empty object to group hyperdrives by chainId and yieldSource | ||
const hyperdrivesByChainAndYieldSource: Record<string, HyperdriveConfig[]> = | ||
{}; | ||
|
||
for (const hyperdrive of appConfig.hyperdrives) { | ||
const key = `${hyperdrive.chainId}-${hyperdrive.yieldSource}`; | ||
if (!hyperdrivesByChainAndYieldSource[key]) { | ||
hyperdrivesByChainAndYieldSource[key] = []; | ||
} | ||
hyperdrivesByChainAndYieldSource[key].push(hyperdrive); | ||
} | ||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. nit: let's use lodash's |
||
if (!account) { | ||
return <NoWalletConnected />; | ||
} | ||
|
@@ -73,44 +84,11 @@ export function LpAndWithdrawalSharesContainer(): ReactElement { | |
|
||
return ( | ||
<PositionContainer className="mt-10"> | ||
{appConfig.hyperdrives.map((hyperdrive) => { | ||
const openLpPosition = openLpPositions?.find( | ||
(position) => | ||
position.hyperdrive.address === hyperdrive.address && | ||
position.hyperdrive.chainId === hyperdrive.chainId, | ||
) ?? { | ||
lpShares: 0n, | ||
withdrawalShares: 0n, | ||
}; | ||
|
||
// Check if this hyperdrive pool has open positions before rendering | ||
if ( | ||
openLpPosition.lpShares === 0n && | ||
openLpPosition.withdrawalShares === 0n | ||
) { | ||
return null; | ||
} | ||
|
||
return ( | ||
<div className="flex flex-col gap-6" key={hyperdrive.address}> | ||
<PositionTableHeading | ||
hyperdrive={hyperdrive} | ||
rightElement={<TotalLpValue hyperdrive={hyperdrive} />} | ||
hyperdriveName={ | ||
// This regex removes the term (eg: "30d") from the hyperdrive | ||
// name since it's already shown in the table. | ||
// https://regex101.com/r/f4A3th/1 | ||
hyperdrive.name.replace(/\d{1,3}d/, "") | ||
} | ||
/> | ||
<OpenLpTableDesktop | ||
hyperdrive={hyperdrive} | ||
openLpPosition={openLpPosition} | ||
openLpPositionStatus={openLpPositionStatus} | ||
/> | ||
</div> | ||
); | ||
})} | ||
{Object.entries(hyperdrivesByChainAndYieldSource).map( | ||
([key, hyperdrives]) => ( | ||
<OpenLpTableDesktop hyperdrives={hyperdrives} key={key} /> | ||
), | ||
)} | ||
</PositionContainer> | ||
); | ||
} |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
nit: this should default to
true
to be consistent with our other hooks that have anenabled
arg