2022-11-10 23:40:01 +00:00
|
|
|
import { router, publicProcedure } from "@calcom/trpc/server/trpc";
|
2022-09-05 21:10:58 +00:00
|
|
|
|
2023-04-25 22:39:47 +00:00
|
|
|
import { ZBalanceInputSchema, ZBalanceOutputSchema } from "./balance.schema";
|
|
|
|
import { ZContractInputSchema, ZContractOutputSchema } from "./contract.schema";
|
|
|
|
|
|
|
|
interface EthRouterHandlersCache {
|
|
|
|
contract?: typeof import("./contract.handler").contractHandler;
|
|
|
|
balance?: typeof import("./balance.handler").balanceHandler;
|
|
|
|
}
|
|
|
|
|
|
|
|
const UNSTABLE_HANDLER_CACHE: EthRouterHandlersCache = {};
|
2022-09-05 21:10:58 +00:00
|
|
|
|
2022-11-10 23:40:01 +00:00
|
|
|
const ethRouter = router({
|
2022-09-05 21:10:58 +00:00
|
|
|
// Fetch contract `name` and `symbol` or error
|
2022-11-10 23:40:01 +00:00
|
|
|
contract: publicProcedure
|
2023-04-25 22:39:47 +00:00
|
|
|
.input(ZContractInputSchema)
|
|
|
|
.output(ZContractOutputSchema)
|
2022-11-10 23:40:01 +00:00
|
|
|
.query(async ({ input }) => {
|
2023-04-25 22:39:47 +00:00
|
|
|
if (!UNSTABLE_HANDLER_CACHE.contract) {
|
|
|
|
UNSTABLE_HANDLER_CACHE.contract = await import("./contract.handler").then(
|
|
|
|
(mod) => mod.contractHandler
|
|
|
|
);
|
|
|
|
}
|
2022-09-05 21:10:58 +00:00
|
|
|
|
2023-04-25 22:39:47 +00:00
|
|
|
// Unreachable code but required for type safety
|
|
|
|
if (!UNSTABLE_HANDLER_CACHE.contract) {
|
|
|
|
throw new Error("Failed to load handler");
|
2022-09-05 21:10:58 +00:00
|
|
|
}
|
2023-04-25 22:39:47 +00:00
|
|
|
|
|
|
|
return UNSTABLE_HANDLER_CACHE.contract({
|
|
|
|
input,
|
|
|
|
});
|
2022-09-05 21:10:58 +00:00
|
|
|
}),
|
2022-11-10 23:40:01 +00:00
|
|
|
// Fetch user's `balance` of either ERC-20 or ERC-721 compliant token or error
|
|
|
|
balance: publicProcedure
|
2023-04-25 22:39:47 +00:00
|
|
|
.input(ZBalanceInputSchema)
|
|
|
|
.output(ZBalanceOutputSchema)
|
2022-11-10 23:40:01 +00:00
|
|
|
.query(async ({ input }) => {
|
2023-04-25 22:39:47 +00:00
|
|
|
if (!UNSTABLE_HANDLER_CACHE.balance) {
|
|
|
|
UNSTABLE_HANDLER_CACHE.balance = await import("./balance.handler").then((mod) => mod.balanceHandler);
|
|
|
|
}
|
2022-09-05 21:10:58 +00:00
|
|
|
|
2023-04-25 22:39:47 +00:00
|
|
|
// Unreachable code but required for type safety
|
|
|
|
if (!UNSTABLE_HANDLER_CACHE.balance) {
|
|
|
|
throw new Error("Failed to load handler");
|
2022-09-05 21:10:58 +00:00
|
|
|
}
|
2023-04-25 22:39:47 +00:00
|
|
|
|
|
|
|
return UNSTABLE_HANDLER_CACHE.balance({
|
|
|
|
input,
|
|
|
|
});
|
2022-11-10 23:40:01 +00:00
|
|
|
}),
|
|
|
|
});
|
2022-09-05 21:10:58 +00:00
|
|
|
|
|
|
|
export default ethRouter;
|