-
Notifications
You must be signed in to change notification settings - Fork 0
/
bet_place.go
286 lines (249 loc) · 9.85 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
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/furya-network/furya/x/bet/types"
"github.com/furya-network/furya/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,
) (betFulfillments []*bettypes.BetFulfillment, err 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) {
err = sdkerrors.Wrapf(types.ErrLockAlreadyExists, "%s", betUID)
return
}
// get book data by its id
book, found := k.GetBook(ctx, bookUID)
if !found {
err = sdkerrors.Wrapf(types.ErrOrderBookNotFound, "%s", bookUID)
return
}
// get exposures of the odds user is placing the bet
bookExposure, found := k.GetBookOddsExposure(ctx, bookUID, oddsUID)
if !found {
err = sdkerrors.Wrapf(types.ErrOrderBookExposureNotFound, "%s , %s", bookUID, oddsUID)
return
}
// make maps for participation and its exposures
participationMap := make(map[uint64]types.BookParticipation)
participationExposureMap := make(map[uint64]types.ParticipationExposure)
bps, err := k.GetParticipationsOfBook(ctx, book.ID)
if err != nil {
return
}
if book.ParticipationCount != cast.ToUint64(len(bps)) {
err = sdkerrors.Wrapf(types.ErrBookParticipationsNotFound, "%s", bookUID)
return
}
for _, bp := range bps {
participationMap[bp.Index] = bp
}
pes, err := k.GetExposureByBookAndOdds(ctx, bookUID, oddsUID)
if err != nil {
return
}
if book.ParticipationCount != cast.ToUint64(len(pes)) {
err = sdkerrors.Wrapf(types.ErrParticipationExposuresNotFound, "%s, %s", bookUID, oddsUID)
return
}
for _, pe := range pes {
participationExposureMap[pe.ParticipationIndex] = pe
}
remainingPayoutProfit, updatedFulfillmentQueue := payoutProfit, bookExposure.FulfillmentQueue
fulfiledBetAmount := sdk.ZeroInt()
truncatedBetAmount := sdk.NewDec(0)
// continue until updatedFulfillmentQueue gets empty
for len(updatedFulfillmentQueue) > 0 {
var participation types.BookParticipation
var participationExposure types.ParticipationExposure
participationIndex := updatedFulfillmentQueue[0]
getQueueItemParticipation := func() error {
participation, found = participationMap[participationIndex]
if !found {
return sdkerrors.Wrapf(types.ErrBookParticipationNotFound, "%s, %d", bookUID, participationIndex)
}
participationExposure, found = participationExposureMap[participationIndex]
if !found {
return sdkerrors.Wrapf(types.ErrParticipationExposureNotFound, "%s, %d", bookUID, participationIndex)
}
if participationExposure.IsFulfilled {
return sdkerrors.Wrapf(types.ErrParticipationExposureAlreadyFilled, "%s, %d", bookUID, participationIndex)
}
return nil
}
removeQueueItem := func() {
updatedFulfillmentQueue = updatedFulfillmentQueue[1:]
}
setFulfilled := func() {
participationExposure.IsFulfilled = true
participation.ExposuresNotFilled--
}
processBetFulfillment := func(payoutToFulfill sdk.Int, isLastFulfillment bool) error {
var betAmountToFulfill sdk.Int
if isLastFulfillment {
// the bet amount
betAmountToFulfill = betAmount
} else {
expectedBetAmountDec, err := bettypes.CalculateBetAmount(oddsType, oddsVal, payoutToFulfill.ToDec())
if err != nil {
return err
}
expectedBetAmountDec = expectedBetAmountDec.Add(truncatedBetAmount)
betAmountToFulfill = expectedBetAmountDec.TruncateInt()
truncatedBetAmount = truncatedBetAmount.Add(expectedBetAmountDec.Sub(betAmountToFulfill.ToDec()))
}
if !isLastFulfillment {
setFulfilled()
}
participationExposure.Exposure = participationExposure.Exposure.Add(payoutToFulfill)
participationExposure.BetAmount = participationExposure.BetAmount.Add(betAmountToFulfill)
participation.TotalBetAmount = participation.TotalBetAmount.Add(betAmountToFulfill)
participation.CurrentRoundTotalBetAmount = participation.CurrentRoundTotalBetAmount.Add(betAmountToFulfill)
maxLoss := participationExposure.Exposure.Sub(participation.CurrentRoundTotalBetAmount).Add(participationExposure.BetAmount)
switch {
case participation.CurrentRoundMaxLoss.IsNil():
participation.CurrentRoundMaxLoss = maxLoss
participation.CurrentRoundMaxLossOddsUID = oddsUID
case participation.CurrentRoundMaxLossOddsUID == oddsUID:
participation.CurrentRoundMaxLoss = maxLoss
default:
originalMaxLoss := participation.CurrentRoundMaxLoss.Sub(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,
PayoutAmount: payoutToFulfill,
})
betAmount = betAmount.Sub(betAmountToFulfill)
fulfiledBetAmount = fulfiledBetAmount.Add(betAmountToFulfill)
remainingPayoutProfit = remainingPayoutProfit.Sub(payoutToFulfill.ToDec())
participationBetPair := types.NewParticipationBetPair(participation.BookUID, 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.GetExposureByBookAndParticipationIndex(ctx, bookUID, participationIndex)
if err != nil {
return
}
for _, pe := range participationExposures {
k.MoveToHistoricalParticipationExposure(ctx, pe)
if eligibleForNextRound {
newPe := types.NewParticipationExposure(book.ID, 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
}
}
}
participation.ExposuresNotFilled = book.OddsCount
participation.CurrentRoundTotalBetAmount = sdk.ZeroInt()
participation.MaxLoss = participation.MaxLoss.Add(participation.CurrentRoundMaxLoss)
participation.CurrentRoundMaxLoss = sdk.ZeroInt()
participationMap[participation.Index] = participation
k.SetBookParticipation(ctx, participation)
if eligibleForNextRound {
var boes []types.BookOddsExposure
boes, err = k.GetOddsExposuresByBook(ctx, bookUID)
if err != nil {
return
}
for _, boe := range boes {
boe.FulfillmentQueue = append(boe.FulfillmentQueue, participationIndex)
if boe.OddsUID == participationExposure.OddsUID {
bookExposure = boe
}
k.SetBookOddsExposure(ctx, boe)
}
updatedFulfillmentQueue = append(updatedFulfillmentQueue, participationIndex)
bookExposure.FulfillmentQueue = updatedFulfillmentQueue
}
return nil
}
// fill participation and exposure values
err = getQueueItemParticipation()
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(remainingPayoutProfit):
// 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 := processBetFulfillment(availableLiquidty, false)
if err != nil {
return betFulfillments, err
}
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 := processBetFulfillment(remainingPayoutProfit.TruncateInt(), true)
if err != nil {
return betFulfillments, err
}
}
k.SetParticipationExposure(ctx, participationExposure)
k.SetBookParticipation(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 remainingPayoutProfit.LT(sdk.OneDec()) || len(updatedFulfillmentQueue) == 0 {
break
}
}
if remainingPayoutProfit.GTE(sdk.OneDec()) {
return betFulfillments, sdkerrors.Wrapf(types.ErrInternalProcessingBet, "insufficient liquidity in order book")
}
bookExposure.FulfillmentQueue = updatedFulfillmentQueue
k.SetBookOddsExposure(ctx, bookExposure)
// Transfer bet fee from bettor to the `bet` module account
err = k.transferFundsFromUserToModule(ctx, bettorAddress, bettypes.ModuleName, betFee)
if err != nil {
return betFulfillments, err
}
// Transfer bet amount from bettor to `book_liquidity_pool` Account
err = k.transferFundsFromUserToModule(ctx, bettorAddress, types.BookLiquidityName, fulfiledBetAmount)
if err != nil {
return betFulfillments, err
}
// Create a unique lock in the Payout Store for the bet
k.SetPayoutLock(ctx, betUID)
return betFulfillments, nil
}