Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adds ViemSigner compatibility layer #294

Closed
wants to merge 39 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
39 commits
Select commit Hold shift + click to select a range
0816de9
Patches `EthBridger.from` to expand provider support
douglance Jun 12, 2023
bbc9844
Adds provider tests
douglance Jun 14, 2023
5c9f2f6
wip
douglance Jun 15, 2023
2df622a
wip
douglance Jun 16, 2023
5a3f349
wip
douglance Jun 16, 2023
af59b68
backports wagmi's viem implementation
douglance Jun 20, 2023
6ed17ab
feat: adds global config experimental flag
douglance Jul 6, 2023
a2e90fa
wip
douglance Jul 10, 2023
bee556f
reorganizes code adds temporary test script
douglance Oct 19, 2023
6936763
fix signing test
douglance Oct 23, 2023
ebec718
wip
douglance Oct 25, 2023
8f32f0d
remove extra stuff
douglance Oct 27, 2023
02e02ce
signs with custom class for viem
douglance Oct 30, 2023
7729774
signs with viem
douglance Oct 30, 2023
c78d2a0
improves compatibility for viem transaction receipt
douglance Oct 31, 2023
1efa6a8
adds more tests
douglance Nov 6, 2023
440c59e
fixes tests
douglance Nov 14, 2023
00201e4
wip
douglance Nov 17, 2023
a30b67e
wip
douglance Nov 28, 2023
8bb2f70
passes tests
douglance Nov 29, 2023
10cf84c
bump node version in ci
douglance Nov 29, 2023
3962cc0
fix builds
douglance Nov 29, 2023
2a51435
fix build
douglance Nov 30, 2023
f73a5fa
wip
douglance Dec 7, 2023
1794dfb
cleans up changes
douglance Dec 11, 2023
2ca1be6
fix test errors
douglance Dec 11, 2023
cf47b1e
attempts matrix of jobs
douglance Dec 11, 2023
6b63481
update tsconfig
douglance Dec 11, 2023
f6268d7
revert build script
douglance Dec 11, 2023
240a9f4
revert node version
douglance Dec 11, 2023
ab56329
add names for matrix tests
douglance Dec 12, 2023
baf2be0
fixes linting errors
douglance Dec 12, 2023
232bfcf
sets viem to latest
douglance Dec 12, 2023
116d764
updates lockfile
douglance Dec 12, 2023
a15bc80
simplifies implementation
douglance Dec 22, 2023
8d2671e
Merge branch 'main' into add-provider-map
douglance Dec 22, 2023
28400bf
moar cleanup and pr responses
douglance Dec 22, 2023
77290dd
adds unit tests
douglance Dec 28, 2023
ad78f32
removes launch.json
douglance Dec 28, 2023
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .env-sample
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,5 @@ GOERLI_RPC="https://goerli.infura.io/v3/2323"
GOERLI_ROLLUP_TESTNET_RPC="https://goerli-rollup.arbitrum.io/rpc"

NOVA_RPC="soontm"
# Swaps integration tests to use Viem signer instead of Ethers
SHOULD_USE_VIEM_SIGNER='0'
douglance marked this conversation as resolved.
Show resolved Hide resolved
9 changes: 8 additions & 1 deletion .github/workflows/build-test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -124,12 +124,19 @@ jobs:
run: CI=true yarn test:unit

test-integration:
name: Test (Integration)
name: Test (Integration) - ${{ matrix.description }}
runs-on: ubuntu-latest
strategy:
matrix:
node-version: [16, 18, 20]
include:
- viem_signer: '0'
description: 'Ethers v5'
- viem_signer: '1'
description: 'Viem'
needs: install
env:
SHOULD_USE_VIEM_SIGNER: ${{ matrix.viem_signer }}
steps:
- name: Checkout
uses: actions/checkout@v3
Expand Down
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
.DS_Store
.idea
.vscode
!.vscode/launch.json
*.ao
compiled.json
ci
Expand Down Expand Up @@ -313,4 +314,4 @@ packages/**/issues

