From 9f182ec54be2c50799174c1833c9a6760bd7f9da Mon Sep 17 00:00:00 2001 From: Eric Lau Date: Wed, 20 Dec 2023 17:23:14 -0500 Subject: [PATCH] Use anvil_metadata if available --- packages/core/src/manifest.ts | 34 +++++++++++++++++++++++++--------- packages/core/src/provider.ts | 8 ++++++++ 2 files changed, 33 insertions(+), 9 deletions(-) diff --git a/packages/core/src/manifest.ts b/packages/core/src/manifest.ts index 499bd3890..1ee9c7897 100644 --- a/packages/core/src/manifest.ts +++ b/packages/core/src/manifest.ts @@ -1,7 +1,14 @@ import os from 'os'; import path from 'path'; import { promises as fs } from 'fs'; -import { EthereumProvider, HardhatMetadata, getChainId, getHardhatMetadata, networkNames } from './provider'; +import { + EthereumProvider, + HardhatMetadata, + getAnvilMetadata, + getChainId, + getHardhatMetadata, + networkNames, +} from './provider'; import lockfile from 'proper-lockfile'; import { compare as compareVersions } from 'compare-versions'; @@ -44,28 +51,37 @@ function defaultManifest(): ManifestData { const MANIFEST_DEFAULT_DIR = '.openzeppelin'; const MANIFEST_TEMP_DIR = 'openzeppelin-upgrades'; +type DevNetworkType = 'hardhat' | 'anvil'; + async function getDevInstanceMetadata( provider: EthereumProvider, chainId: number, ): Promise { - let hardhatMetadata: HardhatMetadata; + let networkMetadata: HardhatMetadata; + let networkType: DevNetworkType; try { - hardhatMetadata = await getHardhatMetadata(provider); + networkMetadata = await getAnvilMetadata(provider); + networkType = 'anvil'; } catch (e: unknown) { - return undefined; + try { + networkMetadata = await getHardhatMetadata(provider); + networkType = 'hardhat'; + } catch (e: unknown) { + return undefined; + } } - if (hardhatMetadata.chainId !== chainId) { + if (networkMetadata.chainId !== chainId) { throw new Error( - `Broken invariant: Hardhat metadata's chainId ${hardhatMetadata.chainId} does not match eth_chainId ${chainId}`, + `Broken invariant: Hardhat or Anvil metadata's chainId ${networkMetadata.chainId} does not match eth_chainId ${chainId}`, ); } return { - networkName: hardhatMetadata.clientVersion.startsWith('anvil') ? 'anvil' : 'hardhat', - instanceId: hardhatMetadata.instanceId, - forkedNetwork: hardhatMetadata.forkedNetwork, + networkName: networkType, + instanceId: networkMetadata.instanceId, + forkedNetwork: networkMetadata.forkedNetwork, }; } diff --git a/packages/core/src/provider.ts b/packages/core/src/provider.ts index 9713d24a9..0fcea674e 100644 --- a/packages/core/src/provider.ts +++ b/packages/core/src/provider.ts @@ -1,4 +1,5 @@ export interface EthereumProvider { + send(method: 'anvil_metadata', params: []): Promise; send(method: 'hardhat_metadata', params: []): Promise; send(method: 'web3_clientVersion', params: []): Promise; send(method: 'net_version', params: []): Promise; @@ -58,6 +59,13 @@ export async function getHardhatMetadata(provider: EthereumProvider): Promise { + return provider.send('anvil_metadata', []); +} + export async function getStorageAt( provider: EthereumProvider, address: string,