revert-flags.ts 1011 Bytes
Newer Older
1
/* External Imports */
2
import { ethers } from 'hardhat'
Kelvin Fichter's avatar
Kelvin Fichter committed
3 4 5 6 7 8 9

export const encodeRevertData = (
  flag: number,
  data: string = '0x',
  nuisanceGasLeft: number = 0,
  ovmGasRefund: number = 0
): string => {
Kelvin Fichter's avatar
Kelvin Fichter committed
10
  const abiEncoded: string = ethers.utils.defaultAbiCoder.encode(
Kelvin Fichter's avatar
Kelvin Fichter committed
11
    ['uint256', 'uint256', 'uint256', 'bytes'],
Kelvin Fichter's avatar
Kelvin Fichter committed
12 13
    [flag, nuisanceGasLeft, ovmGasRefund, data]
  )
Kelvin Fichter's avatar
Kelvin Fichter committed
14 15 16
  return abiEncoded
}

Kelvin Fichter's avatar
Kelvin Fichter committed
17
export const decodeRevertData = (revertData: string): any => {
Kelvin Fichter's avatar
Kelvin Fichter committed
18
  const decoded = ethers.utils.defaultAbiCoder.decode(
Kelvin Fichter's avatar
Kelvin Fichter committed
19
    ['uint256', 'uint256', 'uint256', 'bytes'],
Kelvin Fichter's avatar
Kelvin Fichter committed
20 21
    revertData
  )
ben-chain's avatar
ben-chain committed
22

Kelvin Fichter's avatar
Kelvin Fichter committed
23 24 25 26 27 28 29 30 31 32 33
  return (
    '[revertFlag:' +
    Object.keys(REVERT_FLAGS)[decoded[0]] +
    ', nuisanceGasLeft:' +
    decoded[1] +
    ', ovmGasRefund: ' +
    decoded[2] +
    ', data: ' +
    decoded[3] +
    ']'
  )
Kelvin Fichter's avatar
Kelvin Fichter committed
34 35 36
}

export const REVERT_FLAGS = {
37 38 39 40 41 42 43 44
  OUT_OF_GAS: 0,
  INTENTIONAL_REVERT: 1,
  EXCEEDS_NUISANCE_GAS: 2,
  INVALID_STATE_ACCESS: 3,
  UNSAFE_BYTECODE: 4,
  CREATE_COLLISION: 5,
  STATIC_VIOLATION: 6,
  CREATOR_NOT_ALLOWED: 7,
Kelvin Fichter's avatar
Kelvin Fichter committed
45
}