Skip to content

Commit

Permalink
feat: implement batch RFQ (#573)
Browse files Browse the repository at this point in the history
* feat: add batchRFQ for hashflow-v3

* fix: update path from `v1/firm-quote/calldata` to `v1/firm-quote` as the response no longer returns `amountOut`

* Add `SupportBatch()` func for IPoolRFQ
  • Loading branch information
minhnhathoang authored Nov 8, 2024
1 parent 4e99327 commit 136a17e
Show file tree
Hide file tree
Showing 12 changed files with 118 additions and 38 deletions.
5 changes: 5 additions & 0 deletions pkg/liquidity-source/bebop/rfq.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ type IClient interface {
}

type RFQHandler struct {
pool.RFQHandler
config *Config
client IClient
}
Expand Down Expand Up @@ -57,3 +58,7 @@ func (h *RFQHandler) RFQ(ctx context.Context, params pool.RFQParams) (*pool.RFQR
Extra: result,
}, nil
}

func (h *RFQHandler) BatchRFQ(context.Context, []pool.RFQParams) ([]*pool.RFQResult, error) {
return nil, nil
}
5 changes: 5 additions & 0 deletions pkg/liquidity-source/clipper/rfq.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ type IClient interface {
}

type RFQHandler struct {
pool.RFQHandler
config *Config
client IClient
}
Expand Down Expand Up @@ -67,3 +68,7 @@ func (h *RFQHandler) RFQ(ctx context.Context, params pool.RFQParams) (*pool.RFQR
},
}, nil
}

func (h *RFQHandler) BatchRFQ(context.Context, []pool.RFQParams) ([]*pool.RFQResult, error) {
return nil, nil
}
5 changes: 5 additions & 0 deletions pkg/liquidity-source/dexalot/rfq.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ type IClient interface {
}

type RFQHandler struct {
pool.RFQHandler
config *Config
client IClient
}
Expand Down Expand Up @@ -74,3 +75,7 @@ func (h *RFQHandler) RFQ(ctx context.Context, params pool.RFQParams) (*pool.RFQR
Extra: result,
}, nil
}

func (h *RFQHandler) BatchRFQ(context.Context, []pool.RFQParams) ([]*pool.RFQResult, error) {
return nil, nil
}
78 changes: 51 additions & 27 deletions pkg/liquidity-source/hashflow-v3/rfq.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ type IClient interface {
}

type RFQHandler struct {
pool.RFQHandler
config *Config
client IClient
}
Expand All @@ -35,53 +36,76 @@ func NewRFQHandler(config *Config, client IClient) *RFQHandler {
}

