Skip to content

Commit

Permalink
Change genesis format of liquidity provider module
Browse files Browse the repository at this point in the history
  • Loading branch information
haasted committed Oct 16, 2020
1 parent 4860123 commit 6cda1fa
Show file tree
Hide file tree
Showing 3 changed files with 119 additions and 5 deletions.
10 changes: 7 additions & 3 deletions x/liquidityprovider/genesis.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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)
}
}
104 changes: 104 additions & 0 deletions x/liquidityprovider/genesis_test.go
Original file line number Diff line number Diff line change
@@ -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)
}
10 changes: 8 additions & 2 deletions x/liquidityprovider/module.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 6cda1fa

Please sign in to comment.