From 2c6e0f95cc8b923099fb8059cb294018d36411a5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E2=80=9CDongLieu=E2=80=9D?= Date: Tue, 5 Nov 2024 17:25:24 +0700 Subject: [PATCH 1/2] use collections --- x/auction/keeper/genesis.go | 2 +- x/auction/keeper/keeper.go | 29 +++++++++++++++-------------- x/auction/keeper/liquidate.go | 11 +++++++++-- 3 files changed, 25 insertions(+), 17 deletions(-) diff --git a/x/auction/keeper/genesis.go b/x/auction/keeper/genesis.go index 9bd33850..61f1f6e6 100644 --- a/x/auction/keeper/genesis.go +++ b/x/auction/keeper/genesis.go @@ -10,7 +10,7 @@ func (k Keeper) InitGenesis(ctx sdk.Context, genState types.GenesisState) { if err := k.SetParams(ctx, genState.Params); err != nil { panic(err) } - k.LastestAuctionPeriod = ctx.BlockTime().Unix() + k.LastestAuctionPeriods.Set(ctx, "LastestAuctionPeriods", ctx.BlockTime().Unix()) } func (k Keeper) ExportGenesis(ctx sdk.Context) *types.GenesisState { diff --git a/x/auction/keeper/keeper.go b/x/auction/keeper/keeper.go index 8193f413..d12a98b4 100644 --- a/x/auction/keeper/keeper.go +++ b/x/auction/keeper/keeper.go @@ -31,7 +31,7 @@ type ( authority string // timestamp of lastest auction period (Unix timestamp) - LastestAuctionPeriod int64 + LastestAuctionPeriods collections.Map[string, int64] AuctionIdSeq collections.Sequence @@ -66,19 +66,20 @@ func NewKeeper( sb := collections.NewSchemaBuilder(storeService) return Keeper{ - cdc: cdc, - storeService: storeService, - authority: authority, - logger: logger, - authKeeper: ak, - bankKeeper: bk, - vaultKeeper: vk, - OracleKeeper: ok, - AuctionIdSeq: collections.NewSequence(sb, types.AuctionIdSeqPrefix, "auction_id_sequence"), - BidIdSeq: collections.NewMap(sb, types.BidIdSeqPrefix, "bid_id_sequence", collections.Uint64Key, collections.Uint64Value), - Auctions: collections.NewMap(sb, types.AuctionsPrefix, "auctions", collections.Uint64Key, codec.CollValue[types.Auction](cdc)), - Bids: collections.NewMap(sb, types.BidsPrefix, "bids", collections.Uint64Key, codec.CollValue[types.BidQueue](cdc)), - BidByAddress: collections.NewMap(sb, types.BidByAddressPrefix, "bids_by_address", collections.PairKeyCodec(collections.Uint64Key, sdk.LengthPrefixedAddressKey(sdk.AccAddressKey)), codec.CollValue[types.Bids](cdc)), //nolint:staticcheck + cdc: cdc, + storeService: storeService, + authority: authority, + logger: logger, + authKeeper: ak, + bankKeeper: bk, + vaultKeeper: vk, + OracleKeeper: ok, + AuctionIdSeq: collections.NewSequence(sb, types.AuctionIdSeqPrefix, "auction_id_sequence"), + LastestAuctionPeriods: collections.NewMap(sb, types.LastestAuctionPeriodPrefix, "lastestAuctionPeriods", collections.StringKey, collections.Int64Value), + BidIdSeq: collections.NewMap(sb, types.BidIdSeqPrefix, "bid_id_sequence", collections.Uint64Key, collections.Uint64Value), + Auctions: collections.NewMap(sb, types.AuctionsPrefix, "auctions", collections.Uint64Key, codec.CollValue[types.Auction](cdc)), + Bids: collections.NewMap(sb, types.BidsPrefix, "bids", collections.Uint64Key, codec.CollValue[types.BidQueue](cdc)), + BidByAddress: collections.NewMap(sb, types.BidByAddressPrefix, "bids_by_address", collections.PairKeyCodec(collections.Uint64Key, sdk.LengthPrefixedAddressKey(sdk.AccAddressKey)), codec.CollValue[types.Bids](cdc)), //nolint:staticcheck } } diff --git a/x/auction/keeper/liquidate.go b/x/auction/keeper/liquidate.go index b7178318..c3c2a4de 100644 --- a/x/auction/keeper/liquidate.go +++ b/x/auction/keeper/liquidate.go @@ -14,11 +14,18 @@ func (k Keeper) handleLiquidation(ctx context.Context, mintDenom string) error { params := k.GetParams(ctx) currentTime := sdk.UnwrapSDKContext(ctx).BlockHeader().Time - lastAuctionPeriods := time.Unix(k.LastestAuctionPeriod, 0) + lastestAuctionPeriod, err := k.LastestAuctionPeriods.Get(ctx, "LastestAuctionPeriods") + if err != nil { + return err + } + lastAuctionPeriods := time.Unix(lastestAuctionPeriod, 0) // check if has reached the next auction periods if lastAuctionPeriods.Add(params.AuctionPeriods).Before(currentTime) { // update latest auction period - k.LastestAuctionPeriod = lastAuctionPeriods.Add(params.AuctionPeriods).Unix() + err = k.LastestAuctionPeriods.Set(ctx, "LastestAuctionPeriods", lastAuctionPeriods.Add(params.AuctionPeriods).Unix()) + if err != nil { + return err + } liquidations, err := k.vaultKeeper.GetLiquidations(ctx, mintDenom) if err != nil { From 474bc3c3c5b204998deb4c9ebe37509720922826 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E2=80=9CDongLieu=E2=80=9D?= Date: Tue, 5 Nov 2024 17:27:59 +0700 Subject: [PATCH 2/2] lint --- x/auction/keeper/genesis.go | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/x/auction/keeper/genesis.go b/x/auction/keeper/genesis.go index 61f1f6e6..0e9aa5cf 100644 --- a/x/auction/keeper/genesis.go +++ b/x/auction/keeper/genesis.go @@ -10,7 +10,10 @@ func (k Keeper) InitGenesis(ctx sdk.Context, genState types.GenesisState) { if err := k.SetParams(ctx, genState.Params); err != nil { panic(err) } - k.LastestAuctionPeriods.Set(ctx, "LastestAuctionPeriods", ctx.BlockTime().Unix()) + err := k.LastestAuctionPeriods.Set(ctx, "LastestAuctionPeriods", ctx.BlockTime().Unix()) + if err != nil { + panic(err) + } } func (k Keeper) ExportGenesis(ctx sdk.Context) *types.GenesisState {