Skip to content

Commit

Permalink
Add support for calculating an L2 deposit hash from a deposit tx
Browse files Browse the repository at this point in the history
  • Loading branch information
mdehoog committed Oct 11, 2024
1 parent 5d7f6f4 commit c09c37d
Showing 1 changed file with 60 additions and 11 deletions.
71 changes: 60 additions & 11 deletions op-withdrawer/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import (
"os"
"time"

"github.com/ethereum-optimism/optimism/op-service/cliapp"
"github.com/ethereum-optimism/optimism/op-node/rollup/derive"
"github.com/ethereum-optimism/optimism/op-service/ctxinterrupt"
oplog "github.com/ethereum-optimism/optimism/op-service/log"
"github.com/ethereum/go-ethereum/accounts/abi/bind"
Expand Down Expand Up @@ -41,12 +41,6 @@ var (
EnvVars: []string{"PORTAL_ADDRESS"},
Required: true,
}
WithdrawalTxHashFlag = &cli.StringFlag{
Name: "withdrawal-tx-hash",
Usage: "Hash of the withdrawal transaction",
EnvVars: []string{"WITHDRAWAL_TX_HASH"},
Required: true,
}
PrivateKeyFlag = &cli.StringFlag{
Name: "private-key",
Usage: "Private key to sign the transaction",
Expand All @@ -59,18 +53,35 @@ var Flags = []cli.Flag{
L1URLFlag,
L2URLFlag,
PortalAddressFlag,
WithdrawalTxHashFlag,
PrivateKeyFlag,
}

func main() {
oplog.SetupDefaults()

app := cli.NewApp()
app.Flags = cliapp.ProtectFlags(Flags)
app.Name = "withdrawer"
app.Usage = "Withdraws funds from L2 to L1"
app.Action = Main
app.Action = func(c *cli.Context) error {
return cli.ShowAppHelp(c)
}
app.Commands = []*cli.Command{
{
Name: "depositHash",
Usage: "Calculate L2 deposit hash from L1 deposit tx",
Action: DepositHash,
Flags: []cli.Flag{
L1URLFlag,
PortalAddressFlag,
},
},
{
Name: "proveWithdrawal",
Usage: "Prove and finalize an L2 -> L1 withdrawal",
Action: Main,
Flags: Flags,
},
}

ctx := ctxinterrupt.WithSignalWaiterMain(context.Background())
err := app.RunContext(ctx, os.Args)
Expand All @@ -83,12 +94,16 @@ func Main(cliCtx *cli.Context) error {
l1URL := cliCtx.String(L1URLFlag.Name)
l2URL := cliCtx.String(L2URLFlag.Name)
portalAddress := common.HexToAddress(cliCtx.String(PortalAddressFlag.Name))
withdrawalTxHash := common.HexToHash(cliCtx.String(WithdrawalTxHashFlag.Name))
privateKey, err := crypto.HexToECDSA(cliCtx.String(PrivateKeyFlag.Name))
if err != nil {
return err
}

withdrawalTxHash := common.HexToHash(cliCtx.Args().First())
if (withdrawalTxHash == common.Hash{}) {
return fmt.Errorf("invalid withdrawal transaction hash")
}

ctx := context.Background()
l1, err := ethclient.DialContext(ctx, l1URL)
if err != nil {
Expand Down Expand Up @@ -151,3 +166,37 @@ func ProveWithdrawal(ctx context.Context, l1, l2 *ethclient.Client, l2g *gethcli
}
return withdrawals.WaitForReceipt(ctx, l1, tx.Hash(), pollInterval)
}

func DepositHash(cliCtx *cli.Context) error {
l1URL := cliCtx.String(L1URLFlag.Name)
portalAddr := common.HexToAddress(cliCtx.String(PortalAddressFlag.Name))

ctx := context.Background()
l1, err := ethclient.DialContext(ctx, l1URL)
if err != nil {
return err
}

depositTxHash := common.HexToHash(cliCtx.Args().First())
if (depositTxHash == common.Hash{}) {
return fmt.Errorf("invalid deposit transaction hash")
}

receipt, err := l1.TransactionReceipt(ctx, depositTxHash)
if err != nil {
return err
}

deposits, err := derive.UserDeposits([]*types.Receipt{receipt}, portalAddr)
if err != nil {
return err
}
if len(deposits) != 1 {
return fmt.Errorf("expected 1 deposit, got %d", len(deposits))
}

hash := types.NewTx(deposits[0]).Hash()
fmt.Println(hash.Hex())

return nil
}

0 comments on commit c09c37d

Please sign in to comment.