message-utils.ts 1.58 KB
Newer Older
1
import { hashWithdrawal } from '@eth-optimism/core-utils'
Will Cory's avatar
Will Cory committed
2
import { BigNumber, utils, ethers } from 'ethers'
3 4 5

import { LowLevelMessage } from '../interfaces'

6 7
const { hexDataLength } = utils

8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
/**
 * Utility for hashing a LowLevelMessage object.
 *
 * @param message LowLevelMessage object to hash.
 * @returns Hash of the given LowLevelMessage.
 */
export const hashLowLevelMessage = (message: LowLevelMessage): string => {
  return hashWithdrawal(
    message.messageNonce,
    message.sender,
    message.target,
    message.value,
    message.minGasLimit,
    message.message
  )
}
24

Will Cory's avatar
Will Cory committed
25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40
/**
 * Utility for hashing a message hash. This computes the storage slot
 * where the message hash will be stored in state. HashZero is used
 * because the first mapping in the contract is used.
 *
 * @param messageHash Message hash to hash.
 * @returns Hash of the given message hash.
 */
export const hashMessageHash = (messageHash: string): string => {
  const data = ethers.utils.defaultAbiCoder.encode(
    ['bytes32', 'uint256'],
    [messageHash, ethers.constants.HashZero]
  )
  return ethers.utils.keccak256(data)
}

41 42 43
/**
 * Compute the min gas limit for a migrated withdrawal.
 */
44 45 46 47
export const migratedWithdrawalGasLimit = (
  data: string,
  chainID: number
): BigNumber => {
48
  // Compute the gas limit and cap at 25 million
49
  const dataCost = BigNumber.from(hexDataLength(data)).mul(16)
50 51 52 53 54
  let overhead = 200_000
  if (chainID !== 420) {
    overhead = 1_000_000
  }
  let minGasLimit = dataCost.add(overhead)
55 56 57 58 59
  if (minGasLimit.gt(25_000_000)) {
    minGasLimit = BigNumber.from(25_000_000)
  }
  return minGasLimit
}