diff --git a/packages/backend/.env.example b/packages/backend/.env.example index a7f4aff..11ead42 100644 --- a/packages/backend/.env.example +++ b/packages/backend/.env.example @@ -6,8 +6,6 @@ APP_PASSWORD="" HAT_ID="" # The private key of the EVM account ETH_PRIVATE_KEY="" -# The RPC URL for the EVM network -RPC_URL="" # The path to the TLS cert TLS_CERT="" # The path to the TLS key @@ -15,4 +13,6 @@ TLS_KEY="" # The port to listen on PORT="" # The IP to bind to -BIND_IP="" \ No newline at end of file +BIND_IP="" +# Alchemy ID +ALCHEMY_ID="" \ No newline at end of file diff --git a/packages/backend/src/account.ts b/packages/backend/src/account.ts index 5dcdf43..2943d21 100644 --- a/packages/backend/src/account.ts +++ b/packages/backend/src/account.ts @@ -2,6 +2,7 @@ import { createPublicClient, createWalletClient, http} from 'viem' import { privateKeyToAccount } from 'viem/accounts' import { PRIVATE_KEY } from './constants' import getNetwork from './getNetwork'; +import { getRpcUrl } from './getRpcUrl'; // create an account from the private key export const account = privateKeyToAccount(PRIVATE_KEY) @@ -11,12 +12,12 @@ const chain = getNetwork(); // a public client for reading data export const publicClient = createPublicClient({ chain, - transport: http() + transport: http(getRpcUrl()) }) // a wallet client for signing transactions export const walletClient = createWalletClient({ account, chain, - transport: http() + transport: http(getRpcUrl()) }) diff --git a/packages/backend/src/getRpcUrl.ts b/packages/backend/src/getRpcUrl.ts new file mode 100644 index 0000000..fea99f5 --- /dev/null +++ b/packages/backend/src/getRpcUrl.ts @@ -0,0 +1,22 @@ +import getNetwork from "./getNetwork"; + +/** + * Get the RPC URL based on the network we are connected to + * @returns the alchemy RPC URL + */ +export const getRpcUrl = (): string => { + const chainId = getNetwork().id; + + if (!process.env.ALCHEMY_ID) { + throw new Error("ALCHEMY_ID is not set"); + } + + switch (chainId) { + case 10: + return `https://opt-mainnet.g.alchemy.com/v2/${process.env.ALCHEMY_ID}`; + case 11155420: + return `https://opt-sepolia.g.alchemy.com/v2/${process.env.ALCHEMY_ID}`; + default: + return `https://opt-sepolia.g.alchemy.com/v2/${process.env.ALCHEMY_ID}`; + } + }; \ No newline at end of file diff --git a/packages/interface/src/utils/hatsProtocol.ts b/packages/interface/src/utils/hatsProtocol.ts index 8f18c4d..1f0ba6e 100644 --- a/packages/interface/src/utils/hatsProtocol.ts +++ b/packages/interface/src/utils/hatsProtocol.ts @@ -1,7 +1,7 @@ import { HatsClient } from "@hatsprotocol/sdk-v1-core"; import { createPublicClient, http, type PublicClient } from "viem"; -import { config } from "~/config"; +import { config, getRPCURL } from "~/config"; /** * Get an HatsClient instance to check whether the connected wallet owns the required hat @@ -10,7 +10,7 @@ import { config } from "~/config"; export const getHatsClient = (): HatsClient => { const client = createPublicClient({ chain: config.network, - transport: http(), + transport: http(getRPCURL()), }); const hatsClient = new HatsClient({