Skip to content

Commit

Permalink
fix(rfq-relayer): respond with failure if quote amount is zero [SLT-4…
Browse files Browse the repository at this point in the history
…59] (#3391)

* Feat: return success=false if dest amount is zero

* Feat: add getQuoteResponse helper

* Feat: add test for the no quotes case

* Cleanup: explicit checks for quote type

* Cleanup: comparisons
  • Loading branch information
dwasse authored Nov 20, 2024
1 parent ff1e9ea commit 79a40b5
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 8 deletions.
9 changes: 9 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,15 @@ 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 the user quote request with a large origin amount, expect no quotes will be found
userQuoteReq.Data.OriginAmountExact = big.NewInt(1e18).String()
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
33 changes: 25 additions & 8 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 @@ -547,20 +548,35 @@ func (r *QuoterAPIServer) PutRFQRequest(c *gin.Context) {
span.SetAttributes(attribute.String("passive_quote_dest_amount", *passiveQuote.DestAmount))
}
quote := getBestQuote(activeQuote, passiveQuote)
var quoteType string
if quote == activeQuote {
quoteType = quoteTypeActive
} else if quote == passiveQuote {
quoteType = quoteTypePassive
}

// build and return the response
resp := getQuoteResponse(ctx, quote, quoteType)
c.JSON(http.StatusOK, resp)
}

func getQuoteResponse(ctx context.Context, quote *model.QuoteData, quoteType string) (resp model.PutRFQResponse) {
span := trace.SpanFromContext(ctx)

// construct the response
var resp model.PutRFQResponse
if quote == nil {
destAmount := big.NewInt(0)
if quote != nil && quote.DestAmount != nil {
amt, ok := destAmount.SetString(*quote.DestAmount, 10)
if ok {
destAmount = amt
}
}
if destAmount.Sign() <= 0 {
span.AddEvent("no quotes found")
resp = model.PutRFQResponse{
Success: false,
Reason: "no quotes found",
}
} else {
quoteType := quoteTypeActive
if activeQuote == nil {
quoteType = quoteTypePassive
}
span.SetAttributes(
attribute.String("quote_type", quoteType),
attribute.String("quote_dest_amount", *quote.DestAmount),
Expand All @@ -573,7 +589,8 @@ func (r *QuoterAPIServer) PutRFQRequest(c *gin.Context) {
RelayerAddress: *quote.RelayerAddress,
}
}
c.JSON(http.StatusOK, resp)

return resp
}

func (r *QuoterAPIServer) recordLatestQuoteAge(ctx context.Context, observer metric.Observer) (err error) {
Expand Down

0 comments on commit 79a40b5

Please sign in to comment.