Skip to content

Commit

Permalink
add user redemption record query (Stride-Labs#269)
Browse files Browse the repository at this point in the history
  • Loading branch information
asalzmann authored Sep 5, 2022
1 parent bf1e6ea commit 2757fbb
Show file tree
Hide file tree
Showing 6 changed files with 968 additions and 77 deletions.
19 changes: 19 additions & 0 deletions proto/records/query.proto
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,11 @@ service Query {
option (google.api.http).get = "/Stride-Labs/stride/records/user_redemption_record";
}

// Queries a list of UserRedemptionRecord items by chainId / userId pair.
rpc UserRedemptionRecordForUser(QueryAllUserRedemptionRecordForUserRequest) returns (QueryAllUserRedemptionRecordForUserResponse) {
option (google.api.http).get = "/Stride-Labs/stride/records/user_redemption_record_for_user/{chainId}/{day}/{address}/{limit}";
}

// Queries a EpochUnbondingRecord by id.
rpc EpochUnbondingRecord(QueryGetEpochUnbondingRecordRequest) returns (QueryGetEpochUnbondingRecordResponse) {
option (google.api.http).get = "/Stride-Labs/stride/records/epoch_unbonding_record/{epochNumber}";
Expand Down Expand Up @@ -93,6 +98,20 @@ message QueryAllUserRedemptionRecordResponse {
cosmos.base.query.v1beta1.PageResponse pagination = 2;
}

// Query UserRedemptionRecords by chainId / userId pair
message QueryAllUserRedemptionRecordForUserRequest {
string chainId = 1;
uint64 day = 2;
string address = 3;
uint64 limit = 4;
cosmos.base.query.v1beta1.PageRequest pagination = 5;
}

message QueryAllUserRedemptionRecordForUserResponse {
repeated UserRedemptionRecord UserRedemptionRecord = 1 [(gogoproto.nullable) = false];
cosmos.base.query.v1beta1.PageResponse pagination = 2;
}

message QueryGetEpochUnbondingRecordRequest {
uint64 epochNumber = 1;
}
Expand Down
2 changes: 1 addition & 1 deletion scripts-local/build.sh
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ BUILDDIR="$2"
while getopts sghijo flag; do
case "${flag}" in
s) printf '%s' "Building Stride... ";
go build -mod=readonly -trimpath -o $BUILDDIR ./...;
go build -mod=readonly -trimpath -tags "ledger" -ldflags '-X "github.com/cosmos/cosmos-sdk/version.BuildTags=ledger" -w -s' -o $BUILDDIR ./...; # having a build with ledger is convenient
mkdir -p $BUILDDIR/stride2
go build -mod=readonly -trimpath -o $BUILDDIR/stride2 ./... 2>&1 | grep -v -E "deprecated|keychain" || true;
mkdir -p $BUILDDIR/stride3
Expand Down
34 changes: 11 additions & 23 deletions scripts-local/test-utils/run_any_cli_command.sh
Original file line number Diff line number Diff line change
Expand Up @@ -7,38 +7,26 @@ source ${SCRIPT_DIR}/../account_vars.sh
# transfer tokens to stride
# $GAIA_CMD q tx F382BB9C7B9970C41F0BE5F04CB59B85A68CF360949A97C599DFA92A80CAD5D0
# exit
# $GAIA_CMD tx ibc-transfer transfer transfer channel-0 stride1uk4ze0x4nvh4fk0xm4jdud58eqn4yxhrt52vv7 333uatom --from gval1 --chain-id GAIA -y --keyring-backend test
# $GAIA_CMD tx ibc-transfer transfer transfer channel-0 stride1uk4ze0x4nvh4fk0xm4jdud58eqn4yxhrt52vv7 10000uatom --from gval1 --chain-id GAIA -y --keyring-backend test
# exit

# $STRIDE_CMD q stakeibc show-host-zone OSMO
# $OSMO_CMD q bank balances osmo1cx04p5974f8hzh2lqev48kjrjugdxsxy7mzrd0eyweycpr90vk8q8d6f3h
# check val1 balances
# $STRIDE_CMD keys list
# exit

# $STRIDE_CMD q ibc channel channels
# $STRIDE_CMD q bank balances stride1uk4ze0x4nvh4fk0xm4jdud58eqn4yxhrt52vv7
# exit

# $STRIDE_CMD q stakeibc list-host-zone
# exit
# $STRIDE_CMD tx stakeibc liquid-stake 10 uatom --keyring-backend test --from val1 -y --chain-id $STRIDE_CHAIN
# exit
$STRIDE_CMD q bank balances stride1755g4dkhpw73gz9h9nwhlcefc6sdf8kcmvcwrk4rxfrz8xpxxjms7savm8
exit


# liquid stake
# $STRIDE_CMD tx stakeibc liquid-stake 100000 uatom --keyring-backend test --from val1 -y --chain-id $STRIDE_CHAIN
# $STRIDE_CMD tx stakeibc liquid-stake 10000 uatom --keyring-backend test --from val1 -y --chain-id $STRIDE_CHAIN
# exit

# clear balances
$STRIDE_CMD tx stakeibc clear-balance GAIA 66 channel-0 --keyring-backend test --from val1 --chain-id $STRIDE_CHAIN
exit

# redeem stake
# amt_to_redeem=5
# $STRIDE_CMD tx stakeibc redeem-stake $amt_to_redeem GAIA $GAIA_RECEIVER_ACCT \
# --from val1 --keyring-backend test --chain-id $STRIDE_CHAIN -y
# $STRIDE_CMD q tx B3C8E62837FCF9835EB131386A5F7FDE92A20AF4AB0C46E5495B3CDB9F6CF3C1
# exit

EPOCH=9
SENDER_ACCT=stride1uk4ze0x4nvh4fk0xm4jdud58eqn4yxhrt52vv7
$STRIDE_CMD tx stakeibc claim-undelegated-tokens GAIA $EPOCH $SENDER_ACCT --from val1 --keyring-backend test --chain-id $STRIDE_CHAIN -y
amt_to_redeem=3
$STRIDE_CMD tx stakeibc redeem-stake $amt_to_redeem GAIA $GAIA_RECEIVER_ACCT \
--from val1 --keyring-backend test --chain-id $STRIDE_CHAIN -y

exit
51 changes: 51 additions & 0 deletions x/records/keeper/grpc_query_user_redemption_record_for_user.go
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
}
Loading

0 comments on commit 2757fbb

Please sign in to comment.