withdraw-fees.ts 2.65 KB
Newer Older
1 2 3 4 5 6
'use strict'

import { ethers } from 'ethers'
import { task } from 'hardhat/config'
import * as types from 'hardhat/internal/core/params/argumentTypes'
import { LedgerSigner } from '@ethersproject/hardware-wallets'
7

8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38
import { getContractFactory } from '../src/contract-defs'
import { predeploys } from '../src/predeploys'

// Withdraw fees from the FeeVault to L1
// npx hardhat withdraw-fees --dry-run
task('withdraw-fees')
  .addOptionalParam('dryRun', 'simulate withdrawing fees', false, types.boolean)
  .addOptionalParam(
    'useLedger',
    'use a ledger for signing',
    false,
    types.boolean
  )
  .addOptionalParam(
    'ledgerPath',
    'ledger key derivation path',
    ethers.utils.defaultPath,
    types.string
  )
  .addOptionalParam(
    'contractsRpcUrl',
    'Sequencer HTTP Endpoint',
    process.env.CONTRACTS_RPC_URL,
    types.string
  )
  .addOptionalParam(
    'privateKey',
    'Private Key',
    process.env.CONTRACTS_DEPLOYER_KEY,
    types.string
  )
39
  .setAction(async (args) => {
40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84
    const provider = new ethers.providers.JsonRpcProvider(args.contractsRpcUrl)
    let signer: ethers.Signer
    if (!args.useLedger) {
      if (!args.contractsDeployerKey) {
        throw new Error('Must pass --contracts-deployer-key')
      }
      signer = new ethers.Wallet(args.contractsDeployerKey).connect(provider)
    } else {
      signer = new LedgerSigner(provider, 'default', args.ledgerPath)
    }
    if (args.dryRun) {
      console.log('Performing dry run of fee withdrawal...')
    }

    const l2FeeVault = getContractFactory('OVM_SequencerFeeVault')
      .connect(signer)
      .attach(predeploys.OVM_SequencerFeeVault)

    const signerAddress = await signer.getAddress()
    const signerBalance = await provider.getBalance(signerAddress)
    const signerBalanceInETH = ethers.utils.formatEther(signerBalance)
    console.log(
      `Using L2 signer ${signerAddress} with a balance of ${signerBalanceInETH} ETH`
    )
    const l1FeeWallet = await l2FeeVault.l1FeeWallet()
    const amount = await provider.getBalance(l2FeeVault.address)
    const amountInETH = ethers.utils.formatEther(amount)
    console.log(
      `${
        args.dryRun ? '[DRY RUN] ' : ''
      }Withdrawing ${amountInETH} ETH to the L1 address: ${l1FeeWallet}`
    )
    if (args.dryRun) {
      await l2FeeVault.estimateGas.withdraw()
      return
    } else {
      const withdrawTx = await l2FeeVault.withdraw()
      console.log(
        `Withdrawal complete: https://optimistic.etherscan.io/tx/${withdrawTx.hash}`
      )
      console.log(
        `Complete withdrawal in 1 week here: https://optimistic.etherscan.io/address/${predeploys.OVM_SequencerFeeVault}#withdrawaltxs`
      )
    }
  })