Skip to content

Commit

Permalink
Use anvil_metadata if available
Browse files Browse the repository at this point in the history
  • Loading branch information
ericglau committed Dec 20, 2023
1 parent 2d497ad commit 9f182ec
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 9 deletions.
34 changes: 25 additions & 9 deletions packages/core/src/manifest.ts
Original file line number Diff line number Diff line change
@@ -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';

Expand Down Expand Up @@ -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<DevInstanceMetadata | undefined> {
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,
};
}

Expand Down
8 changes: 8 additions & 0 deletions packages/core/src/provider.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
export interface EthereumProvider {
send(method: 'anvil_metadata', params: []): Promise<HardhatMetadata>;
send(method: 'hardhat_metadata', params: []): Promise<HardhatMetadata>;
send(method: 'web3_clientVersion', params: []): Promise<string>;
send(method: 'net_version', params: []): Promise<string>;
Expand Down Expand Up @@ -58,6 +59,13 @@ export async function getHardhatMetadata(provider: EthereumProvider): Promise<Ha
return provider.send('hardhat_metadata', []);
}

/**
* Anvil could have anvil_metadata, for which hardhat_metadata is an alias.
*/
export async function getAnvilMetadata(provider: EthereumProvider): Promise<HardhatMetadata> {
return provider.send('anvil_metadata', []);
}

export async function getStorageAt(
provider: EthereumProvider,
address: string,
Expand Down

0 comments on commit 9f182ec

Please sign in to comment.