Commit dbfea116 authored by smartcontracts's avatar smartcontracts Committed by GitHub

feat(core): use ethers submodules (#3363)

MetaMask requested that we use ethers submodules instead of importing
the entire ethers package. Relatively straightforward change.
Co-authored-by: default avatarmergify[bot] <37929162+mergify[bot]@users.noreply.github.com>
parent f4bf4f52
---
'@eth-optimism/core-utils': minor
---
Removes ethers as a dependency in favor of individual ethers sub-packages
...@@ -32,14 +32,22 @@ ...@@ -32,14 +32,22 @@
"url": "https://github.com/ethereum-optimism/optimism.git" "url": "https://github.com/ethereum-optimism/optimism.git"
}, },
"dependencies": { "dependencies": {
"@ethersproject/abi": "^5.6.3",
"@ethersproject/abstract-provider": "^5.6.1", "@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/providers": "^5.6.8",
"@ethersproject/rlp": "^5.6.1",
"@ethersproject/transactions": "^5.6.2", "@ethersproject/transactions": "^5.6.2",
"@ethersproject/properties": "^5.6.0", "@ethersproject/properties": "^5.6.0",
"@ethersproject/web": "^5.6.1", "@ethersproject/web": "^5.6.1",
"bufio": "^1.0.7", "bufio": "^1.0.7",
"chai": "^4.3.4", "chai": "^4.3.4"
"ethers": "^5.6.8"
}, },
"devDependencies": { "devDependencies": {
"mocha": "^10.0.0" "mocha": "^10.0.0"
......
import { ethers } from 'ethers' import { BigNumber } from '@ethersproject/bignumber'
import { getAddress } from '@ethersproject/address'
import { remove0x, add0x } from './hex-strings' import { remove0x, add0x } from './hex-strings'
...@@ -8,15 +9,15 @@ import { remove0x, add0x } from './hex-strings' ...@@ -8,15 +9,15 @@ import { remove0x, add0x } from './hex-strings'
* @param bn BigNumber to convert to an address. * @param bn BigNumber to convert to an address.
* @return BigNumber converted to an address, represented as a hex string. * @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. // 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. // 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 // 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). // addr(uint256(addr) - some_number) in Solidity where some_number > uint256(addr).
if (bn.isNegative()) { if (bn.isNegative()) {
bn = ethers.BigNumber.from('0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF') bn = BigNumber.from('0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF')
.add(bn) .add(bn)
.add(1) .add(1)
} }
...@@ -32,7 +33,7 @@ export const bnToAddress = (bn: ethers.BigNumber | number): string => { ...@@ -32,7 +33,7 @@ export const bnToAddress = (bn: ethers.BigNumber | number): string => {
// Add 0x again // Add 0x again
addr = add0x(addr) addr = add0x(addr)
// Convert into a checksummed address // Convert into a checksummed address
addr = ethers.utils.getAddress(addr) addr = getAddress(addr)
return addr return addr
} }
/* Imports: External */ /* 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. * Removes "0x" from start of a string if it exists.
...@@ -112,11 +113,11 @@ export const encodeHex = (val: any, len: number): string => ...@@ -112,11 +113,11 @@ export const encodeHex = (val: any, len: number): string =>
* @return True if equal * @return True if equal
*/ */
export const hexStringEquals = (stringA: string, stringB: string): boolean => { 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}`) 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}`) throw new Error(`input is not a hex string: ${stringB}`)
} }
...@@ -130,5 +131,5 @@ export const hexStringEquals = (stringA: string, stringB: string): boolean => { ...@@ -130,5 +131,5 @@ export const hexStringEquals = (stringA: string, stringB: string): boolean => {
* @return Number cast as a hex string. * @return Number cast as a hex string.
*/ */
export const bytes32ify = (value: number | BigNumber): string => { export const bytes32ify = (value: number | BigNumber): string => {
return ethers.utils.hexZeroPad(BigNumber.from(value).toHexString(), 32) return hexZeroPad(BigNumber.from(value).toHexString(), 32)
} }
import { expect } from 'chai' import { expect } from 'chai'
import { BigNumber } from 'ethers' import { BigNumber } from '@ethersproject/bignumber'
import { sleep } from './misc' import { sleep } from './misc'
......
...@@ -2,9 +2,12 @@ ...@@ -2,9 +2,12 @@
* Provider Utilities * Provider Utilities
*/ */
import { ethers } from 'ethers' import {
import { Provider } from '@ethersproject/providers' Provider,
import { ConnectionInfo } from 'ethers/lib/utils' StaticJsonRpcProvider,
FallbackProvider as EthersFallbackProvider,
} from '@ethersproject/providers'
import { ConnectionInfo } from '@ethersproject/web'
export interface HttpHeaders { export interface HttpHeaders {
[key: string]: string [key: string]: string
...@@ -44,11 +47,11 @@ export const FallbackProvider = ( ...@@ -44,11 +47,11 @@ export const FallbackProvider = (
} }
configs.push({ configs.push({
priority: i, 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)
} }
import { ethers } from 'ethers' import { isAddress } from '@ethersproject/address'
import { BigNumber } from '@ethersproject/bignumber'
import { bnToAddress } from '../common' import { bnToAddress } from '../common'
...@@ -18,11 +19,11 @@ export const L1_TO_L2_ALIAS_OFFSET = ...@@ -18,11 +19,11 @@ export const L1_TO_L2_ALIAS_OFFSET =
* @returns Address with the scheme applied. * @returns Address with the scheme applied.
*/ */
export const applyL1ToL2Alias = (address: string): string => { export const applyL1ToL2Alias = (address: string): string => {
if (!ethers.utils.isAddress(address)) { if (!isAddress(address)) {
throw new Error(`not a valid address: ${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))
} }
/** /**
...@@ -32,9 +33,9 @@ export const applyL1ToL2Alias = (address: string): string => { ...@@ -32,9 +33,9 @@ export const applyL1ToL2Alias = (address: string): string => {
* @returns Alias with the scheme reversed. * @returns Alias with the scheme reversed.
*/ */
export const undoL1ToL2Alias = (address: string): string => { export const undoL1ToL2Alias = (address: string): string => {
if (!ethers.utils.isAddress(address)) { if (!isAddress(address)) {
throw new Error(`not a valid address: ${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))
} }
import zlib from 'zlib' import zlib from 'zlib'
import { parse, serialize } from '@ethersproject/transactions' import { parse, serialize, Transaction } from '@ethersproject/transactions'
import { ethers } from 'ethers'
import { Struct, BufferWriter, BufferReader } from 'bufio' import { Struct, BufferWriter, BufferReader } from 'bufio'
import { id } from '@ethersproject/hash'
import { remove0x } from '../common' import { remove0x } from '../common'
...@@ -28,7 +28,7 @@ export interface AppendSequencerBatchParams { ...@@ -28,7 +28,7 @@ export interface AppendSequencerBatchParams {
const APPEND_SEQUENCER_BATCH_METHOD_ID = 'appendSequencerBatch()' const APPEND_SEQUENCER_BATCH_METHOD_ID = 'appendSequencerBatch()'
const FOUR_BYTE_APPEND_SEQUENCER_BATCH = Buffer.from( 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' 'hex'
) )
...@@ -166,9 +166,9 @@ export class BatchedTx extends Struct { ...@@ -166,9 +166,9 @@ export class BatchedTx extends Struct {
public txSize: number public txSize: number
// rlp encoded transaction // rlp encoded transaction
public raw: Buffer public raw: Buffer
public tx: ethers.Transaction public tx: Transaction
constructor(tx?: ethers.Transaction) { constructor(tx?: Transaction) {
super() super()
this.tx = tx this.tx = tx
} }
...@@ -210,7 +210,7 @@ export class BatchedTx extends Struct { ...@@ -210,7 +210,7 @@ export class BatchedTx extends Struct {
return this return this
} }
toTransaction(): ethers.Transaction { toTransaction(): Transaction {
if (this.tx) { if (this.tx) {
return this.tx return this.tx
} }
......
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 { import {
BigNumber, arrayify,
BigNumberish,
BytesLike, BytesLike,
ContractReceipt, hexDataSlice,
ethers, stripZeros,
Event, hexConcat,
utils, zeroPad,
} from 'ethers' } from '@ethersproject/bytes'
const { hexDataSlice, stripZeros, hexConcat, keccak256, zeroPad } = utils
const formatBoolean = (value: boolean): Uint8Array => { const formatBoolean = (value: boolean): Uint8Array => {
return value ? new Uint8Array([1]) : new Uint8Array([]) return value ? new Uint8Array([1]) : new Uint8Array([])
...@@ -34,7 +37,7 @@ const handleBoolean = (value: string): boolean => { ...@@ -34,7 +37,7 @@ const handleBoolean = (value: string): boolean => {
const handleNumber = (value: string): BigNumber => { const handleNumber = (value: string): BigNumber => {
if (value === '0x') { if (value === '0x') {
return ethers.constants.Zero return Zero
} }
return BigNumber.from(value) return BigNumber.from(value)
} }
...@@ -44,7 +47,7 @@ const handleAddress = (value: string): string => { ...@@ -44,7 +47,7 @@ const handleAddress = (value: string): string => {
// @ts-ignore // @ts-ignore
return null return null
} }
return utils.getAddress(value) return getAddress(value)
} }
export enum SourceHashDomain { export enum SourceHashDomain {
...@@ -142,8 +145,8 @@ export class DepositTx { ...@@ -142,8 +145,8 @@ export class DepositTx {
encode() { encode() {
const fields: any = [ const fields: any = [
this.sourceHash() || '0x', this.sourceHash() || '0x',
utils.getAddress(this.from) || '0x', getAddress(this.from) || '0x',
this.to != null ? utils.getAddress(this.to) : '0x', this.to != null ? getAddress(this.to) : '0x',
formatNumber(this.mint || 0, 'mint'), formatNumber(this.mint || 0, 'mint'),
formatNumber(this.value || 0, 'value'), formatNumber(this.value || 0, 'value'),
formatNumber(this.gas || 0, 'gas'), formatNumber(this.gas || 0, 'gas'),
...@@ -153,17 +156,17 @@ export class DepositTx { ...@@ -153,17 +156,17 @@ export class DepositTx {
return hexConcat([ return hexConcat([
BigNumber.from(this.type).toHexString(), BigNumber.from(this.type).toHexString(),
utils.RLP.encode(fields), RLP.encode(fields),
]) ])
} }
decode(raw: BytesLike, extra: DepositTxExtraOpts = {}) { decode(raw: BytesLike, extra: DepositTxExtraOpts = {}) {
const payload = utils.arrayify(raw) const payload = arrayify(raw)
if (payload[0] !== this.type) { if (payload[0] !== this.type) {
throw new Error(`Invalid type ${payload[0]}`) throw new Error(`Invalid type ${payload[0]}`)
} }
this.version = payload[1] this.version = payload[1]
const transaction = utils.RLP.decode(payload.slice(1)) const transaction = RLP.decode(payload.slice(1))
this._sourceHash = transaction[0] this._sourceHash = transaction[0]
this.from = handleAddress(transaction[1]) this.from = handleAddress(transaction[1])
this.to = handleAddress(transaction[2]) this.to = handleAddress(transaction[2])
......
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(address,address,bytes,uint256)',
'function relayMessage(uint256,address,address,uint256,uint256,bytes)', 'function relayMessage(uint256,address,address,uint256,uint256,bytes)',
]) ])
......
...@@ -2,7 +2,7 @@ ...@@ -2,7 +2,7 @@
* Fee related serialization and deserialization * Fee related serialization and deserialization
*/ */
import { BigNumber } from 'ethers' import { BigNumber } from '@ethersproject/bignumber'
import { remove0x } from '../common' import { remove0x } from '../common'
......
import { BigNumberish, BigNumber, utils } from 'ethers' import { BigNumberish, BigNumber } from '@ethersproject/bignumber'
const { keccak256, defaultAbiCoder } = utils import { keccak256 } from '@ethersproject/keccak256'
import { defaultAbiCoder } from '@ethersproject/abi'
import { import {
decodeVersionedNonce, decodeVersionedNonce,
......
import EventEmitter from 'events' import EventEmitter from 'events'
import { BigNumber } from 'ethers' import { BigNumber } from '@ethersproject/bignumber'
import { deepCopy } from '@ethersproject/properties' import { deepCopy } from '@ethersproject/properties'
import { ConnectionInfo, fetchJson } from '@ethersproject/web' import { ConnectionInfo, fetchJson } from '@ethersproject/web'
......
import './setup' import './setup'
import { BigNumber } from 'ethers' import { BigNumber } from '@ethersproject/bignumber'
import { zeroesAndOnes, calldataCost } from '../src' import { zeroesAndOnes, calldataCost } from '../src'
......
import { BigNumber } from 'ethers' import { BigNumber } from '@ethersproject/bignumber'
/* Imports: Internal */ /* Imports: Internal */
import { expect } from './setup' import { expect } from './setup'
......
This diff is collapsed.
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment