Skip to content

Commit

Permalink
feat(claim): implement CompleteMission (#836)
Browse files Browse the repository at this point in the history
* is mission completed

* initialize error

* add bank interface to claim

* IsMissionCompleted

* implement completeMission

* add account keeper

* refactor keeper tests

* initialize airdrop

* airdrop supply initial test

* initialize airdrop test

* pushback remove

* error case

* full tests

* goimport

* handle genesis init error

* test fix

* add issue link for TODO

* Apply suggestions from code review

Co-authored-by: Alex Johnson <alex.johnson@tendermint.com>

* Update x/claim/genesis_test.go

Co-authored-by: Danilo Pantani <danpantani@gmail.com>

* comments

* test

Co-authored-by: Alex Johnson <alex.johnson@tendermint.com>
Co-authored-by: Danilo Pantani <danpantani@gmail.com>
  • Loading branch information
3 people authored Jun 11, 2022
1 parent 44b136f commit f15ab08
Show file tree
Hide file tree
Showing 26 changed files with 632 additions and 176 deletions.
2 changes: 2 additions & 0 deletions app/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -204,6 +204,7 @@ var (
fundraisingtypes.ModuleName: nil,
monitoringcmoduletypes.ModuleName: nil,
monitoringpmoduletypes.ModuleName: nil,
claimmoduletypes.ModuleName: {authtypes.Minter, authtypes.Burner},
// this line is used by starport scaffolding # stargate/app/maccPerms
}
)
Expand Down Expand Up @@ -533,6 +534,7 @@ func New(
keys[claimmoduletypes.StoreKey],
keys[claimmoduletypes.MemStoreKey],
app.GetSubspace(claimmoduletypes.ModuleName),
app.AuthKeeper,
app.BankKeeper,
)

Expand Down
6 changes: 3 additions & 3 deletions config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -102,10 +102,10 @@ genesis:
requiredAllocations: "10"
benefits:
maxBidAmount: "30000"
# 1/3 of the default unbonding period (21 days): one week
# represents 1/3 of the default unbonding period (21 days): one week
registrationPeriod: "604800s"
# 2/3 of the default unbonding period (21 days): two weeks
# represents 2/3 of the default unbonding period (21 days): two weeks
withdrawalDelay: "1209600s"
client:
typescript:
path: "ignite-ui/src/generated"
path: "ignite-ui/src/generated"
7 changes: 7 additions & 0 deletions testutil/constructor/constructor.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,13 @@ func LastCommitInfo(votes ...Vote) abci.LastCommitInfo {
return lci
}

// Coin returns a sdk.Coin from a string
func Coin(t testing.TB, str string) sdk.Coin {
coin, err := sdk.ParseCoinNormalized(str)
require.NoError(t, err)
return coin
}

