/
pool.go
178 lines (167 loc) 路 6.02 KB
/
pool.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
package keeper
import (
sdk "github.com/cosmos/cosmos-sdk/types"
sdkerrors "github.com/cosmos/cosmos-sdk/types/errors"
utils "github.com/crescent-network/crescent/v5/types"
"github.com/crescent-network/crescent/v5/x/amm/types"
exchangetypes "github.com/crescent-network/crescent/v5/x/exchange/types"
)
func (k Keeper) CreatePool(ctx sdk.Context, creatorAddr sdk.AccAddress, marketId uint64, price sdk.Dec) (pool types.Pool, err error) {
market, found := k.exchangeKeeper.GetMarket(ctx, marketId)
if !found {
err = sdkerrors.Wrap(sdkerrors.ErrNotFound, "market not found")
return
}
if found := k.LookupPoolByMarket(ctx, market.Id); found {
err = sdkerrors.Wrap(sdkerrors.ErrInvalidRequest, "cannot create more than one pool per market")
return
}
creationFee := k.GetPoolCreationFee(ctx)
if creationFee.IsAllPositive() {
if err = k.bankKeeper.SendCoinsFromAccountToModule(ctx, creatorAddr, types.ModuleName, creationFee); err != nil {
err = sdkerrors.Wrap(err, "insufficient pool creation fee")
return
}
}
// Create a new pool
poolId := k.GetNextPoolIdWithUpdate(ctx)
defaultTickSpacing := k.GetDefaultTickSpacing(ctx)
defaultMinOrderQty := k.GetDefaultMinOrderQuantity(ctx)
defaultMinOrderQuote := k.GetDefaultMinOrderQuote(ctx)
pool = types.NewPool(
poolId, marketId, market.BaseDenom, market.QuoteDenom, defaultTickSpacing,
defaultMinOrderQty, defaultMinOrderQuote)
k.SetPool(ctx, pool)
k.SetPoolByMarketIndex(ctx, pool)
k.SetPoolByReserveAddressIndex(ctx, pool)
// Set initial pool state
state := types.NewPoolState(exchangetypes.TickAtPrice(price), price)
k.SetPoolState(ctx, pool.Id, state)
if err = ctx.EventManager().EmitTypedEvent(&types.EventCreatePool{
Creator: creatorAddr.String(),
MarketId: marketId,
Price: price,
PoolId: poolId,
}); err != nil {
return
}
return pool, nil
}
func (k Keeper) IteratePoolOrders(ctx sdk.Context, pool types.Pool, isBuy bool, cb func(price, qty, openQty sdk.Dec) (stop bool)) {
poolState := k.MustGetPoolState(ctx, pool.Id)
reserveBalance := k.bankKeeper.SpendableCoins(ctx, pool.MustGetReserveAddress()).
AmountOf(pool.DenomOut(isBuy)).ToDec()
orderLiquidity := poolState.CurrentLiquidity
currentPrice := poolState.CurrentPrice
iterCb := func(tick int32, tickInfo types.TickInfo) (stop bool) {
if orderLiquidity.IsPositive() {
for {
if !reserveBalance.IsPositive() {
return true
}
var (
orderTick int32
valid bool
)
if isBuy {
orderTick, valid = NextOrderTick(
true, orderLiquidity, currentPrice, pool.MinOrderQuantity, pool.MinOrderQuote, pool.TickSpacing)
if !valid || orderTick < tick {
orderTick = tick
}
} else {
orderTick, valid = NextOrderTick(
false, orderLiquidity, currentPrice, pool.MinOrderQuantity, pool.MinOrderQuote, pool.TickSpacing)
if !valid || orderTick > tick {
orderTick = tick
}
}
orderPrice := exchangetypes.PriceAtTick(orderTick)
orderSqrtPrice := utils.DecApproxSqrt(orderPrice)
currentSqrtPrice := utils.DecApproxSqrt(currentPrice)
var qty, openQty sdk.Dec
if isBuy {
qty = types.Amount1DeltaDec(currentSqrtPrice, orderSqrtPrice, orderLiquidity).QuoTruncate(orderPrice)
openQty = sdk.MinDec(reserveBalance.QuoTruncate(orderPrice), qty)
} else {
qty = types.Amount0DeltaRoundingDec(currentSqrtPrice, orderSqrtPrice, orderLiquidity, false)
openQty = sdk.MinDec(reserveBalance, qty)
}
if openQty.IsPositive() && (orderTick == tick || (openQty.GTE(pool.MinOrderQuantity))) {
if cb(orderPrice, qty, openQty) {
return true
}
reserveBalance = reserveBalance.Sub(exchangetypes.DepositAmount(isBuy, orderPrice, qty))
currentPrice = orderPrice
} else { // No more possible order price
break
}
if orderTick == tick {
break
}
}
} else {
currentPrice = exchangetypes.PriceAtTick(tick)
}
if isBuy {
orderLiquidity = orderLiquidity.Sub(tickInfo.NetLiquidity)
} else {
orderLiquidity = orderLiquidity.Add(tickInfo.NetLiquidity)
}
return false
}
if isBuy {
k.IterateTickInfosBelow(ctx, pool.Id, poolState.CurrentTick, true, iterCb)
} else {
k.IterateTickInfosAbove(ctx, pool.Id, poolState.CurrentTick, iterCb)
}
}
func NextOrderTick(
isBuy bool, liquidity sdk.Int, currentPrice, minOrderQty, minOrderQuote sdk.Dec, tickSpacing uint32) (tick int32, valid bool) {
currentSqrtPrice := utils.DecApproxSqrt(currentPrice)
liquidityDec := liquidity.ToDec()
if isBuy {
// 1. Check min order qty
// L^2 + 4 * MinQty * L * sqrt(P_current)
intermediate := liquidityDec.Power(2).Add(
minOrderQty.Mul(liquidityDec).MulTruncate(currentSqrtPrice).MulInt64(4))
orderSqrtPrice := utils.DecApproxSqrt(intermediate).Sub(liquidityDec).QuoTruncate(minOrderQty.MulInt64(2))
if !orderSqrtPrice.IsPositive() {
return 0, false
}
// 2. Check min order quote
orderSqrtPrice2 := currentSqrtPrice.Mul(liquidityDec).Sub(minOrderQuote).QuoTruncate(liquidityDec)
if !orderSqrtPrice2.IsPositive() {
return 0, false
}
orderPrice := sdk.MinDec(orderSqrtPrice, orderSqrtPrice2).Power(2)
if orderPrice.GT(currentPrice) {
return 0, false
}
tick = types.AdjustPriceToTickSpacing(orderPrice, tickSpacing, false)
if orderPrice.Equal(currentPrice) { // This implies PriceAtTick(tick) == currentPrice
tick -= int32(tickSpacing)
}
return tick, true
}
// 1. Check min order qty
orderSqrtPrice := currentSqrtPrice.Mul(liquidityDec).
QuoRoundUp(liquidityDec.Sub(minOrderQty.Mul(currentSqrtPrice)))
if !orderSqrtPrice.IsPositive() {
return 0, false
}
// 2. Check min order quote
orderSqrtPrice2 := minOrderQuote.Mul(currentSqrtPrice).QuoRoundUp(liquidityDec).Add(currentSqrtPrice)
if !orderSqrtPrice2.IsPositive() {
return 0, false
}
orderPrice := sdk.MaxDec(orderSqrtPrice, orderSqrtPrice2).Power(2)
if orderPrice.LT(currentPrice) {
return 0, false
}
tick = types.AdjustPriceToTickSpacing(orderPrice, tickSpacing, true)
if orderPrice.Equal(currentPrice) { // This implies PriceAtTick(tick) == currentPrice
tick += int32(tickSpacing)
}
return tick, true
}