Skip to content

Commit

Permalink
Custom queries unit tests (#10)
Browse files Browse the repository at this point in the history
* Add full denom unit tests

* Add get pool state unit tests

* Add get spot price unit tests

* Add estimate price extra checks

* Add estimate price unit tests

* Add tests for zero and negative amounts

* Fix: null spot price

* Fix: Avoid panic on negative amounts

Fix rebase errors / missing null checks

* Fix rebase errors

Add negative amount checks
  • Loading branch information
maurolacy authored Mar 22, 2022
1 parent 825d67b commit 61cfb64
Show file tree
Hide file tree
Showing 3 changed files with 510 additions and 2 deletions.
10 changes: 8 additions & 2 deletions app/wasm/message_plugin.go
Original file line number Diff line number Diff line change
Expand Up @@ -93,14 +93,17 @@ func performSwap(keeper *gammkeeper.Keeper, ctx sdk.Context, contractAddr sdk.Ac
TokenOutDenom: step.DenomOut,
})
}
if swap.Amount.ExactIn.Input.IsNegative() {
return nil, wasmvmtypes.InvalidRequest{Err: "gamm perform swap negative amount in"}
}
tokenIn := sdk.Coin{
Denom: swap.First.DenomIn,
Amount: swap.Amount.ExactIn.Input,
}
tokenOutMinAmount := swap.Amount.ExactIn.MinOutput
estimatedAmount, err := keeper.MultihopSwapExactAmountIn(ctx, contractAddr, routes, tokenIn, tokenOutMinAmount)
if err != nil {
return nil, sdkerrors.Wrap(err, "gamm estimate price exact amount in")
return nil, sdkerrors.Wrap(err, "gamm perform swap exact amount in")
}
return &wasmbindings.SwapAmount{Out: &estimatedAmount}, nil
} else if swap.Amount.ExactOut != nil {
Expand All @@ -117,13 +120,16 @@ func performSwap(keeper *gammkeeper.Keeper, ctx sdk.Context, contractAddr sdk.Ac
output = step.DenomOut
}
tokenInMaxAmount := swap.Amount.ExactOut.MaxInput
if swap.Amount.ExactOut.Output.IsNegative() {
return nil, wasmvmtypes.InvalidRequest{Err: "gamm perform swap negative amount out"}
}
tokenOut := sdk.Coin{
Denom: output,
Amount: swap.Amount.ExactOut.Output,
}
estimatedAmount, err := keeper.MultihopSwapExactAmountOut(ctx, contractAddr, routes, tokenInMaxAmount, tokenOut)
if err != nil {
return nil, sdkerrors.Wrap(err, "gamm estimate price exact amount out")
return nil, sdkerrors.Wrap(err, "gamm perform swap exact amount out")
}
return &wasmbindings.SwapAmount{In: &estimatedAmount}, nil
} else {
Expand Down
17 changes: 17 additions & 0 deletions app/wasm/queries.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package wasm

import (
wasmvmtypes "github.com/CosmWasm/wasmvm/types"
sdk "github.com/cosmos/cosmos-sdk/types"
sdkerrors "github.com/cosmos/cosmos-sdk/types/errors"

Expand Down Expand Up @@ -38,6 +39,9 @@ func (qp QueryPlugin) GetPoolState(ctx sdk.Context, poolId uint64) (*types.PoolS
}

func (qp QueryPlugin) GetSpotPrice(ctx sdk.Context, spotPrice *wasmbindings.SpotPrice) (*sdk.Dec, error) {
if spotPrice == nil {
return nil, wasmvmtypes.InvalidRequest{Err: "gamm spot price null"}
}
poolId := spotPrice.Swap.PoolId
denomIn := spotPrice.Swap.DenomIn
denomOut := spotPrice.Swap.DenomOut
Expand All @@ -56,11 +60,24 @@ func (qp QueryPlugin) GetSpotPrice(ctx sdk.Context, spotPrice *wasmbindings.Spot
}

func (qp QueryPlugin) EstimatePrice(ctx sdk.Context, estimatePrice *wasmbindings.EstimatePrice) (*wasmbindings.SwapAmount, error) {
if estimatePrice == nil {
return nil, wasmvmtypes.InvalidRequest{Err: "gamm estimate price null"}
}
if err := sdk.ValidateDenom(estimatePrice.First.DenomIn); err != nil {
return nil, sdkerrors.Wrap(err, "gamm estimate price denom in")
}
if err := sdk.ValidateDenom(estimatePrice.First.DenomOut); err != nil {
return nil, sdkerrors.Wrap(err, "gamm estimate price denom out")
}
contractAddr, err := sdk.AccAddressFromBech32(estimatePrice.Contract)
if err != nil {
return nil, sdkerrors.Wrap(err, "gamm estimate price sender address")
}

if estimatePrice.Amount == (wasmbindings.SwapAmount{}) {
return nil, wasmvmtypes.InvalidRequest{Err: "gamm estimate price empty swap"}
}

estimate, err := performSwap(qp.gammKeeper, ctx, contractAddr, estimatePrice.ToSwapMsg())
return estimate, err
}
Loading

0 comments on commit 61cfb64

Please sign in to comment.