Skip to content

Commit

Permalink
add query handlers
Browse files Browse the repository at this point in the history
  • Loading branch information
dev10 committed Aug 22, 2019
1 parent b754751 commit e303647
Show file tree
Hide file tree
Showing 5 changed files with 96 additions and 10 deletions.
28 changes: 28 additions & 0 deletions x/assetmanagement/alias.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,31 @@ const (
RouterKey = types.RouterKey
StoreKey = types.StoreKey
)

var (
// messages
NewMsgBurnCoins = types.NewMsgBurnCoins
NewMsgFreezeCoins = types.NewMsgFreezeCoins
NewMsgIssueToken = types.NewMsgIssueToken
NewMsgMintCoins = types.NewMsgMintCoins
NewMsgUnfreezeCoins = types.NewMsgUnfreezeCoins

ModuleCdc = types.ModuleCdc
RegisterCode = types.RegisterCodec
)

type (
// messages
MsgBurnCoins = types.MsgBurnCoins
MsgFreezeCoins = types.MsgFreezeCoins
MsgIssueToken = types.MsgIssueToken
MsgMintCoins = types.MsgMintCoins
MsgUnfreezeCoins = types.MsgUnfreezeCoins

// queries
QueryResultSymbol = types.QueryResultSymbol

// state/stored types
CustomCoinAccount = types.CustomAccount
Token = types.Token
)
4 changes: 2 additions & 2 deletions x/assetmanagement/keeper.go
Original file line number Diff line number Diff line change
Expand Up @@ -130,8 +130,8 @@ func (k Keeper) SetTotalSupply(ctx sdk.Context, symbol string, totalSupply sdk.C
return fmt.Errorf("failed to set total supply for symbol '%s' because: %s", symbol, err)
}

// GetNamesIterator - Get an iterator over all symbols in which the keys are the symbols and the values are the token
func (k Keeper) GetNamesIterator(ctx sdk.Context) sdk.Iterator {
// GetTokensIterator - Get an iterator over all symbols in which the keys are the symbols and the values are the token
func (k Keeper) GetTokensIterator(ctx sdk.Context) sdk.Iterator {
store := ctx.KVStore(k.storeKey)
return sdk.KVStorePrefixIterator(store, nil)
}
Expand Down
58 changes: 58 additions & 0 deletions x/assetmanagement/querier.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
package assetmanagement

import (
"github.com/cosmos/cosmos-sdk/codec"

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

// query endpoints supported by the assetmanagement Querier
const (
QuerySymbols = "symbols"
QueryToken = "token"
)

// NewQuerier is the module level router for state queries
func NewQuerier(keeper Keeper) sdk.Querier {
return func(ctx sdk.Context, path []string, req abci.RequestQuery) (res []byte, err sdk.Error) {
switch path[0] {
case QueryToken:
return queryToken(ctx, path[1:], req, keeper)
case QuerySymbols:
return querySymbols(ctx, req, keeper)
default:
return nil, sdk.ErrUnknownRequest("unknown assetmanagement query endpoint")
}
}
}

// nolint: unparam
func queryToken(ctx sdk.Context, path []string, req abci.RequestQuery, keeper Keeper) ([]byte, sdk.Error) {
token, err := keeper.GetToken(ctx, path[0])

res, err := codec.MarshalJSONIndent(keeper.cdc, token)
if err != nil {
panic("could not marshal result to JSON")
}

return res, nil
}

// nolint: unparam
func querySymbols(ctx sdk.Context, req abci.RequestQuery, keeper Keeper) ([]byte, sdk.Error) {
var symbolList QueryResultSymbol

iterator := keeper.GetTokensIterator(ctx)

for ; iterator.Valid(); iterator.Next() {
symbolList = append(symbolList, string(iterator.Key()))
}

res, err := codec.MarshalJSONIndent(keeper.cdc, symbolList)
if err != nil {
panic("could not marshal result to JSON")
}

return res, nil
}
4 changes: 4 additions & 0 deletions x/assetmanagement/types/errors.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
package types

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

const (
DefaultCodespace sdk.CodespaceType = ModuleName

Expand Down
12 changes: 4 additions & 8 deletions x/assetmanagement/types/querier.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,10 @@ package types

import "strings"

// QueryResultTokens is a payload for a tokens query
type QueryResultTokens []Token
// QueryResultSymbol is a payload for a symbols query
type QueryResultSymbol []string

// String implements fmt.Stringer
func (r QueryResultTokens) String() string {
array := make([]string, len(r))
for _, t := range r {
array = append(array, t.String())
}
return strings.Join(array, "\n")
func (r QueryResultSymbol) String() string {
return strings.Join(r[:], "\n")
}

0 comments on commit e303647

Please sign in to comment.