Skip to content

Commit

Permalink
Enriched Network Details (#108)
Browse files Browse the repository at this point in the history
  • Loading branch information
bh2smith authored Sep 6, 2024
1 parent 7cdfa43 commit 9540613
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 1 deletion.
29 changes: 29 additions & 0 deletions src/constants.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
/// A short list of networks with known wrapped tokens.
export const CHAIN_INFO: {
[key: number]: { icon?: string; wrappedToken: string };
} = {
11155111: {
icon: "https://cryptologos.cc/logos/ethereum-eth-logo.svg?v=014",
wrappedToken: "0xD0A1E359811322d97991E03f863a0C30C2cF029C",
},
1: {
icon: "https://cryptologos.cc/logos/ethereum-eth-logo.svg?v=014",
wrappedToken: "0xC02aaA39b223FE8D0A0e5C4F27eAD9083C756Cc2",
},
100: {
icon: "https://cryptologos.cc/logos/gnosis-gno-logo.svg?v=014",
wrappedToken: "0x6a023ccd1ff6f2045c3309768ead9e68f978f6e1",
},
137: {
wrappedToken: "0x7d1afa7b718fb893db30a3abc0cfc608aacfebb0",
},
42161: {
wrappedToken: "0x82af49447d8a07e3bd95bd0d56f35241523fbab1",
},
10: {
wrappedToken: "0x4200000000000000000000000000000000000006",
},
8453: {
wrappedToken: "0x4200000000000000000000000000000000000006",
},
};
31 changes: 30 additions & 1 deletion src/network.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { Chain, createPublicClient, http, PublicClient } from "viem";
import * as chains from "viem/chains";
import { CHAIN_INFO } from "./constants";

// We support all networks exported by viem
const SUPPORTED_NETWORKS = createNetworkMap(Object.values(chains));
Expand All @@ -9,6 +10,13 @@ interface NetworkFields {
rpcUrl: string;
chainId: number;
scanUrl: string;
nativeCurrency: {
decimals: number;
name: string;
symbol: string;
wrappedAddress: string | undefined;
icon: string | undefined;
};
}
/**
* Leveraging Network Data provided from through viem
Expand All @@ -20,8 +28,21 @@ export class Network implements NetworkFields {
chainId: number;
scanUrl: string;
client: PublicClient;
nativeCurrency: {
decimals: number;
name: string;
symbol: string;
wrappedAddress: string | undefined;
icon: string | undefined;
};

constructor({ name, rpcUrl, chainId, scanUrl }: NetworkFields) {
constructor({
name,
rpcUrl,
chainId,
scanUrl,
nativeCurrency,
}: NetworkFields) {
const network = SUPPORTED_NETWORKS[chainId]!;

this.name = name;
Expand All @@ -31,6 +52,7 @@ export class Network implements NetworkFields {
this.client = createPublicClient({
transport: http(network.rpcUrl),
});
this.nativeCurrency = nativeCurrency;
}

static fromChainId(chainId: number): Network {
Expand All @@ -56,6 +78,13 @@ function createNetworkMap(supportedNetworks: Chain[]): NetworkMap {
rpcUrl: network.rpcUrls.default.http[0]!,
chainId: network.id,
scanUrl: network.blockExplorers?.default.url || "",
nativeCurrency: {
...network.nativeCurrency,
wrappedAddress: CHAIN_INFO[network.id]?.wrappedToken,
icon:
CHAIN_INFO[network.id]?.icon ||
`/${network.nativeCurrency.symbol}.svg`,
},
};
});

Expand Down

0 comments on commit 9540613

Please sign in to comment.