Skip to content

Commit

Permalink
[goreleaser]
Browse files Browse the repository at this point in the history
  • Loading branch information
golangisfun123 committed Oct 10, 2024
1 parent dccd638 commit c1d21e4
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 48 deletions.
11 changes: 7 additions & 4 deletions contrib/opbot/botmd/botmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (

"github.com/slack-io/slacker"
"github.com/synapsecns/sanguine/contrib/opbot/config"
"github.com/synapsecns/sanguine/contrib/opbot/internal"
"github.com/synapsecns/sanguine/contrib/opbot/signoz"
screenerClient "github.com/synapsecns/sanguine/contrib/screener-api/client"
"github.com/synapsecns/sanguine/core/dbcommon"
Expand All @@ -29,6 +30,7 @@ type Bot struct {
signozClient *signoz.Client
signozEnabled bool
rpcClient omnirpcClient.RPCClient
rfqClient internal.RFQClient
signer signer.Signer
submitter submitter.TransactionSubmitter
screener screenerClient.ScreenerClient
Expand All @@ -42,10 +44,11 @@ func NewBot(handler metrics.Handler, cfg config.Config) *Bot {
sugaredLogger := otelzap.New(experimentalLogger.MakeZapLogger()).Sugar()

bot := Bot{
handler: handler,
cfg: cfg,
server: server,
logger: experimentalLogger.MakeWrappedSugaredLogger(sugaredLogger),
handler: handler,
cfg: cfg,
server: server,
logger: experimentalLogger.MakeWrappedSugaredLogger(sugaredLogger),
rfqClient: internal.NewRFQClient(handler, cfg.RFQIndexerAPIURL, cfg.RelayerURLS),
}

// you should be able to run opbot even without signoz.
Expand Down
43 changes: 10 additions & 33 deletions contrib/opbot/botmd/commands.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ import (
"github.com/hako/durafmt"
"github.com/slack-go/slack"
"github.com/slack-io/slacker"
"github.com/synapsecns/sanguine/contrib/opbot/internal"
"github.com/synapsecns/sanguine/contrib/opbot/signoz"
"github.com/synapsecns/sanguine/core/retry"
"github.com/synapsecns/sanguine/ethergo/chaindata"
Expand Down Expand Up @@ -165,12 +164,10 @@ func (b *Bot) rfqLookupCommand() *slacker.CommandDefinition {
Handler: func(ctx *slacker.CommandContext) {
tx := stripLinks(ctx.Request().Param("tx"))

rfqClient := internal.NewRFQClient(b.handler, b.cfg.RFQIndexerAPIURL)

res, status, err := rfqClient.GetRFQByTxID(ctx.Context(), tx)
res, status, err := b.rfqClient.GetRFQByTxID(ctx.Context(), tx)
if err != nil {
b.logger.Errorf(ctx.Context(), "error fetching quote request: %v", err)
_, err := ctx.Response().Reply("error fetching quote request")
_, err := ctx.Response().Reply(fmt.Sprintf("error fetching quote request %s", err.Error()))
if err != nil {
log.Println(err)
}
Expand Down Expand Up @@ -241,16 +238,7 @@ func (b *Bot) rfqRefund() *slacker.CommandDefinition {
return
}

var rawRequest *relapi.GetQuoteRequestResponse
var err error
var relClient relapi.RelayerClient
for _, relayer := range b.cfg.RelayerURLS {
relClient = relapi.NewRelayerClient(b.handler, relayer)
rawRequest, err = getQuoteRequest(ctx.Context(), relClient, tx)
if err == nil {
break
}
}
rawRequest, err := b.rfqClient.GetRFQByTxHash(ctx.Context(), tx)
if err != nil {
b.logger.Errorf(ctx.Context(), "error fetching quote request: %v", err)
_, err := ctx.Response().Reply("error fetching quote request")
Expand Down Expand Up @@ -313,12 +301,14 @@ func (b *Bot) rfqRefund() *slacker.CommandDefinition {
)
if err != nil {
b.logger.Errorf(ctx.Context(), "error fetching quote request: %v", err)
_, err := ctx.Response().Reply(fmt.Sprintf("error fetching explorer link to refund, but nonce is %d", nonce))
log.Printf("error fetching quote request: %v\n", err)
_, err := ctx.Response().Reply(fmt.Sprintf("refund submitted with nonce %d", nonce))
if err != nil {
log.Println(err)
}
return
}

_, err = ctx.Response().Reply(fmt.Sprintf("refund submitted: %s", toExplorerSlackLink(status.TxHash().String())))
_, err = ctx.Response().Reply(fmt.Sprintf("refund submitted: %s", toTXSlackLink(status.TxHash().String(), rawRequest.OriginChainID)))
if err != nil {
log.Println(err)
}
Expand Down Expand Up @@ -355,13 +345,13 @@ func (b *Bot) makeFastBridge(ctx context.Context, req *relapi.GetQuoteRequestRes
}

func getTxAge(timestamp int64) string {
return fmt.Sprintf("The block was created %s.\n", humanize.Time(time.Unix(timestamp, 0)))
return humanize.Time(time.Unix(timestamp, 0))
}

func toExplorerSlackLink(ogHash string) string {
rfqHash := strings.ToUpper(ogHash)
// cut off 0x
if strings.HasPrefix(rfqHash, "0x") {
if strings.HasPrefix(rfqHash, "0X") {
rfqHash = strings.ToLower(rfqHash[2:])
}

Expand All @@ -383,16 +373,3 @@ func stripLinks(input string) string {
linkRegex := regexp.MustCompile(`<https?://[^|>]+\|([^>]+)>`)
return linkRegex.ReplaceAllString(input, "$1")
}

func getQuoteRequest(ctx context.Context, client relapi.RelayerClient, tx string) (qr *relapi.GetQuoteRequestResponse, err error) {
if qr, err = client.GetQuoteRequestByTxHash(ctx, tx); err == nil {
return qr, nil
}

// look up quote request
if qr, err = client.GetQuoteRequestByTXID(ctx, tx); err == nil {
return qr, nil
}

return nil, fmt.Errorf("error fetching quote request: %w", err)
}
2 changes: 1 addition & 1 deletion contrib/opbot/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@ require (
github.com/synapsecns/sanguine/services/rfq v0.0.0-00010101000000-000000000000
github.com/uptrace/opentelemetry-go-extra/otelzap v0.3.1
github.com/urfave/cli/v2 v2.27.2
github.com/valyala/fastjson v1.6.4
golang.org/x/sync v0.8.0
gopkg.in/yaml.v2 v2.4.0
gopkg.in/yaml.v3 v3.0.1
Expand Down Expand Up @@ -243,6 +242,7 @@ require (
github.com/uptrace/opentelemetry-go-extra/otelgorm v0.3.1 // indirect
github.com/uptrace/opentelemetry-go-extra/otelsql v0.3.1 // indirect
github.com/uptrace/opentelemetry-go-extra/otelutil v0.3.1 // indirect
github.com/valyala/fastjson v1.6.4 // indirect
github.com/xanzy/ssh-agent v0.3.3 // indirect
github.com/xo/terminfo v0.0.0-20220910002029-abceb7e1c41e // indirect
github.com/xrash/smetrics v0.0.0-20240312152122-5f08fbb34913 // indirect
Expand Down
42 changes: 32 additions & 10 deletions contrib/opbot/internal/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import (
"github.com/go-http-utils/headers"
"github.com/go-resty/resty/v2"
"github.com/synapsecns/sanguine/core/metrics"
"github.com/valyala/fastjson"
"github.com/synapsecns/sanguine/services/rfq/relayer/relapi"
)

const (
Expand All @@ -20,30 +20,38 @@ const (
type RFQClient interface {
// GetRFQByTxID gets a quote request by transaction ID.
GetRFQByTxID(ctx context.Context, txID string) (resp *GetRFQByTxIDResponse, status string, err error)
// GetRFQByTxHash gets a quote request by transaction hash.
GetRFQByTxHash(ctx context.Context, txHash string) (resp *relapi.GetQuoteRequestResponse, err error)
}

type rfqClientImpl struct {
client *resty.Client
client *resty.Client
relayerClients []relapi.RelayerClient
}

// NewRFQClient creates a new RFQClient.
func NewRFQClient(handler metrics.Handler, url string) RFQClient {
func NewRFQClient(handler metrics.Handler, indexerURL string, relayerURLs []string) RFQClient {
client := resty.New()
client.SetBaseURL(url)
client.SetBaseURL(indexerURL)
client.SetHeader(headers.UserAgent, "rfq-client")

otelresty.TraceClient(client, otelresty.WithTracerProvider(handler.GetTracerProvider()))

var relayerClients []relapi.RelayerClient
for _, url := range relayerURLs {
relayerClients = append(relayerClients, relapi.NewRelayerClient(handler, url))
}

return &rfqClientImpl{
client: client,
client: client,
relayerClients: relayerClients,
}
}

// GetRFQByTxID gets a quote request by transaction ID.
func (r *rfqClientImpl) GetRFQByTxID(ctx context.Context, txID string) (*GetRFQByTxIDResponse, string, error) {
var res GetRFQByTxIDResponse
resp, err := r.client.R().SetContext(ctx).
SetQueryParam("hash", txID).
SetResult(&res).
Get(fmt.Sprintf(getRequestByTxHash, txID))
if err != nil {
Expand All @@ -55,13 +63,13 @@ func (r *rfqClientImpl) GetRFQByTxID(ctx context.Context, txID string) (*GetRFQB
}

var status string
if fastjson.GetString(resp.Body(), "BridgeClaim") != "" {
if res.BridgeClaim != (BridgeClaim{}) {
status = "Claimed"
} else if fastjson.GetString(resp.Body(), "BridgeProof") != "" {
} else if res.BridgeProof != (BridgeProof{}) {
status = "Proven"
} else if fastjson.GetString(resp.Body(), "BridgeRelay") != "" {
} else if res.BridgeRelay != (BridgeRelay{}) {
status = "Relayed"
} else if fastjson.GetString(resp.Body(), "BridgeRequest") != "" {
} else if res.BridgeRequest != (BridgeRequest{}) {
status = "Requested"
} else {
status = "Unknown"
Expand All @@ -71,4 +79,18 @@ func (r *rfqClientImpl) GetRFQByTxID(ctx context.Context, txID string) (*GetRFQB

}

// GetRFQByTxHash gets a quote request by transaction hash.
func (r *rfqClientImpl) GetRFQByTxHash(ctx context.Context, txHash string) (*relapi.GetQuoteRequestResponse, error) {
var resp *relapi.GetQuoteRequestResponse
var err error
for _, relayerClient := range r.relayerClients {
resp, err = relayerClient.GetQuoteRequestByTxHash(ctx, txHash)
if err != nil {
return nil, fmt.Errorf("failed to get quote request by tx hash: %w", err)
}
}

return resp, nil
}

var _ RFQClient = &rfqClientImpl{}

0 comments on commit c1d21e4

Please sign in to comment.