Skip to content

Commit

Permalink
fix(mx-trading): reduce inventory limit (#656)
Browse files Browse the repository at this point in the history
  • Loading branch information
minhnhathoang authored Dec 17, 2024
1 parent 4ee6cdf commit fb49260
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 13 deletions.
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))
// 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
}

0 comments on commit fb49260

Please sign in to comment.