Skip to content

Commit

Permalink
[CoW AMM Deployer] Disable action buttons when running amm not from m…
Browse files Browse the repository at this point in the history
…odule (#625)
  • Loading branch information
yvesfracari authored Mar 6, 2024
1 parent 085c811 commit 3d3dba0
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 8 deletions.
17 changes: 15 additions & 2 deletions apps/cow-amm-deployer/src/app/manager/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,10 @@

import { formatNumber } from "@bleu-fi/utils/formatNumber";
import { useSafeAppsSDK } from "@gnosis.pm/safe-apps-react-sdk";
import { tomatoDark } from "@radix-ui/colors";
import {
ArrowTopRightIcon,
ExclamationTriangleIcon,
Pencil2Icon,
ResetIcon,
} from "@radix-ui/react-icons";
Expand All @@ -15,6 +17,7 @@ import { Address } from "viem";
import { Button } from "#/components/Button";
import { Dialog } from "#/components/Dialog";
import { Spinner } from "#/components/Spinner";
import { Tooltip } from "#/components/Tooltip";
import WalletNotConnected from "#/components/WalletNotConnected";
import { useRawTxData } from "#/hooks/useRawTxData";
import { useRunningAMM } from "#/hooks/useRunningAmmInfo";
Expand All @@ -32,7 +35,7 @@ export default function Page() {
const router = useRouter();
const { sendTransactions } = useRawTxData();
const { safe, connected } = useSafeAppsSDK();
const { loaded, cowAmm } = useRunningAMM();
const { loaded, cowAmm, isAmmFromModule } = useRunningAMM();
const [openDialog, setOpenDialog] = useState(false);

if (!connected) {
Expand Down Expand Up @@ -147,10 +150,11 @@ export default function Page() {
</Link>
</div>
<PoolCompositionTable cowAmm={cowAmm} />
<div className="flex gap-4">
<div className="flex gap-4 items-center ">
<Button
className="flex items-center gap-1 py-3 px-6 "
variant="destructive"
disabled={!isAmmFromModule}
onClick={() => {
setOpenDialog(true);
}}
Expand All @@ -163,10 +167,19 @@ export default function Page() {
onClick={() => {
router.push("/new");
}}
disabled={!isAmmFromModule}
>
<Pencil2Icon />
Edit CoW AMM LP parameters
</Button>
{!isAmmFromModule && (
<Tooltip content="This CoW AMM LP position was not created from the supported module.">
<ExclamationTriangleIcon
className="size-6"
color={tomatoDark.tomato10}
/>
</Tooltip>
)}
</div>
</div>
</div>
Expand Down
40 changes: 34 additions & 6 deletions apps/cow-amm-deployer/src/hooks/useRunningAmmInfo.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,11 @@ import { useEffect, useState } from "react";
import { Address, decodeAbiParameters, formatUnits } from "viem";

import { composableCowAbi } from "#/lib/abis/composableCow";
import { cowAmmModuleAbi } from "#/lib/abis/cowAmmModule";
import {
COMPOSABLE_COW_ADDRESS,
COW_AMM_HANDLER_ADDRESS,
COW_AMM_MODULE_ADDRESS,
} from "#/lib/contracts";
import { fetchTokenUsdPrice } from "#/lib/fetchTokenUsdPrice";
import { UserCurrentAmmQuery } from "#/lib/gqlComposableCow/generated";
Expand Down Expand Up @@ -149,30 +151,48 @@ export async function checkIsAmmRunning(
});
}

export async function checkAmmIsFromModule(
chainId: ChainId,
safeAddress: Address,
hashParameters: `0x${string}`,
): Promise<boolean> {
const publicClient = publicClientsFromIds[chainId];
return publicClient
.readContract({
address: COW_AMM_MODULE_ADDRESS[chainId],
abi: cowAmmModuleAbi,
functionName: "activeOrders",
args: [safeAddress],
})
.then((result) => result.toLowerCase() === hashParameters.toLowerCase());
}

export function useRunningAMM(): {
cowAmm?: ICowAmm;
loaded: boolean;
isAmmRunning: boolean;
error: boolean;
updateAmmInfo: () => Promise<void>;
isAmmFromModule: boolean;
} {
const {
safe: { safeAddress, chainId },
} = useSafeAppsSDK();

const [cowAmm, setCowAmm] = useState<ICowAmm>();
const [isAmmRunning, setIsAmmRunning] = useState<boolean>(false);
const [isAmmFromModule, setIsAmmFromModule] = useState<boolean>(false);
const [loaded, setLoaded] = useState(false);
const [error, setError] = useState(false);
const { assets, loaded: assetLoaded } = useSafeBalances();

async function loadCoWAmmRunning(hash: `0x${string}`) {
const newIsAmmRunning = await checkIsAmmRunning(
chainId as ChainId,
safeAddress as Address,
hash,
);
const [newIsAmmRunning, newIsAmmFromModule] = await Promise.all([
checkIsAmmRunning(chainId as ChainId, safeAddress as Address, hash),
checkAmmIsFromModule(chainId as ChainId, safeAddress as Address, hash),
]);
setIsAmmRunning(newIsAmmRunning);
setIsAmmFromModule(newIsAmmFromModule);
}

async function loadCowAmm() {
Expand Down Expand Up @@ -243,6 +263,7 @@ export function useRunningAMM(): {
.then(async (newCowAmm) => {
if (!newCowAmm) return;
setCowAmm(newCowAmm);

await loadCoWAmmRunning(newCowAmm.hash);
})
.catch(() => {
Expand All @@ -256,5 +277,12 @@ export function useRunningAMM(): {
updateAmmInfo();
}, [safeAddress, chainId, assets]);

return { cowAmm, loaded, isAmmRunning, error, updateAmmInfo };
return {
cowAmm,
loaded,
isAmmRunning,
error,
updateAmmInfo,
isAmmFromModule,
};
}

0 comments on commit 3d3dba0

Please sign in to comment.