[R4R] return all claims for incentive queries (#642)

* feat: return all claims for incentive queries

* cleanup test comments

* add struct tags

Co-authored-by: Ruaridh <rhuairahrighairidh@users.noreply.github.com>

Co-authored-by: Ruaridh <rhuairahrighairidh@users.noreply.github.com>
This commit is contained in:
Kevin Davis 2020-09-01 08:34:11 -04:00 committed by GitHub
parent 4121b692a0
commit f22139fcee
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
7 changed files with 66 additions and 9 deletions

View File

@ -24,7 +24,7 @@ func NewHandler(k keeper.Keeper) sdk.Handler {
func handleMsgClaimReward(ctx sdk.Context, k keeper.Keeper, msg types.MsgClaimReward) (*sdk.Result, error) { func handleMsgClaimReward(ctx sdk.Context, k keeper.Keeper, msg types.MsgClaimReward) (*sdk.Result, error) {
claims, found := k.GetClaimsByAddressAndCollateralType(ctx, msg.Sender, msg.CollateralType) claims, found := k.GetActiveClaimsByAddressAndCollateralType(ctx, msg.Sender, msg.CollateralType)
if !found { if !found {
return nil, sdkerrors.Wrapf(types.ErrNoClaimsFound, "address: %s, collateral type: %s", msg.Sender, msg.CollateralType) return nil, sdkerrors.Wrapf(types.ErrNoClaimsFound, "address: %s, collateral type: %s", msg.Sender, msg.CollateralType)
} }

View File

@ -159,8 +159,8 @@ func (suite *KeeperTestSuite) addObjectsToStore() {
suite.keeper.SetClaimPeriod(suite.ctx, cp1) suite.keeper.SetClaimPeriod(suite.ctx, cp1)
suite.keeper.SetClaimPeriod(suite.ctx, cp2) suite.keeper.SetClaimPeriod(suite.ctx, cp2)
suite.keeper.SetNextClaimPeriodID(suite.ctx, "bnb", 1) suite.keeper.SetNextClaimPeriodID(suite.ctx, "bnb", 2)
suite.keeper.SetNextClaimPeriodID(suite.ctx, "xrp", 1) suite.keeper.SetNextClaimPeriodID(suite.ctx, "xrp", 2)
c1 := types.NewClaim(suite.addrs[0], c("ukava", 1000000), "bnb", 1) c1 := types.NewClaim(suite.addrs[0], c("ukava", 1000000), "bnb", 1)
c2 := types.NewClaim(suite.addrs[0], c("ukava", 1000000), "xrp", 1) c2 := types.NewClaim(suite.addrs[0], c("ukava", 1000000), "xrp", 1)

View File

@ -118,8 +118,8 @@ func (k Keeper) DeleteExpiredClaimsAndClaimPeriods(ctx sdk.Context) {
}) })
} }
// GetClaimsByAddressAndCollateralType returns all claims for a specific user and address and a bool for if any were found // GetActiveClaimsByAddressAndCollateralType returns all claims for a specific user and address and a bool for if any were found
func (k Keeper) GetClaimsByAddressAndCollateralType(ctx sdk.Context, addr sdk.AccAddress, collateralType string) (claims types.Claims, found bool) { func (k Keeper) GetActiveClaimsByAddressAndCollateralType(ctx sdk.Context, addr sdk.AccAddress, collateralType string) (claims types.Claims, found bool) {
found = false found = false
k.IterateClaimPeriods(ctx, func(cp types.ClaimPeriod) (stop bool) { k.IterateClaimPeriods(ctx, func(cp types.ClaimPeriod) (stop bool) {
if cp.CollateralType != collateralType { if cp.CollateralType != collateralType {
@ -136,6 +136,32 @@ func (k Keeper) GetClaimsByAddressAndCollateralType(ctx sdk.Context, addr sdk.Ac
return claims, found return claims, found
} }
// GetAllClaimsByAddressAndCollateralType returns all claims for a specific user and address and a bool for if any were found
func (k Keeper) GetAllClaimsByAddressAndCollateralType(ctx sdk.Context, addr sdk.AccAddress, collateralType string) (claims types.AugmentedClaims, found bool) {
found = false
k.IterateClaimPeriods(ctx, func(cp types.ClaimPeriod) (stop bool) {
if cp.CollateralType != collateralType {
return false
}
c, hasClaim := k.GetClaim(ctx, addr, cp.CollateralType, cp.ID)
if !hasClaim {
return false
}
ac := types.NewAugmentedClaim(c, true)
found = true
claims = append(claims, ac)
return false
})
nextClaimID := k.GetNextClaimPeriodID(ctx, collateralType)
c, hasClaim := k.GetClaim(ctx, addr, collateralType, nextClaimID)
if !hasClaim {
return claims, found
}
ac := types.NewAugmentedClaim(c, false)
claims = append(claims, ac)
return claims, true
}
// addCoinsToVestingSchedule adds coins to the input account's vesting schedule where length is the amount of time (from the current block time), in seconds, that the coins will be vesting for // addCoinsToVestingSchedule adds coins to the input account's vesting schedule where length is the amount of time (from the current block time), in seconds, that the coins will be vesting for
// the input address must be a periodic vesting account // the input address must be a periodic vesting account
func (k Keeper) addCoinsToVestingSchedule(ctx sdk.Context, addr sdk.AccAddress, amt sdk.Coins, length int64) { func (k Keeper) addCoinsToVestingSchedule(ctx sdk.Context, addr sdk.AccAddress, amt sdk.Coins, length int64) {

View File

@ -73,7 +73,7 @@ func queryGetClaims(ctx sdk.Context, req abci.RequestQuery, k Keeper) ([]byte, e
if err != nil { if err != nil {
return nil, sdkerrors.Wrap(sdkerrors.ErrJSONUnmarshal, err.Error()) return nil, sdkerrors.Wrap(sdkerrors.ErrJSONUnmarshal, err.Error())
} }
claims, _ := k.GetClaimsByAddressAndCollateralType(ctx, requestParams.Owner, requestParams.CollateralType) claims, _ := k.GetAllClaimsByAddressAndCollateralType(ctx, requestParams.Owner, requestParams.CollateralType)
bz, err := codec.MarshalJSONIndent(k.cdc, claims) bz, err := codec.MarshalJSONIndent(k.cdc, claims)
if err != nil { if err != nil {

View File

@ -26,10 +26,12 @@ func (suite *KeeperTestSuite) TestQuerier() {
} }
bz, err = querier(suite.ctx, []string{types.QueryGetClaims}, query) bz, err = querier(suite.ctx, []string{types.QueryGetClaims}, query)
var claims types.Claims var claims types.AugmentedClaims
suite.Nil(types.ModuleCdc.UnmarshalJSON(bz, &claims)) suite.Nil(types.ModuleCdc.UnmarshalJSON(bz, &claims))
suite.Equal(1, len(claims)) suite.Equal(1, len(claims))
suite.Equal(types.Claims{types.NewClaim(suite.addrs[0], c("ukava", 1000000), "bnb", 1)}, claims) suite.Equal(types.AugmentedClaims{
types.NewAugmentedClaim(types.NewClaim(suite.addrs[0], c("ukava", 1000000), "bnb", 1), true),
}, claims)
var rp types.RewardPeriods var rp types.RewardPeriods
bz, err = querier(suite.ctx, []string{types.QueryGetRewardPeriods}, abci.RequestQuery{}) bz, err = querier(suite.ctx, []string{types.QueryGetRewardPeriods}, abci.RequestQuery{})

View File

@ -78,7 +78,7 @@ func SimulateMsgClaimReward(ak auth.AccountKeeper, sk types.SupplyKeeper, k keep
claimer, claim, found := findValidAccountClaimPair(accs, openClaims, func(acc simulation.Account, claim types.Claim) bool { claimer, claim, found := findValidAccountClaimPair(accs, openClaims, func(acc simulation.Account, claim types.Claim) bool {
if validAccounts[acc.Address.String()] { // Address must be valid type if validAccounts[acc.Address.String()] { // Address must be valid type
if claim.Owner.Equals(acc.Address) { // Account must be claim owner if claim.Owner.Equals(acc.Address) { // Account must be claim owner
allClaims, found := k.GetClaimsByAddressAndCollateralType(ctx, claim.Owner, claim.CollateralType) allClaims, found := k.GetActiveClaimsByAddressAndCollateralType(ctx, claim.Owner, claim.CollateralType)
if found { // found should always be true if found { // found should always be true
var rewards sdk.Coins var rewards sdk.Coins
for _, individualClaim := range allClaims { for _, individualClaim := range allClaims {

View File

@ -206,6 +206,35 @@ func (c Claim) String() string {
// Claims array of Claim // Claims array of Claim
type Claims []Claim type Claims []Claim
// AugmentedClaim claim type with information about whether the claim is currently claimable
type AugmentedClaim struct {
Claim Claim `json:"claim" yaml:"claim"`
Claimable bool `json:"claimable" yaml:"claimable"`
}
func (ac AugmentedClaim) String() string {
return fmt.Sprintf(`Claim:
Owner: %s,
Collateral Type: %s,
Reward: %s,
Claim Period ID: %d,
Claimable: %t,
`, ac.Claim.Owner, ac.Claim.CollateralType, ac.Claim.Reward, ac.Claim.ClaimPeriodID, ac.Claimable,
)
}
// NewAugmentedClaim returns a new augmented claim struct
func NewAugmentedClaim(claim Claim, claimable bool) AugmentedClaim {
return AugmentedClaim{
Claim: claim,
Claimable: claimable,
}
}
// AugmentedClaims array of AugmentedClaim
type AugmentedClaims []AugmentedClaim
// Validate checks if all the claims are valid and there are no duplicated // Validate checks if all the claims are valid and there are no duplicated
// entries. // entries.
func (cs Claims) Validate() error { func (cs Claims) Validate() error {