Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
douglance committed Oct 19, 2023
1 parent 6ed17ab commit a2e90fa
Show file tree
Hide file tree
Showing 5 changed files with 74 additions and 12 deletions.
2 changes: 2 additions & 0 deletions .vscode/launch.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
"type": "node",
"request": "launch",
"name": "Run Unit Tests",
"runtimeVersion": "16.16.0",
"runtimeExecutable": "npm",
"runtimeArgs": [
"run-script",
Expand All @@ -37,6 +38,7 @@
"type": "node",
"request": "launch",
"name": "Run Integration Tests",
"runtimeVersion": "16.16.0",
"runtimeExecutable": "npm",
"runtimeArgs": [
"run-script",
Expand Down
37 changes: 37 additions & 0 deletions scripts/setupViemProvider.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
import { createPublicClient, http } from 'viem'
import { arbitrumGoerli, localhost } from 'viem/chains'

export const setupViemEthProvider = ({ ethUrl }: { ethUrl: string }) => {
const provider = createPublicClient({
chain: localhost,
transport: http(ethUrl),
}) as any
return provider
}
const arbLocalhost = {
...arbitrumGoerli,
id: 412346,
name: 'Arbitrum Goerli',
network: 'arbitrum-localhost',
nativeCurrency: {
decimals: 18,
name: 'Ether',
symbol: 'ETH',
},
rpcUrls: {
default: {
http: ['http://127.0.0.1:8547'],
},
public: {
http: ['http://127.0.0.1:8547'],
},
},
}

export const setupViemArbProvider = ({ arbUrl }: { arbUrl: string }) => {
const provider = createPublicClient({
chain: arbLocalhost,
transport: http(arbUrl),
}) as any
return provider
}
25 changes: 21 additions & 4 deletions scripts/testSetup.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { transformUniversalProviderToEthersV5Provider } from './../src/lib/utils/providerTransforms'
/*
* Copyright 2021, Offchain Labs, Inc.
*
Expand All @@ -14,7 +15,7 @@
* limitations under the License.
*/
/* eslint-env node */
'use strict'
;('use strict')

import { JsonRpcProvider } from '@ethersproject/providers'
import { Wallet } from '@ethersproject/wallet'
Expand All @@ -37,6 +38,7 @@ import { deployErc20AndInit } from './deployBridge'
import * as path from 'path'
import * as fs from 'fs'
import { ArbSdkError } from '../src/lib/dataEntities/errors'
import { setupViemArbProvider, setupViemEthProvider } from './setupViemProvider'

dotenv.config()

Expand All @@ -45,6 +47,7 @@ export const config = {
ethUrl: process.env['ETH_URL'] as string,
arbKey: process.env['ARB_KEY'] as string,
ethKey: process.env['ETH_KEY'] as string,
experimentalFeaturesEnabled: process.env['EXPERIMENTAL_FEATURES'] === 'true',
}

function getDeploymentData(): string {
Expand Down Expand Up @@ -196,7 +199,7 @@ export const setupNetworks = async (
}
}

export const getSigner = (provider: JsonRpcProvider, key?: string) => {
export const getSigner = (provider: any, key?: string) => {
if (!key && !provider)
throw new ArbSdkError('Provide at least one of key or provider.')
if (key) return new Wallet(key).connect(provider)
Expand All @@ -215,8 +218,22 @@ export const testSetup = async (): Promise<{
l1Deployer: Signer
l2Deployer: Signer
}> => {
const ethProvider = new JsonRpcProvider(config.ethUrl)
const arbProvider = new JsonRpcProvider(config.arbUrl)
let ethProvider
let arbProvider

if (config.experimentalFeaturesEnabled) {
console.log('Experimental features enabled')
ethProvider = await transformUniversalProviderToEthersV5Provider(
setupViemEthProvider(config)
)

arbProvider = await transformUniversalProviderToEthersV5Provider(
setupViemArbProvider(config)
)
} else {
ethProvider = new JsonRpcProvider(config.ethUrl)
arbProvider = new JsonRpcProvider(config.arbUrl)
}

const l1Deployer = getSigner(ethProvider, config.ethKey)
const l2Deployer = getSigner(arbProvider, config.arbKey)
Expand Down
14 changes: 8 additions & 6 deletions src/lib/assetBridger/ethBridger.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,9 +48,9 @@ import { MissingProviderArbSdkError } from '../dataEntities/errors'
import { getL2Network } from '../dataEntities/networks'
import {
Providerish,
getProviderUrl,
transformUniversalProviderToEthersV5Provider,
} from '../utils/providerTransforms'
import { experimentalFeaturesEnabled } from '../utils/globalConfig'

export interface EthWithdrawParams {
/**
Expand Down Expand Up @@ -146,11 +146,13 @@ export class EthBridger extends AssetBridger<
if (l2Provider instanceof Provider) {
return new EthBridger(await getL2Network(l2Provider))
}

const ethersV5Provider = await transformUniversalProviderToEthersV5Provider(
l2Provider
)
return new EthBridger(await getL2Network(ethersV5Provider))
if (experimentalFeaturesEnabled()) {
const ethersV5Provider =
await transformUniversalProviderToEthersV5Provider(l2Provider)
return new EthBridger(await getL2Network(ethersV5Provider))
} else {
throw new MissingProviderArbSdkError('l2Provider')
}
}

/**
Expand Down
8 changes: 6 additions & 2 deletions tests/integration/universalProvider.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,17 @@ import { JsonRpcProvider } from 'ethers-v6'
import { createPublicClient, http } from 'viem'
import Web3 from 'web3'
import { config } from '../../scripts/testSetup'
import { EthBridger, addDefaultLocalNetwork } from '../../src'
import {
EthBridger,
addDefaultLocalNetwork,
enableExperimentalFeatures,
} from '../../src'
import 'dotenv/config'
import { arbitrumGoerli } from 'viem/chains'

addDefaultLocalNetwork()
const defaultUrl = config.arbUrl

enableExperimentalFeatures()
describe('provider', () => {
it('should convert viem public client to ethers-v5 provider', async () => {
// TODO: Fix arb goerli url to use local rpc
Expand Down

0 comments on commit a2e90fa

Please sign in to comment.