func (h *RFQHandler) RFQ(ctx context.Context, params pool.RFQParams) (*pool.RFQResult, error) {
swapInfoBytes, err := json.Marshal(params.SwapInfo)
results, err := h.BatchRFQ(ctx, []pool.RFQParams{params})
if err != nil {
return nil, err
}

var swapInfo SwapInfo
if err = json.Unmarshal(swapInfoBytes, &swapInfo); err != nil {
return nil, err
return results[0], nil
}

func (h *RFQHandler) BatchRFQ(ctx context.Context, paramsSlice []pool.RFQParams) ([]*pool.RFQResult, error) {
if len(paramsSlice) == 0 {
return nil, errors.New("empty batch params")
}

result, err := h.client.RFQ(ctx, QuoteParams{
quoteParams := QuoteParams{
BaseChain: Chain{
ChainType: rfqDefaultChainType,
ChainId: params.NetworkID,
ChainId: paramsSlice[0].NetworkID,
},
QuoteChain: Chain{
ChainType: rfqDefaultChainType,
ChainId: params.NetworkID,
ChainId: paramsSlice[0].NetworkID,
},
RFQs: []RFQ{
{
BaseToken: swapInfo.BaseToken,
QuoteToken: swapInfo.QuoteToken,
BaseTokenAmount: swapInfo.BaseTokenAmount,

Trader: params.RFQRecipient,
EffectiveTrader: params.Recipient,
}

// Intentionally not specific marketMakers field to have higher chance to successfully RFQ
// MarketMakers: []string{swapInfo.MarketMaker},
for _, params := range paramsSlice {
swapInfoBytes, err := json.Marshal(params.SwapInfo)
if err != nil {
return nil, err
}

var swapInfo SwapInfo
if err = json.Unmarshal(swapInfoBytes, &swapInfo); err != nil {
return nil, err
}

quoteParams.RFQs = append(quoteParams.RFQs, RFQ{
BaseToken: swapInfo.BaseToken,
QuoteToken: swapInfo.QuoteToken,
BaseTokenAmount: swapInfo.BaseTokenAmount,
Trader: params.RFQRecipient,
EffectiveTrader: params.Recipient,

// Intentionally not specific marketMakers field to have higher chance to successfully RFQ
// MarketMakers: []string{swapInfo.MarketMaker},

ExcludeMarketMakers: h.config.ExcludeMarketMakers,
})
}

ExcludeMarketMakers: h.config.ExcludeMarketMakers,
},
},
})
quoteResult, err := h.client.RFQ(ctx, quoteParams)
if err != nil {
return nil, err
}

if len(result.Quotes) != 1 {
if len(quoteResult.Quotes) != len(paramsSlice) {
return nil, errors.New("mismatch quotes length")
}

newAmountOut, _ := new(big.Int).SetString(result.Quotes[0].QuoteData.QuoteTokenAmount, 10)
var results []*pool.RFQResult
for i := range quoteResult.Quotes {
newAmountOut, _ := new(big.Int).SetString(quoteResult.Quotes[i].QuoteData.QuoteTokenAmount, 10)
results = append(results, &pool.RFQResult{
NewAmountOut: newAmountOut,
Extra: quoteResult.Quotes[i],
})
}

return results, nil
}

return &pool.RFQResult{
NewAmountOut: newAmountOut,
Extra: result.Quotes[0],
}, nil
func (h *RFQHandler) SupportBatch() bool {
return true
}
4 changes: 2 additions & 2 deletions pkg/liquidity-source/native-v1/client/http.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import (
const (
headerApiKey = "apiKey"

pathCalldata = "v1/firm-quote/calldata"
pathFirmQuote = "v1/firm-quote"

errMsgThrottled = "ThrottlerException: Too Many Requests"
errMsgInternalServerError = "Internal server error"
Expand Down Expand Up @@ -54,7 +54,7 @@ func (c *HTTPClient) Quote(ctx context.Context, params nativev1.QuoteParams) (na
SetQueryParams(params.ToMap())

var result nativev1.QuoteResult
resp, err := req.SetResult(&result).SetError(&result).Get(pathCalldata)
resp, err := req.SetResult(&result).SetError(&result).Get(pathFirmQuote)
if err != nil {
return nativev1.QuoteResult{}, err
}
Expand Down
5 changes: 5 additions & 0 deletions pkg/liquidity-source/native-v1/rfq.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ type IClient interface {
}

type RFQHandler struct {
pool.RFQHandler
config *Config
client IClient
}
Expand Down Expand Up @@ -62,3 +63,7 @@ func (h *RFQHandler) RFQ(ctx context.Context, params pool.RFQParams) (*pool.RFQR
Extra: result,
}, nil
}

func (h *RFQHandler) BatchRFQ(context.Context, []pool.RFQParams) ([]*pool.RFQResult, error) {
return nil, nil
}
18 changes: 10 additions & 8 deletions pkg/liquidity-source/native-v1/type.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,14 +42,8 @@ type QuoteResult struct {
// The error message if any
Message string `json:"message"`

// // The address that will send the calldata to the Native router.
// From string `json:"from"`
// The address of the Native router.
To string `json:"to"`
// The raw input data that will be executed by the NativeRouter.
Calldata string `json:"calldata"`
// // The msg.value for the transaction. Will be 0 if the seller token is a non-native token.
// Value string `json:"value"`
// The tx request to be executed by the NativeRouter.
TxRequest TxRequest `json:"txRequest"`
// Amount of token to be sold, in wei unit.
AmountOut string `json:"amountOut"`
// The offset position (in bytes) of the param amountIn. You can modify this value freely. Will be undefined if the
Expand All @@ -59,3 +53,11 @@ type QuoteResult struct {
// slippage accordingly. Will be undefined if the target liquidity pool is not a native pool (non-PMM pool).
AmountOutMinimumOffset int `json:"amountOutMinimumOffset"`
}

type TxRequest struct {
// The address of the Native router.
Target string `json:"target"`
// The raw input data that will be executed by the NativeRouter.
Calldata string `json:"calldata"`
// Value string `json:"value"`
}
5 changes: 5 additions & 0 deletions pkg/liquidity-source/swaap-v2/rfq.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ type IClient interface {
}

type RFQHandler struct {
pool.RFQHandler
config *Config
client IClient
}
Expand Down Expand Up @@ -65,3 +66,7 @@ func (h *RFQHandler) RFQ(ctx context.Context, params pool.RFQParams) (*pool.RFQR
Extra: result,
}, nil
}

func (h *RFQHandler) BatchRFQ(context.Context, []pool.RFQParams) ([]*pool.RFQResult, error) {
return nil, nil
}
5 changes: 5 additions & 0 deletions pkg/source/kyber-pmm/rfq.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (
)

type RFQHandler struct {
pool.RFQHandler
config *Config
client IClient
}
Expand Down Expand Up @@ -77,3 +78,7 @@ func (h *RFQHandler) RFQ(ctx context.Context, params pool.RFQParams) (*pool.RFQR
},
}, nil
}

func (h *RFQHandler) BatchRFQ(context.Context, []pool.RFQParams) ([]*pool.RFQResult, error) {
return nil, nil
}
5 changes: 5 additions & 0 deletions pkg/source/limitorder/rfq.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import (
)

type RFQHandler struct {
pool.RFQHandler
config *Config
client *httpClient
}
Expand Down Expand Up @@ -69,3 +70,7 @@ func (h *RFQHandler) RFQ(ctx context.Context, params pool.RFQParams) (*pool.RFQR
},
}, nil
}

func (h *RFQHandler) BatchRFQ(context.Context, []pool.RFQParams) ([]*pool.RFQResult, error) {
return nil, nil
}
2 changes: 2 additions & 0 deletions pkg/source/pool/iface.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,8 @@ type IPoolExactOutSimulator interface {

type IPoolRFQ interface {
RFQ(ctx context.Context, params RFQParams) (*RFQResult, error)
BatchRFQ(ctx context.Context, paramsSlice []RFQParams) ([]*RFQResult, error)
SupportBatch() bool
}

type ITicksBasedPoolTracker interface {
Expand Down
19 changes: 18 additions & 1 deletion pkg/source/pool/rfq.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
package pool

import "math/big"
import (
"context"
"math/big"
)

type RFQParams struct {
// NetworkID blockchain network id
Expand All @@ -23,3 +26,17 @@ type RFQResult struct {
NewAmountOut *big.Int
Extra any
}

type RFQHandler struct{}

func (p *RFQHandler) RFQ(ctx context.Context, params RFQParams) (*RFQResult, error) {
return nil, nil
}

func (p *RFQHandler) BatchRFQ(ctx context.Context, paramsSlice []RFQParams) ([]*RFQResult, error) {
return nil, nil
}

func (p *RFQHandler) SupportBatch() bool {
return false
}

0 comments on commit 136a17e

Please sign in to comment.