Skip to content

Commit

Permalink
Merge pull request #131 from onomyprotocol/hieu/genesis
Browse files Browse the repository at this point in the history
feat(x/vault, x/auction, x/oracle): Init & ExportGenesis
  • Loading branch information
DongLieu authored Jan 14, 2025
2 parents be2f41f + 0699e34 commit 8bfa648
Show file tree
Hide file tree
Showing 23 changed files with 1,560 additions and 247 deletions.
6 changes: 6 additions & 0 deletions proto/reserve/auction/v1/auction.proto
Original file line number Diff line number Diff line change
Expand Up @@ -95,4 +95,10 @@ message BidQueue {
message Bids {
// array of bid entries with bidder address
repeated Bid bids = 1 [ (gogoproto.nullable) = false, (amino.dont_omitempty) = true ];
}

message BidByAddress {
uint64 auction_id = 1;
bytes bidder = 2;
Bids bids = 3 [ (gogoproto.nullable) = false, (amino.dont_omitempty) = true ];
}
17 changes: 14 additions & 3 deletions proto/reserve/auction/v1/genesis.proto
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,19 @@ message GenesisState {
[ (gogoproto.nullable) = false, (amino.dont_omitempty) = true ];

// list of auctions
repeated Auction auctions = 2;
repeated Auction auctions = 2 [ (gogoproto.nullable) = false, (amino.dont_omitempty) = true ];

// list of all bid entries
repeated Bid bid_entries = 3;
// list of all bid by address
repeated BidByAddress bid_by_address = 3 [ (gogoproto.nullable) = false, (amino.dont_omitempty) = true ];

uint64 auction_sequence = 4;

int64 lastest_auction_periods = 5;

repeated BidSequence bid_sequences = 6 [ (gogoproto.nullable) = false, (amino.dont_omitempty) = true ];
}

message BidSequence {
uint64 auction_id = 1;
uint64 sequence = 2;
}
10 changes: 10 additions & 0 deletions proto/reserve/oracle/genesis.proto
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ message GenesisState {
repeated CalldataRecord calldata_records = 6;
uint64 band_latest_request_id = 7;
BandOracleRequestParams band_oracle_request_params = 8 [ (gogoproto.nullable) = false ];
repeated PairDecimalsRate pair_decimals_rates = 9 [ (gogoproto.nullable) = false ];
}

message BandOracleRequestParams {
Expand Down Expand Up @@ -115,4 +116,13 @@ message BandOracleRequest {
// MinSourceCount is the minimum number of data sources that must be used by
// each validator
uint64 min_source_count = 9;
}

message PairDecimalsRate {
string base = 1;
string quote = 2;
string rate = 3 [
(gogoproto.customtype) = "cosmossdk.io/math.LegacyDec",
(gogoproto.nullable) = false
];
}
11 changes: 5 additions & 6 deletions proto/reserve/vaults/genesis.proto
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import "gogoproto/gogo.proto";
import "reserve/vaults/params.proto";
import "amino/amino.proto";
import "cosmos_proto/cosmos.proto";
import "cosmos/base/v1beta1/coin.proto";

option go_package = "github.com/onomyprotocol/reserve/x/vaults/types";

Expand All @@ -23,10 +24,8 @@ message GenesisState {

LastUpdate last_update = 4;

string shortfall_amount = 5 [
(cosmos_proto.scalar) = "cosmos.Int",
(gogoproto.customtype) = "cosmossdk.io/math.Int",
(amino.dont_omitempty) = true,
(gogoproto.nullable) = false
];
repeated cosmos.base.v1beta1.Coin shortfall_amounts = 5
[ (gogoproto.nullable) = false, (amino.dont_omitempty) = true ];

uint64 vault_sequence = 6;
}
83 changes: 77 additions & 6 deletions x/auction/keeper/genesis.go
Original file line number Diff line number Diff line change
@@ -1,24 +1,95 @@
package keeper

import (
"cosmossdk.io/collections"
sdk "github.com/cosmos/cosmos-sdk/types"

"github.com/onomyprotocol/reserve/x/auction/types"
)

func (k Keeper) InitGenesis(ctx sdk.Context, genState types.GenesisState) {
func (k Keeper) InitGenesis(ctx sdk.Context, genState types.GenesisState) error {
if err := k.SetParams(ctx, genState.Params); err != nil {
panic(err)
return err
}
err := k.LastestAuctionPeriods.Set(ctx, ctx.BlockTime().Unix())
if err != nil {
panic(err)
return err
}

for _, auction := range genState.Auctions {
err := k.Auctions.Set(ctx, auction.AuctionId, auction)
if err != nil {
return err
}
}

allBids := map[uint64][]types.Bid{}
for _, bidByAddress := range genState.BidByAddress {
err := k.BidByAddress.Set(ctx, collections.Join(bidByAddress.AuctionId, sdk.AccAddress(bidByAddress.Bidder)), bidByAddress.Bids)
if err != nil {
return err
}
allBids[bidByAddress.AuctionId] = append(allBids[bidByAddress.AuctionId], bidByAddress.Bids.Bids...)
}

for auctionId, bids:= range allBids {
bidQueue := types.BidQueue{
AuctionId: auctionId,
Bids: bids,
}
err := k.Bids.Set(ctx, auctionId, bidQueue)
if err != nil {
return err
}
}

err = k.AuctionIdSeq.Set(ctx, genState.AuctionSequence)
if err != nil {
return err
}

for _, bidSequence := range genState.BidSequences {
err = k.BidIdSeq.Set(ctx, bidSequence.AuctionId, bidSequence.Sequence)
if err != nil {
return err
}
}
return nil
}

func (k Keeper) ExportGenesis(ctx sdk.Context) *types.GenesisState {
genesis := types.DefaultGenesis()
genesis.Params = k.GetParams(ctx)
params := k.GetParams(ctx)
lastestAuctionPeriod, err := k.LastestAuctionPeriods.Get(ctx)
if err != nil {
panic(err)
}

return genesis
auctions, err := k.GetAllAuctions(ctx)
if err != nil {
panic(err)
}

bidsByAddress, err := k.GetAllBidByAddress(ctx)
if err != nil {
panic(err)
}

auctionIdSequence, err := k.AuctionIdSeq.Peek(ctx)
if err != nil {
panic(err)
}

bidsIdSequence, err := k.GetAllBidsSequence(ctx)
if err != nil {
panic(err)
}

return &types.GenesisState{
Params: params,
Auctions: auctions,
BidByAddress: bidsByAddress,
AuctionSequence: auctionIdSequence,
LastestAuctionPeriods: lastestAuctionPeriod,
BidSequences: bidsIdSequence,
}
}
43 changes: 43 additions & 0 deletions x/auction/keeper/keeper.go
Original file line number Diff line number Diff line change
Expand Up @@ -233,3 +233,46 @@ func (k Keeper) refundToken(ctx context.Context, amt sdk.Coins, bidderAdrr strin

return k.bankKeeper.SendCoinsFromModuleToAccount(ctx, types.ModuleName, bidderAcc, amt)
}

func (k Keeper) GetAllAuctions(ctx context.Context) ([]types.Auction, error) {
allAuction := []types.Auction{}

err := k.Auctions.Walk(ctx, nil, func(key uint64, value types.Auction) (stop bool, err error) {
allAuction = append(allAuction, value)
return false, nil
})
if err != nil {
return nil, err
}
return allAuction, nil
}

func (k Keeper) GetAllBidByAddress(ctx context.Context) ([]types.BidByAddress, error) {
allAuction := []types.BidByAddress{}

err := k.BidByAddress.Walk(ctx, nil, func(key collections.Pair[uint64, sdk.AccAddress], value types.Bids) (stop bool, err error) {
allAuction = append(allAuction, types.BidByAddress{
AuctionId: key.K1(),
Bidder: key.K2(),
Bids: value,
})
return false, nil
})
if err != nil {
return nil, err
}
return allAuction, nil
}

func (k Keeper) GetAllBidsSequence(ctx context.Context) ([]types.BidSequence, error) {
allBidsSequence := []types.BidSequence{}

err := k.BidIdSeq.Walk(ctx, nil, func(key uint64, value uint64) (stop bool, err error) {
allBidsSequence = append(allBidsSequence, types.BidSequence{AuctionId: key, Sequence: value})
return false, nil
})
if err != nil {
return nil, err
}
return allBidsSequence, nil
}
5 changes: 4 additions & 1 deletion x/auction/module/genesis.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,10 @@ import (

// InitGenesis initializes the module's state from a provided genesis state.
func InitGenesis(ctx sdk.Context, k keeper.Keeper, genState types.GenesisState) {
k.InitGenesis(ctx, genState)
err := k.InitGenesis(ctx, genState)
if err != nil {
panic(err)
}
}

// ExportGenesis returns the module's exported genesis.
Expand Down
Loading

0 comments on commit 8bfa648

Please sign in to comment.