/
participation.go
241 lines (200 loc) · 6.96 KB
/
participation.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
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
package keeper
import (
sdkerrors "cosmossdk.io/errors"
sdkmath "cosmossdk.io/math"
sdk "github.com/cosmos/cosmos-sdk/types"
housetypes "github.com/sge-network/sge/x/house/types"
markettypes "github.com/sge-network/sge/x/market/types"
"github.com/sge-network/sge/x/orderbook/types"
)
// SetOrderBookParticipation sets a book participation.
func (k Keeper) SetOrderBookParticipation(ctx sdk.Context, participation types.OrderBookParticipation) {
participationKey := types.GetOrderBookParticipationKey(
participation.OrderBookUID,
participation.Index,
)
store := k.getParticipationStore(ctx)
b := k.cdc.MustMarshal(&participation)
store.Set(participationKey, b)
}
// GetOrderBookParticipation returns a specific participation of an order book by index.
func (k Keeper) GetOrderBookParticipation(
ctx sdk.Context,
bookUID string,
index uint64,
) (val types.OrderBookParticipation, found bool) {
store := k.getParticipationStore(ctx)
participationKey := types.GetOrderBookParticipationKey(bookUID, index)
b := store.Get(participationKey)
if b == nil {
return val, false
}
k.cdc.MustUnmarshal(b, &val)
return val, true
}
// GetParticipationsOfOrderBook returns all participations for an order book.
func (k Keeper) GetParticipationsOfOrderBook(
ctx sdk.Context,
bookUID string,
) (list []types.OrderBookParticipation, err error) {
store := k.getParticipationStore(ctx)
iterator := sdk.KVStorePrefixIterator(store, types.GetOrderBookParticipationsKey(bookUID))
defer func() {
err = iterator.Close()
}()
for ; iterator.Valid(); iterator.Next() {
var val types.OrderBookParticipation
k.cdc.MustUnmarshal(iterator.Value(), &val)
list = append(list, val)
}
return
}
// GetAllOrderBookParticipations returns all book participations used during genesis dump.
func (k Keeper) GetAllOrderBookParticipations(
ctx sdk.Context,
) (list []types.OrderBookParticipation, err error) {
store := k.getParticipationStore(ctx)
iterator := sdk.KVStorePrefixIterator(store, []byte{})
defer func() {
err = iterator.Close()
}()
for ; iterator.Valid(); iterator.Next() {
var val types.OrderBookParticipation
k.cdc.MustUnmarshal(iterator.Value(), &val)
list = append(list, val)
}
return
}
// InitiateOrderBookParticipation starts a participation on a book for a certain account.
//
//nolint:nakedret
func (k Keeper) InitiateOrderBookParticipation(
ctx sdk.Context, addr sdk.AccAddress, bookUID string, depositAmount, feeAmount sdkmath.Int,
) (index uint64, err error) {
market, found := k.marketKeeper.GetMarket(ctx, bookUID)
if !found {
err = sdkerrors.Wrapf(types.ErrMarketNotFound, "%s", bookUID)
return
}
if market.Status != markettypes.MarketStatus_MARKET_STATUS_ACTIVE {
err = sdkerrors.Wrapf(types.ErrParticipationOnInactiveMarket, "%s", bookUID)
return
}
book, found := k.GetOrderBook(ctx, bookUID)
if !found {
err = sdkerrors.Wrapf(types.ErrOrderBookNotFound, "%s", bookUID)
return
}
if book.Status != types.OrderBookStatus_ORDER_BOOK_STATUS_STATUS_ACTIVE {
err = sdkerrors.Wrapf(types.ErrOrderBookNotActive, "%s", book.Status)
return
}
// check if the maximum allowed participations is met or not.
if k.GetMaxOrderBookParticipationsAllowed(ctx) <= book.ParticipationCount {
err = sdkerrors.Wrapf(types.ErrMaxNumberOfParticipationsReached, "%d", book.ParticipationCount)
return
}
// calculate new sequential participation id
book.ParticipationCount++
index = book.ParticipationCount
_, found = k.GetOrderBookParticipation(ctx, book.UID, index)
// This should never happen, just a sanity check
if found {
err = sdkerrors.Wrapf(types.ErrBookParticipationAlreadyExists, "id already exists %d", index)
return
}
liquidity := depositAmount.Sub(feeAmount)
bookParticipation := types.NewOrderBookParticipation(
index, book.UID, addr.String(),
book.OddsCount, // all odds need to be filled in the next steps
liquidity, feeAmount, liquidity, // int the start, liquidity and current round liquidity are the same
sdkmath.ZeroInt(), sdkmath.ZeroInt(), sdkmath.ZeroInt(), sdkmath.Int{}, "", sdkmath.ZeroInt(),
)
// fund order book liquidity pool from participant's account.
if err = k.fund(types.OrderBookLiquidityFunder{}, ctx, addr, liquidity); err != nil {
return
}
// fund house fee collector from participant's account.
if err = k.fund(housetypes.HouseFeeCollectorFunder{}, ctx, addr, feeAmount); err != nil {
return
}
// Make entry for book participation
k.SetOrderBookParticipation(ctx, bookParticipation)
// Update book odds exposures and add participant exposures
if err = k.initParticipationExposures(ctx, book.UID, index); err != nil {
return
}
// Update orderbook
k.SetOrderBook(ctx, book)
return index, nil
}
func (k Keeper) CalcWithdrawalAmount(
ctx sdk.Context,
depositorAddress string,
marketUID string,
participationIndex uint64,
mode housetypes.WithdrawalMode,
totalWithdrawnAmount sdkmath.Int,
amount sdkmath.Int,
) (sdkmath.Int, error) {
bp, found := k.GetOrderBookParticipation(ctx, marketUID, participationIndex)
if !found {
return sdkmath.Int{}, sdkerrors.Wrapf(
types.ErrOrderBookParticipationNotFound,
"%s, %d",
marketUID,
participationIndex,
)
}
if bp.IsSettled {
return sdkmath.Int{}, sdkerrors.Wrapf(
types.ErrBookParticipationAlreadySettled,
"%s, %d",
bp.OrderBookUID,
participationIndex,
)
}
if bp.ParticipantAddress != depositorAddress {
return sdkmath.Int{}, sdkerrors.Wrapf(types.ErrMismatchInDepositorAddress, "%s", bp.ParticipantAddress)
}
bpE, err := k.GetExposureByOrderBookAndParticipationIndex(ctx, marketUID, participationIndex)
if err != nil || len(bpE) == 0 {
return sdkmath.Int{}, sdkerrors.Wrapf(types.ErrParticipationExposuresNotFound, "%d", participationIndex)
}
if bpE[0].Round != 1 {
return sdkmath.Int{}, sdkerrors.Wrapf(types.ErrWithdrawalNotAllowedPostRequeing, "%d", bpE[0].Round)
}
if mode == housetypes.WithdrawalMode_WITHDRAWAL_MODE_PARTIAL {
if bp.Liquidity.Sub(totalWithdrawnAmount).LT(amount) {
return sdkmath.Int{}, sdkerrors.Wrapf(types.ErrWithdrawalTooLarge, "%d", amount.Int64())
}
}
withdrawAmount, err := bp.WithdrawableAmount(mode, amount)
if err != nil {
return sdkmath.Int{}, err
}
return withdrawAmount, nil
}
// WithdrawOrderBookParticipation withdraws the order book participation to the bettor's account
func (k Keeper) WithdrawOrderBookParticipation(
ctx sdk.Context, marketUID string,
participationIndex uint64,
amount sdkmath.Int,
) error {
bp, found := k.GetOrderBookParticipation(ctx, marketUID, participationIndex)
if !found {
return sdkerrors.Wrapf(
types.ErrOrderBookParticipationNotFound,
"%s, %d",
marketUID,
participationIndex,
)
}
// refund participant's account from order book liquidity pool.
if err := k.refund(types.OrderBookLiquidityFunder{}, ctx, sdk.MustAccAddressFromBech32(bp.ParticipantAddress), amount); err != nil {
return err
}
bp.SetLiquidityAfterWithdrawal(amount)
k.SetOrderBookParticipation(ctx, bp)
return k.removeNotWithdrawableFromFulfillmentQueue(ctx, bp)
}