Skip to content

Commit

Permalink
Merge pull request #298 from scorpioborn/fix/genesis-reward-list
Browse files Browse the repository at this point in the history
Feature /  Genesis rewards list and validations
  • Loading branch information
3eyedraga authored Nov 15, 2023
2 parents 2047101 + b59163a commit e48347a
Show file tree
Hide file tree
Showing 7 changed files with 381 additions and 24 deletions.
4 changes: 4 additions & 0 deletions proto/sge/reward/genesis.proto
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,16 @@ package sgenetwork.sge.reward;
import "gogoproto/gogo.proto";
import "sge/reward/params.proto";
import "sge/reward/campaign.proto";
import "sge/reward/reward.proto";

option go_package = "github.com/sge-network/sge/x/reward/types";

// GenesisState defines the reward module's genesis state.
message GenesisState {
Params params = 1 [ (gogoproto.nullable) = false ];
repeated Campaign campaign_list = 2 [ (gogoproto.nullable) = false ];
repeated Reward reward_list = 3 [ (gogoproto.nullable) = false ];
repeated RewardByCategory reward_by_rec_cat_list = 4 [ (gogoproto.nullable) = false ];
repeated RewardByCampaign reward_by_campaign_list = 5 [ (gogoproto.nullable) = false ];
// Fixme: It would need another param for all the distributed rewards
}
12 changes: 12 additions & 0 deletions x/reward/genesis.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,15 @@ func InitGenesis(ctx sdk.Context, k keeper.Keeper, genState types.GenesisState)
for _, elem := range genState.CampaignList {
k.SetCampaign(ctx, elem)
}
for _, elem := range genState.RewardList {
k.SetReward(ctx, elem)
}
for _, elem := range genState.RewardByRecCatList {
k.SetRewardByReceiver(ctx, elem)
}
for _, elem := range genState.RewardByCampaignList {
k.SetRewardByCampaign(ctx, elem)
}
k.SetParams(ctx, genState.Params)
}

Expand All @@ -22,6 +31,9 @@ func ExportGenesis(ctx sdk.Context, k keeper.Keeper) *types.GenesisState {
genesis.Params = k.GetParams(ctx)

genesis.CampaignList = k.GetAllCampaign(ctx)
genesis.RewardList = k.GetAllRewards(ctx)
genesis.RewardByRecCatList = k.GetAllRewardsByReceiverAndCategory(ctx)
genesis.RewardByCampaignList = k.GetAllRewardsByCampaign(ctx)

return genesis
}
24 changes: 24 additions & 0 deletions x/reward/genesis_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,30 @@ func TestGenesis(t *testing.T) {
UID: uuid.NewString(),
},
},
RewardList: []types.Reward{
{
UID: uuid.NewString(),
},
{
UID: uuid.NewString(),
},
},
RewardByRecCatList: []types.RewardByCategory{
{
UID: uuid.NewString(),
},
{
UID: uuid.NewString(),
},
},
RewardByCampaignList: []types.RewardByCampaign{
{
UID: uuid.NewString(),
},
{
UID: uuid.NewString(),
},
},
}

tApp, ctx, err := simapp.GetTestObjects()
Expand Down
36 changes: 34 additions & 2 deletions x/reward/keeper/reward.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,8 @@ func (k Keeper) RemoveReward(
store.Delete(types.GetRewardKey(uid))
}

// GetAllReward returns all reward
func (k Keeper) GetAllReward(ctx sdk.Context) (list []types.Reward) {
// GetAllRewards returns all reward
func (k Keeper) GetAllRewards(ctx sdk.Context) (list []types.Reward) {
store := k.getRewardStore(ctx)
iterator := sdk.KVStorePrefixIterator(store, []byte{})

Expand Down Expand Up @@ -81,8 +81,40 @@ func (k Keeper) GetRewardsByAddressAndCategory(
return
}

// GetAllRewardsByReceiverAndCategory returns all rewards by receiver and category
func (k Keeper) GetAllRewardsByReceiverAndCategory(ctx sdk.Context) (list []types.RewardByCategory) {
store := k.getRewardByReceiverAndCategoryStore(ctx)
iterator := sdk.KVStorePrefixIterator(store, []byte{})

defer iterator.Close()

for ; iterator.Valid(); iterator.Next() {
var val types.RewardByCategory
k.cdc.MustUnmarshal(iterator.Value(), &val)
list = append(list, val)
}

return
}

func (k Keeper) SetRewardByCampaign(ctx sdk.Context, rewByCampaign types.RewardByCampaign) {
store := k.getRewardsByCampaignStore(ctx)
b := k.cdc.MustMarshal(&rewByCampaign)
store.Set(types.GetRewardsByCampaignKey(rewByCampaign.CampaignUID, rewByCampaign.UID), b)
}

// GetAllRewardsByCampaign returns all rewards by campaign
func (k Keeper) GetAllRewardsByCampaign(ctx sdk.Context) (list []types.RewardByCampaign) {
store := k.getRewardsByCampaignStore(ctx)
iterator := sdk.KVStorePrefixIterator(store, []byte{})

defer iterator.Close()

for ; iterator.Valid(); iterator.Next() {
var val types.RewardByCampaign
k.cdc.MustUnmarshal(iterator.Value(), &val)
list = append(list, val)
}

return
}
27 changes: 27 additions & 0 deletions x/reward/types/genesis.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,5 +29,32 @@ func (gs GenesisState) Validate() error {
campaignIndexMap[index] = struct{}{}
}

rewardIndexMap := make(map[string]struct{})
for _, elem := range gs.RewardList {
index := string(GetRewardKey(elem.UID))
if _, ok := rewardIndexMap[index]; ok {
return fmt.Errorf("duplicated index for reward")
}
rewardIndexMap[index] = struct{}{}
}

rewardByRecCatIndexMap := make(map[string]struct{})
for _, elem := range gs.RewardByRecCatList {
index := string(GetRewardKey(elem.UID))
if _, ok := rewardByRecCatIndexMap[index]; ok {
return fmt.Errorf("duplicated index for reward by receiver and category")
}
rewardByRecCatIndexMap[index] = struct{}{}
}

rewardByCampaignIndexMap := make(map[string]struct{})
for _, elem := range gs.RewardByCampaignList {
index := string(GetRewardKey(elem.UID))
if _, ok := rewardByCampaignIndexMap[index]; ok {
return fmt.Errorf("duplicated index for reward by campaign")
}
rewardByCampaignIndexMap[index] = struct{}{}
}

return gs.Params.Validate()
}
Loading

0 comments on commit e48347a

Please sign in to comment.