/
limit_order_tranche.go
162 lines (139 loc) · 4.69 KB
/
limit_order_tranche.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
package types
import (
"github.com/QuadrateOrg/core/x/dex/utils"
math_utils "github.com/QuadrateOrg/core/x/dex/utils/math"
sdk "github.com/cosmos/cosmos-sdk/types"
)
func NewLimitOrderTranche(
makerDenom string,
takerDenom string,
trancheKey string,
tickIndex int64,
reservesMakerDenom sdk.Int,
reservesTakerDenom sdk.Int,
totalMakerDenom sdk.Int,
totalTakerDenom sdk.Int,
) (*LimitOrderTranche, error) {
tradePairID, err := NewTradePairID(takerDenom, makerDenom)
if err != nil {
return nil, err
}
priceTakerToMaker, err := tradePairID.PriceTakerToMaker(tickIndex)
if err != nil {
return nil, err
}
return &LimitOrderTranche{
Key: &LimitOrderTrancheKey{
TradePairID: tradePairID,
TrancheKey: trancheKey,
TickIndexTakerToMaker: tickIndex,
},
ReservesMakerDenom: reservesMakerDenom,
ReservesTakerDenom: reservesTakerDenom,
TotalMakerDenom: totalMakerDenom,
TotalTakerDenom: totalTakerDenom,
PriceTakerToMaker: priceTakerToMaker,
}, nil
}
// Useful for testing
func MustNewLimitOrderTranche(
makerDenom string,
takerDenom string,
trancheKey string,
tickIndex int64,
reservesMakerDenom sdk.Int,
reservesTakerDenom sdk.Int,
totalMakerDenom sdk.Int,
totalTakerDenom sdk.Int,
) *LimitOrderTranche {
limitOrderTranche, err := NewLimitOrderTranche(
makerDenom,
takerDenom,
trancheKey,
tickIndex,
reservesMakerDenom,
reservesTakerDenom,
totalMakerDenom,
totalTakerDenom,
)
if err != nil {
panic(err)
}
return limitOrderTranche
}
func (t LimitOrderTranche) IsPlaceTranche() bool {
return t.ReservesMakerDenom.Equal(t.TotalMakerDenom)
}
func (t LimitOrderTranche) IsFilled() bool {
return t.ReservesMakerDenom.IsZero()
}
func (t LimitOrderTranche) IsJIT() bool {
return t.ExpirationTime != nil && *t.ExpirationTime == JITGoodTilTime()
}
func (t LimitOrderTranche) IsExpired(ctx sdk.Context) bool {
return t.ExpirationTime != nil && !t.IsJIT() && !t.ExpirationTime.After(ctx.BlockTime())
}
func (t LimitOrderTranche) HasTokenIn() bool {
return t.ReservesMakerDenom.GT(sdk.ZeroInt())
}
func (t LimitOrderTranche) HasTokenOut() bool {
return t.ReservesTakerDenom.GT(sdk.ZeroInt())
}
func (t LimitOrderTranche) Price() math_utils.PrecDec {
return t.PriceTakerToMaker
}
func (t LimitOrderTranche) RatioFilled() math_utils.PrecDec {
amountFilled := t.PriceTakerToMaker.MulInt(t.TotalTakerDenom)
ratioFilled := amountFilled.QuoInt(t.TotalMakerDenom)
return ratioFilled
}
func (t LimitOrderTranche) AmountUnfilled() math_utils.PrecDec {
amountFilled := t.PriceTakerToMaker.MulInt(t.TotalTakerDenom)
return math_utils.NewPrecDecFromInt(t.TotalMakerDenom).Sub(amountFilled)
}
func (t LimitOrderTranche) HasLiquidity() bool {
return t.ReservesMakerDenom.GT(sdk.ZeroInt())
}
func (t *LimitOrderTranche) RemoveTokenIn(
trancheUser *LimitOrderTrancheUser,
) (amountToRemove sdk.Int) {
amountUnfilled := t.AmountUnfilled()
maxAmountToRemove := amountUnfilled.MulInt(trancheUser.SharesOwned).
QuoInt(t.TotalMakerDenom).
TruncateInt()
amountToRemove = maxAmountToRemove.Sub(trancheUser.SharesCancelled)
t.ReservesMakerDenom = t.ReservesMakerDenom.Sub(amountToRemove)
return amountToRemove
}
func (t *LimitOrderTranche) Withdraw(trancheUser *LimitOrderTrancheUser) (sdk.Int, math_utils.PrecDec) {
reservesTokenOutDec := math_utils.NewPrecDecFromInt(t.ReservesTakerDenom)
ratioFilled := t.RatioFilled()
maxAllowedToWithdraw := ratioFilled.MulInt(trancheUser.SharesOwned).TruncateInt()
amountOutTokenIn := maxAllowedToWithdraw.Sub(trancheUser.SharesWithdrawn)
amountOutTokenOut := math_utils.NewPrecDecFromInt(amountOutTokenIn).Quo(t.PriceTakerToMaker)
t.ReservesTakerDenom = reservesTokenOutDec.Sub(amountOutTokenOut).TruncateInt()
return amountOutTokenIn, amountOutTokenOut
}
func (t *LimitOrderTranche) Swap(maxAmountTakerIn sdk.Int, maxAmountMakerOut *sdk.Int) (
inAmount sdk.Int,
outAmount sdk.Int,
) {
reservesTokenOut := &t.ReservesMakerDenom
fillTokenIn := &t.ReservesTakerDenom
totalTokenIn := &t.TotalTakerDenom
maxOutGivenIn := t.PriceTakerToMaker.MulInt(maxAmountTakerIn).TruncateInt()
possibleOutAmounts := []sdk.Int{*reservesTokenOut, maxOutGivenIn}
if maxAmountMakerOut != nil {
possibleOutAmounts = append(possibleOutAmounts, *maxAmountMakerOut)
}
outAmount = utils.MinIntArr(possibleOutAmounts)
inAmount = math_utils.NewPrecDecFromInt(outAmount).Quo(t.PriceTakerToMaker).Ceil().TruncateInt()
*fillTokenIn = fillTokenIn.Add(inAmount)
*totalTokenIn = totalTokenIn.Add(inAmount)
*reservesTokenOut = reservesTokenOut.Sub(outAmount)
return inAmount, outAmount
}
func (t *LimitOrderTranche) PlaceMakerLimitOrder(amountIn sdk.Int) {
t.ReservesMakerDenom = t.ReservesMakerDenom.Add(amountIn)
t.TotalMakerDenom = t.TotalMakerDenom.Add(amountIn)
}