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

Add chain filter to pools list #1503

Merged
merged 3 commits into from
Sep 21, 2024
Merged
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
260 changes: 177 additions & 83 deletions apps/hyperdrive-trading/src/ui/markets/PoolRow/PoolsList.tsx
Original file line number Diff line number Diff line change
@@ -1,9 +1,15 @@
import { fixed } from "@delvtech/fixed-point-wasm";
import { BarsArrowDownIcon } from "@heroicons/react/20/solid";
import {
AdjustmentsHorizontalIcon,
BarsArrowDownIcon,
CheckIcon,
ChevronDownIcon,
} from "@heroicons/react/20/solid";
import { appConfig, findBaseToken } from "@hyperdrive/appconfig";
import { QueryStatus, useQuery } from "@tanstack/react-query";
import { getPublicClient } from "@wagmi/core";
import { ReactElement, useState } from "react";
import classNames from "classnames";
import { ReactElement, useEffect, useState } from "react";
import { isTestnetChain } from "src/chains/isTestnetChain";
import { getLpApy } from "src/hyperdrive/getLpApy";
import { getReadHyperdrive } from "src/hyperdrive/getReadHyperdrive";
Expand All @@ -18,104 +24,192 @@ import { useChainId } from "wagmi";

const sortOptions = [
"TVL",
"Chain",
"Fixed APR",
"Variable APY",
"LP APY",
"Chain",
] as const;

type SortOption = (typeof sortOptions)[number];

