forked from Stride-Labs/stride
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add user redemption record query (Stride-Labs#269)
- Loading branch information
Showing
6 changed files
with
968 additions
and
77 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
51 changes: 51 additions & 0 deletions
51
x/records/keeper/grpc_query_user_redemption_record_for_user.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
package keeper | ||
|
||
import ( | ||
"context" | ||
|
||
sdk "github.com/cosmos/cosmos-sdk/types" | ||
sdkerrors "github.com/cosmos/cosmos-sdk/types/errors" | ||
"google.golang.org/grpc/codes" | ||
"google.golang.org/grpc/status" | ||
|
||
"github.com/Stride-Labs/stride/x/records/types" | ||
) | ||
|
||
func (k Keeper) UserRedemptionRecordForUser(c context.Context, req *types.QueryAllUserRedemptionRecordForUserRequest) (*types.QueryAllUserRedemptionRecordForUserResponse, error) { | ||
if req == nil { | ||
return nil, status.Error(codes.InvalidArgument, "invalid request") | ||
} | ||
|
||
// validate the address | ||
_, err := sdk.AccAddressFromBech32(req.Address) | ||
if err != nil { | ||
return nil, sdkerrors.Wrap(sdkerrors.ErrInvalidAddress, req.Address) | ||
} | ||
|
||
var userRedemptionRecords []types.UserRedemptionRecord | ||
|
||
ctx := sdk.UnwrapSDKContext(c) | ||
|
||
// limit loop to 50 records for performance | ||
var loopback uint64 | ||
loopback = req.Limit | ||
if loopback > 50 { | ||
loopback = 50 | ||
} | ||
var i uint64 | ||
for i = 0; i < loopback; i++ { | ||
if i > req.Day { | ||
// we have reached the end of the records | ||
break | ||
} | ||
currentDay := req.Day - i | ||
// query the user redemption record for the current day | ||
userRedemptionRecord, found := k.GetUserRedemptionRecord(ctx, types.UserRedemptionRecordKeyFormatter(req.ChainId, currentDay, req.Address)) | ||
if !found { | ||
continue | ||
} | ||
userRedemptionRecords = append(userRedemptionRecords, userRedemptionRecord) | ||
} | ||
|
||
return &types.QueryAllUserRedemptionRecordForUserResponse{UserRedemptionRecord: userRedemptionRecords}, nil | ||
} |
Oops, something went wrong.