-
Notifications
You must be signed in to change notification settings - Fork 185
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore: Add createProviderFactory to ua-utils-evm-hardhat
- Loading branch information
1 parent
f7bd9e5
commit 9617c33
Showing
2 changed files
with
36 additions
and
20 deletions.
There are no files selected for viewing
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 |
---|---|---|
@@ -1,25 +1,15 @@ | ||
import type { Web3Provider } from '@ethersproject/providers' | ||
import type { ProviderFactory } from '@layerzerolabs/utils-evm' | ||
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' | ||
import pMemoize from 'p-memoize' | ||
import { createNetworkEnvironmentFactory, wrapEIP1193Provider } from './runtime' | ||
|
||
export const createRpcUrlFactory = (hre: HardhatRuntimeEnvironment): RpcUrlFactory => { | ||
const networks = Object.values(hre.config.networks) | ||
const urlsByEndpointId: Map<EndpointId, string> = new Map( | ||
networks.flatMap((networkConfig) => { | ||
if (networkConfig.endpointId == null) return [] | ||
export const createProviderFactory = (hre: HardhatRuntimeEnvironment): ProviderFactory<Web3Provider> => { | ||
const networkEnvironmentFactory = createNetworkEnvironmentFactory(hre) | ||
|
||
return [[networkConfig.endpointId, networkConfig.url]] | ||
}) | ||
) | ||
return pMemoize(async (eid) => { | ||
const env = await networkEnvironmentFactory(eid) | ||
|
||
return async (eid) => { | ||
const url = urlsByEndpointId.get(eid) | ||
if (url == null) throw new Error(`Missing RPC URL for eid ${eid}`) | ||
|
||
return url | ||
} | ||
return wrapEIP1193Provider(env.network.provider) | ||
}) | ||
} | ||
|
||
export const createProviderFactory = (hre: HardhatRuntimeEnvironment): ProviderFactory => | ||
createProviderFactoryBase(createRpcUrlFactory(hre)) |
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,26 @@ | ||
import chai from 'chai' | ||
import chaiAsPromised from 'chai-as-promised' | ||
import { expect } from 'chai' | ||
import { getNetworkRuntimeEnvironment } from '../src/runtime' | ||
import hre from 'hardhat' | ||
import { EndpointId } from '@layerzerolabs/lz-definitions' | ||
import { createProviderFactory } from '../src/provider' | ||
import { Web3Provider } from '@ethersproject/providers' | ||
|
||
chai.use(chaiAsPromised) | ||
|
||
describe('provider', () => { | ||
describe('createProviderFactory', () => { | ||
it('should reject with an endpoint that is not in the hardhat config', async () => { | ||
await expect(createProviderFactory(hre)(EndpointId.CATHAY_TESTNET)).to.eventually.be.rejected | ||
}) | ||
|
||
it('should return a Web3Provider wrapping the network provider', async () => { | ||
const env = await getNetworkRuntimeEnvironment('ethereum-mainnet') | ||
const provider = await createProviderFactory(hre)(EndpointId.ETHEREUM_MAINNET) | ||
|
||
expect(provider).to.be.instanceOf(Web3Provider) | ||
expect(provider.provider).to.equal(env.network.provider) | ||
}) | ||
}) | ||
}) |