Skip to content

Commit

Permalink
fix: add linear pool order price gap ratio function
Browse files Browse the repository at this point in the history
  • Loading branch information
hallazzang committed Jun 12, 2022
1 parent 175e7e4 commit d7ed46b
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 5 deletions.
1 change: 1 addition & 0 deletions x/liquidity/amm/amm.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,5 @@ var (
MinCoinAmount = sdk.NewInt(100)
MaxCoinAmount = sdk.NewIntWithDecimal(1, 40)
MinPoolOrderPriceGapRatio = sdk.NewDecWithPrec(5, 4) // 5bp(0.05%)
MaxPoolOrderPriceGapRatio = sdk.NewDecWithPrec(5, 3) // 50bp(0.5%)
)
14 changes: 10 additions & 4 deletions x/liquidity/amm/pool.go
Original file line number Diff line number Diff line change
Expand Up @@ -199,7 +199,7 @@ func (pool *BasicPool) SellAmountTo(price sdk.Dec) sdk.Int {
}

func PoolBuyOrders(pool Pool, lowestPrice, highestPrice sdk.Dec, tickPrec int) []Order {
minPriceGap := pool.Price().Mul(MinPoolOrderPriceGapRatio)
poolPrice := pool.Price()
tmpPool := pool
var orders []Order
placeOrder := func(price sdk.Dec, amt sdk.Int) {
Expand All @@ -225,13 +225,16 @@ func PoolBuyOrders(pool Pool, lowestPrice, highestPrice sdk.Dec, tickPrec int) [
if !rx.IsPositive() {
break
}
tick = PriceToDownTick(tick.Sub(minPriceGap), tickPrec)
priceDiffRatio := tick.Sub(poolPrice).Quo(poolPrice).Abs()
priceGap := pool.Price().Mul(PoolOrderPriceGapRatio(
MinPoolOrderPriceGapRatio, MaxPoolOrderPriceGapRatio, priceDiffRatio, sdk.NewDecWithPrec(1, 1)))
tick = PriceToDownTick(tick.Sub(priceGap), tickPrec)
}
return orders
}

func PoolSellOrders(pool Pool, lowestPrice, highestPrice sdk.Dec, tickPrec int) []Order {
minPriceGap := pool.Price().Mul(MinPoolOrderPriceGapRatio)
poolPrice := pool.Price()
tmpPool := pool
var orders []Order
placeOrder := func(price sdk.Dec, amt sdk.Int) {
Expand All @@ -257,7 +260,10 @@ func PoolSellOrders(pool Pool, lowestPrice, highestPrice sdk.Dec, tickPrec int)
if !ry.GT(MinCoinAmount) {
break
}
tick = PriceToUpTick(tick.Add(minPriceGap), tickPrec)
priceDiffRatio := tick.Sub(poolPrice).Quo(poolPrice).Abs()
priceGap := pool.Price().Mul(PoolOrderPriceGapRatio(
MinPoolOrderPriceGapRatio, MaxPoolOrderPriceGapRatio, priceDiffRatio, sdk.NewDecWithPrec(1, 1)))
tick = PriceToUpTick(tick.Add(priceGap), tickPrec)
}
return orders
}
Expand Down
8 changes: 8 additions & 0 deletions x/liquidity/amm/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,3 +18,11 @@ func OfferCoinAmount(dir OrderDirection, price sdk.Dec, amt sdk.Int) sdk.Int {
panic(fmt.Sprintf("invalid order direction: %s", dir))
}
}

func PoolOrderPriceGapRatio(min, max, priceDiffRatio, maxPriceLimitRatio sdk.Dec) sdk.Dec {
if priceDiffRatio.IsZero() {
return min
}
a := max.Sub(min).Quo(maxPriceLimitRatio)
return a.Mul(priceDiffRatio).Add(min)
}
2 changes: 1 addition & 1 deletion x/liquidity/types/orderbook_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ func ExampleMakeOrderBookResponse() {

func BenchmarkMakeOrderBookResponse(b *testing.B) {
for i := 0; i < b.N; i++ {
makeOrderBookPairResponse(1000, 10, 20, 4)
makeOrderBookPairResponse(100, 10, 20, 4)
}
}

Expand Down

0 comments on commit d7ed46b

Please sign in to comment.