Skip to content

Commit

Permalink
Merge branch 'main' into env/testnet
Browse files Browse the repository at this point in the history
  • Loading branch information
canhtrinh committed Dec 20, 2023
2 parents 1e02fbf + bdb7221 commit fb45744
Show file tree
Hide file tree
Showing 6 changed files with 119 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
import { Alert, InfoIcon } from "@axelarjs/ui";
import { useMemo, type FC } from "react";
import dynamic from "next/dynamic";

import { useChainFromRoute } from "~/lib/hooks";
import { useGetChainsConfig } from "~/services/axelarConfigs/hooks";
import { MultiStepDialog, StepLoading } from "~/ui/compounds/MultiStepForm";
import {
CanonicalTokenDeploymentStateProvider,
Expand All @@ -24,6 +27,20 @@ const STEPS = [Step1, Step2, Step3];

const CanonicalTokenDeployment: FC = () => {
const { state, actions } = useCanonicalTokenDeploymentStateContainer();
const routeChain = useChainFromRoute();
const { data: tokenInfo } = useGetChainsConfig({
axelarChainId: routeChain?.axelarChainId,
});

const isGatewayToken = useMemo(
() =>
(
tokenInfo?.assets.map((asset: any) =>
asset.tokenAddress.toLowerCase()
) || []
).includes(state.tokenDetails.tokenAddress.toLowerCase()),
[tokenInfo, state.tokenDetails.tokenAddress]
);

const CurrentStep = useMemo(() => STEPS[state.step], [state.step]);

Expand All @@ -32,6 +49,14 @@ const CanonicalTokenDeployment: FC = () => {
[state.step]
);

if (isGatewayToken)
return (
<Alert status="warning" icon={<InfoIcon className="h-6 w-6" />}>
This token is supported directly on Axelar and cannot be registered in
ITS.
</Alert>
);

return (
<MultiStepDialog
triggerLabel="Register interchain token"
Expand Down
31 changes: 31 additions & 0 deletions apps/maestro/src/server/context.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import {
AxelarConfigClient,
AxelarscanClient,
createAxelarConfigClient,
createAxelarQueryClient,
EVMChainConfig,
} from "@axelarjs/api";
Expand Down Expand Up @@ -53,6 +55,8 @@ const createContextInner = async ({ req, res }: ContextConfig) => {

const axelarQueryClient = createAxelarQueryClient(NEXT_PUBLIC_NETWORK_ENV);

const axelarConfigClient = createAxelarConfigClient(NEXT_PUBLIC_NETWORK_ENV);

return {
req,
res,
Expand All @@ -74,6 +78,12 @@ const createContextInner = async ({ req, res }: ContextConfig) => {
axelarscanClient,
"evmChains" as const
),
axelarConfigs: axelarConfigs.bind(
null,
maestroKVClient,
axelarConfigClient,
"axelarConfigs" as const
),
wagmiChainConfigs: WAGMI_CHAIN_CONFIGS,
},
persistence: {
Expand Down Expand Up @@ -186,3 +196,24 @@ async function evmChains<TCacheKey extends string>(

return evmChainsMap;
}

async function axelarConfigs<TCacheKey extends string>(
kvClient: MaestroKVClient,
axelarConfigClient: AxelarConfigClient,
cacheKey: TCacheKey
): Promise<any> {
const chainConfigs = await axelarConfigClient.getChainConfigs(
NEXT_PUBLIC_NETWORK_ENV
);

const cached = await kvClient.getCached<any>(cacheKey);

if (cached) {
return cached;
}

// cache for 1 hour
await kvClient.setCached<any>(cacheKey, chainConfigs, 3600);

return chainConfigs;
}
2 changes: 2 additions & 0 deletions apps/maestro/src/server/routers/_app.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { publicProcedure, router } from "~/server/trpc";
import { authRouter } from "./auth";
import { axelarConfigsRouter } from "./axelarConfigs";
import { axelarjsSDKRouter } from "./axelarjsSDK";
import { axelarscanRouter } from "./axelarscan";
import { erc20Router } from "./erc20";
Expand All @@ -16,6 +17,7 @@ export const appRouter = router({
axelarscan: axelarscanRouter,
erc20: erc20Router,
axelarjsSDK: axelarjsSDKRouter,
axelarConfigs: axelarConfigsRouter,
interchainToken: interchainTokenRouter,
auth: authRouter,
openai: openaiRouter,
Expand Down
37 changes: 37 additions & 0 deletions apps/maestro/src/server/routers/axelarConfigs/getChainConfigs.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
import { TRPCError } from "@trpc/server";
import { z } from "zod";

import { publicProcedure } from "~/server/trpc";

export const getConfigForChain = publicProcedure
.meta({
openapi: {
summary: "Get the full configs for a chain on the Axelar network",
description:
"Get the full configs for a chain on the Axelar network, including the assets registered directly on the network",
method: "GET",
path: "/axelar-chain-configs",
tags: ["axelar-chain-configs"],
},
})
.input(
z.object({
axelarChainId: z.string().max(64),
})
)
.output(z.any())
.query(async ({ ctx, input }) => {
try {
return (await ctx.configs.axelarConfigs()).chains[input.axelarChainId];
} catch (error) {
// If we get a TRPC error, we throw it
if (error instanceof TRPCError) {
throw error;
}
// otherwise, we throw an internal server error
throw new TRPCError({
code: "INTERNAL_SERVER_ERROR",
message: "Failed to get chain configs",
});
}
});
8 changes: 8 additions & 0 deletions apps/maestro/src/server/routers/axelarConfigs/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import { router } from "~/server/trpc";
import { getConfigForChain } from "./getChainConfigs";

export const axelarConfigsRouter = router({
getChainConfigs: getConfigForChain,
});

export type AxelarConfigsRouter = typeof axelarConfigsRouter;
16 changes: 16 additions & 0 deletions apps/maestro/src/services/axelarConfigs/hooks.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import { Maybe } from "@axelarjs/utils";

import { trpc } from "~/lib/trpc";

export function useGetChainsConfig(input: { axelarChainId?: string }) {
return trpc.axelarConfigs.getChainConfigs.useQuery(
{
axelarChainId: Maybe.of(input.axelarChainId).mapOr("", String),
},
{
enabled: Boolean(input.axelarChainId),
staleTime: 1000 * 60 * 60 * 24, // 24 hours
refetchOnWindowFocus: false,
}
);
}

0 comments on commit fb45744

Please sign in to comment.