Skip to content

Commit

Permalink
feat(rfq-api): don't respond to passive quotes for requests with zap …
Browse files Browse the repository at this point in the history
…params [SLT-430, SLT-432] (#3388)

* Feat: add zap params to api model

* Feat: don't consider quotes with zap data as passive quotes

* Feat: add test cases for zap quotes

* Cleanup: lint
  • Loading branch information
dwasse authored Nov 14, 2024
1 parent 23d4a7a commit 83cef1d
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 9 deletions.
5 changes: 4 additions & 1 deletion services/rfq/api/model/request.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,8 +55,11 @@ type QuoteData struct {
DestChainID int `json:"dest_chain_id"`
OriginTokenAddr string `json:"origin_token_addr"`
DestTokenAddr string `json:"dest_token_addr"`
OriginAmountExact string `json:"origin_amount_exact"`
OriginAmount string `json:"origin_amount"`
ExpirationWindow int64 `json:"expiration_window"`
ZapData string `json:"zap_data"`
ZapNative string `json:"zap_native"`
OriginAmountExact string `json:"origin_amount_exact"`
DestAmount *string `json:"dest_amount"`
RelayerAddress *string `json:"relayer_address"`
QuoteID *string `json:"quote_id"`
Expand Down
10 changes: 10 additions & 0 deletions services/rfq/api/rest/rfq_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -307,6 +307,16 @@ func (c *ServerSuite) TestActiveRFQFallbackToPassive() {
c.Assert().Equal("passive", userQuoteResp.QuoteType)
c.Assert().Equal("998000", userQuoteResp.DestAmount) // destAmount is quote destAmount minus fixed fee
c.Assert().Equal(c.relayerWallets[0].Address().Hex(), userQuoteResp.RelayerAddress)

// Submit a user quote request with zap data
userQuoteReq.Data.ZapData = "abc"
userQuoteReq.Data.ZapNative = "100"
userQuoteResp, err = userClient.PutRFQRequest(c.GetTestContext(), userQuoteReq)
c.Require().NoError(err)

// Assert the response
c.Assert().False(userQuoteResp.Success)
c.Assert().Equal("no quotes found", userQuoteResp.Reason)
}

func (c *ServerSuite) TestActiveRFQPassiveBestQuote() {
Expand Down
29 changes: 22 additions & 7 deletions services/rfq/api/rest/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ package rest
import (
"context"
"encoding/json"
"math/big"

"fmt"
"net/http"
Expand Down Expand Up @@ -532,19 +533,21 @@ func (r *QuoterAPIServer) PutRFQRequest(c *gin.Context) {
span.SetAttributes(attribute.Bool("is_active_rfq", isActiveRFQ))

// if specified, fetch the active quote. always consider passive quotes
var activeQuote *model.QuoteData
var activeQuote, passiveQuote *model.QuoteData
if isActiveRFQ {
activeQuote = r.handleActiveRFQ(ctx, &req, requestID)
if activeQuote != nil && activeQuote.DestAmount != nil {
span.SetAttributes(attribute.String("active_quote_dest_amount", *activeQuote.DestAmount))
}
}
passiveQuote, err := r.handlePassiveRFQ(ctx, &req)
if err != nil {
logger.Error("Error handling passive RFQ", "error", err)
}
if passiveQuote != nil && passiveQuote.DestAmount != nil {
span.SetAttributes(attribute.String("passive_quote_dest_amount", *passiveQuote.DestAmount))
if !isZapQuote(&req) {
passiveQuote, err = r.handlePassiveRFQ(ctx, &req)
if err != nil {
logger.Error("Error handling passive RFQ", "error", err)
}
if passiveQuote != nil && passiveQuote.DestAmount != nil {
span.SetAttributes(attribute.String("passive_quote_dest_amount", *passiveQuote.DestAmount))
}
}
quote := getBestQuote(activeQuote, passiveQuote)

Expand Down Expand Up @@ -576,6 +579,18 @@ func (r *QuoterAPIServer) PutRFQRequest(c *gin.Context) {
c.JSON(http.StatusOK, resp)
}

func isZapQuote(req *model.PutRFQRequest) bool {
if req.Data.ZapData != "" {
return true
}

zapNative, ok := new(big.Int).SetString(req.Data.ZapNative, 10)
if !ok {
return false
}
return zapNative.Sign() != 0
}

func (r *QuoterAPIServer) recordLatestQuoteAge(ctx context.Context, observer metric.Observer) (err error) {
if r.handler == nil || r.latestQuoteAgeGauge == nil {
return nil
Expand Down
6 changes: 5 additions & 1 deletion services/rfq/api/rest/ws.go
Original file line number Diff line number Diff line change
Expand Up @@ -198,6 +198,8 @@ func (c *wsClient) sendRelayerRequest(ctx context.Context, req *model.WsRFQReque

// handleRelayerMessage handles messages from the relayer.
// An error returned will result in the websocket connection being closed.
//
//nolint:cyclop
func (c *wsClient) handleRelayerMessage(ctx context.Context, msg []byte) (err error) {
_, span := c.handler.Tracer().Start(ctx, "handleRelayerMessage", trace.WithAttributes(
attribute.String("relayer_address", c.relayerAddr),
Expand Down Expand Up @@ -235,7 +237,9 @@ func (c *wsClient) handleRelayerMessage(ctx context.Context, msg []byte) (err er
}
case SendQuoteOp:
err = c.handleSendQuote(ctx, rfqMsg.Content)
logger.Errorf("error handling send quote: %v", err)
if err != nil {
logger.Errorf("error handling send quote: %v", err)
}
default:
logger.Errorf("received unexpected operation from relayer: %s", rfqMsg.Op)
return nil
Expand Down

0 comments on commit 83cef1d

Please sign in to comment.