Skip to content

Commit

Permalink
Export coinswap pool (#220)
Browse files Browse the repository at this point in the history
* export genesis

* update query api

* fix bug

* can not issue lpt token

* fix test error

* fix test error
  • Loading branch information
Zhiqiang Zhang committed Aug 16, 2021
1 parent 713067d commit ce9657b
Show file tree
Hide file tree
Showing 16 changed files with 280 additions and 73 deletions.
12 changes: 6 additions & 6 deletions modules/coinswap/client/rest/grpc_query_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,7 @@ func (s *IntegrationTestSuite) TestCoinswap() {
s.Require().Equal("399985965", coins.AmountOf(sdk.DefaultBondDenom).String())
s.Require().Equal("1000", coins.AmountOf(lptDenom).String())

url := fmt.Sprintf("%s/coinswap/liquidities/%s", baseURL, symbol)
url := fmt.Sprintf("%s/coinswap/liquidities/%s", baseURL, lptDenom)
resp, err := rest.GetRequest(url)
s.Require().NoError(err)
s.Require().Equal("1000", gjson.Get(string(resp), "result.standard.amount").String())
Expand Down Expand Up @@ -235,7 +235,7 @@ func (s *IntegrationTestSuite) TestCoinswap() {
s.Require().Equal("399983955", coins.AmountOf(sdk.DefaultBondDenom).String())
s.Require().Equal("3000", coins.AmountOf(lptDenom).String())

url = fmt.Sprintf("%s/coinswap/liquidities/%s", baseURL, symbol)
url = fmt.Sprintf("%s/coinswap/liquidities/%s", baseURL, lptDenom)
resp, err = rest.GetRequest(url)
s.Require().NoError(err)
s.Require().Equal("3000", gjson.Get(string(resp), "result.standard.amount").String())
Expand Down Expand Up @@ -297,7 +297,7 @@ func (s *IntegrationTestSuite) TestCoinswap() {
s.Require().Equal("399984693", coins.AmountOf(sdk.DefaultBondDenom).String())
s.Require().Equal("3000", coins.AmountOf(lptDenom).String())

url = fmt.Sprintf("%s/coinswap/liquidities/%s", baseURL, symbol)
url = fmt.Sprintf("%s/coinswap/liquidities/%s", baseURL, lptDenom)
resp, err = rest.GetRequest(url)
s.Require().NoError(err)
s.Require().Equal("2252", gjson.Get(string(resp), "result.standard.amount").String())
Expand Down Expand Up @@ -359,7 +359,7 @@ func (s *IntegrationTestSuite) TestCoinswap() {
s.Require().Equal("399983930", coins.AmountOf(sdk.DefaultBondDenom).String())
s.Require().Equal("3000", coins.AmountOf(lptDenom).String())

url = fmt.Sprintf("%s/coinswap/liquidities/%s", baseURL, symbol)
url = fmt.Sprintf("%s/coinswap/liquidities/%s", baseURL, lptDenom)
resp, err = rest.GetRequest(url)
s.Require().NoError(err)
s.Require().Equal("3005", gjson.Get(string(resp), "result.standard.amount").String())
Expand Down Expand Up @@ -416,7 +416,7 @@ func (s *IntegrationTestSuite) TestCoinswap() {
s.Require().Equal("399985923", coins.AmountOf(sdk.DefaultBondDenom).String())
s.Require().Equal("1000", coins.AmountOf(lptDenom).String())

url = fmt.Sprintf("%s/coinswap/liquidities/%s", baseURL, symbol)
url = fmt.Sprintf("%s/coinswap/liquidities/%s", baseURL, lptDenom)
resp, err = rest.GetRequest(url)
s.Require().NoError(err)
s.Require().Equal("1002", gjson.Get(string(resp), "result.standard.amount").String())
Expand Down Expand Up @@ -473,7 +473,7 @@ func (s *IntegrationTestSuite) TestCoinswap() {
s.Require().Equal("399986915", coins.AmountOf(sdk.DefaultBondDenom).String())
s.Require().Equal("0", coins.AmountOf(lptDenom).String())

url = fmt.Sprintf("%s/coinswap/liquidities/%s", baseURL, symbol)
url = fmt.Sprintf("%s/coinswap/liquidities/%s", baseURL, lptDenom)
resp, err = rest.GetRequest(url)
s.Require().NoError(err)
s.Require().Equal("0", gjson.Get(string(resp), "result.standard.amount").String())
Expand Down
24 changes: 0 additions & 24 deletions modules/coinswap/genesis.go

This file was deleted.

31 changes: 31 additions & 0 deletions modules/coinswap/keeper/genesis.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package keeper

import (
"fmt"

sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/irisnet/irismod/modules/coinswap/types"
)

// InitGenesis initializes the coinswap module's state from a given genesis state.
func (k Keeper) InitGenesis(ctx sdk.Context, genState types.GenesisState) {
if err := types.ValidateGenesis(genState); err != nil {
panic(fmt.Errorf("panic for ValidateGenesis,%v", err))
}
k.SetParams(ctx, genState.Params)
k.SetStandardDenom(ctx, genState.StandardDenom)
k.setSequence(ctx, genState.Sequence)
for _, pool := range genState.Pool {
k.setPool(ctx, &pool)
}
}

// ExportGenesis returns the coinswap module's genesis state.
func (k Keeper) ExportGenesis(ctx sdk.Context) types.GenesisState {
return types.GenesisState{
Params: k.GetParams(ctx),
StandardDenom: k.GetStandardDenom(ctx),
Pool: k.GetAllPools(ctx),
Sequence: k.getSequence(ctx),
}
}
34 changes: 34 additions & 0 deletions modules/coinswap/keeper/genesis_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package keeper_test

import (
"testing"

sdk "github.com/cosmos/cosmos-sdk/types"

"github.com/irisnet/irismod/modules/coinswap/types"
"github.com/stretchr/testify/suite"
)

func TestGenesisSuite(t *testing.T) {
suite.Run(t, new(TestSuite))
}

func (suite *TestSuite) TestInitGenesisAndExportGenesis() {
expGenesis := types.GenesisState{
Params: types.Params{
Fee: sdk.NewDecWithPrec(4, 3),
},
StandardDenom: denomStandard,
Pool: []types.Pool{{
Id: types.GetPoolId(denomETH),
StandardDenom: denomStandard,
CounterpartyDenom: denomETH,
EscrowAddress: types.GetReservePoolAddr("lpt-1").String(),
LptDenom: "lpt-1",
}},
Sequence: 2,
}
suite.app.CoinswapKeeper.InitGenesis(suite.ctx, expGenesis)
actGenesis := suite.app.CoinswapKeeper.ExportGenesis(suite.ctx)
suite.Require().Equal(expGenesis, actGenesis)
}
3 changes: 1 addition & 2 deletions modules/coinswap/keeper/grpc_query.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,7 @@ func (k Keeper) Liquidity(c context.Context, req *types.QueryLiquidityRequest) (
}

ctx := sdk.UnwrapSDKContext(c)
poolId := types.GetPoolId(req.Denom)
pool, exists := k.GetPool(ctx, poolId)
pool, exists := k.GetPoolByLptDenom(ctx, req.Denom)
if !exists {
return nil, sdkerrors.Wrapf(types.ErrReservePoolNotExists, "liquidity pool token: %s", req.Denom)
}
Expand Down
27 changes: 20 additions & 7 deletions modules/coinswap/keeper/pool.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import (

// CreatePool create a liquidity that saves relevant information about popular pool tokens
func (k Keeper) CreatePool(ctx sdk.Context, counterpartyDenom string) types.Pool {
sequence := k.getPoolSequence(ctx)
sequence := k.getSequence(ctx)
lptDenom := types.GetLptDenom(sequence)
pool := &types.Pool{
Id: types.GetPoolId(counterpartyDenom),
Expand All @@ -20,7 +20,7 @@ func (k Keeper) CreatePool(ctx sdk.Context, counterpartyDenom string) types.Pool
EscrowAddress: types.GetReservePoolAddr(lptDenom).String(),
LptDenom: lptDenom,
}
k.increasePoolSequence(ctx, sequence)
k.setSequence(ctx, sequence+1)
k.setPool(ctx, pool)
return *pool
}
Expand All @@ -38,6 +38,19 @@ func (k Keeper) GetPool(ctx sdk.Context, poolId string) (types.Pool, bool) {
return *pool, true
}

// GetAllPools return all the liquidity pools
func (k Keeper) GetAllPools(ctx sdk.Context) (pools []types.Pool) {
store := ctx.KVStore(k.storeKey)
iterator := sdk.KVStorePrefixIterator(store, []byte(types.KeyPool))
defer iterator.Close()
for ; iterator.Valid(); iterator.Next() {
var pool types.Pool
k.cdc.MustUnmarshalBinaryBare(iterator.Value(), &pool)
pools = append(pools, pool)
}
return
}

// GetPoolByLptDenom return the liquidity pool by the specified anotherCoinDenom
func (k Keeper) GetPoolByLptDenom(ctx sdk.Context, lptDenom string) (types.Pool, bool) {
store := ctx.KVStore(k.storeKey)
Expand Down Expand Up @@ -125,8 +138,8 @@ func (k Keeper) setPool(ctx sdk.Context, pool *types.Pool) {
store.Set(types.GetLptDenomKey(pool.LptDenom), poolIdBz)
}

// getPoolSequence gets the next pool sequence from the store.
func (k Keeper) getPoolSequence(ctx sdk.Context) uint64 {
// getSequence gets the next pool sequence from the store.
func (k Keeper) getSequence(ctx sdk.Context) uint64 {
store := ctx.KVStore(k.storeKey)
bz := store.Get([]byte(types.KeyNextPoolSequence))
if bz == nil {
Expand All @@ -135,9 +148,9 @@ func (k Keeper) getPoolSequence(ctx sdk.Context) uint64 {
return sdk.BigEndianToUint64(bz)
}

// increasePoolSequence sets the next pool sequence to the store.
func (k Keeper) increasePoolSequence(ctx sdk.Context, sequence uint64) {
// setSequence sets the next pool sequence to the store.
func (k Keeper) setSequence(ctx sdk.Context, sequence uint64) {
store := ctx.KVStore(k.storeKey)
bz := sdk.Uint64ToBigEndian(sequence + 1)
bz := sdk.Uint64ToBigEndian(sequence)
store.Set([]byte(types.KeyNextPoolSequence), bz)
}
3 changes: 1 addition & 2 deletions modules/coinswap/keeper/querier.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,7 @@ func queryLiquidity(ctx sdk.Context, req abci.RequestQuery, k Keeper, legacyQuer
return nil, sdkerrors.Wrap(sdkerrors.ErrJSONUnmarshal, err.Error())
}

poolId := types.GetPoolId(params.Denom)
pool, exists := k.GetPool(ctx, poolId)
pool, exists := k.GetPoolByLptDenom(ctx, params.Denom)
if !exists {
return nil, sdkerrors.Wrapf(types.ErrReservePoolNotExists, "liquidity pool token: %s", params.Denom)
}
Expand Down
2 changes: 1 addition & 1 deletion modules/coinswap/keeper/querier_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ func (suite *TestSuite) TestNewQuerier() {

// test queryLiquidity

bz, errRes := legacyAmino.MarshalJSON(types.QueryLiquidityParams{Denom: denomBTC})
bz, errRes := legacyAmino.MarshalJSON(types.QueryLiquidityParams{Denom: lptCoin.Denom})
suite.NoError(errRes)

req.Path = fmt.Sprintf("custom/%s/%s", types.QuerierRoute, types.QueryLiquidity)
Expand Down
8 changes: 3 additions & 5 deletions modules/coinswap/module.go
Original file line number Diff line number Diff line change
Expand Up @@ -134,17 +134,15 @@ func (am AppModule) LegacyQuerierHandler(legacyQuerierCdc *codec.LegacyAmino) sd
// no validator updates.
func (am AppModule) InitGenesis(ctx sdk.Context, cdc codec.JSONMarshaler, data json.RawMessage) []abci.ValidatorUpdate {
var genesisState types.GenesisState

cdc.MustUnmarshalJSON(data, &genesisState)

InitGenesis(ctx, am.keeper, genesisState)
am.keeper.InitGenesis(ctx, genesisState)
return []abci.ValidatorUpdate{}
}

// ExportGenesis returns the exported genesis state as raw bytes for the coinswap module.
func (am AppModule) ExportGenesis(ctx sdk.Context, cdc codec.JSONMarshaler) json.RawMessage {
gs := ExportGenesis(ctx, am.keeper)
return cdc.MustMarshalJSON(gs)
gs := am.keeper.ExportGenesis(ctx)
return cdc.MustMarshalJSON(&gs)
}

// BeginBlock performs a no-op.
Expand Down
50 changes: 49 additions & 1 deletion modules/coinswap/types/genesis.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package types

import (
"fmt"

sdk "github.com/cosmos/cosmos-sdk/types"
)

Expand All @@ -14,13 +16,59 @@ func NewGenesisState(params Params, denom string) *GenesisState {

// DefaultGenesisState creates a default GenesisState object
func DefaultGenesisState() *GenesisState {
return NewGenesisState(DefaultParams(), sdk.DefaultBondDenom)
return &GenesisState{
Params: DefaultParams(),
StandardDenom: sdk.DefaultBondDenom,
Sequence: 1,
}
}

// ValidateGenesis validates the given genesis state
func ValidateGenesis(data GenesisState) error {
if err := sdk.ValidateDenom(data.StandardDenom); err != nil {
return err
}

var poolIds = make(map[string]bool, len(data.Pool))
var lptDenoms = make(map[string]bool, len(data.Pool))
var maxSequence = uint64(0)
for _, pool := range data.Pool {
if poolIds[pool.Id] {
return fmt.Errorf("duplicate pool: %s", pool.Id)
}
if lptDenoms[pool.LptDenom] {
return fmt.Errorf("duplicate lptDenom: %s", pool.LptDenom)
}
poolIds[pool.Id] = true
lptDenoms[pool.LptDenom] = true

//validate the liquidity pool token denom
seq, err := ParseLptDenom(pool.LptDenom)
if err != nil {
return err
}

if seq > maxSequence {
maxSequence = seq
}

//validate the token denom
if err := sdk.ValidateDenom(pool.CounterpartyDenom); err != nil {
return err
}

//validate the token denom
if err := sdk.ValidateDenom(pool.StandardDenom); err != nil {
return err
}

//validate the address
if _, err := sdk.AccAddressFromBech32(pool.EscrowAddress); err != nil {
return err
}
}
if maxSequence+1 != data.Sequence {
return fmt.Errorf("invalid sequence: %d", data.Sequence)
}
return data.Params.Validate()
}
Loading

0 comments on commit ce9657b

Please sign in to comment.