Skip to content

Commit

Permalink
chore: Better provider wrapping
Browse files Browse the repository at this point in the history
  • Loading branch information
janjakubnanista committed Dec 12, 2023
1 parent cc3b3f2 commit b41d1fe
Show file tree
Hide file tree
Showing 5 changed files with 22 additions and 16 deletions.
2 changes: 2 additions & 0 deletions packages/utils-evm-hardhat/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@
"@layerzerolabs/lz-evm-sdk-v1": "~1.5.72",
"@layerzerolabs/test-utils": "~0.0.1",
"@layerzerolabs/utils-evm": "~0.0.1",
"@nomiclabs/hardhat-ethers": "^2.2.3",
"@types/jest": "^29.5.10",
"fast-check": "^3.14.0",
"hardhat": "^2.19.2",
Expand All @@ -70,6 +71,7 @@
"@layerzerolabs/io-utils": "~0.0.1",
"@layerzerolabs/lz-definitions": "~1.5.72",
"@layerzerolabs/utils-evm": "~0.0.1",
"@nomiclabs/hardhat-ethers": "^2.2.3",
"hardhat": "^2.19.2",
"hardhat-deploy": "^0.11.43"
}
Expand Down
4 changes: 2 additions & 2 deletions packages/utils-evm-hardhat/src/provider.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type { Web3Provider } from '@ethersproject/providers'
import type { JsonRpcProvider } from '@ethersproject/providers'
import type { ProviderFactory } from '@layerzerolabs/utils-evm'
import type { HardhatRuntimeEnvironment } from 'hardhat/types'
import pMemoize from 'p-memoize'
Expand All @@ -7,7 +7,7 @@ import { createNetworkEnvironmentFactory, getDefaultRuntimeEnvironment, wrapEIP1
export const createProviderFactory = (
hre: HardhatRuntimeEnvironment = getDefaultRuntimeEnvironment(),
networkEnvironmentFactory = createNetworkEnvironmentFactory(hre)
): ProviderFactory<Web3Provider> => {
): ProviderFactory<JsonRpcProvider> => {
return pMemoize(async (eid) => {
const env = await networkEnvironmentFactory(eid)

Expand Down
13 changes: 7 additions & 6 deletions packages/utils-evm-hardhat/src/runtime.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
import type { HardhatRuntimeEnvironment, EIP1193Provider } from 'hardhat/types'
import type { HardhatRuntimeEnvironment, EthereumProvider } from 'hardhat/types'

import pMemoize from 'p-memoize'
import { Web3Provider } from '@ethersproject/providers'
import type { JsonRpcProvider } from '@ethersproject/providers'
import { ConfigurationError } from './errors'
import { HardhatContext } from 'hardhat/internal/context'
import { Environment as HardhatRuntimeEnvironmentImplementation } from 'hardhat/internal/core/runtime-environment'
import { EndpointId } from '@layerzerolabs/lz-definitions'
import { EndpointBasedFactory, formatEid } from '@layerzerolabs/utils'
import { EthersProviderWrapper } from '@nomiclabs/hardhat-ethers/internal/ethers-provider-wrapper'
import assert from 'assert'

/**
Expand Down Expand Up @@ -98,13 +99,13 @@ export const getNetworkRuntimeEnvironment: GetByNetwork<HardhatRuntimeEnvironmen
})

/**
* Helper function that wraps an EIP1193Provider with Web3Provider
* so that we can use it further with ethers
* Helper function that wraps an EthereumProvider with EthersProviderWrapper
* so that we can use it further with ethers as a regular JsonRpcProvider
*
* @param {EIP1193Provider} provider
* @returns {Web3Provider}
* @returns {JsonRpcProvider}
*/
export const wrapEIP1193Provider = (provider: EIP1193Provider): Web3Provider => new Web3Provider(provider)
export const wrapEIP1193Provider = (provider: EthereumProvider): JsonRpcProvider => new EthersProviderWrapper(provider)

/**
* Creates a factory function for creating HardhatRuntimeEnvironment
Expand Down
11 changes: 7 additions & 4 deletions packages/utils-evm-hardhat/test/provider.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@ import { getNetworkRuntimeEnvironment } from '@/runtime'
import hre from 'hardhat'
import { EndpointId } from '@layerzerolabs/lz-definitions'
import { createProviderFactory } from '@/provider'
import { Web3Provider } from '@ethersproject/providers'
import { JsonRpcProvider } from '@ethersproject/providers'

// Ethers calls the eth_chainId RPC method when initializing a provider so we mock the result
jest.spyOn(Web3Provider.prototype, 'send').mockResolvedValue('1')
jest.spyOn(JsonRpcProvider.prototype, 'detectNetwork').mockResolvedValue({ chainId: 1, name: 'mock' })

describe('provider', () => {
describe('createProviderFactory', () => {
Expand All @@ -17,8 +17,11 @@ describe('provider', () => {
const env = await getNetworkRuntimeEnvironment('ethereum-mainnet')
const provider = await createProviderFactory(hre)(EndpointId.ETHEREUM_MAINNET)

expect(provider).toBeInstanceOf(Web3Provider)
expect(provider.provider).toEqual(env.network.provider)
expect(provider).toBeInstanceOf(JsonRpcProvider)

// Here we're checking that the provider is configured with the correct network provider
jest.spyOn(env.network.provider, 'send').mockResolvedValue('sent')
expect(await provider.send('dummy', [])).toBe('sent')

// Ethers has this ugly habit of importing files here and there,
// firing RPC requests and all.
Expand Down
8 changes: 4 additions & 4 deletions packages/utils-evm-hardhat/test/signer.test.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import 'hardhat'
import { EndpointId } from '@layerzerolabs/lz-definitions'
import { createSignerFactory } from '@/signer/factory'
import { JsonRpcSigner, Web3Provider } from '@ethersproject/providers'
import { JsonRpcProvider, JsonRpcSigner } from '@ethersproject/providers'
import { OmniSignerEVM } from '@layerzerolabs/utils-evm'

// Ethers calls the eth_chainId RPC method when initializing a provider so we mock the result
jest.spyOn(Web3Provider.prototype, 'send').mockResolvedValue('1')
jest.spyOn(JsonRpcProvider.prototype, 'detectNetwork').mockResolvedValue({ chainId: 1, name: 'mock' })

describe('signer', () => {
describe('createSignerFactory', () => {
Expand All @@ -18,14 +18,14 @@ describe('signer', () => {

expect(signer).toBeInstanceOf(OmniSignerEVM)
expect(signer.signer).toBeInstanceOf(JsonRpcSigner)
expect(signer.signer.provider).toBeInstanceOf(Web3Provider)
expect(signer.signer.provider).toBeInstanceOf(JsonRpcProvider)

// Ethers has this ugly habit of importing files here and there,
// firing RPC requests and all.
//
// If we don't wait for the provider to be ready, jest will complain
// about requests being made after test teardown
await (signer.signer.provider as Web3Provider)?.ready
await (signer.signer.provider as JsonRpcProvider)?.ready
})
})
})

0 comments on commit b41d1fe

Please sign in to comment.