create-eth
Version:
Create a Scaffold-ETH-2 app
146 lines (130 loc) • 4.37 kB
text/typescript
import * as chains from "viem/chains";
import scaffoldConfig from "~~/scaffold.config";
type ChainAttributes = {
// color | [lightThemeColor, darkThemeColor]
color: string | [string, string];
// Used to fetch price by providing mainnet token address
// for networks having native currency other than ETH
nativeCurrencyTokenAddress?: string;
};
export type ChainWithAttributes = chains.Chain & Partial<ChainAttributes>;
export type AllowedChainIds = (typeof scaffoldConfig.targetNetworks)[number]["id"];
// Mapping of chainId to RPC chain name an format followed by alchemy and infura
export const RPC_CHAIN_NAMES: Record<number, string> = {
[ ]: "eth-mainnet",
[ ]: "eth-goerli",
[ ]: "eth-sepolia",
[ ]: "opt-mainnet",
[ ]: "opt-goerli",
[ ]: "opt-sepolia",
[ ]: "arb-mainnet",
[ ]: "arb-goerli",
[ ]: "arb-sepolia",
[ ]: "polygon-mainnet",
[ ]: "polygon-mumbai",
[ ]: "polygon-amoy",
[ ]: "astar-mainnet",
[ ]: "polygonzkevm-mainnet",
[ ]: "polygonzkevm-testnet",
[ ]: "base-mainnet",
[ ]: "base-goerli",
[ ]: "base-sepolia",
[ ]: "celo-mainnet",
[ ]: "celo-alfajores",
};
export const getAlchemyHttpUrl = (chainId: number) => {
return scaffoldConfig.alchemyApiKey && RPC_CHAIN_NAMES[chainId]
? `https://${RPC_CHAIN_NAMES[chainId]}.g.alchemy.com/v2/${scaffoldConfig.alchemyApiKey}`
: undefined;
};
export const NETWORKS_EXTRA_DATA: Record<string, ChainAttributes> = {
[ ]: {
color: "#b8af0c",
},
[ ]: {
color: "#ff8b9e",
},
[ ]: {
color: ["#5f4bb6", "#87ff65"],
},
[ ]: {
color: "#48a9a6",
},
[ ]: {
color: "#2bbdf7",
nativeCurrencyTokenAddress: "0x7D1AfA7B718fb893dB30A3aBc0Cfc608AaCfeBB0",
},
[ ]: {
color: "#92D9FA",
nativeCurrencyTokenAddress: "0x7D1AfA7B718fb893dB30A3aBc0Cfc608AaCfeBB0",
},
[ ]: {
color: "#f01a37",
},
[ ]: {
color: "#f01a37",
},
[ ]: {
color: "#28a0f0",
},
[ ]: {
color: "#28a0f0",
},
[ ]: {
color: "#1969ff",
},
[ ]: {
color: "#1969ff",
},
[ ]: {
color: "#fbebd4",
},
[ ]: {
color: "#FCFF52",
},
[ ]: {
color: "#476520",
},
};
/**
* Gives the block explorer transaction URL, returns empty string if the network is a local chain
*/
export function getBlockExplorerTxLink(chainId: number, txnHash: string) {
const chainNames = Object.keys(chains);
const targetChainArr = chainNames.filter(chainName => {
const wagmiChain = chains[chainName as keyof typeof chains];
return wagmiChain.id === chainId;
});
if (targetChainArr.length === 0) {
return "";
}
const targetChain = targetChainArr[0] as keyof typeof chains;
const blockExplorerTxURL = chains[targetChain]?.blockExplorers?.default?.url;
if (!blockExplorerTxURL) {
return "";
}
return `${blockExplorerTxURL}/tx/${txnHash}`;
}
/**
* Gives the block explorer URL for a given address.
* Defaults to Etherscan if no (wagmi) block explorer is configured for the network.
*/
export function getBlockExplorerAddressLink(network: chains.Chain, address: string) {
const blockExplorerBaseURL = network.blockExplorers?.default?.url;
if (network.id === chains.hardhat.id) {
return `/blockexplorer/address/${address}`;
}
if (!blockExplorerBaseURL) {
return `https://etherscan.io/address/${address}`;
}
return `${blockExplorerBaseURL}/address/${address}`;
}
/**
* @returns targetNetworks array containing networks configured in scaffold.config including extra network metadata
*/
export function getTargetNetworks(): ChainWithAttributes[] {
return scaffoldConfig.targetNetworks.map(targetNetwork => ({
...targetNetwork,
...NETWORKS_EXTRA_DATA[targetNetwork.id],
}));
}