From 954061331b548a3006a91bad2a6837f0ec7c3f73 Mon Sep 17 00:00:00 2001 From: Benjamin Smith Date: Fri, 6 Sep 2024 23:23:08 +0200 Subject: [PATCH] Enriched Network Details (#108) --- src/constants.ts | 29 +++++++++++++++++++++++++++++ src/network.ts | 31 ++++++++++++++++++++++++++++++- 2 files changed, 59 insertions(+), 1 deletion(-) create mode 100644 src/constants.ts diff --git a/src/constants.ts b/src/constants.ts new file mode 100644 index 0000000..7286dd6 --- /dev/null +++ b/src/constants.ts @@ -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", + }, +}; diff --git a/src/network.ts b/src/network.ts index 21d3748..117d3b2 100644 --- a/src/network.ts +++ b/src/network.ts @@ -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)); @@ -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 @@ -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; @@ -31,6 +52,7 @@ export class Network implements NetworkFields { this.client = createPublicClient({ transport: http(network.rpcUrl), }); + this.nativeCurrency = nativeCurrency; } static fromChainId(chainId: number): Network { @@ -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`, + }, }; });