Skip to content

Commit

Permalink
Query Settled participations
Browse files Browse the repository at this point in the history
  • Loading branch information
P-U-D-G-E committed Dec 4, 2023
1 parent d866e19 commit 1c24bff
Show file tree
Hide file tree
Showing 7 changed files with 1,102 additions and 121 deletions.
17 changes: 17 additions & 0 deletions proto/sge/orderbook/participation.proto
Original file line number Diff line number Diff line change
Expand Up @@ -122,3 +122,20 @@ message ParticipationBetPair {
json_name = "bet_uid"
];
}

// SettledOrderbookParticipation is the type for a settled orderbook participation.
message SettledOrderbookParticipation {
// index is the index of the participation in the participation queue.
uint64 index = 1 [ (gogoproto.moretags) = "yaml:\"index\"" ];

// order_book_uid is the unique identifier corresponding to the order book.
string order_book_uid = 2 [
(gogoproto.customname) = "OrderBookUID",
(gogoproto.jsontag) = "order_book_uid",
json_name = "order_book_uid"
];

// participant_address is the bech32-encoded address of the participant.
string participant_address = 3
[ (gogoproto.moretags) = "yaml:\"participant_address\"" ];
}
20 changes: 20 additions & 0 deletions proto/sge/orderbook/query.proto
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,12 @@ service Query {
"/sge/orderbook/{order_book_uid}/participations/"
"{participation_index}/fulfilled_bets";
}

// Queries list of settled Orderbook Participation items of a certain height.
rpc SettledOrderbookParticipationsOfHeight(QuerySettledOrderBookParticipationsOfHeightRequest)
returns (QuerySettledOrderBookParticipationsOfHeightResponse) {
option (google.api.http).get = "/sge/orderbook/participations/settled/{block_height}";
}
}

// QueryParamsRequest is the request type for the Query/Params RPC method
Expand Down Expand Up @@ -304,3 +310,17 @@ message QueryParticipationFulfilledBetsResponse {
// pagination defines the pagination in the response.
cosmos.base.query.v1beta1.PageResponse pagination = 2;
}

// QuerySettledOrderBookParticipationsOfHeightRequest is the request type for the settled orderbook participations of a
// certain height list query.
message QuerySettledOrderBookParticipationsOfHeightRequest {
cosmos.base.query.v1beta1.PageRequest pagination = 1;
int64 block_height = 2;
}

// QuerySettledOrderBookParticipationsOfHeightResponse is the response type for the settled orderbook participations of
// a certain height list query.
message QuerySettledOrderBookParticipationsOfHeightResponse {
repeated OrderBookParticipation participations = 1 [ (gogoproto.nullable) = false ];
cosmos.base.query.v1beta1.PageResponse pagination = 2;
}
45 changes: 45 additions & 0 deletions x/orderbook/keeper/grpc_query_participation.go
Original file line number Diff line number Diff line change
Expand Up @@ -90,3 +90,48 @@ func (k Keeper) OrderBookParticipations(
Pagination: pageRes,
}, nil
}

// SettledOrderbookParticipationsOfHeight returns settled orderbook participations of a certain height
func (k Keeper) SettledOrderbookParticipationsOfHeight(
c context.Context,
req *types.QuerySettledOrderBookParticipationsOfHeightRequest,
) (*types.QuerySettledOrderBookParticipationsOfHeightResponse, error) {
if req == nil {
return nil, status.Error(codes.InvalidArgument, consts.ErrTextInvalidRequest)
}

var orderbookParticipations []types.OrderBookParticipation
ctx := sdk.UnwrapSDKContext(c)

settledOrderbookParticipationStore := prefix.NewStore(
ctx.KVStore(k.storeKey),
types.SettledOrderbookParticipationListOfBlockHeightPrefix(req.BlockHeight),
)

pageRes, err := query.Paginate(
settledOrderbookParticipationStore,
req.Pagination,
func(key []byte, value []byte) error {
var settledOrderbookParticipation types.SettledOrderbookParticipation
if err := k.cdc.Unmarshal(value, &settledOrderbookParticipation); err != nil {
return err
}

orderBookParticipation, found := k.GetOrderBookParticipation(
ctx,
settledOrderbookParticipation.OrderBookUID,
settledOrderbookParticipation.Index,
)
if found {
orderbookParticipations = append(orderbookParticipations, orderBookParticipation)
}

return nil
},
)
if err != nil {
return nil, status.Error(codes.Internal, err.Error())
}

return &types.QuerySettledOrderBookParticipationsOfHeightResponse{Participations: orderbookParticipations, Pagination: pageRes}, nil
}
8 changes: 8 additions & 0 deletions x/orderbook/types/keys.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,8 @@ var (
0x07,
} // prefix for keys that store book participation and bet pairs
FeeGrantPrefix = []byte{0x08} // prefix for keys that store fee grants
// SettledOrderbookParticipationListPrefix is the prefix to retrieve all settled orderbook participations
SettledOrderbookParticipationListPrefix = []byte{0x09}
)

// GetOrderBookKey returns the bytes of an book key
Expand Down Expand Up @@ -109,3 +111,9 @@ func GetParticipationBetPairKey(bookUID string, bookParticipationIndex uint64, b
GetParticipationByIndexKey(bookUID, bookParticipationIndex),
utils.Uint64ToBytes(betID)...)
}

// SettledOrderbookParticipationListOfBlockHeightPrefix returns prefix of
// settled orderbook participation list on a certain block height.
func SettledOrderbookParticipationListOfBlockHeightPrefix(blockHeight int64) []byte {
return append(SettledOrderbookParticipationListPrefix, utils.Int64ToBytes(blockHeight)...)
}
Loading

0 comments on commit 1c24bff

Please sign in to comment.