docs
json_data
abi
abi
17 changes: 11 additions & 6 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -50,19 +50,21 @@
"@ethersproject/bignumber": "^5.1.1",
"@ethersproject/bytes": "^5.0.8",
"async-mutex": "^0.4.0",
"ethers": "^5.1.0"
"ethers": "^5.1.0",
"isomorphic-unfetch": "^4.0.2"
},
"devDependencies": {
"@arbitrum/nitro-contracts": "1.0.1",
"@nomiclabs/hardhat-ethers": "^2.0.4",
"@typechain/ethers-v5": "9.0.0",
"@types/chai": "^4.2.11",
"@types/mocha": "^9.0.0",
"@types/node-fetch": "^2.6.4",
"@types/prompts": "^2.0.14",
"@types/yargs": "^17.0.9",
"@typescript-eslint/eslint-plugin": "^5.14.0",
"@typescript-eslint/eslint-plugin-tslint": "^5.27.1",
"@typescript-eslint/parser": "^5.14.0",
"@typescript-eslint/eslint-plugin": "^6.14.0",
"@typescript-eslint/eslint-plugin-tslint": "^6.14.0",
"@typescript-eslint/parser": "^6.14.0",
"arb-bridge-peripherals": "1.0.10",
"audit-ci": "^6.3.0",
"axios": "^0.21.3",
Expand All @@ -73,6 +75,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.18.3",
"mocha": "^9.2.1",
"nyc": "^15.1.0",
Expand All @@ -85,10 +88,12 @@
"typechain": "7.0.0",
"typedoc": "^0.24.6",
"typedoc-plugin-markdown": "^3.15.3",
"typescript": "^4.2.2",
"typescript": "^5.3.2",
"viem": "^1.19.13",
"web3": "^4.0.1",
"yargs": "^17.3.1"
},
"files": [
"dist/**/*"
]
}
}
51 changes: 49 additions & 2 deletions scripts/testSetup.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
/* eslint-env node */
'use strict'

import 'isomorphic-unfetch'
import { JsonRpcProvider } from '@ethersproject/providers'
import { Wallet } from '@ethersproject/wallet'

Expand All @@ -37,6 +38,10 @@ import { deployErc20AndInit } from './deployBridge'
import * as path from 'path'
import * as fs from 'fs'
import { ArbSdkError } from '../src/lib/dataEntities/errors'
import { createWalletClient, http } from 'viem'
import { mainnet } from 'viem/chains'
import { privateKeyToAccount } from 'viem/accounts'
import { createViemSigner } from '../src/lib/utils/universal/signerTransforms'

dotenv.config()

Expand All @@ -45,6 +50,8 @@ export const config = {
ethUrl: process.env['ETH_URL'] as string,
arbKey: process.env['ARB_KEY'] as string,
ethKey: process.env['ETH_KEY'] as string,
shouldUseViemSigner:
(process.env['SHOULD_USE_VIEM_SIGNER'] as string) === '1',
}

function getDeploymentData(): string {
Expand Down Expand Up @@ -196,17 +203,33 @@ 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)
else return provider.getSigner(0)
}

export const ethLocal = {
...mainnet,
id: 1337,
rpcUrls: {
default: {
http: [config.ethUrl],
},
public: {
http: [config.ethUrl],
},
},
}

