-
Notifications
You must be signed in to change notification settings - Fork 28
/
payout.go
75 lines (61 loc) · 2.39 KB
/
payout.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
package types
import (
sdkmath "cosmossdk.io/math"
sdk "github.com/cosmos/cosmos-sdk/types"
)
// CalculatePayoutProfit calculates the amount of payout profit portion according to bet odds value and amount
func CalculatePayoutProfit(oddsVal string, amount sdkmath.Int) (sdk.Dec, error) {
payout, err := calculatePayout(oddsVal, amount)
if err != nil {
return sdk.ZeroDec(), err
}
// bettor profit is the subtracted amount of payout from bet amount
profit := payout.Sub(sdk.NewDecFromInt(amount))
return profit, nil
}
// calculatePayout calculates the amount of payout according to bet odds value and amount
func calculatePayout(oddsVal string, amount sdkmath.Int) (sdk.Dec, error) {
// total payout should be paid to bettor
payout, err := CalculateDecimalPayout(oddsVal, amount)
if err != nil {
return sdk.ZeroDec(), err
}
return payout, nil
}
// CalculateBetAmount calculates the amount of bet according to bet odds value and payout profit
func CalculateBetAmount(oddsVal string, payoutProfit sdk.Dec) (sdk.Dec, error) {
betAmount, err := calculateBetAmount(oddsVal, payoutProfit)
if err != nil {
return sdk.ZeroDec(), err
}
return betAmount, nil
}
// CalculateBetAmountInt calculates the amount of bet according to bet odds value and payout profit
// and returns the int and the truncated decimal part.
func CalculateBetAmountInt(
oddsVal string,
payoutProfit sdk.Dec,
truncatedBetAmount sdk.Dec,
) (sdkmath.Int, sdk.Dec, error) {
expectedBetAmountDec, err := CalculateBetAmount(oddsVal, payoutProfit)
if err != nil {
return sdkmath.Int{}, sdk.Dec{}, 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 sdkmath.Int
// so the truncation in inevitable
betAmount := expectedBetAmountDec.RoundInt()
// save the truncated amount in the calculations for the next loop
truncatedBetAmount = truncatedBetAmount.Add(expectedBetAmountDec.Sub(sdk.NewDecFromInt(betAmount)))
return betAmount, truncatedBetAmount, nil
}
// calculateBetAmount calculates the amount of bet according to bet odds value and payoutProfit
func calculateBetAmount(oddsVal string, payoutProfit sdk.Dec) (sdk.Dec, error) {
// total payout should be paid to bettor
betAmount, err := CalculateDecimalBetAmount(oddsVal, payoutProfit)
if err != nil {
return sdk.ZeroDec(), err
}
return betAmount, nil
}