Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(opbot): transaction age textblock #2890

Merged
merged 6 commits into from
Jul 19, 2024
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 38 additions & 6 deletions contrib/opbot/botmd/commands.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,14 @@
"context"
"errors"
"fmt"
"log"
"math/big"
"regexp"
"strings"
"sync"
"time"

"github.com/dustin/go-humanize"
"github.com/ethereum/go-ethereum/accounts/abi/bind"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/core/types"
Expand All @@ -17,12 +25,6 @@
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"
"log"
"math/big"
"regexp"
"strings"
"sync"
"time"
)

func (b *Bot) requiresSignoz(definition *slacker.CommandDefinition) *slacker.CommandDefinition {
Expand Down Expand Up @@ -138,6 +140,7 @@
})
}

// nolint: gocognit
func (b *Bot) rfqLookupCommand() *slacker.CommandDefinition {
return &slacker.CommandDefinition{
Command: "rfq <tx>",
Expand Down Expand Up @@ -204,7 +207,13 @@
}

var slackBlocks []slack.Block

Check warning on line 210 in contrib/opbot/botmd/commands.go

View check run for this annotation

Codecov / codecov/patch

contrib/opbot/botmd/commands.go#L210

Added line #L210 was not covered by tests
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Remove unnecessary leading newline.

There is an unnecessary leading newline at line 209.

-
  for _, status := range statuses {

Committable suggestion was skipped due to low confidence.

for _, status := range statuses {
time, err := b.getTxAge(ctx, status.GetQuoteRequestStatusResponse)
if err != nil {
log.Printf("error getting tx age: %v\n", err)
}

Check warning on line 215 in contrib/opbot/botmd/commands.go

View check run for this annotation

Codecov / codecov/patch

contrib/opbot/botmd/commands.go#L212-L215

Added lines #L212 - L215 were not covered by tests

objects := []*slack.TextBlockObject{
{
Type: slack.MarkdownType,
Expand All @@ -222,6 +231,10 @@
Type: slack.MarkdownType,
Text: fmt.Sprintf("*OriginTxHash*: %s", toTXSlackLink(status.OriginTxHash, status.OriginChainID)),
},
{
Type: slack.MarkdownType,
Text: fmt.Sprintf("*Estimated Tx Age*: %s", humanize.Time(time)),
},

Check warning on line 237 in contrib/opbot/botmd/commands.go

View check run for this annotation

Codecov / codecov/patch

contrib/opbot/botmd/commands.go#L234-L237

Added lines #L234 - L237 were not covered by tests
}

if status.DestTxHash == (common.Hash{}).String() {
Expand Down Expand Up @@ -334,6 +347,25 @@
return fastBridgeHandle, nil
}

func (b *Bot) getTxAge(ctx *slacker.CommandContext, status *relapi.GetQuoteRequestStatusResponse) (time.Time, error) {
// TODO: add CreatedAt field to GetQuoteRequestStatusResponse so we don't need to make network calls?
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Consider adding CreatedAt field to GetQuoteRequestStatusResponse.

Adding a CreatedAt field to the GetQuoteRequestStatusResponse struct can eliminate the need for network calls to fetch transaction details.

// 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))
if err != nil {
return time.Time{}, fmt.Errorf("error getting chain client: %w", err)
}

Check warning on line 355 in contrib/opbot/botmd/commands.go

View check run for this annotation

Codecov / codecov/patch

contrib/opbot/botmd/commands.go#L350-L355

Added lines #L350 - L355 were not covered by tests

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)
if err != nil {
return time.Time{}, fmt.Errorf("error fetching block by hash: %w", err)
}

Check warning on line 364 in contrib/opbot/botmd/commands.go

View check run for this annotation

Codecov / codecov/patch

contrib/opbot/botmd/commands.go#L357-L364

Added lines #L357 - L364 were not covered by tests

return time.Unix(int64(txBlock.Time()), 0), nil

Check warning on line 366 in contrib/opbot/botmd/commands.go

View check run for this annotation

Codecov / codecov/patch

contrib/opbot/botmd/commands.go#L366

Added line #L366 was not covered by tests
}
Comment on lines +350 to +367
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Optimize timestamp conversion.

The timestamp conversion to time.Time can be optimized by directly using the Unix method.

-  return time.Unix(int64(txBlock.Time()), 0)
+  return time.Unix(txBlock.Time().Int64(), 0)
Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
func (b *Bot) getTxAge(ctx *slacker.CommandContext, status *relapi.GetQuoteRequestStatusResponse) (time.Time, error) {
// 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))
if err != nil {
return time.Time{}, fmt.Errorf("error getting chain client: %w", err)
}
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)
if err != nil {
return time.Time{}, fmt.Errorf("error fetching block by hash: %w", err)
}
return time.Unix(int64(txBlock.Time()), 0), nil
}
func (b *Bot) getTxAge(ctx *slacker.CommandContext, status *relapi.GetQuoteRequestStatusResponse) (time.Time, error) {
// 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))
if err != nil {
return time.Time{}, fmt.Errorf("error getting chain client: %w", err)
}
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)
if err != nil {
return time.Time{}, fmt.Errorf("error fetching block by hash: %w", err)
}
return time.Unix(txBlock.Time().Int64(), 0), nil
}


func toExplorerSlackLink(ogHash string) string {
rfqHash := strings.ToUpper(ogHash)
// cut off 0x
Expand Down
2 changes: 1 addition & 1 deletion contrib/opbot/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ require (
github.com/charmbracelet/huh/spinner v0.0.0-20240618200428-90406d79077d
github.com/davidmytton/url-verifier v1.0.1
github.com/dubonzi/otelresty v1.3.0
github.com/dustin/go-humanize v1.0.1
github.com/ethereum/go-ethereum v1.13.8
github.com/go-http-utils/headers v0.0.0-20181008091004-fed159eddc2a
github.com/go-resty/resty/v2 v2.13.1
Expand Down Expand Up @@ -112,7 +113,6 @@ require (
github.com/deckarep/golang-set/v2 v2.6.0 // indirect
github.com/decred/dcrd/dcrec/secp256k1/v4 v4.2.0 // indirect
github.com/dennwc/varint v1.0.0 // indirect
github.com/dustin/go-humanize v1.0.1 // indirect
github.com/emirpasic/gods v1.18.1 // indirect
github.com/erikgeiser/coninput v0.0.0-20211004153227-1c3628e74d0f // indirect
github.com/ethereum/c-kzg-4844 v0.4.0 // indirect
Expand Down
Loading