-
Notifications
You must be signed in to change notification settings - Fork 55
/
auction.go
159 lines (140 loc) 路 4.49 KB
/
auction.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
package types
import (
fmt "fmt"
time "time"
"github.com/cosmos/cosmos-sdk/codec"
sdk "github.com/cosmos/cosmos-sdk/types"
liquiditytypes "github.com/crescent-network/crescent/v5/x/liquidity/types"
)
// NewRewardsAuction creates a new RewardsAuction.
func NewRewardsAuction(
id uint64,
poolId uint64,
startTime time.Time,
endTime time.Time,
) RewardsAuction {
return RewardsAuction{
Id: id,
PoolId: poolId,
BiddingCoinDenom: liquiditytypes.PoolCoinDenom(poolId),
PayingReserveAddress: PayingReserveAddress(poolId).String(),
StartTime: startTime,
EndTime: endTime,
Status: AuctionStatusStarted,
Winner: "", // the value is determined when the auction is finished
WinningAmount: sdk.Coin{}, // the value is determined when the auction is finished
Rewards: sdk.Coins{}, // the value is determined when the auction is finished
Fees: sdk.Coins{}, // the value is determined when the auction is finished
FeeRate: sdk.ZeroDec(), // the value is determined when the auction is finished
}
}
// Validate validates RewardsAuction.
func (a *RewardsAuction) Validate() error {
if a.PoolId == 0 {
return fmt.Errorf("pool id must not be 0")
}
if a.BiddingCoinDenom == "" {
return fmt.Errorf("denom must not be empty")
}
if err := sdk.ValidateDenom(a.BiddingCoinDenom); err != nil {
return fmt.Errorf("invalid coin denom")
}
if _, err := sdk.AccAddressFromBech32(a.PayingReserveAddress); err != nil {
return fmt.Errorf("invalid paying reserve address %w", err)
}
if !a.EndTime.After(a.StartTime) {
return fmt.Errorf("end time must be set after the start time")
}
if a.Status != AuctionStatusStarted && a.Status != AuctionStatusFinished {
return fmt.Errorf("invalid auction status")
}
return nil
}
// SetStatus sets rewards auction status.
func (a *RewardsAuction) SetStatus(status AuctionStatus) {
a.Status = status
}
// SetWinner sets winner address.
func (a *RewardsAuction) SetWinner(winner string) {
a.Winner = winner
}
// SetWinningAmount sets the winning amount.
func (a *RewardsAuction) SetWinningAmount(winningAmount sdk.Coin) {
a.WinningAmount = winningAmount
}
// SetRewards sets auction rewards.
func (a *RewardsAuction) SetRewards(rewards sdk.Coins) {
a.Rewards = rewards
}
// SetFees sets auction fees.
func (a *RewardsAuction) SetFees(fees sdk.Coins) {
a.Fees = fees
}
// SetFeeRate sets auction fee rate.
func (a *RewardsAuction) SetFeeRate(feeRate sdk.Dec) {
a.FeeRate = feeRate
}
// GetPayingReserveAddress returns the paying reserve address in the form of sdk.AccAddress.
func (a RewardsAuction) GetPayingReserveAddress() sdk.AccAddress {
addr, err := sdk.AccAddressFromBech32(a.PayingReserveAddress)
if err != nil {
panic(err)
}
return addr
}
// NewBid creates a new Bid.
func NewBid(poolId uint64, bidder string, amount sdk.Coin) Bid {
return Bid{
PoolId: poolId,
Bidder: bidder,
Amount: amount,
}
}
// GetBidder returns the bidder address in the form of sdk.AccAddress.
func (b Bid) GetBidder() sdk.AccAddress {
addr, err := sdk.AccAddressFromBech32(b.Bidder)
if err != nil {
panic(err)
}
return addr
}
// Validate validates Bid.
func (b Bid) Validate() error {
if b.PoolId == 0 {
return fmt.Errorf("pool id must not be 0")
}
if _, err := sdk.AccAddressFromBech32(b.Bidder); err != nil {
return fmt.Errorf("invalid bidder address %w", err)
}
if !b.Amount.IsPositive() {
return fmt.Errorf("amount must be positive value")
}
if err := b.Amount.Validate(); err != nil {
return fmt.Errorf("invalid bid amount %w", err)
}
return nil
}
// MustMarshalRewardsAuction marshals RewardsAuction and
// it panics upon failure.
func MustMarshalRewardsAuction(cdc codec.BinaryCodec, auction RewardsAuction) []byte {
return cdc.MustMarshal(&auction)
}
// MustUnmarshalRewardsAuction unmarshals RewardsAuction and
// it panics upon failure.
func MustUnmarshalRewardsAuction(cdc codec.BinaryCodec, value []byte) RewardsAuction {
pair, err := UnmarshalRewardsAuction(cdc, value)
if err != nil {
panic(err)
}
return pair
}
// UnmarshalRewardsAuction unmarshals RewardsAuction.
func UnmarshalRewardsAuction(cdc codec.BinaryCodec, value []byte) (auction RewardsAuction, err error) {
err = cdc.Unmarshal(value, &auction)
return auction, err
}
// UnmarshalBid unmarshals bid from a store value.
func UnmarshalBid(cdc codec.BinaryCodec, value []byte) (bid Bid, err error) {
err = cdc.Unmarshal(value, &bid)
return bid, err
}