/
bid.go
291 lines (230 loc) · 9.37 KB
/
bid.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
287
288
289
290
291
package keeper
import (
"strconv"
sdk "github.com/cosmos/cosmos-sdk/types"
sdkerrors "github.com/cosmos/cosmos-sdk/types/errors"
"github.com/tendermint/fundraising/x/fundraising/types"
)
// GetNextBidId increments bid id by one and set it.
func (k Keeper) GetNextBidIdWithUpdate(ctx sdk.Context, auctionId uint64) uint64 {
id := k.GetLastBidId(ctx, auctionId) + 1
k.SetBidId(ctx, auctionId, id)
return id
}
// PlaceBid places a bid for the selling coin of the auction.
func (k Keeper) PlaceBid(ctx sdk.Context, msg *types.MsgPlaceBid) (types.Bid, error) {
auction, found := k.GetAuction(ctx, msg.AuctionId)
if !found {
return types.Bid{}, sdkerrors.Wrapf(sdkerrors.ErrNotFound, "auction %d not found", msg.AuctionId)
}
if auction.GetStatus() != types.AuctionStatusStarted {
return types.Bid{}, types.ErrInvalidAuctionStatus
}
if auction.GetType() == types.AuctionTypeBatch {
if msg.Price.LT(auction.(*types.BatchAuction).MinBidPrice) {
return types.Bid{}, types.ErrInsufficientMinBidPrice
}
}
_, found = k.GetAllowedBidder(ctx, auction.GetId(), msg.GetBidder())
if !found {
return types.Bid{}, types.ErrNotAllowedBidder
}
if err := k.PayPlaceBidFee(ctx, msg.GetBidder()); err != nil {
return types.Bid{}, sdkerrors.Wrap(err, "failed to pay place bid fee")
}
bid := types.Bid{
AuctionId: msg.AuctionId,
Id: k.GetNextBidIdWithUpdate(ctx, auction.GetId()),
Bidder: msg.Bidder,
Type: msg.BidType,
Price: msg.Price,
Coin: msg.Coin,
IsMatched: false,
}
payingCoinDenom := auction.GetPayingCoinDenom()
// Place a bid depending on the bid type
switch bid.Type {
case types.BidTypeFixedPrice:
if err := k.ValidateFixedPriceBid(ctx, auction, bid); err != nil {
return types.Bid{}, err
}
fa := auction.(*types.FixedPriceAuction)
// Reserve bid amount
bidPayingAmt := bid.ConvertToPayingAmount(payingCoinDenom)
bidPayingCoin := sdk.NewCoin(payingCoinDenom, bidPayingAmt)
if err := k.ReservePayingCoin(ctx, msg.AuctionId, msg.GetBidder(), bidPayingCoin); err != nil {
return types.Bid{}, sdkerrors.Wrap(err, "failed to reserve paying coin")
}
// Subtract bid amount from the remaining
bidSellingAmt := bid.ConvertToSellingAmount(payingCoinDenom)
bidSellingCoin := sdk.NewCoin(auction.GetSellingCoin().Denom, bidSellingAmt)
fa.RemainingSellingCoin = fa.RemainingSellingCoin.Sub(bidSellingCoin)
k.SetAuction(ctx, fa)
bid.SetMatched(true)
case types.BidTypeBatchWorth:
if err := k.ValidateBatchWorthBid(ctx, auction, bid); err != nil {
return types.Bid{}, err
}
if err := k.ReservePayingCoin(ctx, msg.AuctionId, msg.GetBidder(), msg.Coin); err != nil {
return types.Bid{}, sdkerrors.Wrap(err, "failed to reserve paying coin")
}
case types.BidTypeBatchMany:
if err := k.ValidateBatchManyBid(ctx, auction, bid); err != nil {
return types.Bid{}, err
}
reserveAmt := bid.ConvertToPayingAmount(payingCoinDenom)
reserveCoin := sdk.NewCoin(payingCoinDenom, reserveAmt)
if err := k.ReservePayingCoin(ctx, msg.AuctionId, msg.GetBidder(), reserveCoin); err != nil {
return types.Bid{}, sdkerrors.Wrap(err, "failed to reserve paying coin")
}
}
// Call before bid placed hook
k.BeforeBidPlaced(ctx, bid.AuctionId, bid.Id, bid.Bidder, bid.Type, bid.Price, bid.Coin)
k.SetBid(ctx, bid)
ctx.EventManager().EmitEvents(sdk.Events{
sdk.NewEvent(
types.EventTypePlaceBid,
sdk.NewAttribute(types.AttributeKeyAuctionId, strconv.FormatUint(auction.GetId(), 10)),
sdk.NewAttribute(types.AttributeKeyBidderAddress, msg.GetBidder().String()),
sdk.NewAttribute(types.AttributeKeyBidPrice, msg.Price.String()),
sdk.NewAttribute(types.AttributeKeyBidCoin, msg.Coin.String()),
),
})
return bid, nil
}
// ValidateFixedPriceBid validates a fixed price bid type.
func (k Keeper) ValidateFixedPriceBid(ctx sdk.Context, auction types.AuctionI, bid types.Bid) error {
if auction.GetType() != types.AuctionTypeFixedPrice {
return types.ErrIncorrectAuctionType
}
if bid.Coin.Denom != auction.GetPayingCoinDenom() &&
bid.Coin.Denom != auction.GetSellingCoin().Denom {
return types.ErrIncorrectCoinDenom
}
if !bid.Price.Equal(auction.GetStartPrice()) {
return sdkerrors.Wrap(types.ErrInvalidStartPrice, "start price must be equal to the start price of the auction")
}
// For remaining coin validation, convert bid amount in selling coin denom
bidAmt := bid.ConvertToSellingAmount(auction.GetPayingCoinDenom())
bidCoin := sdk.NewCoin(auction.GetSellingCoin().Denom, bidAmt)
remainingCoin := auction.(*types.FixedPriceAuction).RemainingSellingCoin
if remainingCoin.IsLT(bidCoin) {
return sdkerrors.Wrapf(types.ErrInsufficientRemainingAmount, "remaining selling coin amount %s", remainingCoin)
}
// Get the total bid amount by the bidder
totalBidAmt := sdk.ZeroInt()
for _, bid := range k.GetBidsByBidder(ctx, bid.GetBidder()) {
if bid.AuctionId == auction.GetId() {
bidSellingAmt := bid.ConvertToSellingAmount(auction.GetPayingCoinDenom())
totalBidAmt = totalBidAmt.Add(bidSellingAmt)
}
}
allowedBidder, found := k.GetAllowedBidder(ctx, bid.AuctionId, bid.GetBidder())
if !found {
return sdkerrors.Wrap(sdkerrors.ErrNotFound, "bidder is not found in allowed bidder list")
}
totalBidAmt = totalBidAmt.Add(bidAmt)
// The total bid amount can't be greater than the bidder's maximum bid amount
if totalBidAmt.GT(allowedBidder.MaxBidAmount) {
return types.ErrOverMaxBidAmountLimit
}
return nil
}
// ValidateBatchWorthBid validates a batch worth bid type.
func (k Keeper) ValidateBatchWorthBid(ctx sdk.Context, auction types.AuctionI, bid types.Bid) error {
if auction.GetType() != types.AuctionTypeBatch {
return types.ErrIncorrectAuctionType
}
if bid.Coin.Denom != auction.GetPayingCoinDenom() {
return types.ErrIncorrectCoinDenom
}
allowedBidder, found := k.GetAllowedBidder(ctx, bid.AuctionId, bid.GetBidder())
if !found {
return sdkerrors.Wrap(sdkerrors.ErrNotFound, "bidder is not found in allowed bidder list")
}
bidAmt := bid.ConvertToSellingAmount(auction.GetPayingCoinDenom())
// The total bid amount can't be greater than the bidder's maximum bid amount
if bidAmt.GT(allowedBidder.MaxBidAmount) {
return types.ErrOverMaxBidAmountLimit
}
return nil
}
// ValidateBatchManyBid validates a batch many bid type.
func (k Keeper) ValidateBatchManyBid(ctx sdk.Context, auction types.AuctionI, bid types.Bid) error {
if auction.GetType() != types.AuctionTypeBatch {
return types.ErrIncorrectAuctionType
}
if bid.Coin.Denom != auction.GetSellingCoin().Denom {
return types.ErrIncorrectCoinDenom
}
allowedBidder, found := k.GetAllowedBidder(ctx, bid.AuctionId, bid.GetBidder())
if !found {
return sdkerrors.Wrap(sdkerrors.ErrNotFound, "bidder is not found in allowed bidder list")
}
bidAmt := bid.ConvertToSellingAmount(auction.GetPayingCoinDenom())
// The total bid amount can't be greater than the bidder's maximum bid amount
if bidAmt.GT(allowedBidder.MaxBidAmount) {
return types.ErrOverMaxBidAmountLimit
}
return nil
}
// ModifyBid handles types.MsgModifyBid and stores the modified bid.
// A bidder must provide either greater bid price or coin amount.
// They are not permitted to modify with less bid price or coin amount.
func (k Keeper) ModifyBid(ctx sdk.Context, msg *types.MsgModifyBid) error {
auction, found := k.GetAuction(ctx, msg.AuctionId)
if !found {
return sdkerrors.Wrap(sdkerrors.ErrNotFound, "auction not found")
}
if auction.GetStatus() != types.AuctionStatusStarted {
return types.ErrInvalidAuctionStatus
}
if auction.GetType() != types.AuctionTypeBatch {
return types.ErrIncorrectAuctionType
}
bid, found := k.GetBid(ctx, msg.AuctionId, msg.BidId)
if !found {
return sdkerrors.Wrap(sdkerrors.ErrNotFound, "bid not found")
}
if !bid.GetBidder().Equals(msg.GetBidder()) {
return sdkerrors.Wrap(sdkerrors.ErrUnauthorized, "only the bid creator can modify the bid")
}
if msg.Price.LT(auction.(*types.BatchAuction).MinBidPrice) {
return types.ErrInsufficientMinBidPrice
}
if bid.Coin.Denom != msg.Coin.Denom {
return types.ErrIncorrectCoinDenom
}
if msg.Price.LT(bid.Price) || msg.Coin.Amount.LT(bid.Coin.Amount) {
return sdkerrors.Wrap(sdkerrors.ErrInvalidRequest, "bid price or coin amount cannot be lower")
}
if msg.Price.Equal(bid.Price) && msg.Coin.Amount.Equal(bid.Coin.Amount) {
return sdkerrors.Wrap(sdkerrors.ErrInvalidRequest, "bid price and coin amount must be changed")
}
// Reserve bid amount difference
switch bid.Type {
case types.BidTypeBatchWorth:
diffReserveCoin := msg.Coin.Sub(bid.Coin)
if diffReserveCoin.IsPositive() {
if err := k.ReservePayingCoin(ctx, msg.AuctionId, msg.GetBidder(), diffReserveCoin); err != nil {
return sdkerrors.Wrap(err, "failed to reserve paying coin")
}
}
case types.BidTypeBatchMany:
prevReserveAmt := sdk.NewDecFromInt(bid.Coin.Amount).Mul(bid.Price).Ceil()
currReserveAmt := sdk.NewDecFromInt(msg.Coin.Amount).Mul(msg.Price).Ceil()
diffReserveAmt := currReserveAmt.Sub(prevReserveAmt).TruncateInt()
diffReserveCoin := sdk.NewCoin(auction.GetPayingCoinDenom(), diffReserveAmt)
if diffReserveCoin.IsPositive() {
if err := k.ReservePayingCoin(ctx, msg.AuctionId, msg.GetBidder(), diffReserveCoin); err != nil {
return sdkerrors.Wrap(err, "failed to reserve paying coin")
}
}
}
bid.Price = msg.Price
bid.Coin = msg.Coin
// Call the before mid modified hook
k.BeforeBidModified(ctx, bid.AuctionId, bid.Id, bid.Bidder, bid.Type, bid.Price, bid.Coin)
k.SetBid(ctx, bid)
return nil
}