/
bet_place.go
364 lines (313 loc) · 11.9 KB
/
bet_place.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
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
package keeper
import (
sdk "github.com/cosmos/cosmos-sdk/types"
sdkerrors "github.com/cosmos/cosmos-sdk/types/errors"
"github.com/spf13/cast"
bettypes "github.com/furynet/furynetwork/x/bet/types"
"github.com/furynet/furynetwork/x/strategicreserve/types"
)
// ProcessBetPlacement processes bet placement
func (k Keeper) ProcessBetPlacement(
ctx sdk.Context,
betUID, bookUID, oddsUID string,
maxLossMultiplier sdk.Dec,
betAmount sdk.Int,
payoutProfit sdk.Dec,
bettorAddress sdk.AccAddress,
betFee sdk.Int,
oddsType bettypes.OddsType,
oddsVal string, betID uint64,
) ([]*bettypes.BetFulfillment, error) {
// If lock exists, return error
// Lock already exists means the bet is already placed for the given bet-uid
if k.payoutLockExists(ctx, betUID) {
return nil, sdkerrors.Wrapf(types.ErrLockAlreadyExists, "%s", betUID)
}
// get book data by its id
book, found := k.GetOrderBook(ctx, bookUID)
if !found {
return nil, sdkerrors.Wrapf(types.ErrOrderBookNotFound, "%s", bookUID)
}
// get exposures of the odds bettor is placing the bet
bookExposure, found := k.GetOrderBookOddsExposure(ctx, bookUID, oddsUID)
if !found {
return nil, sdkerrors.Wrapf(types.ErrOrderBookExposureNotFound, "%s , %s", bookUID, oddsUID)
}
betFulfillments, updatedFulfillmentQueue, fulfiledBetAmount, err := k.fulfillQueueBets(ctx,
betUID,
betID,
oddsUID,
oddsVal,
oddsType,
betAmount,
payoutProfit,
maxLossMultiplier,
&book,
&bookExposure,
)
if err != nil {
return nil, err
}
bookExposure.FulfillmentQueue = updatedFulfillmentQueue
k.SetOrderBookOddsExposure(ctx, bookExposure)
// Transfer bet fee from bettor to the `bet` module account
if err = k.transferFundsFromAccountToModule(ctx, bettorAddress, bettypes.ModuleName, betFee); err != nil {
return nil, err
}
// Transfer bet amount from bettor to `book_liquidity_pool` Account
if err = k.transferFundsFromAccountToModule(ctx, bettorAddress, types.OrderBookLiquidityName, fulfiledBetAmount); err != nil {
return nil, err
}
// Create a unique lock in the Payout Store for the bet
k.SetPayoutLock(ctx, betUID)
return betFulfillments, nil
}
func (k Keeper) fulfillQueueBets(
ctx sdk.Context,
betUID string, betID uint64,
oddsUID, oddsVal string, oddsType bettypes.OddsType,
betAmount sdk.Int,
payoutProfit sdk.Dec,
maxLossMultiplier sdk.Dec,
book *types.OrderBook,
bookExposure *types.OrderBookOddsExposure,
) (
betFulfillments []*bettypes.BetFulfillment,
updatedQueue []uint64,
fulfiledBetAmount sdk.Int,
err error,
) {
participationMap, participationExposureMap, err := k.getExposuresMap(ctx, oddsUID, book)
if err != nil {
return
}
// initialize the fulfilled bet amount with 0
fulfiledBetAmount = sdk.NewInt(0)
updatedQueue = bookExposure.FulfillmentQueue
// the decimal amount that is being lost in the bet amount calculation from payout profit
truncatedBetAmount := sdk.NewDec(0)
// continue until updatedFulfillmentQueue gets empty
for len(updatedQueue) > 0 {
var participation types.OrderBookParticipation
var participationExposure types.ParticipationExposure
participationIndex := updatedQueue[0]
getQueueItemInfo := func() error {
var found bool
participation, found = participationMap[participationIndex]
if !found {
return sdkerrors.Wrapf(types.ErrOrderBookParticipationNotFound, "%s, %d", book.UID, participationIndex)
}
participationExposure, found = participationExposureMap[participationIndex]
if !found {
return sdkerrors.Wrapf(types.ErrParticipationExposureNotFound, "%s, %d", book.UID, participationIndex)
}
if participationExposure.IsFulfilled {
return sdkerrors.Wrapf(types.ErrParticipationExposureAlreadyFilled, "%s, %d", book.UID, participationIndex)
}
return nil
}
removeQueueItem := func() {
updatedQueue = updatedQueue[1:]
}
setFulfilled := func() {
participationExposure.IsFulfilled = true
participation.ExposuresNotFilled--
}
fulfill := func(payoutProfitToFulfill sdk.Int, isLastFulfillment bool) error {
var betAmountToFulfill sdk.Int
// if the fulfillment is in the last state,
// the bet amount should be fulfilled totally
// because there is no decimal truncation.
if isLastFulfillment {
// the bet amount
betAmountToFulfill = betAmount
} else {
// findout what would be the bet amount according to the pyout profit
expectedBetAmountDec, err := bettypes.CalculateBetAmount(oddsType, oddsVal, payoutProfitToFulfill.ToDec())
if err != nil {
return err
}
// add previous loop truncated value to the calculated bet amount
expectedBetAmountDec = expectedBetAmountDec.Add(truncatedBetAmount)
// we need for the bet amount to be of type sdk.Int
// so the truncation in inevitable
betAmountToFulfill = expectedBetAmountDec.TruncateInt()
// save the truncated amount in the calculations for the next loop
truncatedBetAmount = truncatedBetAmount.Add(expectedBetAmountDec.Sub(betAmountToFulfill.ToDec()))
}
if !isLastFulfillment {
setFulfilled()
}
// add the payout profit to the
participationExposure.Exposure = participationExposure.Exposure.Add(payoutProfitToFulfill)
// add the bet amount that is being fulfilled to the exposure and participation
participationExposure.BetAmount = participationExposure.BetAmount.Add(betAmountToFulfill)
participation.TotalBetAmount = participation.TotalBetAmount.Add(betAmountToFulfill)
participation.CurrentRoundTotalBetAmount = participation.CurrentRoundTotalBetAmount.Add(betAmountToFulfill)
// max loss is the maximum amount that an exposure may lose.
maxLoss := participationExposure.CalculateMaxLoss(participation.CurrentRoundTotalBetAmount)
switch {
case participation.CurrentRoundMaxLoss.IsNil():
participation.CurrentRoundMaxLoss = maxLoss
participation.CurrentRoundMaxLossOddsUID = oddsUID
case participation.CurrentRoundMaxLossOddsUID == oddsUID:
participation.CurrentRoundMaxLoss = maxLoss
default:
originalMaxLoss := participation.CalculateMaxLoss(betAmountToFulfill)
if maxLoss.GT(originalMaxLoss) {
participation.CurrentRoundMaxLoss = maxLoss
participation.CurrentRoundMaxLossOddsUID = oddsUID
} else {
participation.CurrentRoundMaxLoss = originalMaxLoss
}
}
betFulfillments = append(betFulfillments, &bettypes.BetFulfillment{
ParticipantAddress: participation.ParticipantAddress,
ParticipationIndex: participation.Index,
BetAmount: betAmountToFulfill,
PayoutProfit: payoutProfitToFulfill,
})
// the amount has been fulfulled, so it should be subtracted from the bet amount of the
betAmount = betAmount.Sub(betAmountToFulfill)
// add the flfilled bet amount to the fulfillment amount tracker variable
fulfiledBetAmount = fulfiledBetAmount.Add(betAmountToFulfill)
// subtract the payout profit that is fulfilled from the initial payout profit
// to prevent being calculated multiple times
payoutProfit = payoutProfit.Sub(payoutProfitToFulfill.ToDec())
// store the bet pair in the state
participationBetPair := types.NewParticipationBetPair(participation.OrderBookUID, betUID, participation.Index)
k.SetParticipationBetPair(ctx, participationBetPair, betID)
return nil
}
refreshQueueAndState := func() (err error) {
maxLoss := sdk.MaxInt(sdk.ZeroInt(), participation.CurrentRoundMaxLoss)
participation.CurrentRoundLiquidity = participation.CurrentRoundLiquidity.Sub(maxLoss)
// check if there is more liquidity amount
eligibleForNextRound := participation.CurrentRoundLiquidity.GT(sdk.ZeroInt())
participationExposures, err := k.GetExposureByOrderBookAndParticipationIndex(ctx, book.UID, participationIndex)
if err != nil {
return
}
// prepare the participation exposure map for the next round of calculations.
for _, pe := range participationExposures {
k.MoveToHistoricalParticipationExposure(ctx, pe)
if eligibleForNextRound {
newPe := types.NewParticipationExposure(book.UID, pe.OddsUID, sdk.ZeroInt(), sdk.ZeroInt(), pe.ParticipationIndex, pe.Round+1, false)
k.SetParticipationExposure(ctx, newPe)
if pe.OddsUID == participationExposure.OddsUID {
participationExposure = newPe
participationExposureMap[pe.ParticipationIndex] = participationExposure
}
}
}
// prepare participation for the next round
participation.ExposuresNotFilled = book.OddsCount
participation.CurrentRoundTotalBetAmount = sdk.ZeroInt()
participation.MaxLoss = participation.MaxLoss.Add(participation.CurrentRoundMaxLoss)
participation.CurrentRoundMaxLoss = sdk.ZeroInt()
participationMap[participation.Index] = participation
k.SetOrderBookParticipation(ctx, participation)
if eligibleForNextRound {
var boes []types.OrderBookOddsExposure
boes, err = k.GetOddsExposuresByOrderBook(ctx, book.UID)
if err != nil {
return
}
for i, boe := range boes {
boe.FulfillmentQueue = append(boe.FulfillmentQueue, participationIndex)
if boe.OddsUID == participationExposure.OddsUID {
// use the index to prevent implicit memory aliasing.
bookExposure = &boes[i]
}
k.SetOrderBookOddsExposure(ctx, boe)
}
updatedQueue = append(updatedQueue, participationIndex)
bookExposure.FulfillmentQueue = updatedQueue
}
return nil
}
// fill participation and exposure values
err = getQueueItemInfo()
if err != nil {
return
}
// availableLiquidty is the available amount of tokens to be used from the participation exposure
availableLiquidty := maxLossMultiplier.
MulInt(participation.CurrentRoundLiquidity).
Sub(sdk.NewDecFromInt(participationExposure.Exposure)).TruncateInt()
switch {
case availableLiquidty.LTE(sdk.ZeroInt()):
setFulfilled()
removeQueueItem()
case availableLiquidty.ToDec().LTE(payoutProfit):
// if the available liquidity is less than remaining payout profit that
// need to be paid, we should use all of available liquidity pull for the calculations.
err = fulfill(availableLiquidty, false)
if err != nil {
return
}
removeQueueItem()
default:
// availableLiquidty is positive and more than remaining payout profit that
// need to be paid, so we can cover all of payout profits with available liquidity.
// this case appends the last fulfillment
err = fulfill(payoutProfit.TruncateInt(), true)
if err != nil {
return
}
}
k.SetParticipationExposure(ctx, participationExposure)
k.SetOrderBookParticipation(ctx, participation)
// if there are no more exposures to be filled
if participation.ExposuresNotFilled == 0 {
err = refreshQueueAndState()
if err != nil {
return
}
}
// if the remaining payout is less than 1.00, means that the decimal part will be ignored
if payoutProfit.LT(sdk.OneDec()) || len(updatedQueue) == 0 {
break
}
}
if payoutProfit.GTE(sdk.OneDec()) {
err = sdkerrors.Wrapf(types.ErrInternalProcessingBet, "insufficient liquidity in order book")
return
}
return betFulfillments, updatedQueue, fulfiledBetAmount, nil
}
func (k Keeper) getExposuresMap(
ctx sdk.Context,
oddsUID string,
book *types.OrderBook,
) (
participationMap map[uint64]types.OrderBookParticipation,
participationExposureMap map[uint64]types.ParticipationExposure,
err error,
) {
participationMap = make(map[uint64]types.OrderBookParticipation)
participationExposureMap = make(map[uint64]types.ParticipationExposure)
bps, err := k.GetParticipationsOfOrderBook(ctx, book.UID)
if err != nil {
return
}
if book.ParticipationCount != cast.ToUint64(len(bps)) {
err = sdkerrors.Wrapf(types.ErrBookParticipationsNotFound, "%s", book.UID)
return
}
for _, bp := range bps {
participationMap[bp.Index] = bp
}
pes, err := k.GetExposureByOrderBookAndOdds(ctx, book.UID, oddsUID)
if err != nil {
return
}
if book.ParticipationCount != cast.ToUint64(len(pes)) {
err = sdkerrors.Wrapf(types.ErrParticipationExposuresNotFound, "%s, %s", book.UID, oddsUID)
return
}
for _, pe := range pes {
participationExposureMap[pe.ParticipationIndex] = pe
}
return participationMap, participationExposureMap, nil
}