Skip to content

Commit

Permalink
Custom queries #2 (#6)
Browse files Browse the repository at this point in the history
* Update reflect contract to v0.3.0

* Adapt to bindings v0.3.0

* Add full denom query

* Add happy path full denom integ test

* Update to v0.3.0 proper (release)
  • Loading branch information
maurolacy authored Mar 17, 2022
1 parent 20d4c7f commit d1a0399
Show file tree
Hide file tree
Showing 6 changed files with 67 additions and 6 deletions.
7 changes: 4 additions & 3 deletions app/wasm/bindings/query.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,10 @@ type SpotPrice struct {
}

type EstimatePrice struct {
First Swap `json:"first"`
Route []Step `json:"route"`
Amount SwapAmount `json:"amount"`
Contract string `json:"contract"`
First Swap `json:"first"`
Route []Step `json:"route"`
Amount SwapAmount `json:"amount"`
}

type FullDenomResponse struct {
Expand Down
23 changes: 22 additions & 1 deletion app/wasm/queries.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package wasm

import (
"fmt"
wasmvmtypes "github.com/CosmWasm/wasmvm/types"
"math"

Expand All @@ -26,6 +27,26 @@ func NewQueryPlugin(
}
}

func (qp QueryPlugin) GetFullDenom(ctx sdk.Context, contract string, subDenom string) (*string, error) {
// Address validation
contractAddress, err := sdk.AccAddressFromBech32(contract)
if err != nil {
return nil, sdkerrors.Wrap(err, "address from bech32")
}
err = sdk.VerifyAddressFormat(contractAddress)
if err != nil {
return nil, sdkerrors.Wrap(err, "verify address format")
}
// TODO: sub-denom validations
// - sub denom length (min/max) checks
// - sub denom chars
// -
// TODO: Confirm "cw" prefix
fullDenom := fmt.Sprintf("cw/%s/%s", contract, subDenom)

return &fullDenom, nil
}

func (qp QueryPlugin) GetPoolState(ctx sdk.Context, poolId uint64) (*types.PoolState, error) {
poolData, err := qp.gammKeeper.GetPool(ctx, poolId)
if err != nil {
Expand Down Expand Up @@ -60,7 +81,7 @@ func (qp QueryPlugin) GetSpotPrice(ctx sdk.Context, spotPrice *bindings.SpotPric
}

func (qp QueryPlugin) EstimatePrice(ctx sdk.Context, estimatePrice *bindings.EstimatePrice) (*bindings.SwapAmount, error) {
sender := "" // FIXME: https://github.com/confio/osmosis-bindings/pull/14
sender := estimatePrice.Contract
poolId := estimatePrice.First.PoolId
denomIn := estimatePrice.First.DenomIn
denomOut := estimatePrice.First.DenomOut
Expand Down
20 changes: 19 additions & 1 deletion app/wasm/query_plugin.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,25 @@ func CustomQuerier(osmoKeeper *QueryPlugin) func(ctx sdk.Context, request json.R
return nil, sdkerrors.Wrap(err, "osmosis query")
}

if contractQuery.PoolState != nil {
if contractQuery.FullDenom != nil {
contract := contractQuery.FullDenom.Contract
subDenom := contractQuery.FullDenom.SubDenom

fullDenom, err := osmoKeeper.GetFullDenom(ctx, contract, subDenom)
if err != nil {
return nil, sdkerrors.Wrap(err, "osmo full denom query")
}

res := bindings.FullDenomResponse{
Denom: *fullDenom,
}
bz, err := json.Marshal(res)
if err != nil {
return nil, sdkerrors.Wrap(err, "osmo full denom query response")
}
return bz, nil

} else if contractQuery.PoolState != nil {
poolId := contractQuery.PoolState.PoolId

state, err := osmoKeeper.GetPoolState(ctx, poolId)
Expand Down
21 changes: 21 additions & 0 deletions app/wasm/test/custom_query_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,27 @@ func SetupCustomApp(t *testing.T, addr sdk.AccAddress) (*app.OsmosisApp, sdk.Con
return osmosis, ctx
}

func TestQueryFullDenom(t *testing.T) {
actor := RandomAccountAddress()
osmosis, ctx := SetupCustomApp(t, actor)

reflect := instantiateReflectContract(t, ctx, osmosis, actor)
require.NotEmpty(t, reflect)

// query full denom
query := wasmbindings.OsmosisQuery{
FullDenom: &wasmbindings.FullDenom{
Contract: actor.String(),
SubDenom: "ustart",
},
}
resp := wasmbindings.FullDenomResponse{}
queryCustom(t, ctx, osmosis, reflect, query, &resp)

expected := fmt.Sprintf("cw/%s/ustart", actor.String())
require.EqualValues(t, expected, resp.Denom)
}

func TestQueryPool(t *testing.T) {
actor := RandomAccountAddress()
osmosis, ctx := SetupCustomApp(t, actor)
Expand Down
Binary file modified app/wasm/testdata/osmo_reflect.wasm
Binary file not shown.
2 changes: 1 addition & 1 deletion app/wasm/testdata/version.txt
Original file line number Diff line number Diff line change
@@ -1 +1 @@
v0.2.0
v0.3.0

0 comments on commit d1a0399

Please sign in to comment.