Skip to content

Commit

Permalink
feat(core): use ethers submodules (ethereum-optimism#3363)
Browse files Browse the repository at this point in the history
MetaMask requested that we use ethers submodules instead of importing
the entire ethers package. Relatively straightforward change.

Co-authored-by: mergify[bot] <37929162+mergify[bot]@users.noreply.github.com>
  • Loading branch information
smartcontracts and mergify[bot] committed Sep 9, 2022
1 parent f4bf4f5 commit dbfea11
Show file tree
Hide file tree
Showing 16 changed files with 270 additions and 53 deletions.
5 changes: 5 additions & 0 deletions .changeset/mighty-eagles-remember.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@eth-optimism/core-utils': minor
---

Removes ethers as a dependency in favor of individual ethers sub-packages
12 changes: 10 additions & 2 deletions packages/core-utils/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -32,14 +32,22 @@
"url": "https://github.com/ethereum-optimism/optimism.git"
},
"dependencies": {
"@ethersproject/abi": "^5.6.3",
"@ethersproject/abstract-provider": "^5.6.1",
"@ethersproject/address": "^5.6.1",
"@ethersproject/bignumber": "^5.6.1",
"@ethersproject/bytes": "^5.6.1",
"@ethersproject/contracts": "^5.6.2",
"@ethersproject/constants": "^5.6.1",
"@ethersproject/hash": "^5.6.1",
"@ethersproject/keccak256": "^5.6.1",
"@ethersproject/providers": "^5.6.8",
"@ethersproject/rlp": "^5.6.1",
"@ethersproject/transactions": "^5.6.2",
"@ethersproject/properties": "^5.6.0",
"@ethersproject/web": "^5.6.1",
"bufio": "^1.0.7",
"chai": "^4.3.4",
"ethers": "^5.6.8"
"chai": "^4.3.4"
},
"devDependencies": {
"mocha": "^10.0.0"
Expand Down
11 changes: 6 additions & 5 deletions packages/core-utils/src/common/bn.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { ethers } from 'ethers'
import { BigNumber } from '@ethersproject/bignumber'
import { getAddress } from '@ethersproject/address'

import { remove0x, add0x } from './hex-strings'

Expand All @@ -8,15 +9,15 @@ import { remove0x, add0x } from './hex-strings'
* @param bn BigNumber to convert to an address.
* @return BigNumber converted to an address, represented as a hex string.
*/
export const bnToAddress = (bn: ethers.BigNumber | number): string => {
export const bnToAddress = (bn: BigNumber | number): string => {
// Coerce numbers into a BigNumber.
bn = ethers.BigNumber.from(bn)
bn = BigNumber.from(bn)

// Negative numbers are converted to addresses by adding MAX_ADDRESS + 1.
// TODO: Explain this in more detail, it's basically just matching the behavior of doing
// addr(uint256(addr) - some_number) in Solidity where some_number > uint256(addr).
if (bn.isNegative()) {
bn = ethers.BigNumber.from('0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF')
bn = BigNumber.from('0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF')
.add(bn)
.add(1)
}
Expand All @@ -32,7 +33,7 @@ export const bnToAddress = (bn: ethers.BigNumber | number): string => {
// Add 0x again
addr = add0x(addr)
// Convert into a checksummed address
addr = ethers.utils.getAddress(addr)
addr = getAddress(addr)

return addr
}
9 changes: 5 additions & 4 deletions packages/core-utils/src/common/hex-strings.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
/* Imports: External */
import { BigNumber, ethers } from 'ethers'
import { BigNumber } from '@ethersproject/bignumber'
import { isHexString, hexZeroPad } from '@ethersproject/bytes'

/**
* Removes "0x" from start of a string if it exists.
Expand Down Expand Up @@ -112,11 +113,11 @@ export const encodeHex = (val: any, len: number): string =>
* @return True if equal
*/
export const hexStringEquals = (stringA: string, stringB: string): boolean => {
if (!ethers.utils.isHexString(stringA)) {
if (!isHexString(stringA)) {
throw new Error(`input is not a hex string: ${stringA}`)
}

if (!ethers.utils.isHexString(stringB)) {
if (!isHexString(stringB)) {
throw new Error(`input is not a hex string: ${stringB}`)
}

Expand All @@ -130,5 +131,5 @@ export const hexStringEquals = (stringA: string, stringB: string): boolean => {
* @return Number cast as a hex string.
*/
export const bytes32ify = (value: number | BigNumber): string => {
return ethers.utils.hexZeroPad(BigNumber.from(value).toHexString(), 32)
return hexZeroPad(BigNumber.from(value).toHexString(), 32)
}
2 changes: 1 addition & 1 deletion packages/core-utils/src/common/test-utils.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { expect } from 'chai'
import { BigNumber } from 'ethers'
import { BigNumber } from '@ethersproject/bignumber'

import { sleep } from './misc'

Expand Down
15 changes: 9 additions & 6 deletions packages/core-utils/src/external/ethers/fallback-provider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,12 @@
* Provider Utilities
*/

import { ethers } from 'ethers'
import { Provider } from '@ethersproject/providers'
import { ConnectionInfo } from 'ethers/lib/utils'
import {
Provider,
StaticJsonRpcProvider,
FallbackProvider as EthersFallbackProvider,
} from '@ethersproject/providers'
import { ConnectionInfo } from '@ethersproject/web'

export interface HttpHeaders {
[key: string]: string
Expand Down Expand Up @@ -44,11 +47,11 @@ export const FallbackProvider = (
}
configs.push({
priority: i,
provider: new ethers.providers.StaticJsonRpcProvider(connectionInfo),
provider: new StaticJsonRpcProvider(connectionInfo),
})
}
return new ethers.providers.FallbackProvider(configs)
return new EthersFallbackProvider(configs)
}

return new ethers.providers.FallbackProvider(config)
return new EthersFallbackProvider(config)
}
11 changes: 6 additions & 5 deletions packages/core-utils/src/optimism/alias.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { ethers } from 'ethers'
import { isAddress } from '@ethersproject/address'
import { BigNumber } from '@ethersproject/bignumber'

import { bnToAddress } from '../common'

Expand All @@ -18,11 +19,11 @@ export const L1_TO_L2_ALIAS_OFFSET =
* @returns Address with the scheme applied.
*/
export const applyL1ToL2Alias = (address: string): string => {
if (!ethers.utils.isAddress(address)) {
if (!isAddress(address)) {
throw new Error(`not a valid address: ${address}`)
}

return bnToAddress(ethers.BigNumber.from(address).add(L1_TO_L2_ALIAS_OFFSET))
return bnToAddress(BigNumber.from(address).add(L1_TO_L2_ALIAS_OFFSET))
}

/**
Expand All @@ -32,9 +33,9 @@ export const applyL1ToL2Alias = (address: string): string => {
* @returns Alias with the scheme reversed.
*/
export const undoL1ToL2Alias = (address: string): string => {
if (!ethers.utils.isAddress(address)) {
if (!isAddress(address)) {
throw new Error(`not a valid address: ${address}`)
}

return bnToAddress(ethers.BigNumber.from(address).sub(L1_TO_L2_ALIAS_OFFSET))
return bnToAddress(BigNumber.from(address).sub(L1_TO_L2_ALIAS_OFFSET))
}
12 changes: 6 additions & 6 deletions packages/core-utils/src/optimism/batch-encoding.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import zlib from 'zlib'

import { parse, serialize } from '@ethersproject/transactions'
import { ethers } from 'ethers'
import { parse, serialize, Transaction } from '@ethersproject/transactions'
import { Struct, BufferWriter, BufferReader } from 'bufio'
import { id } from '@ethersproject/hash'

import { remove0x } from '../common'

Expand All @@ -28,7 +28,7 @@ export interface AppendSequencerBatchParams {

const APPEND_SEQUENCER_BATCH_METHOD_ID = 'appendSequencerBatch()'
const FOUR_BYTE_APPEND_SEQUENCER_BATCH = Buffer.from(
ethers.utils.id(APPEND_SEQUENCER_BATCH_METHOD_ID).slice(2, 10),
id(APPEND_SEQUENCER_BATCH_METHOD_ID).slice(2, 10),
'hex'
)

Expand Down Expand Up @@ -166,9 +166,9 @@ export class BatchedTx extends Struct {
public txSize: number
// rlp encoded transaction
public raw: Buffer
public tx: ethers.Transaction
public tx: Transaction

constructor(tx?: ethers.Transaction) {
constructor(tx?: Transaction) {
super()
this.tx = tx
}
Expand Down Expand Up @@ -210,7 +210,7 @@ export class BatchedTx extends Struct {
return this
}

toTransaction(): ethers.Transaction {
toTransaction(): Transaction {
if (this.tx) {
return this.tx
}
Expand Down
35 changes: 19 additions & 16 deletions packages/core-utils/src/optimism/deposit-transaction.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,17 @@
import { getAddress } from '@ethersproject/address'
import { ContractReceipt, Event } from '@ethersproject/contracts'
import { BigNumber, BigNumberish } from '@ethersproject/bignumber'
import { keccak256 } from '@ethersproject/keccak256'
import { Zero } from '@ethersproject/constants'
import * as RLP from '@ethersproject/rlp'
import {
BigNumber,
BigNumberish,
arrayify,
BytesLike,
ContractReceipt,
ethers,
Event,
utils,
} from 'ethers'

const { hexDataSlice, stripZeros, hexConcat, keccak256, zeroPad } = utils
hexDataSlice,
stripZeros,
hexConcat,
zeroPad,
} from '@ethersproject/bytes'

const formatBoolean = (value: boolean): Uint8Array => {
return value ? new Uint8Array([1]) : new Uint8Array([])
Expand All @@ -34,7 +37,7 @@ const handleBoolean = (value: string): boolean => {

const handleNumber = (value: string): BigNumber => {
if (value === '0x') {
return ethers.constants.Zero
return Zero
}
return BigNumber.from(value)
}
Expand All @@ -44,7 +47,7 @@ const handleAddress = (value: string): string => {
// @ts-ignore
return null
}
return utils.getAddress(value)
return getAddress(value)
}

export enum SourceHashDomain {
Expand Down Expand Up @@ -142,8 +145,8 @@ export class DepositTx {
encode() {
const fields: any = [
this.sourceHash() || '0x',
utils.getAddress(this.from) || '0x',
this.to != null ? utils.getAddress(this.to) : '0x',
getAddress(this.from) || '0x',
this.to != null ? getAddress(this.to) : '0x',
formatNumber(this.mint || 0, 'mint'),
formatNumber(this.value || 0, 'value'),
formatNumber(this.gas || 0, 'gas'),
Expand All @@ -153,17 +156,17 @@ export class DepositTx {

return hexConcat([
BigNumber.from(this.type).toHexString(),
utils.RLP.encode(fields),
RLP.encode(fields),
])
}

decode(raw: BytesLike, extra: DepositTxExtraOpts = {}) {
const payload = utils.arrayify(raw)
const payload = arrayify(raw)
if (payload[0] !== this.type) {
throw new Error(`Invalid type ${payload[0]}`)
}
this.version = payload[1]
const transaction = utils.RLP.decode(payload.slice(1))
const transaction = RLP.decode(payload.slice(1))
this._sourceHash = transaction[0]
this.from = handleAddress(transaction[1])
this.to = handleAddress(transaction[2])
Expand Down
5 changes: 3 additions & 2 deletions packages/core-utils/src/optimism/encoding.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { ethers, BigNumberish, BigNumber } from 'ethers'
import { BigNumberish, BigNumber } from '@ethersproject/bignumber'
import { Interface } from '@ethersproject/abi'

const iface = new ethers.utils.Interface([
const iface = new Interface([
'function relayMessage(address,address,bytes,uint256)',
'function relayMessage(uint256,address,address,uint256,uint256,bytes)',
])
Expand Down
2 changes: 1 addition & 1 deletion packages/core-utils/src/optimism/fees.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
* Fee related serialization and deserialization
*/

import { BigNumber } from 'ethers'
import { BigNumber } from '@ethersproject/bignumber'

import { remove0x } from '../common'

Expand Down
5 changes: 3 additions & 2 deletions packages/core-utils/src/optimism/hashing.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { BigNumberish, BigNumber, utils } from 'ethers'
const { keccak256, defaultAbiCoder } = utils
import { BigNumberish, BigNumber } from '@ethersproject/bignumber'
import { keccak256 } from '@ethersproject/keccak256'
import { defaultAbiCoder } from '@ethersproject/abi'

import {
decodeVersionedNonce,
Expand Down
2 changes: 1 addition & 1 deletion packages/core-utils/src/optimism/op-provider.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import EventEmitter from 'events'

import { BigNumber } from 'ethers'
import { BigNumber } from '@ethersproject/bignumber'
import { deepCopy } from '@ethersproject/properties'
import { ConnectionInfo, fetchJson } from '@ethersproject/web'

Expand Down
2 changes: 1 addition & 1 deletion packages/core-utils/test/fees.spec.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import './setup'

import { BigNumber } from 'ethers'
import { BigNumber } from '@ethersproject/bignumber'

import { zeroesAndOnes, calldataCost } from '../src'

Expand Down
2 changes: 1 addition & 1 deletion packages/core-utils/test/hex-utils.spec.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { BigNumber } from 'ethers'
import { BigNumber } from '@ethersproject/bignumber'

/* Imports: Internal */
import { expect } from './setup'
Expand Down
Loading

0 comments on commit dbfea11

Please sign in to comment.