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 @@
"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"
......
import { ethers } from 'ethers'
import { BigNumber } from '@ethersproject/bignumber'
import { getAddress } from '@ethersproject/address'
import { remove0x, add0x } from './hex-strings'
......@@ -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)
}
......@@ -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
}
/* 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.
......@@ -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}`)
}
......@@ -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)
}
import { expect } from 'chai'
import { BigNumber } from 'ethers'
import { BigNumber } from '@ethersproject/bignumber'
import { sleep } from './misc'
......
......@@ -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
......@@ -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)
}
import { ethers } from 'ethers'
import { isAddress } from '@ethersproject/address'
import { BigNumber } from '@ethersproject/bignumber'
import { bnToAddress } from '../common'
......@@ -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))
}
/**
......@@ -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))
}
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'
......@@ -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'
)
......@@ -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
}
......@@ -210,7 +210,7 @@ export class BatchedTx extends Struct {
return this
}
toTransaction(): ethers.Transaction {
toTransaction(): Transaction {
if (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 {
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([])
......@@ -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)
}
......@@ -44,7 +47,7 @@ const handleAddress = (value: string): string => {
// @ts-ignore
return null
}
return utils.getAddress(value)
return getAddress(value)
}
export enum SourceHashDomain {
......@@ -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'),
......@@ -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])
......
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)',
])
......
......@@ -2,7 +2,7 @@
* Fee related serialization and deserialization
*/
import { BigNumber } from 'ethers'
import { BigNumber } from '@ethersproject/bignumber'
import { remove0x } from '../common'
......
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,
......
import EventEmitter from 'events'
import { BigNumber } from 'ethers'
import { BigNumber } from '@ethersproject/bignumber'
import { deepCopy } from '@ethersproject/properties'
import { ConnectionInfo, fetchJson } from '@ethersproject/web'
......
import './setup'
import { BigNumber } from 'ethers'
import { BigNumber } from '@ethersproject/bignumber'
import { zeroesAndOnes, calldataCost } from '../src'
......
import { BigNumber } from 'ethers'
import { BigNumber } from '@ethersproject/bignumber'
/* Imports: Internal */
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