From 41ac1ca9f4d6759fd0d2250ac1f1505e60429bd3 Mon Sep 17 00:00:00 2001 From: Jan Nanista Date: Mon, 27 Nov 2023 10:13:54 -0800 Subject: [PATCH] feat: Add hardhat wrapper for createProviderFactory --- packages/utils-evm-hardhat/package.json | 2 ++ packages/utils-evm-hardhat/src/index.ts | 1 + packages/utils-evm-hardhat/src/provider.ts | 25 ++++++++++++++++++++++ 3 files changed, 28 insertions(+) create mode 100644 packages/utils-evm-hardhat/src/provider.ts diff --git a/packages/utils-evm-hardhat/package.json b/packages/utils-evm-hardhat/package.json index 41f1e2c29..c6e20220d 100644 --- a/packages/utils-evm-hardhat/package.json +++ b/packages/utils-evm-hardhat/package.json @@ -46,6 +46,7 @@ "@ethersproject/wallet": "^5.7.0", "@layerzerolabs/lz-definitions": "~1.5.62", "@layerzerolabs/lz-evm-sdk-v1": "~1.5.62", + "@layerzerolabs/utils-evm": "~0.0.1", "@types/chai-as-promised": "^7.1.7", "@types/mocha": "^10.0.6", "chai": "^4.3.10", @@ -62,6 +63,7 @@ "@ethersproject/abstract-signer": "^5.7.0", "@ethersproject/providers": "^5.7.0", "@layerzerolabs/lz-definitions": "~1.5.62", + "@layerzerolabs/utils-evm": "~0.0.1", "hardhat": "^2.19.0", "hardhat-deploy": "^0.9.19" } diff --git a/packages/utils-evm-hardhat/src/index.ts b/packages/utils-evm-hardhat/src/index.ts index eebc0b211..f2f21cb31 100644 --- a/packages/utils-evm-hardhat/src/index.ts +++ b/packages/utils-evm-hardhat/src/index.ts @@ -4,5 +4,6 @@ import './type-extensions' // Regular exports export * from './config' export * from './logger' +export * from './provider' export * from './runtime' export * from './types' diff --git a/packages/utils-evm-hardhat/src/provider.ts b/packages/utils-evm-hardhat/src/provider.ts new file mode 100644 index 000000000..483396199 --- /dev/null +++ b/packages/utils-evm-hardhat/src/provider.ts @@ -0,0 +1,25 @@ +import type { HardhatRuntimeEnvironment } from 'hardhat/types' +import type { ProviderFactory, RpcUrlFactory } from '@layerzerolabs/utils-evm' +import { createProviderFactory as createProviderFactoryBase } from '@layerzerolabs/utils-evm' +import { EndpointId } from '@layerzerolabs/lz-definitions' + +export const createRpcUrlFactory = (hre: HardhatRuntimeEnvironment): RpcUrlFactory => { + const networks = Object.values(hre.config.networks) + const urlsByEndpointId: Map = new Map( + networks.flatMap((networkConfig) => { + if (networkConfig.endpointId == null) return [] + + return [[networkConfig.endpointId, networkConfig.url]] + }) + ) + + return async (eid) => { + const url = urlsByEndpointId.get(eid) + if (url == null) throw new Error(`Missing RPC URL for eid ${eid}`) + + return url + } +} + +export const createProviderFactory = (hre: HardhatRuntimeEnvironment): ProviderFactory => + createProviderFactoryBase(createRpcUrlFactory(hre))