Skip to content

Commit

Permalink
feat(claim): add hooks for delegation and vote mission claim (#871)
Browse files Browse the repository at this point in the history
* add delegation hooks

* add staking hook

* add event mission completed

* implement vote hook

* register gov hook

* change gov deposit

* fix staking keeper
  • Loading branch information
lumtis authored Jun 15, 2022
1 parent 6bbb406 commit 493526b
Show file tree
Hide file tree
Showing 7 changed files with 488 additions and 12 deletions.
31 changes: 21 additions & 10 deletions app/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -394,17 +394,11 @@ func New(
app.FeeGrantKeeper = feegrantkeeper.NewKeeper(appCodec, keys[feegrant.StoreKey], app.AuthKeeper)
app.UpgradeKeeper = upgradekeeper.NewKeeper(skipUpgradeHeights, keys[upgradetypes.StoreKey], appCodec, homePath, app.BaseApp)

// register the staking hooks
// NOTE: stakingKeeper above is passed by reference, so that it will contain these hooks
app.StakingKeeper = *stakingKeeper.SetHooks(
stakingtypes.NewMultiStakingHooks(app.DistrKeeper.Hooks(), app.SlashingKeeper.Hooks()),
)

// ... other modules keepers

// Create IBC Keeper
app.IBCKeeper = ibckeeper.NewKeeper(
appCodec, keys[ibchost.StoreKey], app.GetSubspace(ibchost.ModuleName), app.StakingKeeper, app.UpgradeKeeper, scopedIBCKeeper,
appCodec, keys[ibchost.StoreKey], app.GetSubspace(ibchost.ModuleName), stakingKeeper, app.UpgradeKeeper, scopedIBCKeeper,
)

// register the proposal types
Expand All @@ -426,7 +420,7 @@ func New(

// Create evidence Keeper for to register the IBC light client misbehaviour evidence route
evidenceKeeper := evidencekeeper.NewKeeper(
appCodec, keys[evidencetypes.StoreKey], &app.StakingKeeper, app.SlashingKeeper,
appCodec, keys[evidencetypes.StoreKey], &stakingKeeper, app.SlashingKeeper,
)
// If evidence needs to be handled for the app, set routes in router here and seal
app.EvidenceKeeper = *evidenceKeeper
Expand Down Expand Up @@ -511,7 +505,7 @@ func New(
keys[monitoringpmoduletypes.StoreKey],
keys[monitoringpmoduletypes.MemStoreKey],
app.GetSubspace(monitoringpmoduletypes.ModuleName),
app.StakingKeeper,
stakingKeeper,
app.IBCKeeper.ClientKeeper,
app.IBCKeeper.ConnectionKeeper,
app.IBCKeeper.ChannelKeeper,
Expand All @@ -526,7 +520,7 @@ func New(
keys[participationmoduletypes.MemStoreKey],
app.GetSubspace(participationmoduletypes.ModuleName),
app.FundraisingKeeper,
app.StakingKeeper,
stakingKeeper,
)

app.ClaimKeeper = *claimmodulekeeper.NewKeeper(
Expand All @@ -540,6 +534,23 @@ func New(

// this line is used by starport scaffolding # stargate/app/keeperDefinition

// register the staking hooks
// NOTE: stakingKeeper above is passed by reference, so that it will contain these hooks
app.StakingKeeper = *stakingKeeper.SetHooks(
stakingtypes.NewMultiStakingHooks(
app.DistrKeeper.Hooks(),
app.SlashingKeeper.Hooks(),
app.ClaimKeeper.NewMissionDelegationHooks(1),
),
)

// register the gov hooks
app.GovKeeper = *app.GovKeeper.SetHooks(
govtypes.NewMultiGovHooks(
app.ClaimKeeper.NewMissionVoteHooks(2),
),
)

// Create static IBC router, add transfer route, then set and seal it
ibcRouter := ibcporttypes.NewRouter()
ibcRouter.AddRoute(ibctransfertypes.ModuleName, transferIBCModule)
Expand Down
2 changes: 1 addition & 1 deletion config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ genesis:
gov:
deposit_params:
min_deposit:
- "amount": "10000000"
- "amount": "1"
"denom": "uspn"
mint:
params:
Expand Down
9 changes: 9 additions & 0 deletions proto/claim/events.proto
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
syntax = "proto3";
package tendermint.spn.claim;

option go_package = "github.com/tendermint/spn/x/claim/types";

message EventMissionCompleted {
uint64 missionID = 1;
string claimer = 2;
}
7 changes: 6 additions & 1 deletion x/claim/keeper/mission.go
Original file line number Diff line number Diff line change
Expand Up @@ -112,5 +112,10 @@ func (k Keeper) CompleteMission(ctx sdk.Context, missionID uint64, address strin
k.SetAirdropSupply(ctx, airdropSupply)
k.SetClaimRecord(ctx, claimRecord)

return nil
err = ctx.EventManager().EmitTypedEvent(&types.EventMissionCompleted{
MissionID: missionID,
Claimer: address,
})

return err
}
60 changes: 60 additions & 0 deletions x/claim/keeper/mission_delegation_hooks.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
package keeper

import (
sdk "github.com/cosmos/cosmos-sdk/types"
stakingtypes "github.com/cosmos/cosmos-sdk/x/staking/types"
)

type MissionDelegationHooks struct {
k Keeper
missionID uint64
}

// NewMissionDelegationHooks returns a StakingHooks that triggers mission completion on delegation for an account
func (k Keeper) NewMissionDelegationHooks(missionID uint64) MissionDelegationHooks {
return MissionDelegationHooks{k, missionID}
}

var _ stakingtypes.StakingHooks = MissionDelegationHooks{}

// BeforeDelegationCreated completes mission when a delegation is performed
func (h MissionDelegationHooks) BeforeDelegationCreated(ctx sdk.Context, delAddr sdk.AccAddress, _ sdk.ValAddress) {
// TODO: error handling
_ = h.k.CompleteMission(ctx, h.missionID, delAddr.String())
}

// AfterValidatorCreated implements StakingHooks
func (h MissionDelegationHooks) AfterValidatorCreated(_ sdk.Context, _ sdk.ValAddress) {
}

// AfterValidatorRemoved implements StakingHooks
func (h MissionDelegationHooks) AfterValidatorRemoved(_ sdk.Context, _ sdk.ConsAddress, _ sdk.ValAddress) {
}

// BeforeDelegationSharesModified implements StakingHooks
func (h MissionDelegationHooks) BeforeDelegationSharesModified(_ sdk.Context, _ sdk.AccAddress, _ sdk.ValAddress) {
}

// AfterDelegationModified implements StakingHooks
func (h MissionDelegationHooks) AfterDelegationModified(_ sdk.Context, _ sdk.AccAddress, _ sdk.ValAddress) {
}

// BeforeValidatorSlashed implements StakingHooks
func (h MissionDelegationHooks) BeforeValidatorSlashed(_ sdk.Context, _ sdk.ValAddress, _ sdk.Dec) {
}

// BeforeValidatorModified implements StakingHooks
func (h MissionDelegationHooks) BeforeValidatorModified(_ sdk.Context, _ sdk.ValAddress) {
}

// AfterValidatorBonded implements StakingHooks
func (h MissionDelegationHooks) AfterValidatorBonded(_ sdk.Context, _ sdk.ConsAddress, _ sdk.ValAddress) {
}

// AfterValidatorBeginUnbonding implements StakingHooks
func (h MissionDelegationHooks) AfterValidatorBeginUnbonding(_ sdk.Context, _ sdk.ConsAddress, _ sdk.ValAddress) {
}

// BeforeDelegationRemoved implements StakingHooks
func (h MissionDelegationHooks) BeforeDelegationRemoved(_ sdk.Context, _ sdk.AccAddress, _ sdk.ValAddress) {
}
40 changes: 40 additions & 0 deletions x/claim/keeper/mission_vote_hooks.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package keeper

import (
sdk "github.com/cosmos/cosmos-sdk/types"
govtypes "github.com/cosmos/cosmos-sdk/x/gov/types"
)

type MissionVoteHooks struct {
k Keeper
missionID uint64
}

// NewMissionVoteHooks returns a GovHooks that triggers mission completion on voting for a proposal
func (k Keeper) NewMissionVoteHooks(missionID uint64) MissionVoteHooks {
return MissionVoteHooks{k, missionID}
}

var _ govtypes.GovHooks = MissionVoteHooks{}

// AfterProposalVote completes mission when a vote is cast
func (h MissionVoteHooks) AfterProposalVote(ctx sdk.Context, _ uint64, voterAddr sdk.AccAddress) {
// TODO: error handling
_ = h.k.CompleteMission(ctx, h.missionID, voterAddr.String())
}

// AfterProposalSubmission implements GovHooks
func (h MissionVoteHooks) AfterProposalSubmission(_ sdk.Context, _ uint64) {
}

// AfterProposalDeposit implements GovHooks
func (h MissionVoteHooks) AfterProposalDeposit(_ sdk.Context, _ uint64, _ sdk.AccAddress) {
}

// AfterProposalFailedMinDeposit implements GovHooks
func (h MissionVoteHooks) AfterProposalFailedMinDeposit(_ sdk.Context, _ uint64) {
}

// AfterProposalVotingPeriodEnded implements GovHooks
func (h MissionVoteHooks) AfterProposalVotingPeriodEnded(_ sdk.Context, _ uint64) {
}
Loading

0 comments on commit 493526b

Please sign in to comment.