Skip to content

Commit

Permalink
Add fetch pending syncs from subgraph
Browse files Browse the repository at this point in the history
  • Loading branch information
zkSoju committed Mar 27, 2023
1 parent 72d047b commit 0cbd98b
Show file tree
Hide file tree
Showing 4 changed files with 84 additions and 25 deletions.
74 changes: 52 additions & 22 deletions components/DammTabContent.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -191,15 +191,12 @@ const DammTabContent = () => {
const { sync: syncArbi } = useSyncL2(ChainId.ARBITRUM_GOERLI);
const { sync: syncPoly } = useSyncL2(ChainId.POLYGON_MUMBAI);
const { sync: syncAvax } = useSyncL2(ChainId.AVALANCHE_FUJI);
const { finalizeSync: finalizeSyncArbi } = useFinalizeSyncL1(
ChainId.ARBITRUM_GOERLI
);
const { finalizeSync: finalizeSyncPoly } = useFinalizeSyncL1(
ChainId.POLYGON_MUMBAI
);
const { finalizeSync: finalizeSyncAvax } = useFinalizeSyncL1(
ChainId.AVALANCHE_FUJI
);
const { finalizeSync: finalizeSyncArbi, status: statusArbi } =
useFinalizeSyncL1(ChainId.ARBITRUM_GOERLI);
const { finalizeSync: finalizeSyncPoly, status: statusPoly } =
useFinalizeSyncL1(ChainId.POLYGON_MUMBAI);
const { finalizeSync: finalizeSyncAvax, status: statusAvax } =
useFinalizeSyncL1(ChainId.AVALANCHE_FUJI);

const handleSync = (chainId: ChainId) => {
if (chainId === ChainId.ARBITRUM_GOERLI) {
Expand All @@ -221,6 +218,16 @@ const DammTabContent = () => {
}
};

const getStatus = (chainId: ChainId) => {
if (chainId === ChainId.ARBITRUM_GOERLI) {
return statusArbi;
} else if (chainId === ChainId.POLYGON_MUMBAI) {
return statusPoly;
} else if (chainId === ChainId.AVALANCHE_FUJI) {
return statusAvax;
}
};

const { isLocked } = useLiquidityLocked();

const supportedChainIds = Object.values(ChainId)
Expand Down Expand Up @@ -563,23 +570,46 @@ const DammTabContent = () => {
<p className="mb-2 text-white">
{SUPPORTED_CHAIN_NAMES[chainId as ChainId]}
</p>

<p className="flex w-24 items-center justify-center rounded-sm bg-yellow-400/5 py-1 text-xs uppercase tracking-widest text-yellow-400">
<BiLoader className="mr-2" />
Pending
</p>
{(() => {
const status = getStatus(chainId);

switch (status) {
case "PENDING_FINALIZE":
return (
<p className="flex w-24 items-center justify-center rounded-sm bg-yellow-400/5 py-1 text-xs uppercase tracking-widest text-yellow-400">
<BiLoader className="mr-2" />
Pending
</p>
);
default:
return (
<p className="flex w-24 items-center justify-center rounded-sm bg-green-400/5 py-1 text-xs uppercase tracking-widest text-green-400">
<BiCheckCircle className="mr-2" />
Synced
</p>
);
}
})()}
</div>
</div>
</div>
<div className="flex items-center space-x-2">
<button
className="group flex w-36 items-center justify-center rounded-sm border border-white/10 py-3"
onClick={() => handleFinalizeIncomingSync(chainId)}
>
<p className="text-xs uppercase tracking-widest text-sky-400 transition group-hover:drop-shadow-soju">
Finalize Sync
</p>
</button>
{(() => {
const status = getStatus(chainId);

if (status === "PENDING_FINALIZE") {
return (
<button
className="group flex w-36 items-center justify-center rounded-sm border border-white/10 py-3"
onClick={() => handleFinalizeIncomingSync(chainId)}
>
<p className="text-xs uppercase tracking-widest text-sky-400 transition group-hover:drop-shadow-soju">
Finalize Sync
</p>
</button>
);
}
})()}
<button
className="group flex w-36 items-center justify-center rounded-sm border border-sky-400 bg-sky-400/5 py-3"
onClick={() => handleSync(chainId)}
Expand Down
32 changes: 30 additions & 2 deletions lib/hooks/sync/useFinalizeSyncL1.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { gql, useQuery } from "@apollo/client";
import { ChainId, DOVE_ADDRESS, HL_DOMAIN } from "../../../sdk";
import {
useDoveFinalizeSyncFromL2,
Expand All @@ -8,17 +9,44 @@ export default function useFinalizeSyncL1(
expectedChainId: ChainId | undefined
): {
finalizeSync: () => void;
status: string;
} {
const GET_RECENT_SYNC = gql`
query GetRecentSync($domainId: Int!) {
syncs(where: { domainId: $domainId, status_not: SYNCED }, first: 1) {
syncId
status
}
}
`;

type SyncResponse = {
syncs: {
syncId: number;
status: string;
}[];
};

const { data } = useQuery<SyncResponse>(GET_RECENT_SYNC, {
variables: {
domainId: HL_DOMAIN[expectedChainId as ChainId],
},
skip: !expectedChainId,
});

console.log("data", data);

// TODO: use proper syncID
const { config } = usePrepareDoveFinalizeSyncFromL2({
address: DOVE_ADDRESS[ChainId.ETHEREUM_GOERLI] as `0x${string}`,
args: [HL_DOMAIN[expectedChainId as ChainId], 1],
enabled: !!expectedChainId,
args: [HL_DOMAIN[expectedChainId as ChainId], data?.syncs[0]?.syncId ?? 0],
enabled: !!expectedChainId && data?.syncs[0]?.status === "PENDING_FINAlIZE",
});

const { write } = useDoveFinalizeSyncFromL2(config);

return {
finalizeSync: () => write?.(),
status: data?.syncs[0]?.status || "UNKNOWN",
};
}
1 change: 1 addition & 0 deletions lib/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,4 @@ export type ToastContent = {
txid?: string;
type: "success" | "error";
};

2 changes: 1 addition & 1 deletion pages/_app.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ const wagmiClient = createClient({
});

const apolloClient = new ApolloClient({
uri: process.env.GRAPHQL_URI,
uri: process.env.NEXT_PUBLIC_GRAPHQL_ENDPOINT,
cache: new InMemoryCache(),
});

Expand Down

1 comment on commit 0cbd98b

@vercel
Copy link

@vercel vercel bot commented on 0cbd98b Mar 27, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Successfully deployed to the following URLs:

dove-interface – ./

dove-interface.vercel.app
dove-interface-git-main-zksoju.vercel.app
dove-interface-zksoju.vercel.app

Please sign in to comment.