Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(claim): implement CompleteMission #836

Merged
merged 24 commits into from
Jun 11, 2022
Merged
Show file tree
Hide file tree
Changes from 23 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
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() {
lumtis marked this conversation as resolved.
Show resolved Hide resolved
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