From bda318195c9ace6ca227cbe57d80004c7e87c552 Mon Sep 17 00:00:00 2001 From: vro <168573323+golangisfun123@users.noreply.github.com> Date: Tue, 23 Jul 2024 09:15:43 -0500 Subject: [PATCH] fix(opbot): fix opbot txage for l2s (#2906) * fix txage for l2s * [goreleaser] * fix types [goreleaser] --- contrib/opbot/botmd/commands.go | 24 ++++++++++-------------- contrib/opbot/botmd/commands_test.go | 28 +++++++++++++++++++++++++++- contrib/opbot/botmd/export_test.go | 11 +++++++++++ 3 files changed, 48 insertions(+), 15 deletions(-) diff --git a/contrib/opbot/botmd/commands.go b/contrib/opbot/botmd/commands.go index 84f45f161e..62d5d5e164 100644 --- a/contrib/opbot/botmd/commands.go +++ b/contrib/opbot/botmd/commands.go @@ -22,6 +22,7 @@ import ( "github.com/slack-io/slacker" "github.com/synapsecns/sanguine/contrib/opbot/signoz" "github.com/synapsecns/sanguine/ethergo/chaindata" + "github.com/synapsecns/sanguine/ethergo/client" rfqClient "github.com/synapsecns/sanguine/services/rfq/api/client" "github.com/synapsecns/sanguine/services/rfq/contracts/fastbridge" "github.com/synapsecns/sanguine/services/rfq/relayer/relapi" @@ -209,9 +210,9 @@ func (b *Bot) rfqLookupCommand() *slacker.CommandDefinition { var slackBlocks []slack.Block for _, status := range statuses { - time, err := b.getTxAge(ctx, status.GetQuoteRequestStatusResponse) + client, err := b.rpcClient.GetChainClient(ctx.Context(), int(status.OriginChainID)) if err != nil { - log.Printf("error getting tx age: %v\n", err) + log.Printf("error getting chain client: %v\n", err) } objects := []*slack.TextBlockObject{ @@ -233,7 +234,7 @@ func (b *Bot) rfqLookupCommand() *slacker.CommandDefinition { }, { Type: slack.MarkdownType, - Text: fmt.Sprintf("*Estimated Tx Age*: %s", humanize.Time(time)), + Text: fmt.Sprintf("*Estimated Tx Age*: %s", getTxAge(ctx.Context(), client, status.GetQuoteRequestStatusResponse)), }, } @@ -347,23 +348,18 @@ func (b *Bot) makeFastBridge(ctx context.Context, req *relapi.GetQuoteRequestRes return fastBridgeHandle, nil } -func (b *Bot) getTxAge(ctx *slacker.CommandContext, status *relapi.GetQuoteRequestStatusResponse) (time.Time, error) { +func getTxAge(ctx context.Context, client client.EVM, res *relapi.GetQuoteRequestStatusResponse) string { // TODO: add CreatedAt field to GetQuoteRequestStatusResponse so we don't need to make network calls? - client, err := b.rpcClient.GetChainClient(ctx.Context(), int(status.OriginChainID)) + receipt, err := client.TransactionReceipt(ctx, common.HexToHash(res.OriginTxHash)) if err != nil { - return time.Time{}, fmt.Errorf("error getting chain client: %w", err) + return "unknown time ago" } - - receipt, err := client.TransactionReceipt(ctx.Context(), common.HexToHash(status.OriginTxHash)) - if err != nil { - return time.Time{}, fmt.Errorf("error fetching transaction receipt: %w", err) - } - txBlock, err := client.BlockByHash(ctx.Context(), receipt.BlockHash) + txBlock, err := client.HeaderByHash(ctx, receipt.BlockHash) if err != nil { - return time.Time{}, fmt.Errorf("error fetching block by hash: %w", err) + return "unknown time ago" } - return time.Unix(int64(txBlock.Time()), 0), nil + return humanize.Time(time.Unix(int64(txBlock.Time), 0)) } func toExplorerSlackLink(ogHash string) string { diff --git a/contrib/opbot/botmd/commands_test.go b/contrib/opbot/botmd/commands_test.go index 402ab3a4a0..26784aa7a0 100644 --- a/contrib/opbot/botmd/commands_test.go +++ b/contrib/opbot/botmd/commands_test.go @@ -1,8 +1,13 @@ package botmd_test import ( - "github.com/synapsecns/sanguine/contrib/opbot/botmd" + "context" "testing" + + "github.com/synapsecns/sanguine/contrib/opbot/botmd" + "github.com/synapsecns/sanguine/core/metrics" + omnirpcClient "github.com/synapsecns/sanguine/services/omnirpc/client" + "github.com/synapsecns/sanguine/services/rfq/relayer/relapi" ) func TestStripLinks(t *testing.T) { @@ -13,3 +18,24 @@ func TestStripLinks(t *testing.T) { t.Errorf("StripLinks(%s) = %s; want %s", testLink, got, expected) } } + +func TestTxAge(t *testing.T) { + notExpected := "unknown time ago" // should be a definite time + + status := &relapi.GetQuoteRequestStatusResponse{ + OriginTxHash: "0x954264d120f5f3cf50edc39ebaf88ea9dc647d9d6843b7a120ed3677e23d7890", + OriginChainID: 421611, + } + + ctx := context.Background() + + client := omnirpcClient.NewOmnirpcClient("https://arb1.arbitrum.io/rpc", metrics.Get()) + cc, err := client.GetChainClient(ctx, int(status.OriginChainID)) + if err != nil { + t.Fatalf("GetChainClient() failed: %v", err) + } + + if got := botmd.GetTxAge(context.Background(), cc, status); got == notExpected { + t.Errorf("TxAge(%s) = %s; want not %s", status.OriginTxHash, got, notExpected) + } +} diff --git a/contrib/opbot/botmd/export_test.go b/contrib/opbot/botmd/export_test.go index 36a3ba400c..5e06c43616 100644 --- a/contrib/opbot/botmd/export_test.go +++ b/contrib/opbot/botmd/export_test.go @@ -1,5 +1,16 @@ package botmd +import ( + "context" + + "github.com/synapsecns/sanguine/ethergo/client" + "github.com/synapsecns/sanguine/services/rfq/relayer/relapi" +) + func StripLinks(input string) string { return stripLinks(input) } + +func GetTxAge(ctx context.Context, client client.EVM, res *relapi.GetQuoteRequestStatusResponse) string { + return getTxAge(ctx, client, res) +}