diff --git a/docs/core/proto-docs.md b/docs/core/proto-docs.md index bbd36a30..f34f05d7 100644 --- a/docs/core/proto-docs.md +++ b/docs/core/proto-docs.md @@ -4832,6 +4832,7 @@ GenesisState is the state that must be provided at genesis. | `savings_claims` | [SavingsClaim](#kava.incentive.v1beta1.SavingsClaim) | repeated | | | `earn_reward_state` | [GenesisRewardState](#kava.incentive.v1beta1.GenesisRewardState) | | | | `earn_claims` | [EarnClaim](#kava.incentive.v1beta1.EarnClaim) | repeated | | +| `claims` | [Claim](#kava.incentive.v1beta1.Claim) | repeated | | diff --git a/proto/kava/incentive/v1beta1/genesis.proto b/proto/kava/incentive/v1beta1/genesis.proto index 451bc226..fabc5354 100644 --- a/proto/kava/incentive/v1beta1/genesis.proto +++ b/proto/kava/incentive/v1beta1/genesis.proto @@ -64,4 +64,6 @@ message GenesisState { GenesisRewardState earn_reward_state = 13 [(gogoproto.nullable) = false]; repeated EarnClaim earn_claims = 14 [(gogoproto.castrepeated) = "EarnClaims", (gogoproto.nullable) = false]; + + repeated Claim claims = 15 [(gogoproto.castrepeated) = "Claims", (gogoproto.nullable) = false]; } diff --git a/x/incentive/genesis.go b/x/incentive/genesis.go index 6f2580ff..26949197 100644 --- a/x/incentive/genesis.go +++ b/x/incentive/genesis.go @@ -44,6 +44,11 @@ func InitGenesis( k.SetParams(ctx, gs.Params) + // Set Claims of all types + for _, claim := range gs.Claims { + k.SetClaim(ctx, claim) + } + // USDX Minting for _, claim := range gs.USDXMintingClaims { k.SetUSDXMintingClaim(ctx, claim) @@ -146,6 +151,8 @@ func InitGenesis( func ExportGenesis(ctx sdk.Context, k keeper.Keeper) types.GenesisState { params := k.GetParams(ctx) + claims := k.GetAllClaims(ctx) + usdxClaims := k.GetAllUSDXMintingClaims(ctx) usdxRewardState := getUSDXMintingGenesisRewardState(ctx, k) @@ -170,7 +177,7 @@ func ExportGenesis(ctx sdk.Context, k keeper.Keeper) types.GenesisState { // Reward states usdxRewardState, hardSupplyRewardState, hardBorrowRewardState, delegatorRewardState, swapRewardState, savingsRewardState, earnRewardState, // Claims - usdxClaims, hardClaims, delegatorClaims, swapClaims, savingsClaims, earnClaims, + claims, usdxClaims, hardClaims, delegatorClaims, swapClaims, savingsClaims, earnClaims, ) } diff --git a/x/incentive/genesis_test.go b/x/incentive/genesis_test.go index 887c8ea0..92df67d7 100644 --- a/x/incentive/genesis_test.go +++ b/x/incentive/genesis_test.go @@ -102,6 +102,7 @@ func (suite *GenesisTestSuite) SetupTest() { types.DefaultGenesisRewardState, types.DefaultGenesisRewardState, types.DefaultGenesisRewardState, + types.DefaultClaims, types.DefaultUSDXClaims, types.DefaultHardClaims, types.DefaultDelegatorClaims, @@ -219,6 +220,14 @@ func (suite *GenesisTestSuite) TestExportedGenesisMatchesImported() { types.NewMultiRewardIndex("usdx", types.RewardIndexes{{CollateralType: "usdx", RewardFactor: d("0.2")}}), }, ), + types.Claims{ + types.NewClaim( + types.CLAIM_TYPE_USDX_MINTING, + suite.addrs[3], + nil, + types.MultiRewardIndexes{{CollateralType: "btcb/usdx", RewardIndexes: types.RewardIndexes{{CollateralType: "swap", RewardFactor: d("0.0")}}}}, + ), + }, types.USDXMintingClaims{ types.NewUSDXMintingClaim( suite.addrs[0], diff --git a/x/incentive/keeper/keeper.go b/x/incentive/keeper/keeper.go index 1b90f971..a6d1c60d 100644 --- a/x/incentive/keeper/keeper.go +++ b/x/incentive/keeper/keeper.go @@ -881,3 +881,103 @@ func (k Keeper) IterateEarnRewardAccrualTimes(ctx sdk.Context, cb func(string, t } } } + +// ----------------------------------------------------------------------------- +// New deduplicated methods + +// GetClaim returns the claim in the store corresponding the the owner and +// claimType, and a boolean for if the claim was found +func (k Keeper) GetClaim( + ctx sdk.Context, + claimType types.ClaimType, + addr sdk.AccAddress, +) (types.Claim, bool) { + store := prefix.NewStore(ctx.KVStore(k.key), types.GetClaimKeyPrefix(claimType)) + bz := store.Get(addr) + if bz == nil { + return types.Claim{}, false + } + var c types.Claim + k.cdc.MustUnmarshal(bz, &c) + return c, true +} + +// SetClaim sets the claim in the store corresponding to the owner and claimType +func (k Keeper) SetClaim( + ctx sdk.Context, + c types.Claim, +) { + store := prefix.NewStore(ctx.KVStore(k.key), types.GetClaimKeyPrefix(c.Type)) + bz := k.cdc.MustMarshal(&c) + store.Set(c.Owner, bz) +} + +// DeleteClaim deletes the claim in the store corresponding to the owner and claimType +func (k Keeper) DeleteClaim( + ctx sdk.Context, + claimType types.ClaimType, + owner sdk.AccAddress, +) { + store := prefix.NewStore(ctx.KVStore(k.key), types.GetClaimKeyPrefix(claimType)) + store.Delete(owner) +} + +// IterateClaims iterates over all claim objects in the store of a given +// claimType and preforms a callback function +func (k Keeper) IterateClaims( + ctx sdk.Context, + claimType types.ClaimType, + cb func(c types.Claim) (stop bool), +) { + iterator := sdk.KVStorePrefixIterator(ctx.KVStore(k.key), types.GetClaimKeyPrefix(claimType)) + defer iterator.Close() + for ; iterator.Valid(); iterator.Next() { + var c types.Claim + k.cdc.MustUnmarshal(iterator.Value(), &c) + if cb(c) { + break + } + } +} + +// GetClaims returns all Claim objects in the store of a given claimType +func (k Keeper) GetClaims( + ctx sdk.Context, + claimType types.ClaimType, +) types.Claims { + var cs types.Claims + k.IterateClaims(ctx, claimType, func(c types.Claim) (stop bool) { + cs = append(cs, c) + return false + }) + + return cs +} + +// IterateAllClaims iterates over all claim objects of any claimType in the +// store and preforms a callback function +func (k Keeper) IterateAllClaims( + ctx sdk.Context, + cb func(c types.Claim) (stop bool), +) { + iterator := sdk.KVStorePrefixIterator(ctx.KVStore(k.key), types.ClaimKeyPrefix) + defer iterator.Close() + for ; iterator.Valid(); iterator.Next() { + var c types.Claim + k.cdc.MustUnmarshal(iterator.Value(), &c) + if cb(c) { + break + } + } +} + +// GetAllClaims returns all Claim objects in the store of any claimType +func (k Keeper) GetAllClaims(ctx sdk.Context) types.Claims { + var cs types.Claims + k.IterateAllClaims(ctx, func(c types.Claim) (stop bool) { + cs = append(cs, c) + return false + }) + + return cs +} diff --git a/x/incentive/keeper/keeper_state_test.go b/x/incentive/keeper/keeper_state_test.go new file mode 100644 index 00000000..4a92713a --- /dev/null +++ b/x/incentive/keeper/keeper_state_test.go @@ -0,0 +1,87 @@ +package keeper_test + +import ( + "github.com/kava-labs/kava/x/incentive/types" +) + +func (suite *KeeperTestSuite) TestGetSetDeleteClaims() { + suite.SetupApp() + + for claimTypeName, claimTypeValue := range types.ClaimType_value { + suite.Run(claimTypeName, func() { + claimType := types.ClaimType(claimTypeValue) + + c := types.NewClaim( + claimType, + suite.addrs[0], + arbitraryCoins(), + nonEmptyMultiRewardIndexes, + ) + + _, found := suite.keeper.GetClaim(suite.ctx, claimType, suite.addrs[0]) + suite.Require().False(found) + + suite.Require().NotPanics(func() { + suite.keeper.SetClaim(suite.ctx, c) + }) + testC, found := suite.keeper.GetClaim(suite.ctx, claimType, suite.addrs[0]) + suite.Require().True(found) + suite.Require().Equal(c, testC) + + // Check that other claim types do not exist for the same address + for otherClaimTypeName, otherClaimTypeValue := range types.ClaimType_value { + // Skip the current claim type + if otherClaimTypeValue == claimTypeValue { + continue + } + + otherClaimType := types.ClaimType(otherClaimTypeValue) + _, found := suite.keeper.GetClaim(suite.ctx, otherClaimType, suite.addrs[0]) + suite.Require().False(found, "claim type %s should not exist", otherClaimTypeName) + } + + suite.Require().NotPanics(func() { + suite.keeper.DeleteClaim(suite.ctx, claimType, suite.addrs[0]) + }) + _, found = suite.keeper.GetClaim(suite.ctx, claimType, suite.addrs[0]) + suite.Require().False(found) + }) + } +} + +func (suite *KeeperTestSuite) TestIterateClaims() { + suite.SetupApp() + var claims types.Claims + + // Add 2 of each type of claim + for _, claimTypeValue := range types.ClaimType_value { + c := types.Claims{ + types.NewClaim(types.ClaimType(claimTypeValue), suite.addrs[0], arbitraryCoins(), nonEmptyMultiRewardIndexes), + types.NewClaim(types.ClaimType(claimTypeValue), suite.addrs[1], nil, nil), + } + claims = append(claims, c...) + } + + for _, claim := range claims { + suite.keeper.SetClaim(suite.ctx, claim) + } + + for _, claimTypeValue := range types.ClaimType_value { + claimType := types.ClaimType(claimTypeValue) + + // Claims of specific claim type only should be returned + claims := suite.keeper.GetClaims(suite.ctx, claimType) + suite.Require().Len(claims, 2) + suite.Require().Equalf( + claims, types.Claims{ + types.NewClaim(claimType, suite.addrs[0], arbitraryCoins(), nonEmptyMultiRewardIndexes), + types.NewClaim(claimType, suite.addrs[1], nil, nil), + }, + "GetClaims(%s) should only return claims of those type", claimType, + ) + } + + allClaims := suite.keeper.GetAllClaims(suite.ctx) + suite.Require().Len(allClaims, len(claims)) + suite.Require().ElementsMatch(allClaims, claims, "GetAllClaims() should return claims of all types") +} diff --git a/x/incentive/types/claims.go b/x/incentive/types/claims.go index b9862b2a..a807e11a 100644 --- a/x/incentive/types/claims.go +++ b/x/incentive/types/claims.go @@ -163,6 +163,20 @@ func (cs USDXMintingClaims) Validate() error { return nil } +// Claims defines a slice of Claims +type Claims []Claim + +// Validate checks if all the claims are valid. +func (cs Claims) Validate() error { + for _, c := range cs { + if err := c.Validate(); err != nil { + return err + } + } + + return nil +} + // NewHardLiquidityProviderClaim returns a new HardLiquidityProviderClaim func NewHardLiquidityProviderClaim(owner sdk.AccAddress, rewards sdk.Coins, supplyRewardIndexes, borrowRewardIndexes MultiRewardIndexes, diff --git a/x/incentive/types/claims_test.go b/x/incentive/types/claims_test.go index 0e147e2f..36689ab8 100644 --- a/x/incentive/types/claims_test.go +++ b/x/incentive/types/claims_test.go @@ -21,6 +21,59 @@ func cs(coins ...sdk.Coin) sdk.Coins { return sdk.NewCoins(coins...) } func TestClaims_Validate(t *testing.T) { owner := sdk.AccAddress(crypto.AddressHash([]byte("KavaTestUser1"))) + t.Run("Claims", func(t *testing.T) { + validRewardIndexes := RewardIndexes{}.With("ukava", d("0.002")) + validMultiRewardIndexes := MultiRewardIndexes{}.With("ukava", validRewardIndexes) + invalidRewardIndexes := RewardIndexes{}.With("ukava", d("-0.002")) + invalidMultiRewardIndexes := MultiRewardIndexes{}.With("ukava", invalidRewardIndexes) + + testCases := []struct { + name string + claims Claims + expPass bool + }{ + { + name: "valid", + claims: Claims{ + NewClaim(CLAIM_TYPE_USDX_MINTING, owner, cs(c("bnb", 1)), validMultiRewardIndexes), + }, + expPass: true, + }, + { + name: "invalid owner", + claims: Claims{ + NewClaim(CLAIM_TYPE_USDX_MINTING, nil, cs(c("bnb", 1)), validMultiRewardIndexes), + }, + expPass: false, + }, + { + name: "invalid reward", + claims: Claims{ + NewClaim(CLAIM_TYPE_USDX_MINTING, owner, sdk.Coins{sdk.Coin{Denom: "invalid😫"}}, validMultiRewardIndexes), + }, + expPass: false, + }, + { + name: "invalid indexes", + claims: Claims{ + NewClaim(CLAIM_TYPE_USDX_MINTING, nil, cs(c("bnb", 1)), invalidMultiRewardIndexes), + }, + expPass: false, + }, + } + + for _, tc := range testCases { + t.Run(tc.name, func(t *testing.T) { + err := tc.claims.Validate() + if tc.expPass { + require.NoError(t, err) + } else { + require.Error(t, err) + } + }) + } + }) + t.Run("USDXMintingClaims", func(t *testing.T) { testCases := []struct { name string diff --git a/x/incentive/types/genesis.go b/x/incentive/types/genesis.go index 1955617b..699d5ea0 100644 --- a/x/incentive/types/genesis.go +++ b/x/incentive/types/genesis.go @@ -6,6 +6,7 @@ import ( ) var ( + DefaultClaims = Claims{} DefaultUSDXClaims = USDXMintingClaims{} DefaultHardClaims = HardLiquidityProviderClaims{} DefaultDelegatorClaims = DelegatorClaims{} @@ -22,7 +23,8 @@ var ( func NewGenesisState( params Params, usdxState, hardSupplyState, hardBorrowState, delegatorState, swapState, savingsState, earnState GenesisRewardState, - c USDXMintingClaims, hc HardLiquidityProviderClaims, dc DelegatorClaims, sc SwapClaims, savingsc SavingsClaims, + c Claims, + uc USDXMintingClaims, hc HardLiquidityProviderClaims, dc DelegatorClaims, sc SwapClaims, savingsc SavingsClaims, earnc EarnClaims, ) GenesisState { return GenesisState{ @@ -36,7 +38,9 @@ func NewGenesisState( SavingsRewardState: savingsState, EarnRewardState: earnState, - USDXMintingClaims: c, + // Claims of all types + Claims: c, + USDXMintingClaims: uc, HardLiquidityProviderClaims: hc, DelegatorClaims: dc, SwapClaims: sc, @@ -56,6 +60,7 @@ func DefaultGenesisState() GenesisState { SwapRewardState: DefaultGenesisRewardState, SavingsRewardState: DefaultGenesisRewardState, EarnRewardState: DefaultGenesisRewardState, + Claims: DefaultClaims, USDXMintingClaims: DefaultUSDXClaims, HardLiquidityProviderClaims: DefaultHardClaims, DelegatorClaims: DefaultDelegatorClaims, @@ -94,6 +99,10 @@ func (gs GenesisState) Validate() error { return err } + if err := gs.Claims.Validate(); err != nil { + return err + } + if err := gs.USDXMintingClaims.Validate(); err != nil { return err } diff --git a/x/incentive/types/genesis.pb.go b/x/incentive/types/genesis.pb.go index 3178c213..2ffcd9f6 100644 --- a/x/incentive/types/genesis.pb.go +++ b/x/incentive/types/genesis.pb.go @@ -121,6 +121,7 @@ type GenesisState struct { SavingsClaims SavingsClaims `protobuf:"bytes,12,rep,name=savings_claims,json=savingsClaims,proto3,castrepeated=SavingsClaims" json:"savings_claims"` EarnRewardState GenesisRewardState `protobuf:"bytes,13,opt,name=earn_reward_state,json=earnRewardState,proto3" json:"earn_reward_state"` EarnClaims EarnClaims `protobuf:"bytes,14,rep,name=earn_claims,json=earnClaims,proto3,castrepeated=EarnClaims" json:"earn_claims"` + Claims Claims `protobuf:"bytes,15,rep,name=claims,proto3,castrepeated=Claims" json:"claims"` } func (m *GenesisState) Reset() { *m = GenesisState{} } @@ -167,57 +168,58 @@ func init() { } var fileDescriptor_8b76737885d05afd = []byte{ - // 785 bytes of a gzipped FileDescriptorProto - 0x1f, 0x8b, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0xff, 0xa4, 0x95, 0xcf, 0x4f, 0xdb, 0x48, - 0x14, 0xc7, 0x63, 0x60, 0x59, 0x98, 0x00, 0x21, 0xb3, 0x01, 0xb2, 0x41, 0x72, 0x58, 0x40, 0xbb, - 0xd1, 0x56, 0xb5, 0x45, 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0, 0xef, 0x63, 0x68, 0x80, 0x9a, 0x1f, - 0xe0, 0x2e, 0xf1, 0x22, 0xda, 0x46, 0x29, 0x95, 0x36, 0xc3, 0x54, 0xa7, 0x36, 0xa4, 0x46, 0xb1, - 0x59, 0x53, 0x62, 0x0f, 0x4a, 0xe2, 0x41, 0x39, 0x4c, 0x3c, 0x68, 0x73, 0xe7, 0x97, 0xf5, 0xc2, - 0xd9, 0x55, 0x5d, 0xd2, 0xab, 0x89, 0xce, 0xa8, 0x99, 0xcd, 0xf7, 0x53, 0x00, 0x3e, 0x8d, 0x2f, - 0x53, 0xc7, 0x3d, 0x14, 0x98, 0xad, 0x10, 0x85, 0x18, 0x06, 0x00, 0x8e, 0x11, 0x69, 0x55, 0xda, - 0x98, 0x6e, 0x14, 0x9b, 0x0d, 0xe5, 0xf6, 0xeb, 0x56, 0x46, 0xc5, 0xb5, 0xbf, 0x99, 0x81, 0xcf, - 0x57, 0xf5, 0xf2, 0xe8, 0x0e, 0xd5, 0xcb, 0x68, 0x34, 0x04, 0xbb, 0xa0, 0xe2, 0x44, 0x76, 0x48, - 0xda, 0x01, 0x37, 0xd2, 0x26, 0xae, 0x89, 0x4f, 0x31, 0xad, 0x4e, 0xdd, 0x4d, 0xdd, 0x67, 0x67, - 0x62, 0xef, 0x7b, 0xec, 0x84, 0x56, 0x13, 0x54, 0x38, 0xba, 0x83, 0xa9, 0x0e, 0x9d, 0xb1, 0xd8, - 0xe6, 0x97, 0x22, 0x58, 0x10, 0x57, 0x10, 0x17, 0xff, 0x10, 0xcc, 0xc6, 0x6d, 0xe6, 0x7d, 0x29, - 0x36, 0xe5, 0x3c, 0xf4, 0x01, 0xcf, 0xd2, 0x66, 0x18, 0x50, 0x17, 0x67, 0xa0, 0x07, 0xca, 0x11, - 0x35, 0x4f, 0x93, 0x2a, 0x28, 0x93, 0x14, 0xcd, 0xfa, 0x3f, 0x4f, 0x68, 0xbc, 0x03, 0xda, 0x1a, - 0x13, 0x1d, 0x5c, 0x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 0xbf, 0xa6, 0x0f, 0x7e, 0x04, 0x00, 0x00, 0xff, 0xff, 0x40, 0x8f, 0xb7, 0x18, 0x7f, 0x08, 0x00, - 0x00, + // 803 bytes of a gzipped FileDescriptorProto + 0x1f, 0x8b, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0xff, 0xa4, 0x56, 0x4f, 0x4f, 0x3b, 0x45, + 0x18, 0xee, 0x02, 0x56, 0x98, 0x42, 0x4b, 0xc7, 0x02, 0xb5, 0xc4, 0x2d, 0x02, 0xd1, 0x46, 0xe3, + 0x6e, 0xa8, 0x57, 0x2f, 0xae, 0x10, 0x25, 0x91, 0x84, 0x6c, 0x91, 0x18, 0x63, 0xd2, 0xcc, 0x76, + 0x87, 0x65, 0x74, 0xff, 0xb9, 0xb3, 0xdb, 0xd2, 0x9b, 0x47, 0x0f, 0x1e, 0xf8, 0x00, 0x26, 0xde, + 0xfd, 0x24, 0x1c, 0x39, 0x7a, 0x02, 0x2d, 0x5f, 0xc4, 0xcc, 0x9f, 0x2d, 0xbb, 0x2d, 0x8b, 0xf9, + 0xf5, 0x77, 0x9b, 0x7d, 0xe7, 0x79, 0x9f, 0xe7, 0x79, 0xe7, 0x7d, 0xa7, 0x53, 0x70, 0xf8, 0x33, + 0x1a, 0x22, 0x9d, 0xf8, 0x03, 0xec, 0xc7, 0x64, 0x88, 0xf5, 0xe1, 0x91, 0x85, 0x63, 0x74, 0xa4, + 0x3b, 0xd8, 0xc7, 0x94, 0x50, 0x2d, 0x8c, 0x82, 0x38, 0x80, 0xdb, 0x0c, 0xa5, 0x4d, 0x51, 0x9a, + 0x44, 0xb5, 0x1a, 0x4e, 0xe0, 0x04, 0x1c, 0xa2, 0xb3, 0x95, 0x40, 0xb7, 0x0e, 0x0a, 0x38, 0x07, + 0x2e, 0x22, 0x1e, 0xfd, 0x1f, 0x50, 0x88, 0x22, 0x34, 0x05, 0xb5, 0x9d, 0x20, 0x70, 0x5c, 0xac, + 0xf3, 0x2f, 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0xd4, 0x84, 0xde, 0x5c, 0x6c, 0xff, 0xf7, 0x75, + 0xb0, 0x2e, 0x8f, 0x40, 0x14, 0xff, 0x05, 0x28, 0x8b, 0x36, 0xf3, 0xbe, 0x54, 0xba, 0x6a, 0x91, + 0xf4, 0x39, 0x47, 0x19, 0x2b, 0x4c, 0xd0, 0x94, 0x39, 0x30, 0x00, 0xf5, 0x84, 0xda, 0x37, 0x69, + 0x15, 0x94, 0x51, 0xca, 0x66, 0x7d, 0x52, 0x44, 0x34, 0xdf, 0x01, 0x63, 0x87, 0x91, 0x4e, 0x1e, + 0xda, 0xb5, 0xef, 0x7a, 0xc7, 0xdf, 0x67, 0x36, 0xcc, 0x1a, 0x63, 0xcf, 0xf6, 0x8a, 0x80, 0xe6, + 0x35, 0x57, 0x4a, 0xc2, 0xd0, 0x1d, 0xe7, 0x75, 0x97, 0xdf, 0x58, 0x57, 0x14, 0xb3, 0xc5, 0x18, + 0x7b, 0x9c, 0xf0, 0x25, 0x29, 0x2b, 0x88, 0xa2, 0x60, 0x94, 0x97, 0x5a, 0x79, 0x1b, 0x29, 0x83, + 0x13, 0x66, 0xa5, 0xae, 0xc0, 0xb6, 0x8d, 0x5d, 0xec, 0xa0, 0x38, 0x88, 0xf2, 0x42, 0xef, 0x2c, + 0x28, 0xd4, 0x98, 0xf2, 0x65, 0x75, 0x7e, 0x04, 0x75, 0x3a, 0x42, 0x61, 0x5e, 0xa2, 0xbc, 0xa0, + 0x44, 0x8d, 0x51, 0x65, 0xd9, 0x7f, 0x53, 0xc0, 0x7b, 0x7c, 0x1a, 0x3c, 0xe2, 0xc7, 0xc4, 0x77, + 0xfa, 0xe2, 0x47, 0xa6, 0xf9, 0xee, 0xeb, 0x33, 0xcd, 0x7a, 0x7e, 0x26, 0x32, 0xbe, 0x62, 0x09, + 0x86, 0x26, 0xa7, 0xa1, 0x3e, 0xbb, 0x43, 0xd9, 0xf5, 0x9a, 0x0b, 0x9a, 0x7c, 0x04, 0x73, 0x21, + 0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 0x1a, 0x9e, 0x80, 0xb2, 0xa4, 0xac, 0x71, 0xca, 0x0f, + 0x8a, 0x28, 0x05, 0x5d, 0x55, 0xd2, 0x95, 0x25, 0x95, 0x4c, 0x36, 0x4e, 0xef, 0xfe, 0x55, 0x4b, + 0x77, 0x13, 0x55, 0xb9, 0x9f, 0xa8, 0xca, 0x3f, 0x13, 0x55, 0xb9, 0x7d, 0x52, 0x4b, 0xf7, 0x4f, + 0x6a, 0xe9, 0xef, 0x27, 0xb5, 0xf4, 0xc3, 0xa7, 0x0e, 0x89, 0xaf, 0x13, 0x4b, 0x1b, 0x04, 0x9e, + 0xce, 0xe8, 0x3f, 0x73, 0x91, 0x45, 0xf9, 0x4a, 0xbf, 0xc9, 0xfc, 0x5d, 0x60, 0xaf, 0x3a, 0xb5, + 0xca, 0xfc, 0x51, 0xfe, 0xfc, 0xbf, 0x00, 0x00, 0x00, 0xff, 0xff, 0xc0, 0xab, 0xe5, 0xd0, 0xc6, + 0x08, 0x00, 0x00, } func (m *AccumulationTime) Marshal() (dAtA []byte, err error) { @@ -329,6 +331,20 @@ func (m *GenesisState) MarshalToSizedBuffer(dAtA []byte) (int, error) { _ = i var l int _ = l + if len(m.Claims) > 0 { + for iNdEx := len(m.Claims) - 1; iNdEx >= 0; iNdEx-- { + { + size, err := m.Claims[iNdEx].MarshalToSizedBuffer(dAtA[:i]) + if err != nil { + return 0, err + } + i -= size + i = encodeVarintGenesis(dAtA, i, uint64(size)) + } + i-- + dAtA[i] = 0x7a + } + } if len(m.EarnClaims) > 0 { for iNdEx := len(m.EarnClaims) - 1; iNdEx >= 0; iNdEx-- { { @@ -601,6 +617,12 @@ func (m *GenesisState) Size() (n int) { n += 1 + l + sovGenesis(uint64(l)) } } + if len(m.Claims) > 0 { + for _, e := range m.Claims { + l = e.Size() + n += 1 + l + sovGenesis(uint64(l)) + } + } return n } @@ -1340,6 +1362,40 @@ func (m *GenesisState) Unmarshal(dAtA []byte) error { return err } iNdEx = postIndex + case 15: + if wireType != 2 { + return fmt.Errorf("proto: wrong wireType = %d for field Claims", wireType) + } + var msglen int + for shift := uint(0); ; shift += 7 { + if shift >= 64 { + return ErrIntOverflowGenesis + } + if iNdEx >= l { + return io.ErrUnexpectedEOF + } + b := dAtA[iNdEx] + iNdEx++ + msglen |= int(b&0x7F) << shift + if b < 0x80 { + break + } + } + if msglen < 0 { + return ErrInvalidLengthGenesis + } + postIndex := iNdEx + msglen + if postIndex < 0 { + return ErrInvalidLengthGenesis + } + if postIndex > l { + return io.ErrUnexpectedEOF + } + m.Claims = append(m.Claims, Claim{}) + if err := m.Claims[len(m.Claims)-1].Unmarshal(dAtA[iNdEx:postIndex]); err != nil { + return err + } + iNdEx = postIndex default: iNdEx = preIndex skippy, err := skipGenesis(dAtA[iNdEx:]) diff --git a/x/incentive/types/genesis_test.go b/x/incentive/types/genesis_test.go index c3fa9f8f..5aaa52f8 100644 --- a/x/incentive/types/genesis_test.go +++ b/x/incentive/types/genesis_test.go @@ -68,16 +68,17 @@ func TestGenesisState_Validate(t *testing.T) { RewardIndexes: normalRewardIndexes, }}, }, - USDXMintingClaims: USDXMintingClaims{ + Claims: Claims{ { - BaseClaim: BaseClaim{ - Owner: sdk.AccAddress(crypto.AddressHash([]byte("KavaTestUser1"))), - Reward: sdk.NewCoin("ukava", sdk.NewInt(100000000)), - }, - RewardIndexes: []RewardIndex{ + Type: CLAIM_TYPE_USDX_MINTING, + Owner: sdk.AccAddress(crypto.AddressHash([]byte("KavaTestUser1"))), + Reward: cs(c("ukava", 100000000)), + RewardIndexes: []MultiRewardIndex{ { CollateralType: "bnb-a", - RewardFactor: sdk.ZeroDec(), + RewardIndexes: RewardIndexes{ + NewRewardIndex(USDXMintingRewardDenom, sdk.ZeroDec()), + }, }, }, }, @@ -101,7 +102,7 @@ func TestGenesisState_Validate(t *testing.T) { RewardIndexes: normalRewardIndexes, }}, }, - USDXMintingClaims: DefaultUSDXClaims, + Claims: DefaultClaims, }, errArgs: errArgs{ expectPass: false, @@ -113,16 +114,17 @@ func TestGenesisState_Validate(t *testing.T) { genesis: GenesisState{ Params: DefaultParams(), USDXRewardState: DefaultGenesisRewardState, - USDXMintingClaims: USDXMintingClaims{ + Claims: Claims{ { - BaseClaim: BaseClaim{ - Owner: nil, // invalid address - Reward: sdk.NewCoin("ukava", sdk.NewInt(100000000)), - }, - RewardIndexes: []RewardIndex{ + Type: CLAIM_TYPE_USDX_MINTING, + Owner: nil, // invalid address + Reward: cs(c("ukava", 100000000)), + RewardIndexes: []MultiRewardIndex{ { CollateralType: "bnb-a", - RewardFactor: sdk.ZeroDec(), + RewardIndexes: RewardIndexes{ + NewRewardIndex(USDXMintingRewardDenom, sdk.ZeroDec()), + }, }, }, }, diff --git a/x/incentive/types/keys.go b/x/incentive/types/keys.go index db921b3d..380cf17e 100644 --- a/x/incentive/types/keys.go +++ b/x/incentive/types/keys.go @@ -1,5 +1,7 @@ package types +import "encoding/binary" + const ( // ModuleName The name that will be used throughout the module ModuleName = "incentive" @@ -40,3 +42,42 @@ var ( EarnRewardIndexesKeyPrefix = []byte{0x19} // prefix for key that stores earn reward indexes PreviousEarnRewardAccrualTimeKeyPrefix = []byte{0x20} // prefix for key that stores the previous time earn rewards accrued ) + +var ( + ClaimKeyPrefix = []byte{0x21} + RewardIndexesKeyPrefix = []byte{0x22} + PreviousRewardAccrualTimeKeyPrefix = []byte{0x23} +) + +var sep = []byte("|") + +func createKey(bytes ...[]byte) (r []byte) { + for _, b := range bytes { + r = append(r, b...) + } + return +} + +func getKeyPrefix(dataTypePrefix []byte, claimType ClaimType) []byte { + b := make([]byte, 4) + binary.BigEndian.PutUint32(b, uint32(claimType)) + + return createKey(dataTypePrefix, sep, b) +} + +// GetClaimKeyPrefix returns the claim store key prefix for the given ClaimType. +func GetClaimKeyPrefix(claimType ClaimType) []byte { + return getKeyPrefix(ClaimKeyPrefix, claimType) +} + +// GetRewardIndexesKeyPrefix returns the reward indexes key prefix for the given +// ClaimType. +func GetRewardIndexesKeyPrefix(claimType ClaimType) []byte { + return getKeyPrefix(RewardIndexesKeyPrefix, claimType) +} + +// GetPreviousRewardAccrualTimeKeyPrefix returns the previous reward accrual time +// key prefix for the given ClaimType. +func GetPreviousRewardAccrualTimeKeyPrefix(claimType ClaimType) []byte { + return getKeyPrefix(PreviousRewardAccrualTimeKeyPrefix, claimType) +}