Skip to content

Commit

Permalink
Adds provider tests
Browse files Browse the repository at this point in the history
  • Loading branch information
douglance committed Jun 14, 2023
1 parent 1ecd163 commit dc5f21e
Show file tree
Hide file tree
Showing 4 changed files with 82 additions and 1 deletion.
3 changes: 3 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@
"eslint-config-prettier": "^8.3.0",
"eslint-plugin-mocha": "^9.0.0",
"eslint-plugin-prettier": "^4.0.0",
"ethers-v6": "npm:ethers@^6.5.1",
"hardhat": "^2.8.3",
"mocha": "^9.2.1",
"nyc": "^15.1.0",
Expand All @@ -85,6 +86,8 @@
"typedoc": "^0.24.6",
"typedoc-plugin-markdown": "^3.15.3",
"typescript": "^4.2.2",
"viem": "^1.0.5",
"web3": "^4.0.1",
"yargs": "^17.3.1"
},
"files": [
Expand Down
7 changes: 6 additions & 1 deletion src/lib/assetBridger/ethBridger.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
import { Signer } from '@ethersproject/abstract-signer'
import { Provider } from '@ethersproject/abstract-provider'
import { PayableOverrides, Overrides } from '@ethersproject/contracts'
import { JsonRpcProvider } from '@ethersproject/providers'
import { JsonRpcProvider, WebSocketProvider } from '@ethersproject/providers'
import { BigNumber } from 'ethers'

import { Inbox__factory } from '../abi/factories/Inbox__factory'
Expand Down Expand Up @@ -149,6 +149,11 @@ export class EthBridger extends AssetBridger<
throw new Error('Unable to get URL from provider')
}

if (url.startsWith('ws')) {
const provider = new WebSocketProvider(url)
return new EthBridger(await getL2Network(provider))
}

try {
new URL(url)
} catch (_) {
Expand Down
11 changes: 11 additions & 0 deletions src/lib/utils/providerTransforms.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ export type Providerish = {
currentProvider?: { clientUrl?: unknown }
transport?: { url?: unknown }
connection?: { url?: unknown }
clientUrl?: unknown
_socketPath?: unknown
}

export const getEthersV5Url = (provider: Providerish) => {
Expand All @@ -28,6 +30,10 @@ export const getEthersV6Url = (provider: Providerish) => {
}

export const getWeb3Url = (provider: Providerish) => {
if ('clientUrl' in provider && typeof provider.clientUrl === 'string') {
const url = provider.clientUrl
return url
}
if (
'currentProvider' in provider &&
typeof provider.currentProvider === 'object' &&
Expand All @@ -39,6 +45,11 @@ export const getWeb3Url = (provider: Providerish) => {
return url
}
}
if ('_socketPath' in provider && typeof provider._socketPath === 'string') {
const url = provider._socketPath
return url
}

return undefined
}

Expand Down
62 changes: 62 additions & 0 deletions tests/integration/universalProvider.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
'use strict'

import { expect } from 'chai'
import { providers } from 'ethers'
import { createPublicClient, http } from 'viem'
import Web3 from 'web3'
import { config } from '../../scripts/testSetup'
import { EthBridger, addDefaultLocalNetwork } from '../../src'
import 'dotenv/config'

addDefaultLocalNetwork()

describe('provider', () => {
it('should convert viem public client to ethers provider', async () => {
const url = config.arbUrl

const publicClient = createPublicClient({ transport: http(url) })
const viemEthBridger = await EthBridger.fromProvider(publicClient)

const provider = new providers.StaticJsonRpcProvider(url)
const ethersEthBridger = await EthBridger.fromProvider(provider)

expect(viemEthBridger).to.be.deep.equal(ethersEthBridger)
})

it('should convert generic web3 provider to ethers provider', async () => {
const url = config.arbUrl

const l2Provider = new Web3(url)
//@ts-expect-error - TODO: update Providerish type
const web3EthBridger = await EthBridger.fromProvider(l2Provider)

const provider = new providers.StaticJsonRpcProvider(url)
const ethersEthBridger = await EthBridger.fromProvider(provider)

expect(web3EthBridger).to.be.deep.equal(ethersEthBridger)
})

it('should convert web3 HttpProvider to ethers provider', async () => {
const url = config.arbUrl

const l2Provider = new Web3.providers.HttpProvider(url)
//@ts-expect-error - TODO: update Providerish type
const web3EthBridger = await EthBridger.fromProvider(l2Provider)

const provider = new providers.StaticJsonRpcProvider(url)
const ethersEthBridger = await EthBridger.fromProvider(provider)

expect(web3EthBridger).to.be.deep.equal(ethersEthBridger)
})

it('should convert web3 WebSocket to ethers provider', async () => {
const url = 'ws://localhost:8548'

const l2Provider = new Web3.providers.WebsocketProvider(url)
//@ts-expect-error - TODO: update Providerish type
const web3EthBridger = await EthBridger.fromProvider(l2Provider)
const provider = new providers.WebSocketProvider(url)
const ethersEthBridger = await EthBridger.fromProvider(provider)
expect(web3EthBridger).to.be.deep.equal(ethersEthBridger)
})
})

0 comments on commit dc5f21e

Please sign in to comment.