2022-09-05 21:10:58 +00:00
|
|
|
import { ethers } from "ethers";
|
|
|
|
import { configureChains, createClient } from "wagmi";
|
|
|
|
import { z } from "zod";
|
|
|
|
|
2022-11-10 23:40:01 +00:00
|
|
|
import { router, publicProcedure } from "@calcom/trpc/server/trpc";
|
2022-09-05 21:10:58 +00:00
|
|
|
|
|
|
|
import abi from "../utils/abi.json";
|
|
|
|
import { checkBalance, getProviders, SUPPORTED_CHAINS } from "../utils/ethereum";
|
|
|
|
|
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
|
|
|
|
.input(
|
|
|
|
z.object({
|
|
|
|
address: z.string(),
|
|
|
|
chainId: z.number(),
|
|
|
|
})
|
|
|
|
)
|
|
|
|
.output(
|
|
|
|
z.object({
|
|
|
|
data: z
|
|
|
|
.object({
|
|
|
|
name: z.string(),
|
|
|
|
symbol: z.string(),
|
|
|
|
})
|
|
|
|
.nullish(),
|
|
|
|
error: z.string().nullish(),
|
|
|
|
})
|
|
|
|
)
|
|
|
|
.query(async ({ input }) => {
|
|
|
|
const { address, chainId } = input;
|
2022-09-05 21:10:58 +00:00
|
|
|
const { provider } = configureChains(
|
|
|
|
SUPPORTED_CHAINS.filter((chain) => chain.id === chainId),
|
|
|
|
getProviders()
|
|
|
|
);
|
|
|
|
|
|
|
|
const client = createClient({
|
|
|
|
provider,
|
|
|
|
});
|
|
|
|
|
|
|
|
const contract = new ethers.Contract(address, abi, client.provider);
|
|
|
|
|
|
|
|
try {
|
|
|
|
const name = await contract.name();
|
|
|
|
const symbol = await contract.symbol();
|
|
|
|
|
|
|
|
return {
|
|
|
|
data: {
|
|
|
|
name,
|
|
|
|
symbol: `$${symbol}`,
|
|
|
|
},
|
|
|
|
};
|
|
|
|
} catch (e) {
|
|
|
|
return {
|
|
|
|
data: {
|
|
|
|
name: address,
|
|
|
|
symbol: "$UNKNOWN",
|
|
|
|
},
|
|
|
|
};
|
|
|
|
}
|
|
|
|
}),
|
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
|
|
|
|
.input(
|
|
|
|
z.object({
|
|
|
|
address: z.string(),
|
|
|
|
tokenAddress: z.string(),
|
|
|
|
chainId: z.number(),
|
|
|
|
})
|
|
|
|
)
|
|
|
|
.output(
|
|
|
|
z.object({
|
|
|
|
data: z
|
|
|
|
.object({
|
|
|
|
hasBalance: z.boolean(),
|
|
|
|
})
|
|
|
|
.nullish(),
|
|
|
|
error: z.string().nullish(),
|
|
|
|
})
|
|
|
|
)
|
|
|
|
.query(async ({ input }) => {
|
|
|
|
const { address, tokenAddress, chainId } = input;
|
2022-09-05 21:10:58 +00:00
|
|
|
try {
|
|
|
|
const hasBalance = await checkBalance(address, tokenAddress, chainId);
|
|
|
|
|
|
|
|
return {
|
|
|
|
data: {
|
|
|
|
hasBalance,
|
|
|
|
},
|
|
|
|
};
|
|
|
|
} catch (e) {
|
|
|
|
return {
|
|
|
|
data: {
|
|
|
|
hasBalance: false,
|
|
|
|
},
|
|
|
|
};
|
|
|
|
}
|
2022-11-10 23:40:01 +00:00
|
|
|
}),
|
|
|
|
});
|
2022-09-05 21:10:58 +00:00
|
|
|
|
|
|
|
export default ethRouter;
|