Skip to content

Commit

Permalink
use alchemy rpc url (#43)
Browse files Browse the repository at this point in the history
Co-authored-by: John Guilding <[email protected]>
  • Loading branch information
JohnGuilding and John Guilding authored Sep 16, 2024
1 parent 23a18e4 commit 46a336a
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 7 deletions.
6 changes: 3 additions & 3 deletions packages/backend/.env.example
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,13 @@ 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
TLS_KEY=""
# The port to listen on
PORT=""
# The IP to bind to
BIND_IP=""
BIND_IP=""
# Alchemy ID
ALCHEMY_ID=""
5 changes: 3 additions & 2 deletions packages/backend/src/account.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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())
})
22 changes: 22 additions & 0 deletions packages/backend/src/getRpcUrl.ts
Original file line number Diff line number Diff line change
@@ -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}`;
}
};
4 changes: 2 additions & 2 deletions packages/interface/src/utils/hatsProtocol.ts
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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({
Expand Down

0 comments on commit 46a336a

Please sign in to comment.