Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(rfq-relayer): add MaxBalance param #2917

Merged
merged 21 commits into from
Aug 7, 2024
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 8 additions & 4 deletions services/rfq/relayer/quoter/quoter.go
Original file line number Diff line number Diff line change
Expand Up @@ -519,13 +519,17 @@ func (m *Manager) getOriginAmount(parentCtx context.Context, origin, dest int, a
quoteAmount = minQuoteAmount
}

// Finally, clip the quoteAmount by the balance
if quoteAmount.Cmp(balance) > 0 {
span.AddEvent("quote amount greater than balance", trace.WithAttributes(
// Finally, clip the quoteAmount by the minimum balance
minBalance := m.config.GetMinBalance(dest, address)
quotableBalance := new(big.Int).Sub(balance, minBalance)
ChiTimesChi marked this conversation as resolved.
Show resolved Hide resolved
if quoteAmount.Cmp(quotableBalance) > 0 {
span.AddEvent("quote amount greater than quotable balance", trace.WithAttributes(
dwasse marked this conversation as resolved.
Show resolved Hide resolved
attribute.String("quote_amount", quoteAmount.String()),
attribute.String("balance", balance.String()),
attribute.String("quotable_balance", quotableBalance.String()),
attribute.String("min_balance", minBalance.String()),
))
quoteAmount = balance
quoteAmount = quotableBalance
}

// Deduct gas cost from the quote amount, if necessary
Expand Down
20 changes: 14 additions & 6 deletions services/rfq/relayer/quoter/quoter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -164,10 +164,11 @@ func (s *QuoterSuite) TestGetOriginAmount() {
address := common.HexToAddress("0x0b2c639c533813f4aa9d7837caf62653d097ff85")
balance := big.NewInt(1000_000_000) // 1000 USDC

setQuoteParams := func(quotePct, quoteOffset float64, minQuoteAmount string) {
setQuoteParams := func(quotePct, quoteOffset float64, minQuoteAmount string, minBalance string) {
s.config.BaseChainConfig.QuotePct = quotePct
destTokenCfg := s.config.Chains[dest].Tokens["USDC"]
destTokenCfg.MinQuoteAmount = minQuoteAmount
destTokenCfg.MinBalance = minBalance
originTokenCfg := s.config.Chains[origin].Tokens["USDC"]
originTokenCfg.QuoteOffsetBps = quoteOffset
s.config.Chains[dest].Tokens["USDC"] = destTokenCfg
Expand All @@ -182,40 +183,47 @@ func (s *QuoterSuite) TestGetOriginAmount() {
s.Equal(expectedAmount, quoteAmount)

// Set QuotePct to 50 with MinQuoteAmount of 0; should be 50% of balance.
setQuoteParams(50, 0, "0")
setQuoteParams(50, 0, "0", "0")
quoteAmount, err = s.manager.GetOriginAmount(s.GetTestContext(), origin, dest, address, balance)
s.NoError(err)
expectedAmount = big.NewInt(500_000_000)
s.Equal(expectedAmount, quoteAmount)

// Set QuotePct to 50 with QuoteOffset of -1%. Should be 1% less than 50% of balance.
setQuoteParams(50, -100, "0")
setQuoteParams(50, -100, "0", "0")
quoteAmount, err = s.manager.GetOriginAmount(s.GetTestContext(), origin, dest, address, balance)
s.NoError(err)
expectedAmount = big.NewInt(495_000_000)
s.Equal(expectedAmount, quoteAmount)

// Set QuotePct to 25 with MinQuoteAmount of 500; should be 50% of balance.
setQuoteParams(25, 0, "500")
setQuoteParams(25, 0, "500", "0")
quoteAmount, err = s.manager.GetOriginAmount(s.GetTestContext(), origin, dest, address, balance)
s.NoError(err)
expectedAmount = big.NewInt(500_000_000)
s.Equal(expectedAmount, quoteAmount)

// Set QuotePct to 25 with MinQuoteAmount of 500; should be 50% of balance.
setQuoteParams(25, 0, "500")
setQuoteParams(25, 0, "500", "0")
quoteAmount, err = s.manager.GetOriginAmount(s.GetTestContext(), origin, dest, address, balance)
s.NoError(err)
expectedAmount = big.NewInt(500_000_000)
s.Equal(expectedAmount, quoteAmount)

// Set QuotePct to 25 with MinQuoteAmount of 1500; should be total balance.
setQuoteParams(25, 0, "1500")
setQuoteParams(25, 0, "1500", "0")
quoteAmount, err = s.manager.GetOriginAmount(s.GetTestContext(), origin, dest, address, balance)
s.NoError(err)
expectedAmount = big.NewInt(1000_000_000)
s.Equal(expectedAmount, quoteAmount)

// Set QuotePct to 25 with MinQuoteAmount of 1500 and MinBalance of 200; should be total balance minus 500.
setQuoteParams(25, 0, "1500", "200")
quoteAmount, err = s.manager.GetOriginAmount(s.GetTestContext(), origin, dest, address, balance)
s.NoError(err)
expectedAmount = big.NewInt(800_000_000)
s.Equal(expectedAmount, quoteAmount)

// Toggle insufficient gas; should be 0.
s.setGasSufficiency(false)
quoteAmount, err = s.manager.GetOriginAmount(s.GetTestContext(), origin, dest, address, balance)
Expand Down
2 changes: 2 additions & 0 deletions services/rfq/relayer/relconfig/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,8 @@ type TokenConfig struct {
// Note that this value can be positive or negative; if positive it effectively increases the quoted price
// of the given token, and vice versa.
QuoteOffsetBps float64 `yaml:"quote_offset_bps"`
// MinBalance is the minimum balance that should be leftover from quoting, in human-readable units.
MinBalance string `yaml:"min_balance"`
}

// DatabaseConfig represents the configuration for the database.
Expand Down
35 changes: 35 additions & 0 deletions services/rfq/relayer/relconfig/getters.go
Original file line number Diff line number Diff line change
Expand Up @@ -298,6 +298,41 @@ func (c Config) GetQuoteOffsetBps(chainID int, tokenName string, isOrigin bool)
return offset, nil
}

const defaultMinBalance = 0

// GetMinBalance returns the MinBalance for the given chain and address.
// Note that this getter returns the value in native token decimals.
func (c Config) GetMinBalance(chainID int, addr common.Address) *big.Int {
chainCfg, ok := c.Chains[chainID]
if !ok {
return big.NewInt(defaultMinBalance)
}

var tokenCfg *TokenConfig
for _, cfg := range chainCfg.Tokens {
if common.HexToAddress(cfg.Address).Hex() == addr.Hex() {
cfgCopy := cfg
tokenCfg = &cfgCopy
break
}
}
if tokenCfg == nil {
return big.NewInt(defaultMinBalance)
}
quoteAmountFlt, ok := new(big.Float).SetString(tokenCfg.MinBalance)
if !ok {
return big.NewInt(defaultMinBalance)
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Logic: Potential issue: If MinBalance is not a valid string representation of a big.Float, the function defaults to 0 without logging an error.

}
if quoteAmountFlt.Cmp(big.NewFloat(0)) <= 0 {
return big.NewInt(defaultMinBalance)
}
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Logic: Potential issue: If MinBalance is less than or equal to 0, the function defaults to 0 without logging an error.


// Scale the minBalance by the token decimals.
denomDecimalsFactor := new(big.Int).Exp(big.NewInt(10), big.NewInt(int64(tokenCfg.Decimals)), nil)
quoteAmountScaled, _ := new(big.Float).Mul(quoteAmountFlt, new(big.Float).SetInt(denomDecimalsFactor)).Int(nil)
return quoteAmountScaled
}

// GetQuoteWidthBps returns the QuoteWidthBps for the given chainID.
func (c Config) GetQuoteWidthBps(chainID int) (value float64, err error) {
rawValue, err := c.getChainConfigValue(chainID, "QuoteWidthBps")
Expand Down
Loading