Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

use collections #80

Merged
merged 2 commits into from
Nov 5, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion x/auction/keeper/genesis.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.LastestAuctionPeriod = 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 {
Expand Down
29 changes: 15 additions & 14 deletions x/auction/keeper/keeper.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ type (
authority string

// timestamp of lastest auction period (Unix timestamp)
LastestAuctionPeriod int64
LastestAuctionPeriods collections.Map[string, int64]

AuctionIdSeq collections.Sequence

Expand Down Expand Up @@ -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
}
}

Expand Down
11 changes: 9 additions & 2 deletions x/auction/keeper/liquidate.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
Loading