Skip to content

Instantly share code, notes, and snippets.

@mdehoog
Created September 25, 2023 23:48
Show Gist options
  • Save mdehoog/53c5d0037a1f6837aa8f6b0823582fc1 to your computer and use it in GitHub Desktop.
Save mdehoog/53c5d0037a1f6837aa8f6b0823582fc1 to your computer and use it in GitHub Desktop.
op-stack: calculate L2 tx hash from L1 deposit receipt
package main
import (
"context"
"errors"
"fmt"
"github.com/ethereum-optimism/optimism/op-node/rollup/derive"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/core/types"
"github.com/ethereum/go-ethereum/ethclient"
)
const l1Node = "https://goerli.gateway.tenderly.co"
const l2Node = "https://goerli.base.org"
func main() {
ctx := context.Background()
l1Client, err := ethclient.DialContext(ctx, l1Node)
if err != nil {
panic(err)
}
rec, err := l1Client.TransactionReceipt(ctx, common.HexToHash("0x8e622356d47ceee653522d6fb7c26d32dde5bcc7ac28007b8297402d0a6daf7d"))
if err != nil {
panic(err)
}
hash, err := ReceiptToHash(rec, common.HexToAddress("0xe93c8cd0d409341205a592f8c4ac1a5fe5585cfa"))
if err != nil {
panic(err)
}
l2Client, err := ethclient.DialContext(ctx, l2Node)
if err != nil {
panic(err)
}
tx, _, err := l2Client.TransactionByHash(ctx, hash)
if err != nil {
panic(err)
}
fmt.Printf("tx %s to %s\n", tx.Hash().String(), tx.To().String())
}
func ReceiptToHash(rec *types.Receipt, depositContractAddr common.Address) (common.Hash, error) {
if rec.Status != types.ReceiptStatusSuccessful {
return common.Hash{}, errors.New("unsuccessful receipt")
}
for _, log := range rec.Logs {
if log.Address == depositContractAddr && len(log.Topics) > 0 && log.Topics[0] == derive.DepositEventABIHash {
dep, err := derive.UnmarshalDepositLogEvent(log)
if err != nil {
return common.Hash{}, err
}
return types.NewTx(dep).Hash(), nil
}
}
return common.Hash{}, errors.New("no deposit logs found in receipt")
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment