From eccbfd11e86c85a9c982b24a1efc5fe5ab766e1d Mon Sep 17 00:00:00 2001 From: vro <168573323+golangisfun123@users.noreply.github.com> Date: Wed, 28 Aug 2024 08:31:23 -0500 Subject: [PATCH] refactor(relapi): deprecate `GetQuoteRequestStatusResponse` model (#3045) * deprecate qrstatus model * add remaining fields * fix test [goreleaser] * fix test [goreleaser] * [goreleaser] --- contrib/opbot/botmd/commands.go | 19 +++--- contrib/opbot/botmd/commands_test.go | 2 +- contrib/opbot/botmd/export_test.go | 2 +- services/rfq/relayer/relapi/client.go | 38 ------------ services/rfq/relayer/relapi/client_test.go | 4 +- services/rfq/relayer/relapi/handler.go | 67 +++------------------- services/rfq/relayer/relapi/model.go | 14 ++--- services/rfq/relayer/relapi/server.go | 19 ++---- services/rfq/relayer/relapi/server_test.go | 42 ++++++++------ 9 files changed, 56 insertions(+), 151 deletions(-) diff --git a/contrib/opbot/botmd/commands.go b/contrib/opbot/botmd/commands.go index 62d5d5e164..a71ae49a3d 100644 --- a/contrib/opbot/botmd/commands.go +++ b/contrib/opbot/botmd/commands.go @@ -152,7 +152,7 @@ func (b *Bot) rfqLookupCommand() *slacker.CommandDefinition { Handler: func(ctx *slacker.CommandContext) { type Status struct { relayer string - *relapi.GetQuoteRequestStatusResponse + *relapi.GetQuoteRequestResponse } var statuses []Status @@ -175,26 +175,26 @@ func (b *Bot) rfqLookupCommand() *slacker.CommandDefinition { client := relapi.NewRelayerClient(b.handler, relayer) go func() { defer wg.Done() - res, err := client.GetQuoteRequestStatusByTxHash(ctx.Context(), tx) + res, err := client.GetQuoteRequestByTxHash(ctx.Context(), tx) if err != nil { log.Printf("error fetching quote request status by tx hash: %v\n", err) return } sliceMux.Lock() defer sliceMux.Unlock() - statuses = append(statuses, Status{relayer: relayer, GetQuoteRequestStatusResponse: res}) + statuses = append(statuses, Status{relayer: relayer, GetQuoteRequestResponse: res}) }() go func() { defer wg.Done() - res, err := client.GetQuoteRequestStatusByTxID(ctx.Context(), tx) + res, err := client.GetQuoteRequestByTXID(ctx.Context(), tx) if err != nil { log.Printf("error fetching quote request status by tx id: %v\n", err) return } sliceMux.Lock() defer sliceMux.Unlock() - statuses = append(statuses, Status{relayer: relayer, GetQuoteRequestStatusResponse: res}) + statuses = append(statuses, Status{relayer: relayer, GetQuoteRequestResponse: res}) }() } wg.Wait() @@ -234,7 +234,7 @@ func (b *Bot) rfqLookupCommand() *slacker.CommandDefinition { }, { Type: slack.MarkdownType, - Text: fmt.Sprintf("*Estimated Tx Age*: %s", getTxAge(ctx.Context(), client, status.GetQuoteRequestStatusResponse)), + Text: fmt.Sprintf("*Estimated Tx Age*: %s", getTxAge(ctx.Context(), client, status.GetQuoteRequestResponse)), }, } @@ -257,7 +257,8 @@ func (b *Bot) rfqLookupCommand() *slacker.CommandDefinition { if err != nil { log.Println(err) } - }} + }, + } } // nolint: gocognit, cyclop. @@ -348,7 +349,7 @@ func (b *Bot) makeFastBridge(ctx context.Context, req *relapi.GetQuoteRequestRes return fastBridgeHandle, nil } -func getTxAge(ctx context.Context, client client.EVM, res *relapi.GetQuoteRequestStatusResponse) string { +func getTxAge(ctx context.Context, client client.EVM, res *relapi.GetQuoteRequestResponse) string { // TODO: add CreatedAt field to GetQuoteRequestStatusResponse so we don't need to make network calls? receipt, err := client.TransactionReceipt(ctx, common.HexToHash(res.OriginTxHash)) if err != nil { @@ -390,7 +391,7 @@ func stripLinks(input string) string { func getQuoteRequest(ctx context.Context, client relapi.RelayerClient, tx string) (*relapi.GetQuoteRequestResponse, error) { // at this point tx can be a txid or a has, we try both - txRequest, err := client.GetQuoteRequestStatusByTxHash(ctx, tx) + txRequest, err := client.GetQuoteRequestByTxHash(ctx, tx) if err == nil { // override tx with txid tx = txRequest.TxID diff --git a/contrib/opbot/botmd/commands_test.go b/contrib/opbot/botmd/commands_test.go index 26784aa7a0..b9ab0a39ee 100644 --- a/contrib/opbot/botmd/commands_test.go +++ b/contrib/opbot/botmd/commands_test.go @@ -22,7 +22,7 @@ func TestStripLinks(t *testing.T) { func TestTxAge(t *testing.T) { notExpected := "unknown time ago" // should be a definite time - status := &relapi.GetQuoteRequestStatusResponse{ + status := &relapi.GetQuoteRequestResponse{ OriginTxHash: "0x954264d120f5f3cf50edc39ebaf88ea9dc647d9d6843b7a120ed3677e23d7890", OriginChainID: 421611, } diff --git a/contrib/opbot/botmd/export_test.go b/contrib/opbot/botmd/export_test.go index 5e06c43616..79b1e7a0f2 100644 --- a/contrib/opbot/botmd/export_test.go +++ b/contrib/opbot/botmd/export_test.go @@ -11,6 +11,6 @@ func StripLinks(input string) string { return stripLinks(input) } -func GetTxAge(ctx context.Context, client client.EVM, res *relapi.GetQuoteRequestStatusResponse) string { +func GetTxAge(ctx context.Context, client client.EVM, res *relapi.GetQuoteRequestResponse) string { return getTxAge(ctx, client, res) } diff --git a/services/rfq/relayer/relapi/client.go b/services/rfq/relayer/relapi/client.go index ffc67056ec..30ea7edfed 100644 --- a/services/rfq/relayer/relapi/client.go +++ b/services/rfq/relayer/relapi/client.go @@ -15,10 +15,6 @@ import ( // RelayerClient is the interface for the relayer client. type RelayerClient interface { Health(ctx context.Context) (ok bool, err error) - // Deprecated: use GetQuoteRequestByTxHash - GetQuoteRequestStatusByTxHash(ctx context.Context, hash string) (*GetQuoteRequestStatusResponse, error) - // Deprecated: use GetQuoteRequestStatusByTxID - GetQuoteRequestStatusByTxID(ctx context.Context, hash string) (*GetQuoteRequestStatusResponse, error) RetryTransaction(ctx context.Context, txhash string) (*GetTxRetryResponse, error) Withdraw(ctx context.Context, req *WithdrawRequest) (*WithdrawResponse, error) GetTxHashByNonce(ctx context.Context, req *GetTxByNonceRequest) (*TxHashByNonceResponse, error) @@ -58,40 +54,6 @@ func (r *relayerClient) Health(ctx context.Context) (ok bool, err error) { return ok, nil } -func (r *relayerClient) GetQuoteRequestStatusByTxHash(ctx context.Context, hash string) (*GetQuoteRequestStatusResponse, error) { - var res GetQuoteRequestStatusResponse - - resp, err := r.client.R().SetContext(ctx). - SetQueryParam("hash", hash). - SetResult(&res). - Get(getQuoteStatusByTxHashRoute) - if err != nil { - return nil, fmt.Errorf("failed to get quote request status by tx hash: %w", err) - } - if resp.StatusCode() != http.StatusOK { - return nil, fmt.Errorf("unexpected status code: %d", resp.StatusCode()) - } - - return &res, nil -} - -func (r *relayerClient) GetQuoteRequestStatusByTxID(ctx context.Context, txid string) (*GetQuoteRequestStatusResponse, error) { - var res GetQuoteRequestStatusResponse - - resp, err := r.client.R().SetContext(ctx). - SetQueryParam("id", txid). - SetResult(&res). - Get(getQuoteStatusByTxIDRoute) - if err != nil { - return nil, fmt.Errorf("failed to get quote request status by tx hash: %w", err) - } - if resp.StatusCode() != http.StatusOK { - return nil, fmt.Errorf("unexpected status code: %d", resp.StatusCode()) - } - - return &res, nil -} - func (r *relayerClient) RetryTransaction(ctx context.Context, txhash string) (*GetTxRetryResponse, error) { var res GetTxRetryResponse resp, err := r.client.R().SetContext(ctx). diff --git a/services/rfq/relayer/relapi/client_test.go b/services/rfq/relayer/relapi/client_test.go index 4af08de43e..424a678633 100644 --- a/services/rfq/relayer/relapi/client_test.go +++ b/services/rfq/relayer/relapi/client_test.go @@ -26,7 +26,7 @@ func (c *RelayerClientSuite) TestGetQuoteRequestStatusByTxHash() { err := c.underlying.database.StoreQuoteRequest(c.GetTestContext(), testReq) c.Require().NoError(err) - resp, err := c.Client.GetQuoteRequestStatusByTxHash(c.GetTestContext(), testReq.OriginTxHash.String()) + resp, err := c.Client.GetQuoteRequestByTxHash(c.GetTestContext(), testReq.OriginTxHash.String()) c.Require().NoError(err) c.Equal(resp.Status, testReq.Status.String()) @@ -40,7 +40,7 @@ func (c *RelayerClientSuite) TestGetQuoteRequestStatusByTxID() { err := c.underlying.database.StoreQuoteRequest(c.GetTestContext(), testReq) c.Require().NoError(err) - resp, err := c.Client.GetQuoteRequestStatusByTxID(c.GetTestContext(), hexutil.Encode(testReq.TransactionID[:])) + resp, err := c.Client.GetQuoteRequestByTXID(c.GetTestContext(), hexutil.Encode(testReq.TransactionID[:])) c.Require().NoError(err) c.Equal(resp.Status, testReq.Status.String()) diff --git a/services/rfq/relayer/relapi/handler.go b/services/rfq/relayer/relapi/handler.go index bfa867b3f6..8f40a75798 100644 --- a/services/rfq/relayer/relapi/handler.go +++ b/services/rfq/relayer/relapi/handler.go @@ -51,65 +51,6 @@ func (h *Handler) GetHealth(c *gin.Context) { const unspecifiedTxHash = "Must specify 'hash' (corresponding to origin tx)" -// GetQuoteRequestStatusByTxHash gets the status of a quote request, given an origin tx hash. -func (h *Handler) GetQuoteRequestStatusByTxHash(c *gin.Context) { - txHashStr := c.Query("hash") - if txHashStr == "" { - c.JSON(http.StatusBadRequest, gin.H{"error": unspecifiedTxHash}) - return - } - - txHash := common.HexToHash(txHashStr) - quoteRequest, err := h.db.GetQuoteRequestByOriginTxHash(c, txHash) - if err != nil { - c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()}) - return - } - - resp := GetQuoteRequestStatusResponse{ - Status: quoteRequest.Status.String(), - TxID: hexutil.Encode(quoteRequest.TransactionID[:]), - OriginTxHash: quoteRequest.OriginTxHash.String(), - OriginChainID: quoteRequest.Transaction.OriginChainId, - DestChainID: quoteRequest.Transaction.DestChainId, - DestTxHash: quoteRequest.DestTxHash.String(), - } - c.JSON(http.StatusOK, resp) -} - -// GetQuoteRequestStatusByTxID gets the status of a quote request, given a tx id. -func (h *Handler) GetQuoteRequestStatusByTxID(c *gin.Context) { - txIDStr := c.Query("id") - if txIDStr == "" { - c.JSON(http.StatusBadRequest, gin.H{"error": "Must specify 'id'"}) - return - } - - txIDBytes, err := hexutil.Decode(txIDStr) - if err != nil { - c.JSON(http.StatusBadRequest, gin.H{"error": "Invalid txID"}) - return - } - var txID [32]byte - copy(txID[:], txIDBytes) - - quoteRequest, err := h.db.GetQuoteRequestByID(c, txID) - if err != nil { - c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()}) - return - } - - resp := GetQuoteRequestStatusResponse{ - Status: quoteRequest.Status.String(), - TxID: hexutil.Encode(quoteRequest.TransactionID[:]), - OriginTxHash: quoteRequest.OriginTxHash.String(), - OriginChainID: quoteRequest.Transaction.OriginChainId, - DestChainID: quoteRequest.Transaction.DestChainId, - DestTxHash: quoteRequest.DestTxHash.String(), - } - c.JSON(http.StatusOK, resp) -} - // GetTxRetry retries a transaction based on tx hash. func (h *Handler) GetTxRetry(c *gin.Context) { txHashStr := c.Query("hash") @@ -171,7 +112,11 @@ func (h *Handler) GetQuoteRequestByTxID(c *gin.Context) { } resp := GetQuoteRequestResponse{ + Status: quoteRequest.Status.String(), + TxID: hexutil.Encode(quoteRequest.TransactionID[:]), QuoteRequestRaw: common.Bytes2Hex(quoteRequest.RawRequest), + OriginTxHash: quoteRequest.OriginTxHash.String(), + DestTxHash: quoteRequest.DestTxHash.String(), OriginChainID: quoteRequest.Transaction.OriginChainId, DestChainID: quoteRequest.Transaction.DestChainId, OriginToken: quoteRequest.Transaction.OriginToken.Hex(), @@ -196,7 +141,11 @@ func (h *Handler) GetQuoteRequestByTxHash(c *gin.Context) { } resp := GetQuoteRequestResponse{ + Status: quoteRequest.Status.String(), + TxID: hexutil.Encode(quoteRequest.TransactionID[:]), QuoteRequestRaw: common.Bytes2Hex(quoteRequest.RawRequest), + OriginTxHash: quoteRequest.OriginTxHash.String(), + DestTxHash: quoteRequest.DestTxHash.String(), OriginChainID: quoteRequest.Transaction.OriginChainId, DestChainID: quoteRequest.Transaction.DestChainId, OriginToken: quoteRequest.Transaction.OriginToken.Hex(), diff --git a/services/rfq/relayer/relapi/model.go b/services/rfq/relayer/relapi/model.go index e84c313983..a1cbb74cd7 100644 --- a/services/rfq/relayer/relapi/model.go +++ b/services/rfq/relayer/relapi/model.go @@ -1,15 +1,5 @@ package relapi -// GetQuoteRequestStatusResponse contains the schema for a GET /quote response. -type GetQuoteRequestStatusResponse struct { - Status string `json:"status"` - TxID string `json:"tx_id"` - OriginTxHash string `json:"origin_tx_hash"` - OriginChainID uint32 `json:"origin_chain_id"` - DestTxHash string `json:"dest_tx_hash"` - DestChainID uint32 `json:"dest_chain_id"` -} - // GetTxRetryResponse contains the schema for a PUT /tx/retry response. type GetTxRetryResponse struct { TxID string `json:"tx_id"` @@ -27,7 +17,11 @@ type PutRelayAckResponse struct { // GetQuoteRequestResponse is the response to a get quote request. type GetQuoteRequestResponse struct { + Status string `json:"status"` + TxID string `json:"tx_id"` QuoteRequestRaw string `json:"quote_request"` + OriginTxHash string `json:"origin_tx_hash"` + DestTxHash string `json:"dest_tx_hash"` OriginChainID uint32 `json:"origin_chain_id"` DestChainID uint32 `json:"dest_chain_id"` OriginToken string `json:"origin_token"` diff --git a/services/rfq/relayer/relapi/server.go b/services/rfq/relayer/relapi/server.go index 61cfc09919..65fff5ac70 100644 --- a/services/rfq/relayer/relapi/server.go +++ b/services/rfq/relayer/relapi/server.go @@ -95,15 +95,12 @@ func NewRelayerAPI( } const ( - getHealthRoute = "/health" - // TODO: replace with non-status specific endpoints. - getQuoteStatusByTxHashRoute = "/status" - getQuoteStatusByTxIDRoute = "/status/by_tx_id" - getRetryRoute = "/retry" - postWithdrawRoute = "/withdraw" - getTxHashByNonceRoute = "/tx_hash/by_nonce" - getRequestByTxID = "/request/by_tx_id" - getRequestByTxHash = "/request/by_tx_hash" + getHealthRoute = "/health" + getRetryRoute = "/retry" + postWithdrawRoute = "/withdraw" + getTxHashByNonceRoute = "/tx_hash/by_nonce" + getRequestByTxID = "/request/by_tx_id" + getRequestByTxHash = "/request/by_tx_hash" ) var logger = log.Logger("relayer-api") @@ -117,10 +114,6 @@ func (r *RelayerAPIServer) Run(ctx context.Context) error { // Assign GET routes engine.GET(getHealthRoute, h.GetHealth) - // TODO: replace with non-status specific endpoints - engine.GET(getQuoteStatusByTxHashRoute, h.GetQuoteRequestStatusByTxHash) - // TODO: replace with non-status specific endpoints - engine.GET(getQuoteStatusByTxIDRoute, h.GetQuoteRequestStatusByTxID) engine.GET(getRetryRoute, h.GetTxRetry) engine.GET(getRequestByTxID, h.GetQuoteRequestByTxID) engine.GET(getRequestByTxHash, h.GetQuoteRequestByTxHash) diff --git a/services/rfq/relayer/relapi/server_test.go b/services/rfq/relayer/relapi/server_test.go index 9d54bcba0c..cde56987af 100644 --- a/services/rfq/relayer/relapi/server_test.go +++ b/services/rfq/relayer/relapi/server_test.go @@ -50,7 +50,7 @@ func (c *RelayerServerSuite) TestGetQuoteRequestByTxHash() { // Fetch the quote request by tx hash client := &http.Client{} - req, err := http.NewRequestWithContext(c.GetTestContext(), http.MethodGet, fmt.Sprintf("http://localhost:%d/status?hash=%s", c.port, quoteRequest.OriginTxHash), nil) + req, err := http.NewRequestWithContext(c.GetTestContext(), http.MethodGet, fmt.Sprintf("http://localhost:%d/request/by_tx_hash?hash=%s", c.port, quoteRequest.OriginTxHash), nil) c.Require().NoError(err) resp, err := client.Do(req) c.Require().NoError(err) @@ -63,16 +63,19 @@ func (c *RelayerServerSuite) TestGetQuoteRequestByTxHash() { c.Equal(http.StatusOK, resp.StatusCode) // Compare to expected result - var result relapi.GetQuoteRequestStatusResponse + var result relapi.GetQuoteRequestResponse err = json.NewDecoder(resp.Body).Decode(&result) c.Require().NoError(err) - expectedResult := relapi.GetQuoteRequestStatusResponse{ - Status: quoteRequest.Status.String(), - TxID: hexutil.Encode(quoteRequest.TransactionID[:]), - OriginTxHash: quoteRequest.OriginTxHash.String(), - OriginChainID: quoteRequest.Transaction.OriginChainId, - DestChainID: quoteRequest.Transaction.DestChainId, - DestTxHash: quoteRequest.DestTxHash.String(), + expectedResult := relapi.GetQuoteRequestResponse{ + Status: quoteRequest.Status.String(), + TxID: hexutil.Encode(quoteRequest.TransactionID[:]), + QuoteRequestRaw: result.QuoteRequestRaw, + OriginTxHash: quoteRequest.OriginTxHash.String(), + DestTxHash: quoteRequest.DestTxHash.String(), + OriginChainID: quoteRequest.Transaction.OriginChainId, + DestChainID: quoteRequest.Transaction.DestChainId, + OriginToken: quoteRequest.Transaction.OriginToken.String(), + DestToken: quoteRequest.Transaction.DestToken.String(), } c.Equal(expectedResult, result) c.GetTestContext().Done() @@ -89,7 +92,7 @@ func (c *RelayerServerSuite) TestGetQuoteRequestByTxID() { // Fetch the quote request by tx hash client := &http.Client{} txIDStr := hexutil.Encode(quoteRequest.TransactionID[:]) - req, err := http.NewRequestWithContext(c.GetTestContext(), http.MethodGet, fmt.Sprintf("http://localhost:%d/status/by_tx_id?id=%s", c.port, txIDStr), nil) + req, err := http.NewRequestWithContext(c.GetTestContext(), http.MethodGet, fmt.Sprintf("http://localhost:%d/request/by_tx_id?id=%s", c.port, txIDStr), nil) c.Require().NoError(err) resp, err := client.Do(req) c.Require().NoError(err) @@ -102,16 +105,19 @@ func (c *RelayerServerSuite) TestGetQuoteRequestByTxID() { c.Equal(http.StatusOK, resp.StatusCode) // Compare to expected result - var result relapi.GetQuoteRequestStatusResponse + var result relapi.GetQuoteRequestResponse err = json.NewDecoder(resp.Body).Decode(&result) c.Require().NoError(err) - expectedResult := relapi.GetQuoteRequestStatusResponse{ - Status: quoteRequest.Status.String(), - TxID: hexutil.Encode(quoteRequest.TransactionID[:]), - OriginTxHash: quoteRequest.OriginTxHash.String(), - OriginChainID: quoteRequest.Transaction.OriginChainId, - DestChainID: quoteRequest.Transaction.DestChainId, - DestTxHash: quoteRequest.DestTxHash.String(), + expectedResult := relapi.GetQuoteRequestResponse{ + Status: quoteRequest.Status.String(), + TxID: hexutil.Encode(quoteRequest.TransactionID[:]), + QuoteRequestRaw: result.QuoteRequestRaw, + OriginTxHash: quoteRequest.OriginTxHash.String(), + DestTxHash: quoteRequest.DestTxHash.String(), + OriginChainID: quoteRequest.Transaction.OriginChainId, + DestChainID: quoteRequest.Transaction.DestChainId, + OriginToken: quoteRequest.Transaction.OriginToken.String(), + DestToken: quoteRequest.Transaction.DestToken.String(), } c.Equal(expectedResult, result) c.GetTestContext().Done()