-
Notifications
You must be signed in to change notification settings - Fork 1.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #5680 from NomicFoundation/network-manager-m1
Network manager implementation (M1)
- Loading branch information
Showing
32 changed files
with
972 additions
and
85 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
import { network } from "@ignored/hardhat-vnext"; | ||
|
||
// This network connection has access to an optimism-specific viem api | ||
const optimism = await network.connect("localhost", "optimism"); | ||
optimism.viem.client.getL1BaseFee({ chain: null }); | ||
|
||
// This one doesn't | ||
const mainnet = await network.connect("localhost", "l1"); | ||
// @ts-expect-error | ||
mainnet.viem.client.getL1BaseFee({ chain: null }); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
import { HardhatPlugin } from "@ignored/hardhat-vnext/types/plugins"; | ||
import { ChainType } from "@ignored/hardhat-vnext/types/config"; | ||
import { HookContext } from "@ignored/hardhat-vnext/types/hooks"; | ||
|
||
import { NetworkConnection } from "@ignored/hardhat-vnext/types/network"; | ||
|
||
import "@ignored/hardhat-vnext/types/network"; | ||
|
||
import { | ||
Client, | ||
createPublicClient, | ||
custom, | ||
CustomTransport, | ||
PublicActions, | ||
PublicClient, | ||
PublicRpcSchema, | ||
} from "viem"; | ||
import { PublicActionsL2, publicActionsL2 } from "viem/op-stack"; | ||
|
||
export type ViemPublicClient<ChainTypeT extends ChainType | string> = | ||
ChainTypeT extends "optimism" | ||
? Client< | ||
CustomTransport, | ||
undefined, | ||
undefined, | ||
PublicRpcSchema, | ||
PublicActions<CustomTransport, undefined> & | ||
PublicActionsL2<undefined, undefined> | ||
> | ||
: PublicClient; | ||
|
||
declare module "@ignored/hardhat-vnext/types/network" { | ||
export interface NetworkConnection<ChainTypeT extends ChainType | string> { | ||
viem: { | ||
client: ViemPublicClient<ChainTypeT>; | ||
}; | ||
} | ||
} | ||
|
||
export const viemScketchPlugin: HardhatPlugin = { | ||
id: "viem-scketch", | ||
hookHandlers: { | ||
network: async () => ({ | ||
async newConnection<ChainTypeT extends ChainType | string>( | ||
context: HookContext, | ||
next: ( | ||
nextContext: HookContext, | ||
) => Promise<NetworkConnection<ChainTypeT>>, | ||
) { | ||
const connection: NetworkConnection<ChainTypeT> = await next(context); | ||
|
||
const transport = custom(connection.provider); | ||
|
||
const client = | ||
connection.chainType === "optimism" | ||
? createPublicClient({ | ||
transport: custom(connection.provider), | ||
}).extend(publicActionsL2()) | ||
: createPublicClient({ | ||
transport, | ||
}); | ||
|
||
connection.viem = { client: client as ViemPublicClient<ChainTypeT> }; | ||
|
||
return connection; | ||
}, | ||
}), | ||
}, | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
78 changes: 78 additions & 0 deletions
78
v-next/hardhat/src/internal/builtin-plugins/network-manager/hook-handlers/config.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
import type { | ||
HttpNetworkConfig, | ||
NetworkConfig, | ||
NetworkUserConfig, | ||
} from "../../../../types/config.js"; | ||
import type { ConfigHooks } from "../../../../types/hooks.js"; | ||
|
||
import { validateUserConfig } from "../type-validation.js"; | ||
|
||
function resolveBigIntOrAuto( | ||
value: number | bigint | "auto" | undefined, | ||
): bigint | "auto" { | ||
if (value === undefined || value === "auto") { | ||
return "auto"; | ||
} | ||
|
||
// TODO: Validate that it's a valid BigInt | ||
return BigInt(value); | ||
} | ||
|
||
export default async (): Promise<Partial<ConfigHooks>> => ({ | ||
extendUserConfig: async (config, next) => { | ||
const extendedConfig = await next(config); | ||
|
||
const networks: Record<string, NetworkUserConfig> = | ||
extendedConfig.networks ?? {}; | ||
|
||
return { | ||
...extendedConfig, | ||
networks: { | ||
...networks, | ||
localhost: { | ||
url: "http://localhost:8545", | ||
...networks.localhost, | ||
type: "http", | ||
}, | ||
}, | ||
}; | ||
}, | ||
validateUserConfig, | ||
resolveUserConfig: async (userConfig, resolveConfigurationVariable, next) => { | ||
const resolvedConfig = await next(userConfig, resolveConfigurationVariable); | ||
|
||
const networks: Record<string, NetworkUserConfig> = | ||
userConfig.networks ?? {}; | ||
|
||
const resolvedNetworks: Record<string, NetworkConfig> = {}; | ||
|
||
for (const [networkName, networkConfig] of Object.entries(networks)) { | ||
if (networkConfig.type !== "http") { | ||
// eslint-disable-next-line no-restricted-syntax -- TODO | ||
throw new Error("Only HTTP network is supported for now"); | ||
} | ||
|
||
const resolvedNetworkConfig: HttpNetworkConfig = { | ||
type: "http", | ||
chainId: networkConfig.chainId, | ||
chainType: networkConfig.chainType, | ||
from: networkConfig.from, | ||
gas: resolveBigIntOrAuto(networkConfig.gas), | ||
gasMultiplier: networkConfig.gasMultiplier ?? 1, | ||
gasPrice: resolveBigIntOrAuto(networkConfig.gasPrice), | ||
url: networkConfig.url, | ||
timeout: networkConfig.timeout ?? 20_000, | ||
httpHeaders: networkConfig.httpHeaders ?? {}, | ||
}; | ||
|
||
resolvedNetworks[networkName] = resolvedNetworkConfig; | ||
} | ||
|
||
return { | ||
...resolvedConfig, | ||
defaultNetwork: resolvedConfig.defaultNetwork ?? "localhost", | ||
defaultChainType: resolvedConfig.defaultChainType ?? "unknown", | ||
networks: resolvedNetworks, | ||
}; | ||
}, | ||
}); |
16 changes: 16 additions & 0 deletions
16
v-next/hardhat/src/internal/builtin-plugins/network-manager/hook-handlers/hre.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
import type { HardhatRuntimeEnvironmentHooks } from "../../../../types/hooks.js"; | ||
|
||
import { NetworkManagerImplementation } from "../network-manager.js"; | ||
|
||
export default async (): Promise<Partial<HardhatRuntimeEnvironmentHooks>> => ({ | ||
created: async (context, hre) => { | ||
hre.network = new NetworkManagerImplementation( | ||
hre.globalOptions.network !== "" | ||
? hre.globalOptions.network | ||
: hre.config.defaultNetwork, | ||
hre.config.defaultChainType, | ||
hre.config.networks, | ||
context.hooks, | ||
); | ||
}, | ||
}); |
Oops, something went wrong.