Skip to content

Commit

Permalink
feat: merged final pieces to tradestoreV2 & cleanup original tradestore
Browse files Browse the repository at this point in the history
  • Loading branch information
k0beLeenders committed Dec 9, 2024
1 parent 4c5d6a2 commit 9c077bf
Show file tree
Hide file tree
Showing 35 changed files with 312 additions and 1,183 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import { IconPlus } from "@tabler/icons-react";
import { PublicKey } from "@solana/web3.js";
import { cn } from "@mrgnlabs/mrgn-utils";

import { useTradeStore } from "~/store";
import { useIsMobile } from "~/hooks/use-is-mobile";

import {
Expand All @@ -30,7 +29,7 @@ type CreatePoolDialogProps = {
export type SUPPORTED_QUOTE_BANKS = "USDC" | "LST";

export const CreatePoolDialog = ({ trigger }: CreatePoolDialogProps) => {
const [resetSearchResults, searchBanks] = useTradeStore((state) => [state.resetSearchResults, state.searchBanks]);
// const [resetSearchResults, searchBanks] = useTradeStore((state) => [state.resetSearchResults, state.searchBanks]);
const [isOpen, setIsOpen] = React.useState(false);
const [createPoolState, setCreatePoolState] = React.useState<CreatePoolState>(CreatePoolState.MINT);
const [isSearchingToken, setIsSearchingToken] = React.useState(false);
Expand Down Expand Up @@ -79,13 +78,13 @@ export const CreatePoolDialog = ({ trigger }: CreatePoolDialogProps) => {
}
}, [mintAddress]);

React.useEffect(() => {
if (!searchQuery.length) {
resetSearchResults();
return;
}
searchBanks(searchQuery);
}, [searchBanks, resetSearchResults, searchQuery]);
// React.useEffect(() => {
// if (!searchQuery.length) {
// resetSearchResults();
// return;
// }
// searchBanks(searchQuery);
// }, [searchBanks, resetSearchResults, searchQuery]);

const reset = React.useCallback(() => {
setIsSearchingToken(false);
Expand Down Expand Up @@ -117,7 +116,7 @@ export const CreatePoolDialog = ({ trigger }: CreatePoolDialogProps) => {
<Dialog
open={isOpen}
onOpenChange={(open) => {
if (!open) resetSearchResults();
// if (!open) resetSearchResults();
setIsOpen(open);
}}
>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ import {
} from "@mrgnlabs/marginfi-client-v2";
import { cn, getBearerToken, getFeeAccount, createReferalTokenAccount } from "@mrgnlabs/mrgn-utils";

import { useUiStore, useTradeStore } from "~/store";
import { Button } from "~/components/ui/button";
import { useConnection } from "~/hooks/use-connection";
import { useWallet } from "~/components/wallet-v2";
Expand Down Expand Up @@ -180,7 +179,7 @@ type PoolCreationState = {
export const CreatePoolLoading = ({ quoteBank, poolData, setPoolData, setCreatePoolState }: CreatePoolLoadingProps) => {
const { wallet } = useWallet();
const { connection } = useConnection();
const [fetchTradeState] = useTradeStore((state) => [state.fetchTradeState]);
// const [fetchTradeState] = useTradeStore((state) => [state.fetchTradeState]);
const [activeStep, setActiveStep] = React.useState<number>(0);
const [status, setStatus] = React.useState<StepperStatus>("default");

Expand Down Expand Up @@ -542,27 +541,17 @@ export const CreatePoolLoading = ({ quoteBank, poolData, setPoolData, setCreateP
return { ...state, group: seeds.marginfiGroupSeed.publicKey };
});
setCreatePoolState(CreatePoolState.SUCCESS);
fetchTradeState({
connection,
wallet,
refresh: true,
});
// fetchTradeState({
// connection,
// wallet,
// refresh: true,
// });
} catch (error) {
setStatus("error");
console.error("Failed to create permissionless pool");
console.error(error);
}
}, [
connection,
createOracleIx,
fetchTradeState,
initializeClient,
poolData,
quoteBank,
setCreatePoolState,
setPoolData,
wallet,
]);
}, [connection, createOracleIx, initializeClient, poolData, quoteBank, setCreatePoolState, setPoolData, wallet]);

const createTransaction = async (
ixs: TransactionInstruction[],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,6 @@ import { PublicKey } from "@solana/web3.js";

import { cn, getTokenImageURL } from "@mrgnlabs/mrgn-utils";

import { useTradeStore } from "~/store";
import { GroupData } from "~/store/tradeStore";

import { Button } from "~/components/ui/button";
import { Input } from "~/components/ui/input";

Expand All @@ -27,9 +24,9 @@ export const CreatePoolMint = ({
setIsOpen,
fetchTokenInfo,
}: CreatePoolMintProps) => {
const [groupMap] = useTradeStore((state) => [state.groupMap]);
// const [groupMap] = useTradeStore((state) => [state.groupMap]);
const [error, setError] = React.useState<string | null>(null);
const [poolExists, setPoolExists] = React.useState<GroupData | null>(null);
const [poolExists, setPoolExists] = React.useState<any | null>(null);

const verifyPublickey = (key: string, allowPDA: boolean = false) => {
try {
Expand All @@ -56,21 +53,21 @@ export const CreatePoolMint = ({
return;
}

const groups = [...groupMap.values()];
// const groups = []; //[...groupMap.values()];

// check if mint address is in groupMap it will be found at entry.pool.token.mint
const group = groups.find((group: GroupData) => {
return group.pool.token.info.rawBank.mint.equals(new PublicKey(mintAddress));
});
// // check if mint address is in groupMap it will be found at entry.pool.token.mint
// const group = groups.find((group: GroupData) => {
// return group.pool.token.info.rawBank.mint.equals(new PublicKey(mintAddress));
// });

if (group) {
setPoolExists(group);
return;
}
// if (group) {
// setPoolExists(group);
// return;
// }

fetchTokenInfo();
},
[mintAddress, groupMap, fetchTokenInfo]
[mintAddress, fetchTokenInfo]
);

return (
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import { useWindowSize } from "@uidotdev/usehooks";
import { IconPlus } from "@tabler/icons-react";
import { PublicKey } from "@solana/web3.js";

import { useTradeStore } from "~/store";
import { useIsMobile } from "~/hooks/use-is-mobile";

import { Dialog, DialogContent, DialogTrigger } from "~/components/ui/dialog";
Expand All @@ -26,7 +25,7 @@ type CreatePoolDialogProps = {
};

export const CreatePoolScriptDialog = ({ trigger }: CreatePoolDialogProps) => {
const [resetSearchResults, searchBanks] = useTradeStore((state) => [state.resetSearchResults, state.searchBanks]);
// const [resetSearchResults, searchBanks] = useTradeStore((state) => [state.resetSearchResults, state.searchBanks]);
const [isOpen, setIsOpen] = React.useState(false);
const [createPoolState, setCreatePoolState] = React.useState<CreatePoolState>(CreatePoolState.SETUP);
const [currentIndex, setCurrentIndex] = React.useState<number>(0);
Expand Down Expand Up @@ -99,7 +98,7 @@ export const CreatePoolScriptDialog = ({ trigger }: CreatePoolDialogProps) => {
<Dialog
open={isOpen}
onOpenChange={(open) => {
if (!open) resetSearchResults();
// if (!open) resetSearchResults();
setIsOpen(open);
}}
>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,22 +10,19 @@ import { IconConfetti, IconPlus } from "@tabler/icons-react";

import { showErrorToast, capture, cn } from "@mrgnlabs/mrgn-utils";

import { useTradeStore } from "~/store";
import { useIsMobile } from "~/hooks/use-is-mobile";

import { Dialog, DialogContent, DialogTrigger } from "~/components/ui/dialog";
import { Form, FormControl, FormField, FormItem, FormLabel, FormMessage } from "~/components/ui/form";
import { Button } from "~/components/ui/button";
import { TokenData } from "~/types";
import { formSchema } from "./types";
import { Input } from "~/components/ui/input";
import { formSchema } from "./types";

type CreatePoolDialogProps = {
trigger?: React.ReactNode;
};

export const CreatePoolSoon = ({ trigger }: CreatePoolDialogProps) => {
const [resetSearchResults, searchBanks] = useTradeStore((state) => [state.resetSearchResults, state.searchBanks]);
// const [resetSearchResults, searchBanks] = useTradeStore((state) => [state.resetSearchResults, state.searchBanks]);
const [isOpen, setIsOpen] = React.useState(false);
const [createPoolState, setCreatePoolState] = React.useState<"FORM" | "SUCCESS">("FORM");
const [formData, setFormData] = React.useState<z.infer<typeof formSchema>>();
Expand Down Expand Up @@ -58,7 +55,7 @@ export const CreatePoolSoon = ({ trigger }: CreatePoolDialogProps) => {
mint: values.mint,
});
} else {
showErrorToast({ message: "Pool submission failed."});
showErrorToast({ message: "Pool submission failed." });
}
setIsSubmitting(false);
},
Expand All @@ -81,7 +78,7 @@ export const CreatePoolSoon = ({ trigger }: CreatePoolDialogProps) => {
<Dialog
open={isOpen}
onOpenChange={(open) => {
if (!open) resetSearchResults();
// if (!open) resetSearchResults();
setIsOpen(open);
}}
>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { Desktop, Mobile } from "@mrgnlabs/mrgn-utils";

import { useDebounce } from "@uidotdev/usehooks";

import { useTradeStore } from "~/store";
import { useTradeStoreV2 } from "~/store";
import { useIsMobile } from "~/hooks/use-is-mobile";

import { PoolSearchDefault, PoolSearchDialog } from "./components/";
Expand All @@ -28,27 +28,27 @@ export const PoolSearch = ({
showNoResults = true,
}: PoolSearchProps) => {
const router = useRouter();
const [groupMap, searchBanks, searchResults, resetSearchResults] = useTradeStore((state) => [
state.groupMap,
state.searchBanks,
state.searchResults,
const [searchPools, searchPoolResults, resetSearchResults, arenaPools] = useTradeStoreV2((state) => [
state.searchPools,
state.searchPoolResults,
state.resetSearchResults,
state.arenaPools,
]);

const [searchQuery, setSearchQuery] = React.useState("");
const [isFocused, setIsFocused] = React.useState(false);
const debouncedSearchQuery = useDebounce(searchQuery, 300);
const isMobile = useIsMobile();

const groups = [...groupMap.values()];
// const groups = [...groupMap.values()];

React.useEffect(() => {
if (!debouncedSearchQuery.length) {
resetSearchResults();
return;
}
searchBanks(debouncedSearchQuery);
}, [debouncedSearchQuery, searchBanks, resetSearchResults]);
searchPools(debouncedSearchQuery);
}, [debouncedSearchQuery, searchPools, resetSearchResults]);

const resetSearch = React.useCallback(() => {
resetSearchResults();
Expand All @@ -62,13 +62,13 @@ export const PoolSearch = ({
searchQuery={searchQuery}
setSearchQuery={setSearchQuery}
resetSearch={resetSearch}
searchResults={searchResults}
searchResults={searchPoolResults}
size={size}
additionalContent={additionalContent}
additionalContentQueryMin={additionalContentQueryMin}
showNoResults={showNoResults}
onBankSelect={(value) => {
const foundGroup = groups.find((g) => g.groupPk.toBase58().toLowerCase() === value);
const foundGroup = arenaPools[value];
if (!foundGroup) return;

router.push(`/trade/${foundGroup.groupPk.toBase58()}`);
Expand All @@ -82,12 +82,12 @@ export const PoolSearch = ({
searchQuery={searchQuery}
setSearchQuery={setSearchQuery}
resetSearch={resetSearch}
searchResults={searchResults}
searchResults={searchPoolResults}
additionalContent={additionalContent}
additionalContentQueryMin={additionalContentQueryMin}
showNoResults={showNoResults}
onBankSelect={(value) => {
const foundGroup = groups.find((g) => g.groupPk.toBase58().toLowerCase() === value);
const foundGroup = arenaPools[value];
if (!foundGroup) return;

router.push(`/trade/${foundGroup.groupPk.toBase58()}`);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,15 @@ import { Command, CommandEmpty, CommandGroup, CommandInput, CommandItem } from "
import { Button } from "~/components/ui/button";

import type { FuseResult } from "fuse.js";
import type { GroupData } from "~/store/tradeStore";
import { ArenaPoolV2 } from "~/store/tradeStoreV2";
import { useExtendedPool } from "~/hooks/useExtendedPools";
import { useTradeStoreV2 } from "~/store";

type PoolSearchDefaultProps = {
searchQuery: string;
setSearchQuery: (query: string) => void;
resetSearch: () => void;
searchResults: FuseResult<GroupData>[];
searchResults: FuseResult<ArenaPoolV2>[];
size: "sm" | "lg";
additionalContent: React.ReactNode;
additionalContentQueryMin: number;
Expand All @@ -39,6 +41,7 @@ export const PoolSearchDefault = ({
}: PoolSearchDefaultProps) => {
const searchInputRef = React.useRef<HTMLInputElement>(null);
const [isFocused, setIsFocused] = React.useState(false);
const [banksByBankPk] = useTradeStoreV2((state) => [state.banksByBankPk]);

React.useEffect(() => {
const handleKeyDown = (event: KeyboardEvent) => {
Expand Down Expand Up @@ -111,9 +114,9 @@ export const PoolSearchDefault = ({
{searchResults.length > 0 && (
<CommandGroup className={cn("bg-background", size === "lg" && "shadow-lg md:w-4/5 md:mx-auto")}>
{searchResults.slice(0, maxResults).map((result) => {
const group = result.item;
const address = group.groupPk.toBase58();
const tokenBank = group.pool.token;
const pool = result.item;
const address = pool.groupPk.toBase58();
const tokenBank = banksByBankPk[pool.tokenBankPk.toBase58()];

return (
<CommandItem
Expand Down
Original file line number Diff line number Diff line change
@@ -1,23 +1,21 @@
import React from "react";

import Image from "next/image";
import type { FuseResult } from "fuse.js";

import { cn } from "@mrgnlabs/mrgn-utils";
import { tokenPriceFormatter, numeralFormatter, percentFormatter } from "@mrgnlabs/mrgn-common";
import { IconX, IconSearch } from "@tabler/icons-react";

import { cn } from "@mrgnlabs/mrgn-utils";

import { CommandDialog, Command, CommandEmpty, CommandGroup, CommandInput, CommandItem } from "~/components/ui/command";
import { Input } from "~/components/ui/input";

import type { FuseResult } from "fuse.js";
import type { GroupData } from "~/store/tradeStore";
import { CommandDialog, CommandEmpty, CommandGroup, CommandInput, CommandItem } from "~/components/ui/command";
import { ArenaPoolV2 } from "~/store/tradeStoreV2";
import { useTradeStoreV2 } from "~/store";

type PoolSearchDialogProps = {
searchQuery: string;
setSearchQuery: (query: string) => void;
resetSearch: () => void;
searchResults: FuseResult<GroupData>[];
searchResults: FuseResult<ArenaPoolV2>[];
additionalContent: React.ReactNode;
additionalContentQueryMin: number;
showNoResults: boolean;
Expand All @@ -37,6 +35,8 @@ export const PoolSearchDialog = ({
maxResults = 5,
}: PoolSearchDialogProps) => {
const [open, setOpen] = React.useState(false);
const [banksByBankPk] = useTradeStoreV2((state) => [state.banksByBankPk]);

return (
<div className="relative w-full">
<button onClick={() => setOpen(true)} className="relative w-full text-muted-foreground">
Expand Down Expand Up @@ -68,9 +68,9 @@ export const PoolSearchDialog = ({
{searchResults.length > 0 && (
<CommandGroup>
{searchResults.slice(0, maxResults).map((result) => {
const group = result.item;
const address = group.groupPk.toBase58();
const tokenBank = group.pool.token;
const pool = result.item;
const address = pool.groupPk.toBase58();
const tokenBank = banksByBankPk[pool.tokenBankPk.toBase58()];

return (
<CommandItem key={address} value={address} className="py-4" onSelect={onBankSelect}>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,6 @@ import {
} from "@tabler/icons-react";
import { CopyToClipboard } from "react-copy-to-clipboard";

import { useTradeStore } from "~/store";
import { GroupData } from "~/store/tradeStore";

import { Popover, PopoverContent, PopoverTrigger } from "~/components/ui/popover";
import { Button } from "~/components/ui/button";
import { ArenaPoolV2 } from "~/store/tradeStoreV2";
Expand Down
Loading

0 comments on commit 9c077bf

Please sign in to comment.