// Coins returns a sdk.Coins from a string
func Coins(t testing.TB, str string) sdk.Coins {
coins, err := sdk.ParseCoinsNormalized(str)
Expand Down
54 changes: 0 additions & 54 deletions testutil/keeper/claim.go

This file was deleted.

3 changes: 3 additions & 0 deletions testutil/keeper/initializer.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ var moduleAccountPerms = map[string][]string{
stakingtypes.NotBondedPoolName: {authtypes.Burner, authtypes.Staking},
rewardmoduletypes.ModuleName: {authtypes.Minter, authtypes.Burner},
fundraisingtypes.ModuleName: nil,
claimtypes.ModuleName: {authtypes.Minter, authtypes.Burner},
}

// initializer allows to initialize each module keeper
Expand Down Expand Up @@ -413,6 +414,7 @@ func (i initializer) Participation(

func (i initializer) Claim(
paramKeeper paramskeeper.Keeper,
accountKeeper authkeeper.AccountKeeper,
bankKeeper bankkeeper.Keeper,
) *claimkeeper.Keeper {
storeKey := sdk.NewKVStoreKey(claimtypes.StoreKey)
Expand All @@ -429,6 +431,7 @@ func (i initializer) Claim(
storeKey,
memStoreKey,
subspace,
accountKeeper,
bankKeeper,
)
}
8 changes: 6 additions & 2 deletions testutil/keeper/keeper.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"time"

sdk "github.com/cosmos/cosmos-sdk/types"
authkeeper "github.com/cosmos/cosmos-sdk/x/auth/keeper"
bankkeeper "github.com/cosmos/cosmos-sdk/x/bank/keeper"
distrtypes "github.com/cosmos/cosmos-sdk/x/distribution/types"
stakingkeeper "github.com/cosmos/cosmos-sdk/x/staking/keeper"
Expand Down Expand Up @@ -54,6 +55,7 @@ type TestKeepers struct {
RewardKeeper *rewardkeeper.Keeper
MonitoringConsumerKeeper *monitoringckeeper.Keeper
MonitoringProviderKeeper *monitoringpkeeper.Keeper
AccountKeeper authkeeper.AccountKeeper
BankKeeper bankkeeper.Keeper
IBCKeeper *ibckeeper.Keeper
StakingKeeper stakingkeeper.Keeper
Expand Down Expand Up @@ -101,7 +103,7 @@ func NewTestSetup(t testing.TB) (sdk.Context, TestKeepers, TestMsgServers) {
[]Channel{},
)
launchKeeper.SetMonitoringcKeeper(monitoringConsumerKeeper)
claimKeeper := initializer.Claim(paramKeeper, bankKeeper)
claimKeeper := initializer.Claim(paramKeeper, authKeeper, bankKeeper)
require.NoError(t, initializer.StateStore.LoadLatestVersion())

// Create a context using a custom timestamp
Expand Down Expand Up @@ -144,6 +146,7 @@ func NewTestSetup(t testing.TB) (sdk.Context, TestKeepers, TestMsgServers) {
ProfileKeeper: profileKeeper,
RewardKeeper: rewardKeeper,
MonitoringConsumerKeeper: monitoringConsumerKeeper,
AccountKeeper: authKeeper,
BankKeeper: bankKeeper,
IBCKeeper: ibcKeeper,
StakingKeeper: stakingKeeper,
Expand Down Expand Up @@ -193,7 +196,7 @@ func NewTestSetupWithIBCMocks(
channelMock,
)
launchKeeper.SetMonitoringcKeeper(monitoringConsumerKeeper)
claimKeeper := initializer.Claim(paramKeeper, bankKeeper)
claimKeeper := initializer.Claim(paramKeeper, authKeeper, bankKeeper)
require.NoError(t, initializer.StateStore.LoadLatestVersion())

// Create a context using a custom timestamp
Expand Down Expand Up @@ -234,6 +237,7 @@ func NewTestSetupWithIBCMocks(
ProfileKeeper: profileKeeper,
RewardKeeper: rewardKeeper,
MonitoringConsumerKeeper: monitoringConsumerKeeper,
AccountKeeper: authKeeper,
BankKeeper: bankKeeper,
IBCKeeper: ibcKeeper,
StakingKeeper: stakingKeeper,
Expand Down
4 changes: 3 additions & 1 deletion x/claim/genesis.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,9 @@ func InitGenesis(ctx sdk.Context, k keeper.Keeper, genState types.GenesisState)
k.SetMission(ctx, elem)
}

k.SetAirdropSupply(ctx, genState.AirdropSupply)
if err := k.InitializeAirdropSupply(ctx, genState.AirdropSupply); err != nil {
panic("airdrop supply failed to initialize: " + err.Error())
}

// this line is used by starport scaffolding # genesis/module/init
k.SetParams(ctx, genState.Params)
Expand Down
9 changes: 4 additions & 5 deletions x/claim/genesis_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,7 @@ import (
"testing"

"github.com/stretchr/testify/require"

keepertest "github.com/tendermint/spn/testutil/keeper"
testkeeper "github.com/tendermint/spn/testutil/keeper"
"github.com/tendermint/spn/testutil/nullify"
"github.com/tendermint/spn/testutil/sample"
"github.com/tendermint/spn/x/claim"
Expand Down Expand Up @@ -45,9 +44,9 @@ func TestGenesis(t *testing.T) {
// this line is used by starport scaffolding # genesis/test/state
}

k, ctx := keepertest.ClaimKeeper(t)
claim.InitGenesis(ctx, *k, genesisState)
got := claim.ExportGenesis(ctx, *k)
ctx, tk, _ := testkeeper.NewTestSetup(t)
claim.InitGenesis(ctx, *tk.ClaimKeeper, genesisState)
got := claim.ExportGenesis(ctx, *tk.ClaimKeeper)
require.NotNil(t, got)

nullify.Fill(&genesisState)
Expand Down
32 changes: 32 additions & 0 deletions x/claim/keeper/airdrop_supply.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"github.com/cosmos/cosmos-sdk/store/prefix"
sdk "github.com/cosmos/cosmos-sdk/types"

spnerrors "github.com/tendermint/spn/pkg/errors"
"github.com/tendermint/spn/x/claim/types"
)

Expand All @@ -26,3 +27,34 @@ func (k Keeper) GetAirdropSupply(ctx sdk.Context) (val sdk.Coin, found bool) {
k.cdc.MustUnmarshal(b, &val)
return val, true
}

// RemoveAirdropSupply removes the AirdropSupply from the store
func (k Keeper) RemoveAirdropSupply(ctx sdk.Context) {
store := prefix.NewStore(ctx.KVStore(k.storeKey), types.KeyPrefix(types.AirdropSupplyKey))
store.Delete([]byte{0})
}

// InitializeAirdropSupply set the airdrop supply in the store and set the module balance
func (k Keeper) InitializeAirdropSupply(ctx sdk.Context, airdropSupply sdk.Coin) error {
// get the eventual existing balance of the module for the airdrop supply
moduleBalance := k.bankKeeper.GetBalance(
ctx,
k.accountKeeper.GetModuleAddress(types.ModuleName),
airdropSupply.Denom,
)

// if the module has an existing balance, we burn the entire balance
if moduleBalance.IsPositive() {
if err := k.bankKeeper.BurnCoins(ctx, types.ModuleName, sdk.NewCoins(moduleBalance)); err != nil {
return spnerrors.Criticalf("can't burn module balance %s", err.Error())
}
}

// set the module balance with the airdrop supply
if err := k.bankKeeper.MintCoins(ctx, types.ModuleName, sdk.NewCoins(airdropSupply)); err != nil {
return spnerrors.Criticalf("can't mint airdrop suply into module balance %s", err.Error())
}

k.SetAirdropSupply(ctx, airdropSupply)
return nil
}
78 changes: 66 additions & 12 deletions x/claim/keeper/airdrop_supply_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,25 +6,79 @@ import (
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/stretchr/testify/require"

keepertest "github.com/tendermint/spn/testutil/keeper"
tc "github.com/tendermint/spn/testutil/constructor"
testkeeper "github.com/tendermint/spn/testutil/keeper"
"github.com/tendermint/spn/testutil/nullify"
"github.com/tendermint/spn/testutil/sample"
"github.com/tendermint/spn/x/claim/keeper"
claim "github.com/tendermint/spn/x/claim/types"
)

func createTestAirdropSupply(keeper *keeper.Keeper, ctx sdk.Context) sdk.Coin {
item := sample.Coin(r)
keeper.SetAirdropSupply(ctx, item)
return item
}

func TestAirdropSupplyGet(t *testing.T) {
k, ctx := keepertest.ClaimKeeper(t)
item := createTestAirdropSupply(k, ctx)
rst, found := k.GetAirdropSupply(ctx)
ctx, tk, _ := testkeeper.NewTestSetup(t)

sampleSupply := sample.Coin(r)
tk.ClaimKeeper.SetAirdropSupply(ctx, sampleSupply)

rst, found := tk.ClaimKeeper.GetAirdropSupply(ctx)
require.True(t, found)
require.Equal(t,
nullify.Fill(&item),
nullify.Fill(&sampleSupply),
nullify.Fill(&rst),
)
}

func TestAirdropSupplyRemove(t *testing.T) {
ctx, tk, _ := testkeeper.NewTestSetup(t)

tk.ClaimKeeper.SetAirdropSupply(ctx, sample.Coin(r))
_, found := tk.ClaimKeeper.GetAirdropSupply(ctx)
require.True(t, found)
tk.ClaimKeeper.RemoveAirdropSupply(ctx)
_, found = tk.ClaimKeeper.GetAirdropSupply(ctx)
require.False(t, found)
}

func TestKeeper_InitializeAirdropSupply(t *testing.T) {
// TODO: use mock for bank module to test critical errors
// https://github.com/tendermint/spn/issues/838
ctx, tk, _ := testkeeper.NewTestSetup(t)

tests := []struct {
name string
airdropSupply sdk.Coin
}{
{
name: "should allows setting airdrop supply",
airdropSupply: tc.Coin(t, "10000foo"),
},
{
name: "should allows specifying a new token for the supply",
airdropSupply: tc.Coin(t, "125000bar"),
},
{
name: "should allows modifying a token for the supply",
airdropSupply: tc.Coin(t, "525000bar"),
},
{
name: "should allows setting airdrop supply to zero",
airdropSupply: sdk.NewCoin("foo", sdk.ZeroInt()),
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
err := tk.ClaimKeeper.InitializeAirdropSupply(ctx, tt.airdropSupply)
require.NoError(t, err)

airdropSupply, found := tk.ClaimKeeper.GetAirdropSupply(ctx)
require.True(t, found)
require.True(t, airdropSupply.IsEqual(tt.airdropSupply))

moduleBalance := tk.BankKeeper.GetBalance(
ctx,
tk.AccountKeeper.GetModuleAddress(claim.ModuleName),
airdropSupply.Denom,
)
require.True(t, moduleBalance.IsEqual(tt.airdropSupply))
})
}
}
15 changes: 4 additions & 11 deletions x/claim/keeper/claim_record.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,15 +17,10 @@ func (k Keeper) SetClaimRecord(ctx sdk.Context, claimRecord types.ClaimRecord) {
}

// GetClaimRecord returns a claimRecord from its index
func (k Keeper) GetClaimRecord(
ctx sdk.Context,
index string,
) (val types.ClaimRecord, found bool) {
func (k Keeper) GetClaimRecord(ctx sdk.Context, address string) (val types.ClaimRecord, found bool) {
store := prefix.NewStore(ctx.KVStore(k.storeKey), types.KeyPrefix(types.ClaimRecordKeyPrefix))

b := store.Get(types.ClaimRecordKey(
index,
))
b := store.Get(types.ClaimRecordKey(address))
if b == nil {
return val, false
}
Expand All @@ -37,12 +32,10 @@ func (k Keeper) GetClaimRecord(
// RemoveClaimRecord removes a claimRecord from the store
func (k Keeper) RemoveClaimRecord(
ctx sdk.Context,
index string,
address string,
) {
store := prefix.NewStore(ctx.KVStore(k.storeKey), types.KeyPrefix(types.ClaimRecordKeyPrefix))
store.Delete(types.ClaimRecordKey(
index,
))
store.Delete(types.ClaimRecordKey(address))
}

// GetAllClaimRecord returns all claimRecord
Expand Down
Loading

0 comments on commit f15ab08

Please sign in to comment.