From 6cda1fa3ebfd09f3889b58816c243c2d6c33f961 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Henrik=20Aasted=20S=C3=B8rensen?= Date: Fri, 16 Oct 2020 15:14:35 +0200 Subject: [PATCH] Change genesis format of liquidity provider module --- x/liquidityprovider/genesis.go | 10 ++- x/liquidityprovider/genesis_test.go | 104 ++++++++++++++++++++++++++++ x/liquidityprovider/module.go | 10 ++- 3 files changed, 119 insertions(+), 5 deletions(-) create mode 100644 x/liquidityprovider/genesis_test.go diff --git a/x/liquidityprovider/genesis.go b/x/liquidityprovider/genesis.go index d8b1741c..b5810154 100644 --- a/x/liquidityprovider/genesis.go +++ b/x/liquidityprovider/genesis.go @@ -6,11 +6,15 @@ package liquidityprovider import ( sdk "github.com/cosmos/cosmos-sdk/types" - "github.com/e-money/em-ledger/x/liquidityprovider/types" ) +type GenesisAcc struct { + Account sdk.AccAddress `json:"address" yaml:"address"` + Mintable sdk.Coins `json:"mintable" yaml:"mintable"` +} + type genesisState struct { - Accounts []types.LiquidityProviderAccount `json:"accounts" yaml:"accounts"` + Accounts []GenesisAcc `json:"accounts" yaml:"accounts"` } func defaultGenesisState() genesisState { @@ -19,6 +23,6 @@ func defaultGenesisState() genesisState { func InitGenesis(ctx sdk.Context, keeper Keeper, gs genesisState) { for _, lp := range gs.Accounts { - keeper.CreateLiquidityProvider(ctx, lp.GetAddress(), lp.Mintable) + keeper.CreateLiquidityProvider(ctx, lp.Account, lp.Mintable) } } diff --git a/x/liquidityprovider/genesis_test.go b/x/liquidityprovider/genesis_test.go new file mode 100644 index 00000000..c8f8fd51 --- /dev/null +++ b/x/liquidityprovider/genesis_test.go @@ -0,0 +1,104 @@ +package liquidityprovider + +import ( + sdk "github.com/cosmos/cosmos-sdk/types" + "github.com/tidwall/gjson" + "testing" + + "github.com/stretchr/testify/require" + + apptypes "github.com/e-money/em-ledger/types" +) + +func init() { + apptypes.ConfigureSDK() +} + +func TestGenesisStruct1(t *testing.T) { + input := `{ + "accounts": [] + }` + + gs := genesisState{} + err := ModuleCdc.UnmarshalJSON([]byte(input), &gs) + require.NoError(t, err) + require.Empty(t, gs.Accounts) +} + +func TestGenesisStruct2(t *testing.T) { + input := `{ + "accounts": [ + { + "address" : "emoney16j4trwyg8a3pfwqu2ely96tkzl05eh4vvyyfts", + "mintable": [ + { + "denom": "eeur", + "amount": "50000000" + } + ] + }, + { + "address" : "emoney1cs4323dyzu0wxfj4vc62m8q3xsczfavqx9x3zd", + "mintable": [ + { + "denom": "echf", + "amount": "900" + }, + { + "denom": "esek", + "amount": "4100000" + } + ] + } + ] + }` + + gs := genesisState{} + err := ModuleCdc.UnmarshalJSON([]byte(input), &gs) + require.NoError(t, err) + require.Len(t, gs.Accounts, 2) + + require.Len(t, gs.Accounts[0].Mintable, 1) + require.Len(t, gs.Accounts[1].Mintable, 2) + + require.True(t, gs.Accounts[0].Mintable.IsValid()) + require.True(t, gs.Accounts[1].Mintable.IsValid()) +} + +func TestSerialize(t *testing.T) { + gs := genesisState{ + Accounts: []GenesisAcc{ + { + Account: sdk.AccAddress("account1"), + Mintable: sdk.Coins{ + sdk.Coin{ + Denom: "eeur", + Amount: sdk.NewInt(6000000), + }, + sdk.Coin{ + Denom: "echf", + Amount: sdk.NewInt(130000), + }, + }, + }, + { + Account: sdk.AccAddress("account2"), + Mintable: sdk.Coins{ + sdk.Coin{ + Denom: "esek", + Amount: sdk.NewInt(750000), + }, + }, + }, + }, + } + + json, err := ModuleCdc.MarshalJSON(gs) + require.NoError(t, err) + doc := gjson.ParseBytes(json) + + require.Len(t, doc.Get("accounts").Array(), 2) + + require.Len(t, doc.Get("accounts.0.mintable").Array(), 2) + require.Len(t, doc.Get("accounts.1.mintable").Array(), 1) +} diff --git a/x/liquidityprovider/module.go b/x/liquidityprovider/module.go index a04c6062..aa3ed789 100644 --- a/x/liquidityprovider/module.go +++ b/x/liquidityprovider/module.go @@ -130,8 +130,14 @@ func (am AppModule) InitGenesis(ctx sdk.Context, data json.RawMessage) (_ []abci // module export genesis func (am AppModule) ExportGenesis(ctx sdk.Context) json.RawMessage { allLPs := am.keeper.GetAllLiquidityProviderAccounts(ctx) - gs := genesisState{allLPs} - return ModuleCdc.MustMarshalJSON(gs) + + genAccs := make([]GenesisAcc, len(allLPs)) + for i, lp := range allLPs { + genAccs[i] = GenesisAcc{lp.GetAddress(), lp.Mintable} + } + + gs := genesisState{genAccs} + return ModuleCdc.MustMarshalJSON(&gs) } // module begin-block