/
bet_settle.go
116 lines (98 loc) · 4.14 KB
/
bet_settle.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
package keeper
import (
sdk "github.com/cosmos/cosmos-sdk/types"
sdkerrors "github.com/cosmos/cosmos-sdk/types/errors"
bettypes "github.com/playerfury/fanx/x/bet/types"
"github.com/playerfury/fanx/x/strategicreserve/types"
)
// RefundBettor process bets in case market gets cancelled or aborted,
// this method transfers th bet amount from order book liquidity module account balance to the bettor account balance.
func (k Keeper) RefundBettor(
ctx sdk.Context,
bettorAddress sdk.AccAddress,
betAmount, payout sdk.Int,
uniqueLock string,
) (err error) {
// if no lock exist means that there is nothing to be processed.
if !k.payoutLockExists(ctx, uniqueLock) {
return sdkerrors.Wrapf(types.ErrPayoutLockDoesnotExist, uniqueLock)
}
// transfer bet amount from `bet_reserve` to bettor's account.
err = k.transferFundsFromModuleToAccount(ctx, types.OrderBookLiquidityName, bettorAddress, betAmount)
if err != nil {
return
}
// delete the lock from the payout store as the bet is settled
k.removePayoutLock(ctx, uniqueLock)
return nil
}
// BettorWins process bets in case bettor is the winner,
// transfers the bet amount and the payout profit to the bettor's account and,
// updates actual profit of the participation to the subtracted value from the payout profit.
func (k Keeper) BettorWins(
ctx sdk.Context,
bettorAddress sdk.AccAddress,
betAmount sdk.Int,
payoutProfit sdk.Int,
uniqueLock string,
betFulfillments []*bettypes.BetFulfillment,
orderBookUID string,
) (err error) {
// if no lock exist means that there is nothing to be processed.
if !k.payoutLockExists(ctx, uniqueLock) {
return sdkerrors.Wrapf(types.ErrPayoutLockDoesnotExist, uniqueLock)
}
for _, betFulfillment := range betFulfillments {
orderBookParticipation, found := k.GetOrderBookParticipation(ctx, orderBookUID, betFulfillment.ParticipationIndex)
if !found {
return sdkerrors.Wrapf(types.ErrOrderBookParticipationNotFound, "%s, %d", orderBookUID, betFulfillment.ParticipationIndex)
}
// transfer payout from the `book_liquidity_pool` account to bettor
err = k.transferFundsFromModuleToAccount(ctx, types.OrderBookLiquidityName, bettorAddress, betFulfillment.PayoutProfit)
if err != nil {
return
}
// transfer bet amount from the `book_liquidity_pool` account to bettor
err = k.transferFundsFromModuleToAccount(ctx, types.OrderBookLiquidityName, bettorAddress, betFulfillment.BetAmount)
if err != nil {
return
}
// update actual profit of the participation, the bettor is the winner, so we need to
// payout from the participant profit.
orderBookParticipation.ActualProfit = orderBookParticipation.ActualProfit.Sub(betFulfillment.PayoutProfit)
k.SetOrderBookParticipation(ctx, orderBookParticipation)
}
// Delete lock from the payout store as the bet is settled
k.removePayoutLock(ctx, uniqueLock)
return nil
}
// BettorLoses process bets in case bettor loses,
// adds the bet amount to the actual profit of the participation
// for each of the bet fulfillemnt records and,
// removes the payout lock.
func (k Keeper) BettorLoses(ctx sdk.Context, address sdk.AccAddress,
betAmount sdk.Int,
payoutProfit sdk.Int,
uniqueLock string,
betFulfillments []*bettypes.BetFulfillment,
orderBookUID string,
) error {
// if no lock exist means that there is nothing to be processed.
if !k.payoutLockExists(ctx, uniqueLock) {
return sdkerrors.Wrapf(types.ErrPayoutLockDoesnotExist, uniqueLock)
}
for _, betFulfillment := range betFulfillments {
// update amount to be transferred to house
orderBookParticipation, found := k.GetOrderBookParticipation(ctx, orderBookUID, betFulfillment.ParticipationIndex)
if !found {
return sdkerrors.Wrapf(types.ErrOrderBookParticipationNotFound, "%s, %d", orderBookUID, betFulfillment.ParticipationIndex)
}
// update actual profit of the participation, the bettor is the loser, so we need to
// add the lost bet amount to the participant profit.
orderBookParticipation.ActualProfit = orderBookParticipation.ActualProfit.Add(betFulfillment.BetAmount)
k.SetOrderBookParticipation(ctx, orderBookParticipation)
}
// Delete lock from the payout store as the bet is settled
k.removePayoutLock(ctx, uniqueLock)
return nil
}