From 41c94a88ebd6b3007422e0814e1bc70865163a20 Mon Sep 17 00:00:00 2001 From: bendanzhentan <455462586@qq.com> Date: Sat, 9 Mar 2024 10:56:15 +0800 Subject: [PATCH] feat: record onchain proven timestamp --- cmd/bot/run.go | 8 ++++++-- core/processor.go | 29 +++++++++++++++++++++++++++++ 2 files changed, 35 insertions(+), 2 deletions(-) diff --git a/cmd/bot/run.go b/cmd/bot/run.go index d4d2533..6dfe8a5 100644 --- a/cmd/bot/run.go +++ b/cmd/bot/run.go @@ -144,12 +144,16 @@ func ProcessUnprovenBotDelegatedWithdrawals(ctx context.Context, log log.Logger, continue } - now := time.Now() err := processor.ProveWithdrawalTransaction(ctx, &unproven, *currentNonce) if err != nil { if strings.Contains(err.Error(), "OptimismPortal: withdrawal hash has already been proven") { // The withdrawal has already proven, mark it - result := db.Model(&unproven).Update("proven_time", now) + provenTime, err := processor.GetProvenTime(&unproven) + if err != nil { + log.Error("fail to get proven time", "error", err) + } + + result := db.Model(&unproven).Update("proven_time", provenTime) if result.Error != nil { log.Error("failed to update proven withdrawals", "error", result.Error) } diff --git a/core/processor.go b/core/processor.go index b21cc72..c5c4332 100644 --- a/core/processor.go +++ b/core/processor.go @@ -242,6 +242,35 @@ func (b *Processor) FinalizeMessage(ctx context.Context, wi *WithdrawalInitiated return nil } +func (b *Processor) GetProvenTime(wi *WithdrawalInitiatedLog) (*big.Int, error) { + optimismPortal, err := bindings.NewOptimismPortalCaller(b.cfg.L1Contracts.OptimismPortalProxy, b.L1Client) + if err != nil { + return nil, err + } + + receipt, err := b.L1Client.TransactionReceipt(context.Background(), common.HexToHash(wi.TransactionHash)) + if err != nil { + return nil, err + } + + withdrawal, err := b.toWithdrawal(wi, receipt) + if err != nil { + return nil, err + } + + withdrawalHash, err := b.hashWithdrawal(withdrawal) + if err != nil { + return nil, err + } + + provenWithdrawal, err := optimismPortal.ProvenWithdrawals(nil, common.HexToHash(withdrawalHash)) + if err != nil { + return nil, err + } + + return provenWithdrawal.Timestamp, nil +} + func (b *Processor) hashWithdrawal(w *bindings.TypesWithdrawalTransaction) (string, error) { uint256Type, _ := abi.NewType("uint256", "", nil) addressType, _ := abi.NewType("address", "", nil)