Skip to content

Commit

Permalink
fix(opbot): fix opbot txage for l2s (#2906)
Browse files Browse the repository at this point in the history
* fix txage for l2s

* [goreleaser]

* fix types [goreleaser]
  • Loading branch information
golangisfun123 authored Jul 23, 2024
1 parent 25f18c0 commit bda3181
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 15 deletions.
24 changes: 10 additions & 14 deletions contrib/opbot/botmd/commands.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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{
Expand All @@ -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)),
},
}

Expand Down Expand Up @@ -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 {
Expand Down
28 changes: 27 additions & 1 deletion contrib/opbot/botmd/commands_test.go
Original file line number Diff line number Diff line change
@@ -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) {
Expand All @@ -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)
}
}
11 changes: 11 additions & 0 deletions contrib/opbot/botmd/export_test.go
Original file line number Diff line number Diff line change
@@ -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)
}

0 comments on commit bda3181

Please sign in to comment.