From 3d3dba07f193db3eb13f86299a5076e3b73f550b Mon Sep 17 00:00:00 2001
From: Pedro Yves Fracari <55461956+yvesfracari@users.noreply.github.com>
Date: Wed, 6 Mar 2024 14:20:29 -0300
Subject: [PATCH] [CoW AMM Deployer] Disable action buttons when running amm
not from module (#625)
---
.../cow-amm-deployer/src/app/manager/page.tsx | 17 +++++++-
.../src/hooks/useRunningAmmInfo.ts | 40 ++++++++++++++++---
2 files changed, 49 insertions(+), 8 deletions(-)
diff --git a/apps/cow-amm-deployer/src/app/manager/page.tsx b/apps/cow-amm-deployer/src/app/manager/page.tsx
index e8a3f9096..5b8baf1b5 100644
--- a/apps/cow-amm-deployer/src/app/manager/page.tsx
+++ b/apps/cow-amm-deployer/src/app/manager/page.tsx
@@ -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";
@@ -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";
@@ -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) {
@@ -147,10 +150,11 @@ export default function Page() {
-
+
+ {!isAmmFromModule && (
+
+
+
+ )}
diff --git a/apps/cow-amm-deployer/src/hooks/useRunningAmmInfo.ts b/apps/cow-amm-deployer/src/hooks/useRunningAmmInfo.ts
index ba0134c9a..73d3aec86 100644
--- a/apps/cow-amm-deployer/src/hooks/useRunningAmmInfo.ts
+++ b/apps/cow-amm-deployer/src/hooks/useRunningAmmInfo.ts
@@ -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";
@@ -149,12 +151,29 @@ export async function checkIsAmmRunning(
});
}
+export async function checkAmmIsFromModule(
+ chainId: ChainId,
+ safeAddress: Address,
+ hashParameters: `0x${string}`,
+): Promise {
+ 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;
+ isAmmFromModule: boolean;
} {
const {
safe: { safeAddress, chainId },
@@ -162,17 +181,18 @@ export function useRunningAMM(): {
const [cowAmm, setCowAmm] = useState();
const [isAmmRunning, setIsAmmRunning] = useState(false);
+ const [isAmmFromModule, setIsAmmFromModule] = useState(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() {
@@ -243,6 +263,7 @@ export function useRunningAMM(): {
.then(async (newCowAmm) => {
if (!newCowAmm) return;
setCowAmm(newCowAmm);
+
await loadCoWAmmRunning(newCowAmm.hash);
})
.catch(() => {
@@ -256,5 +277,12 @@ export function useRunningAMM(): {
updateAmmInfo();
}, [safeAddress, chainId, assets]);
- return { cowAmm, loaded, isAmmRunning, error, updateAmmInfo };
+ return {
+ cowAmm,
+ loaded,
+ isAmmRunning,
+ error,
+ updateAmmInfo,
+ isAmmFromModule,
+ };
}