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

New Bonds Info List Query #225

Merged
merged 4 commits into from
Feb 22, 2021
Merged
Show file tree
Hide file tree
Changes from all 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
4 changes: 2 additions & 2 deletions scripts/run_only.sh
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@

# Uncomment the below to broadcast REST endpoint
# Do not forget to comment the bottom lines !!
#ixod start --pruning "everything" &
#ixod start --pruning "nothing" &
#ixocli rest-server --chain-id pandora-1 --laddr="tcp://0.0.0.0:1317" --trust-node && fg

ixod start --pruning "everything" &
ixod start --pruning "nothing" &
ixocli rest-server --chain-id pandora-1 --trust-node && fg
4 changes: 2 additions & 2 deletions scripts/run_with_all_data.sh
Original file line number Diff line number Diff line change
Expand Up @@ -86,8 +86,8 @@ ixod validate-genesis

# Uncomment the below to broadcast REST endpoint
# Do not forget to comment the bottom lines !!
#ixod start --pruning "everything" &
#ixod start --pruning "nothing" &
#ixocli rest-server --chain-id pandora-1 --laddr="tcp://0.0.0.0:1317" --trust-node && fg

ixod start --pruning "everything" &
ixod start --pruning "nothing" &
ixocli rest-server --chain-id pandora-1 --trust-node && fg
4 changes: 2 additions & 2 deletions scripts/run_with_all_data_and_genesis_file.sh
Original file line number Diff line number Diff line change
Expand Up @@ -92,8 +92,8 @@ ixod validate-genesis

# Uncomment the below to broadcast REST endpoint
# Do not forget to comment the bottom lines !!
#ixod start --pruning "everything" &
#ixod start --pruning "nothing" &
#ixocli rest-server --chain-id pandora-1 --laddr="tcp://0.0.0.0:1317" --trust-node && fg

ixod start --pruning "everything" &
ixod start --pruning "nothing" &
ixocli rest-server --chain-id pandora-1 --trust-node && fg
2 changes: 1 addition & 1 deletion scripts/run_with_all_data_dev.sh
Original file line number Diff line number Diff line change
Expand Up @@ -83,5 +83,5 @@ FROM="laddr = \"tcp:\/\/127.0.0.1:26657\""
TO="laddr = \"tcp:\/\/0.0.0.0:26657\""
sed -i "s/$FROM/$TO/" "$HOME"/.ixod/config/config.toml

ixod start --pruning "everything" &
ixod start --pruning "nothing" &
ixocli rest-server --chain-id pandora-1 --laddr="tcp://0.0.0.0:1317" --trust-node && fg
2 changes: 1 addition & 1 deletion scripts/run_with_export.sh
Original file line number Diff line number Diff line change
Expand Up @@ -21,5 +21,5 @@ ixod validate-genesis
#TO="laddr = \"tcp:\/\/0.0.0.0:26657\""
#sed -i "s/$FROM/$TO/" "$HOME"/.ixod/config/config.toml

ixod start --pruning "everything" &
ixod start --pruning "nothing" &
ixocli rest-server --chain-id pandora-1 --trust-node && fg
2 changes: 1 addition & 1 deletion scripts/run_with_genesis_file.sh
Original file line number Diff line number Diff line change
Expand Up @@ -14,5 +14,5 @@ ixod validate-genesis
#TO="laddr = \"tcp:\/\/0.0.0.0:26657\""
#sed -i "s/$FROM/$TO/" "$HOME"/.ixod/config/config.toml

ixod start --pruning "everything" &
ixod start --pruning "nothing" &
ixocli rest-server --chain-id pandora-1 --trust-node && fg
4 changes: 2 additions & 2 deletions scripts/run_with_some_data.sh
Original file line number Diff line number Diff line change
Expand Up @@ -61,8 +61,8 @@ ixod validate-genesis

# Uncomment the below to broadcast REST endpoint
# Do not forget to comment the bottom lines !!
#ixod start --pruning "everything" &
#ixod start --pruning "nothing" &
#ixocli rest-server --chain-id pandora-1 --laddr="tcp://0.0.0.0:1317" --trust-node && fg

