-
Notifications
You must be signed in to change notification settings - Fork 32
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
Add MinRebalanceAmount to RFQ relayer #2263
Changes from 3 commits
11e1ac4
7347ae6
e74b193
6f0e782
3f55ebb
c751c46
5e75220
a2423d9
112197b
43436e3
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -466,12 +466,18 @@ func getRebalance(span trace.Span, cfg relconfig.Config, tokens map[int]map[comm | |
initialThresh, _ := new(big.Float).Mul(new(big.Float).SetInt(totalBalance), big.NewFloat(initialPct/100)).Int(nil) | ||
amount := new(big.Int).Sub(maxTokenData.Balance, initialThresh) | ||
|
||
// no need to rebalance since amount would be negative | ||
if amount.Cmp(big.NewInt(0)) < 0 { | ||
// no need to rebalance since amount would not be positive | ||
if amount.Cmp(big.NewInt(0)) <= 0 { | ||
//nolint:nilnil | ||
return nil, nil | ||
} | ||
|
||
// clip the rebalance amount by the configured min | ||
minAmount := cfg.GetMinRebalanceAmount(maxTokenData.ChainID, maxTokenData.Addr) | ||
if amount.Cmp(minAmount) < 0 { | ||
amount = minAmount | ||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The implementation of clipping the rebalance amount by the configured minimum before the maximum is correctly aligned with the PR objectives. However, it's crucial to ensure that the Additionally, incorporating logging or metrics around rebalance amounts could facilitate monitoring and debugging of the rebalancing process. This would provide visibility into the operation of the rebalance logic, especially in production environments. Would you like assistance in adding error handling and logging around these configurations? |
||
|
||
// clip the rebalance amount by the configured max | ||
maxAmount := cfg.GetMaxRebalanceAmount(maxTokenData.ChainID, maxTokenData.Addr) | ||
if amount.Cmp(maxAmount) > 0 { | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -88,7 +88,7 @@ func (i *InventoryTestSuite) TestGetRebalance() { | |
usdcDataDest.Addr: &usdcDataDest, | ||
}, | ||
} | ||
getConfig := func(maxRebalanceAmount string) relconfig.Config { | ||
getConfig := func(minRebalanceAmount, maxRebalanceAmount string) relconfig.Config { | ||
return relconfig.Config{ | ||
Chains: map[int]relconfig.ChainConfig{ | ||
origin: { | ||
|
@@ -98,6 +98,7 @@ func (i *InventoryTestSuite) TestGetRebalance() { | |
Decimals: 6, | ||
MaintenanceBalancePct: 20, | ||
InitialBalancePct: 50, | ||
MinRebalanceAmount: minRebalanceAmount, | ||
MaxRebalanceAmount: maxRebalanceAmount, | ||
}, | ||
}, | ||
|
@@ -109,6 +110,7 @@ func (i *InventoryTestSuite) TestGetRebalance() { | |
Decimals: 6, | ||
MaintenanceBalancePct: 20, | ||
InitialBalancePct: 50, | ||
MinRebalanceAmount: minRebalanceAmount, | ||
MaxRebalanceAmount: maxRebalanceAmount, | ||
}, | ||
}, | ||
|
@@ -120,6 +122,7 @@ func (i *InventoryTestSuite) TestGetRebalance() { | |
Decimals: 6, | ||
MaintenanceBalancePct: 0, | ||
InitialBalancePct: 0, | ||
MinRebalanceAmount: minRebalanceAmount, | ||
MaxRebalanceAmount: maxRebalanceAmount, | ||
}, | ||
}, | ||
|
@@ -129,13 +132,20 @@ func (i *InventoryTestSuite) TestGetRebalance() { | |
} | ||
|
||
// 10 USDC on both chains; no rebalance needed | ||
cfg := getConfig("") | ||
cfg := getConfig("", "") | ||
usdcDataOrigin.Balance = big.NewInt(1e7) | ||
usdcDataDest.Balance = big.NewInt(1e7) | ||
rebalance, err := inventory.GetRebalance(cfg, tokens, origin, usdcDataOrigin.Addr) | ||
i.NoError(err) | ||
i.Nil(rebalance) | ||
|
||
// Set balances to zero | ||
usdcDataOrigin.Balance = big.NewInt(0) | ||
usdcDataDest.Balance = big.NewInt(0) | ||
rebalance, err = inventory.GetRebalance(cfg, tokens, origin, usdcDataOrigin.Addr) | ||
i.NoError(err) | ||
i.Nil(rebalance) | ||
|
||
// Set origin balance below maintenance threshold; need rebalance | ||
usdcDataOrigin.Balance = big.NewInt(9e6) | ||
usdcDataDest.Balance = big.NewInt(1e6) | ||
|
@@ -148,8 +158,19 @@ func (i *InventoryTestSuite) TestGetRebalance() { | |
} | ||
i.Equal(expected, rebalance) | ||
|
||
// Set min rebalance amount | ||
cfgWithMax := getConfig("10", "1000000000") | ||
rebalance, err = inventory.GetRebalance(cfgWithMax, tokens, origin, usdcDataOrigin.Addr) | ||
i.NoError(err) | ||
expected = &inventory.RebalanceData{ | ||
OriginMetadata: &usdcDataOrigin, | ||
DestMetadata: &usdcDataDest, | ||
Amount: big.NewInt(1e7), | ||
} | ||
i.Equal(expected, rebalance) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The test case setup using Would you like me to help generate these additional test cases? |
||
|
||
// Set max rebalance amount | ||
cfgWithMax := getConfig("1.1") | ||
cfgWithMax = getConfig("0", "1.1") | ||
rebalance, err = inventory.GetRebalance(cfgWithMax, tokens, origin, usdcDataOrigin.Addr) | ||
i.NoError(err) | ||
expected = &inventory.RebalanceData{ | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The implementation correctly applies the
MinRebalanceAmount
before considering the maximum rebalance amount. This ensures that rebalances are not executed for amounts below the specified minimum threshold, aligning with the PR's objectives. However, it's important to ensure thatcfg.GetMinRebalanceAmount
andcfg.GetMaxRebalanceAmount
handle potential errors gracefully and return sensible defaults in case of misconfigurations or missing values. Additionally, consider adding logging or metrics around rebalance amounts to facilitate monitoring and debugging of the rebalancing process.