-
Notifications
You must be signed in to change notification settings - Fork 28
/
bet.go
81 lines (69 loc) · 1.75 KB
/
bet.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
package types
import (
sdkmath "cosmossdk.io/math"
markettypes "github.com/sge-network/sge/x/market/types"
)
func NewPendingBet(uid, creator string) *PendingBet {
return &PendingBet{
UID: uid,
Creator: creator,
}
}
func NewSettledBet(uid, bettorAddress string) *SettledBet {
return &SettledBet{
UID: uid,
BettorAddress: bettorAddress,
}
}
func NewBetFulfillment(
participantAddress string,
participationIndex uint64,
betAmount, payoutProfit sdkmath.Int,
) *BetFulfillment {
return &BetFulfillment{
ParticipantAddress: participantAddress,
ParticipationIndex: participationIndex,
BetAmount: betAmount,
PayoutProfit: payoutProfit,
}
}
// CheckSettlementEligiblity checks status of bet. It returns an error if
// bet is canceled or settled already.
func (bet *Bet) CheckSettlementEligiblity() error {
switch bet.Status {
case Bet_STATUS_SETTLED:
return ErrBetIsSettled
case Bet_STATUS_CANCELED:
return ErrBetIsCanceled
}
return nil
}
// SetResult sets the bet object results according to the market resolusion.
func (bet *Bet) SetResult(market *markettypes.Market) error {
// check if market result is declared or not
if market.Status != markettypes.MarketStatus_MARKET_STATUS_RESULT_DECLARED {
return ErrResultNotDeclared
}
var exist bool
for _, wid := range market.WinnerOddsUIDs {
if wid == bet.OddsUID {
exist = true
break
}
}
if exist {
// bettor is winner
bet.Result = Bet_RESULT_WON
bet.Status = Bet_STATUS_RESULT_DECLARED
return nil
}
// bettor is loser
bet.Result = Bet_RESULT_LOST
bet.Status = Bet_STATUS_RESULT_DECLARED
return nil
}
// SetFee calculates and sets the betting fee.
func (bet *Bet) SetFee(fee sdkmath.Int) {
bet.Amount = bet.Amount.Sub(fee)
bet.Fee = fee
}