export function PoolsList(): ReactElement {
const { pools, status } = usePoolsList();
const [sort, setSort] = useState<SortOption>("TVL");
const [allChainIds, setAllChainIds] = useState<number[]>([]);
const [selectedChainIds, setSelectedChainIds] =
useState<number[]>(allChainIds);

function handleSortChange(option: SortOption) {
setSort(option);
}

const list = pools?.slice().sort((a, b) => {
switch (sort) {
case "Chain":
const chainA = appConfig.chains[a.hyperdrive.chainId] || {};
const chainB = appConfig.chains[b.hyperdrive.chainId] || {};
return chainA.name.localeCompare(chainB.name);
case "Fixed APR":
return Number(b.fixedApr - a.fixedApr);
case "LP APY":
return Number((b.lpApy.lpApy || 0n) - (a.lpApy.lpApy || 0n));
case "Variable APY":
return Number(b.vaultRate - a.vaultRate);
case "TVL":
return fixed(b.tvl, b.hyperdrive.decimals)
.sub(a.tvl, a.hyperdrive.decimals)
.toNumber();
default:
return 0;
}
});
// Reset allChainIds and selectedChainIds if the chain ids in pools change
useEffect(() => {
setAllChainIds((prev) => {
const newChainIds = Array.from(
new Set(pools?.map((pool) => pool.hyperdrive.chainId) || []),
).sort();
if (prev.length !== newChainIds.length) {
setSelectedChainIds(newChainIds);
return newChainIds;
}
for (let i = 0; i < prev.length; i++) {
if (prev[i] !== newChainIds[i]) {
setSelectedChainIds(newChainIds);
return newChainIds;
}
}
return prev;
});
}, [pools]);

// Filter and sort pools
const list = pools
?.filter((pool) => {
if (selectedChainIds.length === 0) {
return true;
}
return selectedChainIds.includes(pool.hyperdrive.chainId);
})
.toSorted((a, b) => {
switch (sort) {
case "Chain":
const chainA = appConfig.chains[a.hyperdrive.chainId] || {};
const chainB = appConfig.chains[b.hyperdrive.chainId] || {};
return chainA.name.localeCompare(chainB.name);
case "Fixed APR":
return Number(b.fixedApr - a.fixedApr);
case "LP APY":
return Number((b.lpApy.lpApy || 0n) - (a.lpApy.lpApy || 0n));
case "Variable APY":
return Number(b.vaultRate - a.vaultRate);
case "TVL":
return fixed(b.tvl, b.hyperdrive.decimals)
.sub(a.tvl, a.hyperdrive.decimals)
.toNumber();
default:
return 0;
}
});

return (
<div className="flex w-full flex-col gap-5">
{
// Show the newest pools first
status === "loading" && !list ? (
<LoadingState />
) : list ? (
<>
{/* Sorting & Filtering */}
<div className="relative z-20 flex items-center justify-end gap-4">
<div className="flex items-center gap-1">
<div className="daisy-dropdown daisy-dropdown-end">
<div
tabIndex={0}
role="button"
title="Sort by"
className="daisy-btn daisy-btn-outline daisy-btn-sm flex items-center justify-center border-gray-600"
>
{sort}
<BarsArrowDownIcon className="size-5" />
</div>
<ul
tabIndex={0}
className="daisy-menu daisy-dropdown-content z-[1] gap-2 rounded-lg bg-base-100 p-4 shadow"
>
{sortOptions.map((option) => (
<li key={option}>
<button
type="button"
onClick={() => {
(document.activeElement as HTMLElement)?.blur();
handleSortChange(option);
}}
className="cursor-pointer whitespace-nowrap text-left"
>
{option}
</button>
</li>
))}
</ul>
{status === "loading" && !list ? (
<LoadingState />
) : list ? (
<>
{/* List controls */}
<div className="relative z-20 flex items-center gap-4">
<AdjustmentsHorizontalIcon className="size-5" />
{/* Chain filter */}
<div className="flex items-center gap-1">
<div className="daisy-dropdown">
<div
tabIndex={0}
role="button"
title="Filter by chain"
className="daisy-btn daisy-btn-outline daisy-btn-sm flex items-center justify-center border-gray-600"
>
{selectedChainIds.length === 1
? appConfig.chains[selectedChainIds[0]]?.name ||
`chain ${selectedChainIds[0]}`
: `${selectedChainIds.length === 0 ? allChainIds.length : selectedChainIds.length} chains`}
<ChevronDownIcon className="size-5" />
</div>
<ul
tabIndex={0}
className="daisy-menu daisy-dropdown-content z-[1] mt-1 gap-2 rounded-lg border border-base-200 bg-base-100 p-2 shadow"
>
{allChainIds.map((chainId) => (
<li key={chainId}>
<button
type="button"
onClick={() => {
setSelectedChainIds((prev) =>
prev.includes(chainId)
? prev.filter((id) => id !== chainId)
: [...prev, chainId],
);
}}
className="group flex min-w-max cursor-pointer items-center justify-between gap-3 whitespace-nowrap text-left"
>
<span className="flex items-center gap-1.5">
{appConfig.chains[chainId]?.iconUrl && (
<img
className="size-4 rounded-full"
src={appConfig.chains[chainId]?.iconUrl}
/>
)}
{appConfig.chains[chainId]?.name ||
`chain ${chainId}`}{" "}
<span className="daisy-badge daisy-badge-neutral">
{
pools?.filter(
(pool) => pool.hyperdrive.chainId === chainId,
).length
}
</span>
</span>
<CheckIcon
className={classNames("size-5 fill-aquamarine", {
invisible: !selectedChainIds.includes(chainId),
})}
/>
</button>
</li>
))}
</ul>
</div>
</div>
{list.map(
({ fixedApr, hyperdrive, isFiat, lpApy, tvl, vaultRate }) => (
<PoolRow
// Combine address and chainId for a unique key, as addresses may
// overlap across chains (e.g. cloudchain and mainnet)
key={`${hyperdrive.address}-${hyperdrive.chainId}`}
hyperdrive={hyperdrive}
tvl={tvl}
isFiat={isFiat}
fixedApr={fixedApr}
vaultRate={vaultRate}
lpApy={lpApy}
/>
),
)}
</>
) : null
}

{/* Sorting */}
<div className="ml-auto flex items-center gap-1">
<div className="daisy-dropdown daisy-dropdown-end">
<div
tabIndex={0}
role="button"
title="Sort by"
className="daisy-btn daisy-btn-outline daisy-btn-sm flex items-center justify-center border-gray-600"
>
{sort}
<BarsArrowDownIcon className="size-5" />
</div>
<ul
tabIndex={0}
className="daisy-menu daisy-dropdown-content z-[1] mt-1 gap-2 rounded-lg border border-base-200 bg-base-100 p-2 shadow"
>
{sortOptions.map((option) => (
<li key={option}>
<button
type="button"
onClick={() => {
(document.activeElement as HTMLElement)?.blur();
setSort(option);
}}
className="cursor-pointer whitespace-nowrap text-left"
>
{option}
</button>
</li>
))}
</ul>
</div>
</div>
</div>

{list.map(
({ fixedApr, hyperdrive, isFiat, lpApy, tvl, vaultRate }) => (
<PoolRow
// Combine address and chainId for a unique key, as addresses may
// overlap across chains (e.g. cloudchain and mainnet)
key={`${hyperdrive.address}-${hyperdrive.chainId}`}
hyperdrive={hyperdrive}
tvl={tvl}
isFiat={isFiat}
fixedApr={fixedApr}
vaultRate={vaultRate}
lpApy={lpApy}
/>
),
)}
</>
) : null}
</div>
);
}
Expand Down
Loading