Skip to content

Commit

Permalink
Add custom chain config (#259)
Browse files Browse the repository at this point in the history
  • Loading branch information
ryangoree authored Jul 21, 2023
1 parent d2a1cd7 commit e9eb147
Show file tree
Hide file tree
Showing 3 changed files with 74 additions and 17 deletions.
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
export const supportedChainIds = [5, 31337] as const;
export const supportedChainIds = [42069, 31337] as const;

export type SupportedChainId = (typeof supportedChainIds)[number];
54 changes: 46 additions & 8 deletions apps/hyperdrive-trading/src/network/wagmiClient.ts
Original file line number Diff line number Diff line change
@@ -1,28 +1,66 @@
import { getDefaultWallets } from "@rainbow-me/rainbowkit";
import { configureChains, createConfig } from "wagmi";
import { foundry, goerli } from "wagmi/chains";
import { alchemyProvider } from "wagmi/providers/alchemy";
import { Chain, ChainProviderFn, configureChains, createConfig } from "wagmi";
import { foundry } from "wagmi/chains";
import { jsonRpcProvider } from "wagmi/providers/jsonRpc";
const {
VITE_ALCHEMY_GOERLI_RPC_KEY: ALCHEMY_GOERLI_RPC_KEY,
VITE_LOCALHOST_NODE_RPC_URL: LOCALHOST_NODE_RPC_URL,
VITE_CUSTOM_CHAIN_NODE_RPC_URL: CUSTOM_CHAIN_NODE_RPC_URL,
VITE_CUSTOM_CHAIN_ADDRESSES_URL: CUSTOM_CHAIN_NODE_ADDRESSES_URL,
VITE_CUSTOM_CHAIN_CHAIN_ID: CUSTOM_CHAIN_CHAIN_ID,
VITE_WALLET_CONNECT_PROJECT_ID: PROJECT_ID,
} = import.meta.env;

if (!ALCHEMY_GOERLI_RPC_KEY) {
throw new Error("Provide an ALCHEMY_GOERLI_RPC_KEY variable in .env");
}

const { chains, publicClient, webSocketPublicClient } = configureChains(
[foundry, goerli],
[
const chainsToConfigure: Chain[] = [];
const providersToConfigure: ChainProviderFn[] = [];

if (LOCALHOST_NODE_RPC_URL) {
chainsToConfigure.push(foundry);
providersToConfigure.push(
jsonRpcProvider({
rpc: () => ({
http: LOCALHOST_NODE_RPC_URL,
}),
}),
alchemyProvider({ apiKey: ALCHEMY_GOERLI_RPC_KEY }),
],
);
}

if (
CUSTOM_CHAIN_NODE_RPC_URL &&
CUSTOM_CHAIN_CHAIN_ID &&
CUSTOM_CHAIN_NODE_ADDRESSES_URL
) {
chainsToConfigure.push({
id: +CUSTOM_CHAIN_CHAIN_ID,
name: "☁️ \u00A0 Chain",
network: "custom-chain",
nativeCurrency: {
decimals: 18,
name: "Ether",
symbol: "ETH",
},
rpcUrls: {
public: { http: [CUSTOM_CHAIN_NODE_RPC_URL] },
default: { http: [CUSTOM_CHAIN_NODE_RPC_URL] },
},
});

providersToConfigure.push(
jsonRpcProvider({
rpc: () => ({
http: CUSTOM_CHAIN_NODE_RPC_URL,
}),
}),
);
}

const { chains, publicClient, webSocketPublicClient } = configureChains(
chainsToConfigure,
providersToConfigure,
);

export const wagmiChains = chains;
Expand Down
34 changes: 26 additions & 8 deletions apps/hyperdrive-trading/src/ui/appconfig/useAppConfig.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import { HyperdriveGoerliAddresses } from "@hyperdrive/core";
import { useQuery } from "@tanstack/react-query";
import assertNever from "assert-never";
import { SupportedChainId } from "src/appconfig/chains/supportedChains";
Expand All @@ -8,6 +7,9 @@ import { Address } from "viem";
import { useChainId, usePublicClient } from "wagmi";

const LOCALHOST_ADDRESSES_URL = import.meta.env.VITE_LOCALHOST_ADDRESSES_URL;
const CUSTOM_CHAIN_ADDRESSES_URL = import.meta.env
.VITE_CUSTOM_CHAIN_ADDRESSES_URL;

export function useAppConfig(): {
appConfig: AppConfig | undefined;
appConfigStatus: "idle" | "error" | "loading" | "success";
Expand All @@ -18,11 +20,8 @@ export function useAppConfig(): {
queryKey: ["app-config", { chainId }],
queryFn: async () => {
switch (chainId) {
case 5:
return getAppConfig(HyperdriveGoerliAddresses, publicClient);

case 31337:
const localAddresses = await fetchLocalAddresses();
const localAddresses = await fetchLocalhostAddresses();
return getAppConfig(
// TODO: This is a temporary "cross-walk" object to use until SC
// team unifies the shape of the addresses.json file across the
Expand All @@ -34,6 +33,19 @@ export function useAppConfig(): {
publicClient,
);

case 42069:
const addresses = await fetchCustomChainAddresses();
return getAppConfig(
// TODO: This is a temporary "cross-walk" object to use until SC
// team unifies the shape of the addresses.json file across the
// different chains.
{
dsrHyperdrive: addresses.mockHyperdrive,
mockHyperdriveMath: addresses.mockHyperdriveMath,
},
publicClient,
);

default:
assertNever(chainId);
}
Expand All @@ -45,9 +57,15 @@ export function useAppConfig(): {
return { appConfig, appConfigStatus };
}

async function fetchLocalAddresses() {
async function fetchLocalhostAddresses() {
return await fetch(LOCALHOST_ADDRESSES_URL).then(
(res) => res.json() as Promise<LocalAddressesJson>,
(res) => res.json() as Promise<AddressesJson>,
);
}

async function fetchCustomChainAddresses() {
return await fetch(CUSTOM_CHAIN_ADDRESSES_URL).then(
(res) => res.json() as Promise<AddressesJson>,
);
}

Expand All @@ -56,7 +74,7 @@ async function fetchLocalAddresses() {
* addresses.json file and the local one (see Docker image in infra repo) share
* the same shape. Smart Contract team will implement this.
*/
interface LocalAddressesJson {
interface AddressesJson {
baseToken: Address;
mockHyperdrive: Address;
mockHyperdriveMath: Address;
Expand Down

2 comments on commit e9eb147

@vercel
Copy link

@vercel vercel bot commented on e9eb147 Jul 21, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Successfully deployed to the following URLs:

hyperdrive-fixed-borrow – ./apps/fixed-borrow

hyperdrive-fixed-borrow.vercel.app
hyperdrive-fixed-borrow-git-main-delvtech.vercel.app
hyperdrive-fixed-borrow-delvtech.vercel.app

@vercel
Copy link

@vercel vercel bot commented on e9eb147 Jul 21, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Successfully deployed to the following URLs:

hyperdrive-monorepo-hyperdrive-trading – ./apps/hyperdrive-trading

hyperdrive-monorepo-hyperdrive-trading-delvtech.vercel.app
hyperdrive-monorepo-hyperdrive-trading-git-main-delvtech.vercel.app
hyperdrive-monorepo-hyperdrive-trading.vercel.app

Please sign in to comment.