/
keeper.go
662 lines (542 loc) · 21.2 KB
/
keeper.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
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
package keeper
import (
"encoding/hex"
"encoding/json"
"fmt"
"time"
"github.com/cerc-io/laconicd/x/auction/types"
"github.com/cosmos/cosmos-sdk/codec"
storetypes "github.com/cosmos/cosmos-sdk/store/types"
sdk "github.com/cosmos/cosmos-sdk/types"
sdkerrors "github.com/cosmos/cosmos-sdk/types/errors"
auth "github.com/cosmos/cosmos-sdk/x/auth/keeper"
bank "github.com/cosmos/cosmos-sdk/x/bank/keeper"
params "github.com/cosmos/cosmos-sdk/x/params/types"
wnsUtils "github.com/cerc-io/laconicd/utils"
)
// CompletedAuctionDeleteTimeout => Completed auctions are deleted after this timeout (after reveals end time).
const CompletedAuctionDeleteTimeout = time.Hour * 24
// PrefixIDToAuctionIndex is the prefix for Id -> Auction index in the KVStore.
// Note: This is the primary index in the system.
// Note: Golang doesn't support const arrays.
var PrefixIDToAuctionIndex = []byte{0x00}
// prefixOwnerToAuctionsIndex is the prefix for the Owner -> [Auction] index in the KVStore.
var prefixOwnerToAuctionsIndex = []byte{0x01}
// PrefixAuctionBidsIndex is the prefix for the (auction, bidder) -> Bid index in the KVStore.
var PrefixAuctionBidsIndex = []byte{0x02}
// PrefixBidderToAuctionsIndex is the prefix for the Bidder -> [Auction] index in the KVStore.
var PrefixBidderToAuctionsIndex = []byte{0x03}
// Keeper maintains the link to storage and exposes getter/setter methods for the various parts of the state machine
type Keeper struct {
accountKeeper auth.AccountKeeper
bankKeeper bank.Keeper
// Track auction usage in other cosmos-sdk modules (more like a usage tracker).
usageKeepers []types.AuctionUsageKeeper
storeKey storetypes.StoreKey // Unexposed key to access store from sdk.Context
cdc codec.BinaryCodec // The wire codec for binary encoding/decoding.
paramSubspace params.Subspace
}
// AuctionClientKeeper is the subset of functionality exposed to other modules.
type AuctionClientKeeper interface {
HasAuction(ctx sdk.Context, id string) bool
GetAuction(ctx sdk.Context, id string) types.Auction
MatchAuctions(ctx sdk.Context, matchFn func(*types.Auction) bool) []*types.Auction
}
// NewKeeper creates new instances of the auction Keeper
func NewKeeper(accountKeeper auth.AccountKeeper,
bankKeeper bank.Keeper,
storeKey storetypes.StoreKey,
cdc codec.BinaryCodec,
ps params.Subspace,
) Keeper {
if !ps.HasKeyTable() {
ps = ps.WithKeyTable(types.ParamKeyTable())
}
return Keeper{
accountKeeper: accountKeeper,
bankKeeper: bankKeeper,
storeKey: storeKey,
cdc: cdc,
paramSubspace: ps,
}
}
func (k *Keeper) SetUsageKeepers(usageKeepers []types.AuctionUsageKeeper) {
k.usageKeepers = usageKeepers
}
func (k Keeper) GetUsageKeepers() []types.AuctionUsageKeeper {
return k.usageKeepers
}
// Generates Auction Id -> Auction index key.
func GetAuctionIndexKey(id string) []byte {
return append(PrefixIDToAuctionIndex, []byte(id)...)
}
// Generates Owner -> Auctions index key.
func GetOwnerToAuctionsIndexKey(owner string, auctionID string) []byte {
return append(append(prefixOwnerToAuctionsIndex, []byte(owner)...), []byte(auctionID)...)
}
func GetBidderToAuctionsIndexKey(bidder string, auctionID string) []byte {
return append(append(PrefixBidderToAuctionsIndex, []byte(bidder)...), []byte(auctionID)...)
}
func GetBidIndexKey(auctionID string, bidder string) []byte {
return append(GetAuctionBidsIndexPrefix(auctionID), []byte(bidder)...)
}
func GetAuctionBidsIndexPrefix(auctionID string) []byte {
return append(PrefixAuctionBidsIndex, []byte(auctionID)...)
}
// SaveAuction - saves a auction to the store.
func (k Keeper) SaveAuction(ctx sdk.Context, auction *types.Auction) {
store := ctx.KVStore(k.storeKey)
// Auction Id -> Auction index.
store.Set(GetAuctionIndexKey(auction.Id), k.cdc.MustMarshal(auction))
// Owner -> [Auction] index.
store.Set(GetOwnerToAuctionsIndexKey(auction.OwnerAddress, auction.Id), []byte{})
// Notify interested parties.
for _, keeper := range k.usageKeepers {
keeper.OnAuction(ctx, auction.Id)
}
}
func (k Keeper) SaveBid(ctx sdk.Context, bid *types.Bid) {
store := ctx.KVStore(k.storeKey)
store.Set(GetBidIndexKey(bid.AuctionId, bid.BidderAddress), k.cdc.MustMarshal(bid))
store.Set(GetBidderToAuctionsIndexKey(bid.BidderAddress, bid.AuctionId), []byte{})
// Notify interested parties.
for _, keeper := range k.usageKeepers {
keeper.OnAuctionBid(ctx, bid.AuctionId, bid.BidderAddress)
}
}
func (k Keeper) DeleteBid(ctx sdk.Context, bid types.Bid) {
store := ctx.KVStore(k.storeKey)
store.Delete(GetBidIndexKey(bid.AuctionId, bid.BidderAddress))
store.Delete(GetOwnerToAuctionsIndexKey(bid.BidderAddress, bid.AuctionId))
}
// HasAuction - checks if a auction by the given Id exists.
func (k Keeper) HasAuction(ctx sdk.Context, id string) bool {
store := ctx.KVStore(k.storeKey)
return store.Has(GetAuctionIndexKey(id))
}
func (k Keeper) HasBid(ctx sdk.Context, id string, bidder string) bool {
store := ctx.KVStore(k.storeKey)
return store.Has(GetBidIndexKey(id, bidder))
}
// DeleteAuction - deletes the auction.
func (k Keeper) DeleteAuction(ctx sdk.Context, auction types.Auction) {
// Delete all bids first.
bids := k.GetBids(ctx, auction.Id)
for _, bid := range bids {
k.DeleteBid(ctx, *bid)
}
// Delete the auction itself.
store := ctx.KVStore(k.storeKey)
store.Delete(GetAuctionIndexKey(auction.Id))
store.Delete(GetOwnerToAuctionsIndexKey(auction.OwnerAddress, auction.Id))
}
// GetAuction - gets a record from the store.
func (k Keeper) GetAuction(ctx sdk.Context, id string) *types.Auction {
store := ctx.KVStore(k.storeKey)
auctionKey := GetAuctionIndexKey(id)
if !store.Has(auctionKey) {
return nil
}
bz := store.Get(auctionKey)
var obj types.Auction
k.cdc.MustUnmarshal(bz, &obj)
return &obj
}
// GetBids gets the auction bids.
func (k Keeper) GetBids(ctx sdk.Context, id string) []*types.Bid {
store := ctx.KVStore(k.storeKey)
bids := []*types.Bid{}
itr := sdk.KVStorePrefixIterator(store, GetAuctionBidsIndexPrefix(id))
defer itr.Close()
for ; itr.Valid(); itr.Next() {
bz := store.Get(itr.Key())
if bz != nil {
var obj types.Bid
k.cdc.MustUnmarshal(bz, &obj)
bids = append(bids, &obj)
}
}
return bids
}
func (k Keeper) GetBid(ctx sdk.Context, id string, bidder string) types.Bid {
store := ctx.KVStore(k.storeKey)
bz := store.Get(GetBidIndexKey(id, bidder))
var obj types.Bid
k.cdc.MustUnmarshal(bz, &obj)
return obj
}
// ListAuctions - get all auctions.
func (k Keeper) ListAuctions(ctx sdk.Context) []types.Auction {
var auctions []types.Auction
store := ctx.KVStore(k.storeKey)
itr := sdk.KVStorePrefixIterator(store, PrefixIDToAuctionIndex)
defer itr.Close()
for ; itr.Valid(); itr.Next() {
bz := store.Get(itr.Key())
if bz != nil {
var obj types.Auction
k.cdc.MustUnmarshal(bz, &obj)
auctions = append(auctions, obj)
}
}
return auctions
}
// QueryAuctionsByOwner - query auctions by owner.
func (k Keeper) QueryAuctionsByOwner(ctx sdk.Context, ownerAddress string) []types.Auction {
auctions := []types.Auction{}
ownerPrefix := append(prefixOwnerToAuctionsIndex, []byte(ownerAddress)...) //nolint: all
store := ctx.KVStore(k.storeKey)
itr := sdk.KVStorePrefixIterator(store, ownerPrefix)
defer itr.Close()
for ; itr.Valid(); itr.Next() {
auctionID := itr.Key()[len(ownerPrefix):]
bz := store.Get(append(PrefixIDToAuctionIndex, auctionID...))
if bz != nil {
var obj types.Auction
k.cdc.MustUnmarshal(bz, &obj)
auctions = append(auctions, obj)
}
}
return auctions
}
// QueryAuctionsByBidder - query auctions by bidder
func (k Keeper) QueryAuctionsByBidder(ctx sdk.Context, bidderAddress string) []types.Auction {
auctions := []types.Auction{}
bidderPrefix := append(PrefixBidderToAuctionsIndex, []byte(bidderAddress)...) //nolint: all
store := ctx.KVStore(k.storeKey)
itr := sdk.KVStorePrefixIterator(store, bidderPrefix)
defer itr.Close()
for ; itr.Valid(); itr.Next() {
auctionID := itr.Key()[len(bidderPrefix):]
bz := store.Get(append(PrefixIDToAuctionIndex, auctionID...))
if bz != nil {
var obj types.Auction
k.cdc.MustUnmarshal(bz, &obj)
auctions = append(auctions, obj)
}
}
return auctions
}
// MatchAuctions - get all matching auctions.
func (k Keeper) MatchAuctions(ctx sdk.Context, matchFn func(*types.Auction) bool) []*types.Auction {
var auctions []*types.Auction
store := ctx.KVStore(k.storeKey)
itr := sdk.KVStorePrefixIterator(store, PrefixIDToAuctionIndex)
defer itr.Close()
for ; itr.Valid(); itr.Next() {
bz := store.Get(itr.Key())
if bz != nil {
var obj types.Auction
k.cdc.MustUnmarshal(bz, &obj)
if matchFn(&obj) {
auctions = append(auctions, &obj)
}
}
}
return auctions
}
// CreateAuction creates a new auction.
func (k Keeper) CreateAuction(ctx sdk.Context, msg types.MsgCreateAuction) (*types.Auction, error) {
// Might be called from another module directly, always validate.
err := msg.ValidateBasic()
if err != nil {
return nil, err
}
signerAddress, err := sdk.AccAddressFromBech32(msg.Signer)
if err != nil {
return nil, err
}
// Generate auction Id.
account := k.accountKeeper.GetAccount(ctx, signerAddress)
if account == nil {
return nil, sdkerrors.Wrap(sdkerrors.ErrInvalidAddress, "Account not found.")
}
auctionID := types.AuctionID{
Address: signerAddress,
AccNum: account.GetAccountNumber(),
Sequence: account.GetSequence(),
}.Generate()
// Compute timestamps.
now := ctx.BlockTime()
commitsEndTime := now.Add(msg.CommitsDuration)
revealsEndTime := now.Add(msg.CommitsDuration + msg.RevealsDuration)
auction := types.Auction{
Id: auctionID,
Status: types.AuctionStatusCommitPhase,
OwnerAddress: signerAddress.String(),
CreateTime: now,
CommitsEndTime: commitsEndTime,
RevealsEndTime: revealsEndTime,
CommitFee: msg.CommitFee,
RevealFee: msg.RevealFee,
MinimumBid: msg.MinimumBid,
}
// Save auction in store.
k.SaveAuction(ctx, &auction)
return &auction, nil
}
func (k Keeper) CommitBid(ctx sdk.Context, msg types.MsgCommitBid) (*types.Bid, error) {
if !k.HasAuction(ctx, msg.AuctionId) {
return nil, sdkerrors.Wrap(sdkerrors.ErrInvalidRequest, "Auction not found.")
}
auction := k.GetAuction(ctx, msg.AuctionId)
if auction.Status != types.AuctionStatusCommitPhase {
return nil, sdkerrors.Wrap(sdkerrors.ErrInvalidRequest, "Auction is not in commit phase.")
}
signerAddress, err := sdk.AccAddressFromBech32(msg.Signer)
if err != nil {
return nil, err
}
// Take auction fees from account.
totalFee := auction.CommitFee.Add(auction.RevealFee)
sdkErr := k.bankKeeper.SendCoinsFromAccountToModule(ctx, signerAddress, types.ModuleName, sdk.NewCoins(totalFee))
if sdkErr != nil {
return nil, sdkErr
}
// Check if an old bid already exists, if so, return old bids auction fee (update bid scenario).
bidder := signerAddress.String()
if k.HasBid(ctx, msg.AuctionId, bidder) {
oldBid := k.GetBid(ctx, msg.AuctionId, bidder)
oldTotalFee := oldBid.CommitFee.Add(oldBid.RevealFee)
sdkErr := k.bankKeeper.SendCoinsFromModuleToAccount(ctx, types.ModuleName, signerAddress, sdk.NewCoins(oldTotalFee))
if sdkErr != nil {
return nil, sdkErr
}
}
// Save new bid.
bid := types.Bid{
AuctionId: msg.AuctionId,
BidderAddress: bidder,
Status: types.BidStatusCommitted,
CommitHash: msg.CommitHash,
CommitTime: ctx.BlockTime(),
CommitFee: auction.CommitFee,
RevealFee: auction.RevealFee,
}
k.SaveBid(ctx, &bid)
return &bid, nil
}
// RevealBid reeals a bid committed earlier.
func (k Keeper) RevealBid(ctx sdk.Context, msg types.MsgRevealBid) (*types.Auction, error) {
if !k.HasAuction(ctx, msg.AuctionId) {
return nil, sdkerrors.Wrap(sdkerrors.ErrInvalidRequest, "Auction not found.")
}
auction := k.GetAuction(ctx, msg.AuctionId)
if auction.Status != types.AuctionStatusRevealPhase {
return nil, sdkerrors.Wrap(sdkerrors.ErrInvalidRequest, "Auction is not in reveal phase.")
}
signerAddress, err := sdk.AccAddressFromBech32(msg.Signer)
if err != nil {
return nil, err
}
if !k.HasBid(ctx, msg.AuctionId, signerAddress.String()) {
return nil, sdkerrors.Wrap(sdkerrors.ErrInvalidRequest, "Bid not found.")
}
bid := k.GetBid(ctx, auction.Id, signerAddress.String())
if bid.Status != types.BidStatusCommitted {
return nil, sdkerrors.Wrap(sdkerrors.ErrInvalidRequest, "Bid not in committed state.")
}
revealBytes, err := hex.DecodeString(msg.Reveal)
if err != nil {
return nil, sdkerrors.Wrap(sdkerrors.ErrInvalidRequest, "Invalid reveal string.")
}
cid, err := wnsUtils.CIDFromJSONBytes(revealBytes)
if err != nil {
return nil, sdkerrors.Wrap(sdkerrors.ErrInvalidRequest, "Invalid reveal JSON.")
}
if bid.CommitHash != cid {
return nil, sdkerrors.Wrap(sdkerrors.ErrInvalidRequest, "Commit hash mismatch.")
}
var reveal map[string]interface{}
err = json.Unmarshal(revealBytes, &reveal)
if err != nil {
return nil, sdkerrors.Wrap(sdkerrors.ErrInvalidRequest, "Reveal JSON unmarshal error.")
}
chainID, err := wnsUtils.GetAttributeAsString(reveal, "chainId")
if err != nil || chainID != ctx.ChainID() {
return nil, sdkerrors.Wrap(sdkerrors.ErrInvalidRequest, "Invalid reveal chainID.")
}
auctionID, err := wnsUtils.GetAttributeAsString(reveal, "auctionId")
if err != nil || auctionID != msg.AuctionId {
return nil, sdkerrors.Wrap(sdkerrors.ErrInvalidRequest, "Invalid reveal auction Id.")
}
bidderAddress, err := wnsUtils.GetAttributeAsString(reveal, "bidderAddress")
if err != nil {
return nil, sdkerrors.Wrap(sdkerrors.ErrInvalidRequest, "Invalid reveal bid address.")
}
if bidderAddress != signerAddress.String() {
return nil, sdkerrors.Wrap(sdkerrors.ErrInvalidRequest, "Reveal bid address mismatch.")
}
bidAmountStr, err := wnsUtils.GetAttributeAsString(reveal, "bidAmount")
if err != nil {
return nil, sdkerrors.Wrap(sdkerrors.ErrInvalidRequest, "Invalid reveal bid amount.")
}
bidAmount, err := sdk.ParseCoinNormalized(bidAmountStr)
if err != nil {
return nil, sdkerrors.Wrap(sdkerrors.ErrInvalidRequest, "Invalid reveal bid amount.")
}
if bidAmount.IsLT(auction.MinimumBid) {
return nil, sdkerrors.Wrap(sdkerrors.ErrInvalidRequest, "Bid is lower than minimum bid.")
}
// Lock bid amount.
sdkErr := k.bankKeeper.SendCoinsFromAccountToModule(ctx, signerAddress, types.ModuleName, sdk.NewCoins(bidAmount))
if sdkErr != nil {
return nil, sdkErr
}
// Update bid.
bid.BidAmount = bidAmount
bid.RevealTime = ctx.BlockTime()
bid.Status = types.BidStatusRevealed
k.SaveBid(ctx, &bid)
return auction, nil
}
// GetAuctionModuleBalances gets the auction module account(s) balances.
func (k Keeper) GetAuctionModuleBalances(ctx sdk.Context) sdk.Coins {
moduleAddress := k.accountKeeper.GetModuleAddress(types.ModuleName)
balances := k.bankKeeper.GetAllBalances(ctx, moduleAddress)
return balances
}
func (k Keeper) EndBlockerProcessAuctions(ctx sdk.Context) {
// Transition auction state (commit, reveal, expired, completed).
k.processAuctionPhases(ctx)
// Delete stale auctions.
k.deleteCompletedAuctions(ctx)
}
func (k Keeper) processAuctionPhases(ctx sdk.Context) {
auctions := k.MatchAuctions(ctx, func(_ *types.Auction) bool {
return true
})
for _, auction := range auctions {
// Commit -> Reveal state.
if auction.Status == types.AuctionStatusCommitPhase && ctx.BlockTime().After(auction.CommitsEndTime) {
auction.Status = types.AuctionStatusRevealPhase
k.SaveAuction(ctx, auction)
ctx.Logger().Info(fmt.Sprintf("Moved auction %s to reveal phase.", auction.Id))
}
// Reveal -> Expired state.
if auction.Status == types.AuctionStatusRevealPhase && ctx.BlockTime().After(auction.RevealsEndTime) {
auction.Status = types.AuctionStatusExpired
k.SaveAuction(ctx, auction)
ctx.Logger().Info(fmt.Sprintf("Moved auction %s to expired state.", auction.Id))
}
// If auction has expired, pick a winner from revealed bids.
if auction.Status == types.AuctionStatusExpired {
k.pickAuctionWinner(ctx, auction)
}
}
}
// Delete completed stale auctions.
func (k Keeper) deleteCompletedAuctions(ctx sdk.Context) {
auctions := k.MatchAuctions(ctx, func(auction *types.Auction) bool {
deleteTime := auction.RevealsEndTime.Add(CompletedAuctionDeleteTimeout)
return auction.Status == types.AuctionStatusCompleted && ctx.BlockTime().After(deleteTime)
})
for _, auction := range auctions {
ctx.Logger().Info(fmt.Sprintf("Deleting completed auction %s after timeout.", auction.Id))
k.DeleteAuction(ctx, *auction)
}
}
func (k Keeper) pickAuctionWinner(ctx sdk.Context, auction *types.Auction) {
ctx.Logger().Info(fmt.Sprintf("Picking auction %s winner.", auction.Id))
var highestBid *types.Bid
var secondHighestBid *types.Bid
bids := k.GetBids(ctx, auction.Id)
for _, bid := range bids {
ctx.Logger().Info(fmt.Sprintf("Processing bid %s %s", bid.BidderAddress, bid.BidAmount.String()))
// Only consider revealed bids.
if bid.Status != types.BidStatusRevealed {
ctx.Logger().Info(fmt.Sprintf("Ignoring unrevealed bid %s %s", bid.BidderAddress, bid.BidAmount.String()))
continue
}
// Init highest bid.
if highestBid == nil {
highestBid = bid
ctx.Logger().Info(fmt.Sprintf("Initializing 1st bid %s %s", bid.BidderAddress, bid.BidAmount.String()))
continue
}
//nolint: all
if highestBid.BidAmount.IsLT(bid.BidAmount) {
ctx.Logger().Info(fmt.Sprintf("New highest bid %s %s", bid.BidderAddress, bid.BidAmount.String()))
secondHighestBid = highestBid
highestBid = bid
ctx.Logger().Info(fmt.Sprintf("Updated 1st bid %s %s", highestBid.BidderAddress, highestBid.BidAmount.String()))
ctx.Logger().Info(fmt.Sprintf("Updated 2nd bid %s %s", secondHighestBid.BidderAddress, secondHighestBid.BidAmount.String()))
} else if secondHighestBid == nil || secondHighestBid.BidAmount.IsLT(bid.BidAmount) {
ctx.Logger().Info(fmt.Sprintf("New 2nd highest bid %s %s", bid.BidderAddress, bid.BidAmount.String()))
secondHighestBid = bid
ctx.Logger().Info(fmt.Sprintf("Updated 2nd bid %s %s", secondHighestBid.BidderAddress, secondHighestBid.BidAmount.String()))
} else {
ctx.Logger().Info(fmt.Sprintf("Ignoring bid as it doesn't affect 1st/2nd price %s %s", bid.BidderAddress, bid.BidAmount.String()))
}
}
// Highest bid is the winner, but pays second highest bid price.
auction.Status = types.AuctionStatusCompleted
if highestBid != nil {
auction.WinnerAddress = highestBid.BidderAddress
auction.WinningBid = highestBid.BidAmount
// Winner pays 2nd price, if a 2nd price exists.
auction.WinningPrice = highestBid.BidAmount
if secondHighestBid != nil {
auction.WinningPrice = secondHighestBid.BidAmount
}
ctx.Logger().Info(fmt.Sprintf("Auction %s winner %s.", auction.Id, auction.WinnerAddress))
ctx.Logger().Info(fmt.Sprintf("Auction %s winner bid %s.", auction.Id, auction.WinningBid.String()))
ctx.Logger().Info(fmt.Sprintf("Auction %s winner price %s.", auction.Id, auction.WinningPrice.String()))
} else {
ctx.Logger().Info(fmt.Sprintf("Auction %s has no valid revealed bids (no winner).", auction.Id))
}
k.SaveAuction(ctx, auction)
for _, bid := range bids {
bidderAddress, err := sdk.AccAddressFromBech32(bid.BidderAddress)
if err != nil {
ctx.Logger().Error(fmt.Sprintf("Invalid bidderAddress address. %v", err))
panic("Invalid bidder address.")
}
if bid.Status == types.BidStatusRevealed {
// Send reveal fee back to bidders that've revealed the bid.
sdkErr := k.bankKeeper.SendCoinsFromModuleToAccount(ctx, types.ModuleName, bidderAddress, sdk.NewCoins(bid.RevealFee))
if sdkErr != nil {
ctx.Logger().Error(fmt.Sprintf("Auction error returning reveal fee: %v", sdkErr))
panic(sdkErr)
}
}
// Send back locked bid amount to all bidders.
sdkErr := k.bankKeeper.SendCoinsFromModuleToAccount(ctx, types.ModuleName, bidderAddress, sdk.NewCoins(bid.BidAmount))
if sdkErr != nil {
ctx.Logger().Error(fmt.Sprintf("Auction error returning bid amount: %v", sdkErr))
panic(sdkErr)
}
}
// Process winner account (if nobody bids, there won't be a winner).
if auction.WinnerAddress != "" {
winnerAddress, err := sdk.AccAddressFromBech32(auction.WinnerAddress)
if err != nil {
ctx.Logger().Error(fmt.Sprintf("Invalid winner address. %v", err))
panic("Invalid winner address.")
}
// Take 2nd price from winner.
sdkErr := k.bankKeeper.SendCoinsFromAccountToModule(ctx, winnerAddress, types.ModuleName, sdk.NewCoins(auction.WinningPrice))
if sdkErr != nil {
ctx.Logger().Error(fmt.Sprintf("Auction error taking funds from winner: %v", sdkErr))
panic(sdkErr)
}
// Burn anything over the min. bid amount.
amountToBurn := auction.WinningPrice.Sub(auction.MinimumBid)
if amountToBurn.IsNegative() {
ctx.Logger().Error("Auction coins to burn cannot be negative.")
panic("Auction coins to burn cannot be negative.")
}
// Use auction burn module account instead of actually burning coins to better keep track of supply.
sdkErr = k.bankKeeper.SendCoinsFromModuleToModule(ctx, types.ModuleName, types.AuctionBurnModuleAccountName, sdk.NewCoins(amountToBurn))
if sdkErr != nil {
ctx.Logger().Error(fmt.Sprintf("Auction error burning coins: %v", sdkErr))
panic(sdkErr)
}
}
// Notify other modules (hook).
ctx.Logger().Info(fmt.Sprintf("Auction %s notifying %d modules.", auction.Id, len(k.usageKeepers)))
for _, keeper := range k.usageKeepers {
ctx.Logger().Info(fmt.Sprintf("Auction %s notifying module %s.", auction.Id, keeper.ModuleName()))
keeper.OnAuctionWinnerSelected(ctx, auction.Id)
}
}