export const testSetup = async (): Promise<{
seed: Wallet
pk: any
l1Network: L1Network
l2Network: L2Network
l1Signer: Signer
ethersL1Signer: Signer
l2Signer: Signer
erc20Bridger: Erc20Bridger
ethBridger: EthBridger
Expand All @@ -222,9 +245,30 @@ export const testSetup = async (): Promise<{
const l2Deployer = getSigner(arbProvider, config.arbKey)

const seed = Wallet.createRandom()
const l1Signer = seed.connect(ethProvider)
const ethersL1Signer = seed.connect(ethProvider)
const l2Signer = seed.connect(arbProvider)

const pk = ethersL1Signer._signingKey().privateKey as `0x${string}`
const ethWalletClient = createWalletClient({
account: privateKeyToAccount(pk),
transport: http(config.ethUrl),
chain: {
...mainnet,
id: 1337,
rpcUrls: {
default: {
http: [config.ethUrl],
},
public: {
http: [config.ethUrl],
},
},
},
Comment on lines +255 to +266
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why wouldn't you use ethLocal here?

})
const l1Signer = config.shouldUseViemSigner
? createViemSigner(ethWalletClient)
: ethersL1Signer

let setL1Network: L1Network, setL2Network: L2Network
try {
const l1Network = await getL1Network(l1Deployer)
Expand Down Expand Up @@ -268,7 +312,10 @@ export const testSetup = async (): Promise<{
const inboxTools = new InboxTools(l1Signer, setL2Network)

return {
seed,
pk,
l1Signer,
ethersL1Signer,
l2Signer,
l1Network: setL1Network,
l2Network: setL2Network,
Expand Down
5 changes: 5 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,4 +62,9 @@ export {
RetryableDataTools,
} from './lib/dataEntities/retryableData'

export {
createViemSigner,
type ViemSigner,
} from './lib/utils/universal/signerTransforms'

export { Address } from './lib/dataEntities/address'
14 changes: 12 additions & 2 deletions src/lib/assetBridger/ethBridger.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,10 @@ import { OmitTyped } from '../utils/types'
import { SignerProviderUtils } from '../dataEntities/signerOrProvider'
import { MissingProviderArbSdkError } from '../dataEntities/errors'
import { getL2Network } from '../dataEntities/networks'
import {
Providerish,
transformUniversalProviderToEthersV5Provider,
} from '../utils/universal/providerTransforms'

export interface EthWithdrawParams {
/**
Expand Down Expand Up @@ -137,8 +141,14 @@ export class EthBridger extends AssetBridger<
* @param l2Provider
* @returns
*/
public static async fromProvider(l2Provider: Provider) {
return new EthBridger(await getL2Network(l2Provider))
public static async fromProvider(l2Provider: Provider | Providerish) {
if (l2Provider instanceof Provider) {
return new EthBridger(await getL2Network(l2Provider))
}
const ethersV5Provider = await transformUniversalProviderToEthersV5Provider(
l2Provider
)
return new EthBridger(await getL2Network(ethersV5Provider))
}

/**
Expand Down
25 changes: 23 additions & 2 deletions src/lib/dataEntities/retryableData.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { Interface } from '@ethersproject/abi'
import { BigNumber } from 'ethers'
import { BaseError } from 'viem'
import { isDefined } from '../utils/lib'

// TODO: add typechain support
const errorInterface = new Interface([
'error RetryableData(address from, address to, uint256 l2CallValue, uint256 deposit, uint256 maxSubmissionCost, address excessFeeRefundAddress, address callValueRefundAddress, uint256 gasLimit, uint256 maxFeePerGas, bytes data)',
Expand Down Expand Up @@ -112,8 +112,13 @@ export class RetryableDataTools {
* @returns
*/
public static tryParseError(
ethersJsErrorOrData: Error | { errorData: string } | string
ethersJsErrorOrData: Error | { errorData: string } | string | ViemError
): RetryableData | null {
if (isViemError(ethersJsErrorOrData)) {
return errorInterface.parseError(
ethersJsErrorOrData.cause.cause.cause.data
).args as unknown as RetryableData
}
const errorData =
typeof ethersJsErrorOrData === 'string'
? ethersJsErrorOrData
Expand All @@ -122,3 +127,19 @@ export class RetryableDataTools {
return errorInterface.parseError(errorData).args as unknown as RetryableData
}
}

interface ViemError extends BaseError {
cause: {
cause: {
cause: {
data: string
}
}
}
}

const isViemError = (err: any): err is ViemError =>
typeof err !== 'string' &&
'version' in err &&
typeof err.version === 'string' &&
err.version.includes('viem')
Loading