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

fix(mx-trading): reduce inventory limit #656

Merged
merged 1 commit into from
Dec 17, 2024
Merged
Show file tree
Hide file tree
Changes from all 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
6 changes: 5 additions & 1 deletion pkg/liquidity-source/mx-trading/pool_simulator.go
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,11 @@ func (p *PoolSimulator) CalculateLimit() map[string]*big.Int {
tokens, reserves := p.GetTokens(), p.GetReserves()
inventory := make(map[string]*big.Int, len(tokens))
for i, token := range tokens {
inventory[token] = new(big.Int).Set(reserves[i])
var reducedReserve big.Float
reducedReserve.SetInt(reserves[i]).Mul(&reducedReserve, big.NewFloat(0.95))
Copy link
Member

Choose a reason for hiding this comment

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

Might be faster to do div 20 then sub, but this is fine

// Reduce each token's reserve by 5% to prevent the total quote amount of a token
// from potentially exceeding the maker's balance when building the route
inventory[token], _ = reducedReserve.Int(nil)
}

return inventory
Expand Down
43 changes: 31 additions & 12 deletions pkg/liquidity-source/mx-trading/pool_simulator_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,13 +84,19 @@ func TestPoolSimulator_GetAmountOut(t *testing.T) {
expectedErr: ErrAmountInIsGreaterThanTotalLevelSize,
},
{
name: "it should return correct amountOut1 when all levels are filled",
amountIn0: bignumber.NewBig("4929473425227476992"),
name: "it should return correct amountOut1 when all levels are filled",
amountIn0: bignumber.NewBig("4929473425227476992"),
expectedErr: ErrAmountOutIsGreaterThanInventory,

// Expected output before reducing inventory limit
expectedAmountOut: bignumber.NewBig("16488225768595991298048"),
},
{
name: "it should return correct amountOut0 when all levels are filled",
amountIn1: bignumber.NewBig("201363156713348041539584"),
name: "it should return correct amountOut0 when all levels are filled",
amountIn1: bignumber.NewBig("201363156713348041539584"),
expectedErr: ErrAmountOutIsGreaterThanInventory,

// Expected output before reducing inventory limit
expectedAmountOut: bignumber.NewBig("59925038314246815744"),
},
{
Expand Down Expand Up @@ -198,8 +204,11 @@ func TestPoolSimulator_UpdateBalance(t *testing.T) {
},
},
{
name: "fill all levels 0to1",
amountIn0: bignumber.NewBig("4929473425227476992"),
name: "fill all levels 0to1",
amountIn0: bignumber.NewBig("4929473425227476992"),
expectedErr: ErrAmountOutIsGreaterThanInventory,

// Expected output before reducing inventory limit
expectedZeroToOnePriceLevels: nil,
expectedOneToZeroPriceLevels: []PriceLevel{
{Size: 546.1, Price: 0.00029818453400477844},
Expand Down Expand Up @@ -237,8 +246,11 @@ func TestPoolSimulator_UpdateBalance(t *testing.T) {
},
},
{
name: "fill token1",
amountIn1: bignumber.NewBig("201362500000000000000000"),
name: "fill token1",
amountIn1: bignumber.NewBig("201362500000000000000000"),
expectedErr: ErrAmountOutIsGreaterThanInventory,

// Expected output before reducing inventory limit
expectedZeroToOnePriceLevels: []PriceLevel{
{Size: 0.719, Price: 3347.4385889037885},
{Size: 0.015, Price: 3347.141106167435},
Expand Down Expand Up @@ -284,8 +296,8 @@ func TestPoolSimulator_UpdateBalance(t *testing.T) {
}
limit := swaplimit.NewInventory("mx-trading", p.CalculateLimit())

assert.Equal(t, entityPoolData.Reserves[0], limit.GetLimit(p.token0.Address).String())
assert.Equal(t, entityPoolData.Reserves[1], limit.GetLimit(p.token1.Address).String())
assert.Equal(t, getReducedReserve(p.GetReserves()[0]).String(), limit.GetLimit(p.token0.Address).String())
assert.Equal(t, getReducedReserve(p.GetReserves()[1]).String(), limit.GetLimit(p.token1.Address).String())

calcAmountOutResult, err := p.CalcAmountOut(pool.CalcAmountOutParams{
TokenAmountIn: pool.TokenAmount{Token: token, Amount: amountIn},
Expand All @@ -309,13 +321,20 @@ func TestPoolSimulator_UpdateBalance(t *testing.T) {

tokenInIndex := p.GetTokenIndex(token)
assert.Equal(t,
new(big.Int).Add(p.GetReserves()[tokenInIndex], amountIn).String(),
new(big.Int).Add(getReducedReserve(p.GetReserves()[tokenInIndex]), amountIn).String(),
limit.GetLimit(token).String(),
)
assert.Equal(t,
new(big.Int).Sub(p.GetReserves()[1-tokenInIndex], calcAmountOutResult.TokenAmountOut.Amount).String(),
new(big.Int).Sub(getReducedReserve(p.GetReserves()[1-tokenInIndex]), calcAmountOutResult.TokenAmountOut.Amount).String(),
limit.GetLimit(calcAmountOutResult.TokenAmountOut.Token).String(),
)
})
}
}

func getReducedReserve(reserve *big.Int) *big.Int {
reducedReserve := new(big.Float).SetInt(reserve)
reducedReserve.Mul(reducedReserve, big.NewFloat(0.95))
reserveInt, _ := reducedReserve.Int(nil)
return reserveInt
}
Loading