ixod start --pruning "everything" &
ixod start --pruning "nothing" &
ixocli rest-server --chain-id pandora-1 --trust-node && fg
28 changes: 26 additions & 2 deletions x/bonds/client/cli/query.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,8 @@ func GetQueryCmd(storeKey string, cdc *codec.Codec) *cobra.Command {
}

bondsQueryCmd.AddCommand(flags.GetCommands(
GetCmdBonds(storeKey, cdc),
GetCmdBondsList(storeKey, cdc),
GetCmdBondsListDetailed(storeKey, cdc),
GetCmdBond(storeKey, cdc),
GetCmdBatch(storeKey, cdc),
GetCmdLastBatch(storeKey, cdc),
Expand All @@ -40,7 +41,7 @@ func GetQueryCmd(storeKey string, cdc *codec.Codec) *cobra.Command {
return bondsQueryCmd
}

func GetCmdBonds(queryRoute string, cdc *codec.Codec) *cobra.Command {
func GetCmdBondsList(queryRoute string, cdc *codec.Codec) *cobra.Command {
return &cobra.Command{
Use: "bonds-list",
Short: "List of all bonds",
Expand All @@ -63,6 +64,29 @@ func GetCmdBonds(queryRoute string, cdc *codec.Codec) *cobra.Command {
}
}

func GetCmdBondsListDetailed(queryRoute string, cdc *codec.Codec) *cobra.Command {
return &cobra.Command{
Use: "bonds-list-detailed",
Short: "List of all bonds with information about current state",
Args: cobra.ExactArgs(0),
RunE: func(cmd *cobra.Command, args []string) error {
cliCtx := context.NewCLIContext().WithCodec(cdc)

res, _, err := cliCtx.QueryWithData(
fmt.Sprintf("custom/%s/%s", queryRoute,
keeper.QueryBondsDetailed), nil)
if err != nil {
fmt.Printf("%s", err.Error())
return nil
}

var out types.QueryBondsDetailed
cdc.MustUnmarshalJSON(res, &out)
return cliCtx.PrintOutput(out)
},
}
}

func GetCmdBond(queryRoute string, cdc *codec.Codec) *cobra.Command {
return &cobra.Command{
Use: "bond [bond-did]",
Expand Down
69 changes: 48 additions & 21 deletions x/bonds/client/rest/query.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,11 @@ func registerQueryRoutes(cliCtx context.CLIContext, r *mux.Router, queryRoute st
"/bonds", queryBondsHandler(cliCtx, queryRoute),
).Methods("GET")

r.HandleFunc(
"/bonds_detailed",
queryBondsDetailedHandler(cliCtx, queryRoute),
).Methods("GET")

r.HandleFunc(
fmt.Sprintf("/bonds/{%s}", RestBondDid),
queryBondHandler(cliCtx, queryRoute),
Expand Down Expand Up @@ -75,7 +80,8 @@ func registerQueryRoutes(cliCtx context.CLIContext, r *mux.Router, queryRoute st
func queryBondsHandler(cliCtx context.CLIContext, queryRoute string) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
res, _, err := cliCtx.QueryWithData(
fmt.Sprintf("custom/%s/bonds", queryRoute), nil)
fmt.Sprintf("custom/%s/%s",
queryRoute, keeper.QueryBonds), nil)
if err != nil {
rest.WriteErrorResponse(w, http.StatusNotFound, err.Error())
return
Expand All @@ -84,14 +90,35 @@ func queryBondsHandler(cliCtx context.CLIContext, queryRoute string) http.Handle
}
}

func queryBondsDetailedHandler(cliCtx context.CLIContext, queryRoute string) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
cliCtx, ok := rest.ParseQueryHeightOrReturnBadRequest(w, cliCtx, r)
if !ok {
return
}

res, height, err := cliCtx.QueryWithData(
fmt.Sprintf("custom/%s/%s",
queryRoute, keeper.QueryBondsDetailed), nil)
if err != nil {
rest.WriteErrorResponse(w, http.StatusNotFound, err.Error())
return
}

cliCtx = cliCtx.WithHeight(height)

rest.PostProcessResponse(w, cliCtx, res)
}
}

func queryBondHandler(cliCtx context.CLIContext, queryRoute string) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
vars := mux.Vars(r)
bondDid := vars[RestBondDid]

res, _, err := cliCtx.QueryWithData(
fmt.Sprintf("custom/%s/bond/%s",
queryRoute, bondDid), nil)
fmt.Sprintf("custom/%s/%s/%s",
queryRoute, keeper.QueryBond, bondDid), nil)
if err != nil {
rest.WriteErrorResponse(w, http.StatusNotFound, err.Error())
return
Expand All @@ -107,8 +134,8 @@ func queryBatchHandler(cliCtx context.CLIContext, queryRoute string) http.Handle
bondDid := vars[RestBondDid]

res, _, err := cliCtx.QueryWithData(
fmt.Sprintf("custom/%s/batch/%s",
queryRoute, bondDid), nil)
fmt.Sprintf("custom/%s/%s/%s",
queryRoute, keeper.QueryBatch, bondDid), nil)
if err != nil {
rest.WriteErrorResponse(w, http.StatusNotFound, err.Error())
return
Expand All @@ -124,8 +151,8 @@ func queryLastBatchHandler(cliCtx context.CLIContext, queryRoute string) http.Ha
bondDid := vars[RestBondDid]

res, _, err := cliCtx.QueryWithData(
fmt.Sprintf("custom/%s/last_batch/%s",
queryRoute, bondDid), nil)
fmt.Sprintf("custom/%s/%s/%s",
queryRoute, keeper.QueryLastBatch, bondDid), nil)
if err != nil {
rest.WriteErrorResponse(w, http.StatusNotFound, err.Error())
return
Expand All @@ -141,8 +168,8 @@ func queryCurrentPriceHandler(cliCtx context.CLIContext, queryRoute string) http
bondDid := vars[RestBondDid]

res, _, err := cliCtx.QueryWithData(
fmt.Sprintf("custom/%s/current_price/%s",
queryRoute, bondDid), nil)
fmt.Sprintf("custom/%s/%s/%s",
queryRoute, keeper.QueryCurrentPrice, bondDid), nil)
if err != nil {
rest.WriteErrorResponse(w, http.StatusNotFound, err.Error())
return
Expand All @@ -158,8 +185,8 @@ func queryCurrentReserveHandler(cliCtx context.CLIContext, queryRoute string) ht
bondDid := vars[RestBondDid]

res, _, err := cliCtx.QueryWithData(
fmt.Sprintf("custom/%s/current_reserve/%s",
queryRoute, bondDid), nil)
fmt.Sprintf("custom/%s/%s/%s",
queryRoute, keeper.QueryCurrentReserve, bondDid), nil)
if err != nil {
rest.WriteErrorResponse(w, http.StatusNotFound, err.Error())
return
Expand All @@ -176,8 +203,8 @@ func queryCustomPriceHandler(cliCtx context.CLIContext, queryRoute string) http.
bondAmount := vars[RestBondAmount]

res, _, err := cliCtx.QueryWithData(
fmt.Sprintf("custom/%s/custom_price/%s/%s",
queryRoute, bondDid, bondAmount), nil)
fmt.Sprintf("custom/%s/%s/%s/%s",
queryRoute, keeper.QueryCustomPrice, bondDid, bondAmount), nil)
if err != nil {
rest.WriteErrorResponse(w, http.StatusNotFound, err.Error())
return
Expand All @@ -194,8 +221,8 @@ func queryBuyPriceHandler(cliCtx context.CLIContext, queryRoute string) http.Han
bondAmount := vars[RestBondAmount]

res, _, err := cliCtx.QueryWithData(
fmt.Sprintf("custom/%s/buy_price/%s/%s",
queryRoute, bondDid, bondAmount), nil)
fmt.Sprintf("custom/%s/%s/%s/%s",
queryRoute, keeper.QueryBuyPrice, bondDid, bondAmount), nil)
if err != nil {
rest.WriteErrorResponse(w, http.StatusNotFound, err.Error())
return
Expand All @@ -212,8 +239,8 @@ func querySellReturnHandler(cliCtx context.CLIContext, queryRoute string) http.H
bondAmount := vars[RestBondAmount]

res, _, err := cliCtx.QueryWithData(
fmt.Sprintf("custom/%s/sell_return/%s/%s",
queryRoute, bondDid, bondAmount), nil)
fmt.Sprintf("custom/%s/%s/%s/%s",
queryRoute, keeper.QuerySellReturn, bondDid, bondAmount), nil)
if err != nil {
rest.WriteErrorResponse(w, http.StatusNotFound, err.Error())
return
Expand All @@ -237,8 +264,8 @@ func querySwapReturnHandler(cliCtx context.CLIContext, queryRoute string) http.H
}

res, _, err := cliCtx.QueryWithData(
fmt.Sprintf("custom/%s/swap_return/%s/%s/%s/%s",
queryRoute, bondDid, reserveCoinWithAmount.Denom,
fmt.Sprintf("custom/%s/%s/%s/%s/%s/%s",
queryRoute, keeper.QuerySwapReturn, bondDid, reserveCoinWithAmount.Denom,
reserveCoinWithAmount.Amount.String(), toToken), nil)
if err != nil {
rest.WriteErrorResponse(w, http.StatusNotFound, err.Error())
Expand All @@ -255,8 +282,8 @@ func queryAlphaMaximumsHandler(cliCtx context.CLIContext, queryRoute string) htt
bondDid := vars[RestBondDid]

res, _, err := cliCtx.QueryWithData(
fmt.Sprintf("custom/%s/alpha_maximums/%s",
queryRoute, bondDid), nil)
fmt.Sprintf("custom/%s/%s/%s",
queryRoute, keeper.QueryAlphaMaximums, bondDid), nil)
if err != nil {
rest.WriteErrorResponse(w, http.StatusNotFound, err.Error())
return
Expand Down
30 changes: 30 additions & 0 deletions x/bonds/internal/keeper/querier.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (

const (
QueryBonds = "bonds"
QueryBondsDetailed = "bonds_detailed"
QueryBond = "bond"
QueryBatch = "batch"
QueryLastBatch = "last_batch"
Expand All @@ -30,6 +31,8 @@ func NewQuerier(keeper Keeper) sdk.Querier {
switch path[0] {
case QueryBonds:
return queryBonds(ctx, keeper)
case QueryBondsDetailed:
return queryBondsDetailed(ctx, keeper)
case QueryBond:
return queryBond(ctx, path[1:], keeper)
case QueryBatch:
Expand Down Expand Up @@ -97,6 +100,33 @@ func queryBonds(ctx sdk.Context, keeper Keeper) (res []byte, err error) {
return bz, nil
}

func queryBondsDetailed(ctx sdk.Context, keeper Keeper) (res []byte, err error) {
var bondsList types.QueryBondsDetailed
iterator := keeper.GetBondIterator(ctx)
for ; iterator.Valid(); iterator.Next() {
var bond types.Bond
keeper.cdc.MustUnmarshalBinaryBare(iterator.Value(), &bond)

reserveBalances := keeper.GetReserveBalances(ctx, bond.BondDid)
reservePrices, _ := bond.GetCurrentPricesPT(reserveBalances)
reservePrices = zeroReserveTokensIfEmptyDec(reservePrices, bond)

bondsList = append(bondsList, types.BondDetails{
BondDid: bond.BondDid,
SpotPrice: reservePrices,
Supply: bond.CurrentSupply,
Reserve: reserveBalances,
})
}

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

return bz, nil
}

func queryBond(ctx sdk.Context, path []string, keeper Keeper) (res []byte, err error) {
bondDid := path[0]

Expand Down
8 changes: 8 additions & 0 deletions x/bonds/internal/types/querier.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,14 @@ func (b QueryBonds) String() string {
return strings.Join(b[:], "\n")
}

type QueryBondsDetailed []BondDetails
type BondDetails struct {
BondDid string `json:"did" yaml:"did"`
SpotPrice sdk.DecCoins `json:"spot_price" yaml:"spot_price"`
Supply sdk.Coin `json:"supply" yaml:"supply"`
Reserve sdk.Coins `json:"reserve" yaml:"reserve"`
}

type QueryBuyPrice struct {
AdjustedSupply sdk.Coin `json:"adjusted_supply" yaml:"asdjusted_supply"`
Prices sdk.Coins `json:"prices" yaml:"prices"`
Expand Down
Loading