Skip to content

Commit

Permalink
cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
trajan0x committed Jul 5, 2024
1 parent d14682e commit b62cb02
Show file tree
Hide file tree
Showing 7 changed files with 85 additions and 9 deletions.
62 changes: 57 additions & 5 deletions contrib/opbot/botmd/commands.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
package botmd

import (
"context"
"fmt"
"github.com/ethereum/go-ethereum/accounts/abi/bind"
"github.com/ethereum/go-ethereum/common"
Expand Down Expand Up @@ -260,13 +261,33 @@ func (b *Bot) rfqRefund() *slacker.CommandDefinition {
contracts, err := client.GetRFQContracts(ctx.Context())
if err != nil {
log.Printf("error fetching rfq contracts: %v\n", err)
_, err = ctx.Response().Reply("error fetching rfq contracts")
if err != nil {
return
}
return

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

View check run for this annotation

Codecov / codecov/patch

contrib/opbot/botmd/commands.go#L261-L268

Added lines #L261 - L268 were not covered by tests
}

tx := stripLinks(ctx.Request().Param("tx"))

if len(tx) == 0 {
_, err := ctx.Response().Reply("please provide a tx hash")
if err != nil {
log.Println(err)
}
return

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

View check run for this annotation

Codecov / codecov/patch

contrib/opbot/botmd/commands.go#L271-L278

Added lines #L271 - L278 were not covered by tests
}

originChainIDStr := ctx.Request().Param("origin_chainid")
if len(originChainIDStr) == 0 {
_, err := ctx.Response().Reply("please provide an origin chain id")
if err != nil {
log.Println(err)
}
return

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

View check run for this annotation

Codecov / codecov/patch

contrib/opbot/botmd/commands.go#L281-L287

Added lines #L281 - L287 were not covered by tests
}

originChainID, err := strconv.Atoi(originChainIDStr)
originChainID, err := strconv.Atoi(convertChainName(originChainIDStr))
if err != nil {
_, err := ctx.Response().Reply("origin_chainid must be a number")
if err != nil {
Expand Down Expand Up @@ -298,14 +319,18 @@ func (b *Bot) rfqRefund() *slacker.CommandDefinition {

for _, relayer := range b.cfg.RelayerURLS {
relClient := relapi.NewRelayerClient(b.handler, relayer)
qr, err := relClient.GetQuoteRequestByTXID(ctx.Context(), tx)

rawRequest, err := getQuoteRequest(ctx.Context(), relClient, tx)
if err != nil {
log.Printf("error fetching quote request: %v\n", err)
continue
_, err := ctx.Response().Reply("error fetching quote request")
if err != nil {
log.Println(err)
}
return

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

View check run for this annotation

Codecov / codecov/patch

contrib/opbot/botmd/commands.go#L320-L329

Added lines #L320 - L329 were not covered by tests
}

nonce, err := b.submitter.SubmitTransaction(ctx.Context(), big.NewInt(int64(originChainID)), func(transactor *bind.TransactOpts) (tx *types.Transaction, err error) {
return fastBridgeHandle.Refund(transactor, common.Hex2Bytes(qr.QuoteRequestRaw))
return fastBridgeHandle.Refund(transactor, rawRequest)

Check failure on line 333 in contrib/opbot/botmd/commands.go

View workflow job for this annotation

GitHub Actions / Lint (contrib/opbot)

error returned from external package is unwrapped: sig: func (*github.com/synapsecns/sanguine/services/rfq/contracts/fastbridge.FastBridgeTransactor).Refund(opts *github.com/ethereum/go-ethereum/accounts/abi/bind.TransactOpts, request []byte) (*github.com/ethereum/go-ethereum/core/types.Transaction, error) (wrapcheck)
})
if err != nil {
log.Printf("error submitting refund: %v\n", err)
Expand Down Expand Up @@ -347,3 +372,30 @@ func stripLinks(input string) string {
linkRegex := regexp.MustCompile(`<https?://[^|>]+\|([^>]+)>`)
return linkRegex.ReplaceAllString(input, "$1")
}

// convertChainName detects if the string contains letters and if it does, tries to convert it to a chain id.
// otherwise return the original string

Check failure on line 377 in contrib/opbot/botmd/commands.go

View workflow job for this annotation

GitHub Actions / Lint (contrib/opbot)

Comment should end in a period (godot)
func convertChainName(input string) string {
res := chaindata.ChainNameToChainID(input)
if res != 0 {
return strconv.Itoa(int(res))
}
return input

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

View check run for this annotation

Codecov / codecov/patch

contrib/opbot/botmd/commands.go#L378-L383

Added lines #L378 - L383 were not covered by tests
}

func getQuoteRequest(ctx context.Context, client relapi.RelayerClient, tx string) ([]byte, error) {
// at this point tx can be a txid or a has, we try both
txRequest, err := client.GetQuoteRequestStatusByTxHash(ctx, tx)
if err == nil {
// override tx with txid
tx = txRequest.TxID
}

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

View check run for this annotation

Codecov / codecov/patch

contrib/opbot/botmd/commands.go#L386-L392

Added lines #L386 - L392 were not covered by tests

// look up quote request
qr, err := client.GetQuoteRequestByTXID(ctx, tx)
if err != nil {
return nil, fmt.Errorf("error fetching quote request: %w", err)
}

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

View check run for this annotation

Codecov / codecov/patch

contrib/opbot/botmd/commands.go#L395-L398

Added lines #L395 - L398 were not covered by tests

return common.Hex2Bytes(qr.QuoteRequestRaw), nil

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

View check run for this annotation

Codecov / codecov/patch

contrib/opbot/botmd/commands.go#L400

Added line #L400 was not covered by tests
}
3 changes: 2 additions & 1 deletion contrib/opbot/cmd/commands.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package cmd
import (
"fmt"
"github.com/synapsecns/sanguine/contrib/opbot/botmd"
"github.com/synapsecns/sanguine/core"
"github.com/synapsecns/sanguine/core/metrics"

// used for testing.
Expand All @@ -23,7 +24,7 @@ var slackBotCommand = &cli.Command{
Usage: "start the slack bot",
Flags: []cli.Flag{fileFlag},
Action: func(c *cli.Context) error {
configFile, err := os.ReadFile(c.String(fileFlag.Name))
configFile, err := os.ReadFile(core.ExpandOrReturnPath(c.String(fileFlag.Name)))

Check warning on line 27 in contrib/opbot/cmd/commands.go

View check run for this annotation

Codecov / codecov/patch

contrib/opbot/cmd/commands.go#L27

Added line #L27 was not covered by tests
if err != nil {
return fmt.Errorf("failed to open config file: %w", err)
}
Expand Down
2 changes: 1 addition & 1 deletion contrib/opbot/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ type Config struct {
// RFQApiURL is the URL of the RFQ API.
RFQApiURL string `yaml:"rfq_api_url"`
// OmniRPCURL is the URL of the Omni RPC.
OmniRPCURL string `yaml:"omni_rpc_url"`
OmniRPCURL string `yaml:"omnirpc_url"`
// Signer is the signer config.
Signer config.SignerConfig `yaml:"signer"`
// SubmitterConfig is the submitter config.
Expand Down
11 changes: 11 additions & 0 deletions ethergo/chaindata/chaindata.go
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,17 @@ var ChainMetadataList = []ChainMetadata{
},
}

// ChainNameToChainID converts the chain name to the chain id.
// It returns 0 if the chain name is not found.
func ChainNameToChainID(chainName string) uint64 {
for _, chainMetadata := range ChainMetadataList {
if strings.EqualFold(chainMetadata.ChainName, chainName) {
return uint64(chainMetadata.ChainID)
}

Check warning on line 173 in ethergo/chaindata/chaindata.go

View check run for this annotation

Codecov / codecov/patch

ethergo/chaindata/chaindata.go#L169-L173

Added lines #L169 - L173 were not covered by tests
}
return 0

Check warning on line 175 in ethergo/chaindata/chaindata.go

View check run for this annotation

Codecov / codecov/patch

ethergo/chaindata/chaindata.go#L175

Added line #L175 was not covered by tests
}

// ChainIDToChainName converts the chain id to the chain name.
func ChainIDToChainName(chainID int64, isUpper bool) string {
for _, chainMetadata := range ChainMetadataList {
Expand Down
5 changes: 5 additions & 0 deletions ethergo/signer/wallet/import.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,11 @@ func FromKeyFile(keyFile string) (Wallet, error) {

// FromHex gets the wallet from the private key.
func FromHex(privateKey string) (Wallet, error) {
// Check for '0x' prefix and remove it if it exists
if len(privateKey) >= 2 && strings.EqualFold(privateKey[:2], "0x") {
privateKey = privateKey[2:]
}

Check warning on line 64 in ethergo/signer/wallet/import.go

View check run for this annotation

Codecov / codecov/patch

ethergo/signer/wallet/import.go#L63-L64

Added lines #L63 - L64 were not covered by tests

privKey, err := crypto.HexToECDSA(privateKey)
if err != nil {
return nil, fmt.Errorf("could not decode key: %w", err)
Expand Down
9 changes: 8 additions & 1 deletion services/omnirpc/client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
"io"
"math/big"
"net/http"
"strings"
)

// RPCClient is an interface for the omnirpc service.
Expand Down Expand Up @@ -55,7 +56,13 @@ func (c *rpcClient) GetClient(ctx context.Context, chainID *big.Int) (client.EVM
return c.GetChainClient(ctx, int(chainID.Uint64()))
}

func (c *rpcClient) GetEndpoint(chainID, confirmations int) string {
func (c *rpcClient) GetEndpoint(chainID, confirmations int) (res string) {
defer func() {
res = strings.ReplaceAll(c.endpoint, "://", "TEMP_PROTOCOL")
res = strings.ReplaceAll(c.endpoint, "//", "/")
res = strings.ReplaceAll(c.endpoint, "TEMP_PROTOCOL", "://")
}()

if confirmations == 0 {
return fmt.Sprintf("%s/rpc/%d", c.endpoint, chainID)
}
Expand Down
2 changes: 1 addition & 1 deletion services/rfq/api/client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -232,5 +232,5 @@ func getStatus(resp *resty.Response) string {
if resp == nil {
return "http status unavailable"
}

Check warning on line 234 in services/rfq/api/client/client.go

View check run for this annotation

Codecov / codecov/patch

services/rfq/api/client/client.go#L231-L234

Added lines #L231 - L234 were not covered by tests
return getStatus(resp)
return resp.Status()
}

0 comments on commit b62cb02

Please sign in to comment.