diff --git a/CHANGELOG.md b/CHANGELOG.md index fe1965f5592..a84e6939b5c 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -55,6 +55,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ### API breaks * [#3763](https://github.com/osmosis-labs/osmosis/pull/3763) Move binary search and error tolerance code from `osmoutils` into `osmomath` +* [#376x](https://github.com/osmosis-labs/osmosis/pull/3763) Remove Osmosis gamm and twap `bindings` that were previously supported as custom wasm plugins. ### Bug fixes diff --git a/app/keepers/keepers.go b/app/keepers/keepers.go index bf77b332d95..cbdec69092c 100644 --- a/app/keepers/keepers.go +++ b/app/keepers/keepers.go @@ -371,7 +371,7 @@ func (appKeepers *AppKeepers) InitNormalKeepers( // if we want to allow any custom callbacks supportedFeatures := "iterator,staking,stargate,osmosis,cosmwasm_1_1" - wasmOpts = append(owasm.RegisterCustomPlugins(appKeepers.GAMMKeeper, appKeepers.BankKeeper, appKeepers.TwapKeeper, appKeepers.TokenFactoryKeeper), wasmOpts...) + wasmOpts = append(owasm.RegisterCustomPlugins(appKeepers.BankKeeper, appKeepers.TokenFactoryKeeper), wasmOpts...) wasmOpts = append(owasm.RegisterStargateQueries(*bApp.GRPCQueryRouter(), appCodec), wasmOpts...) wasmKeeper := wasm.NewKeeper( diff --git a/wasmbinding/bindings/msg.go b/wasmbinding/bindings/msg.go index c58dd5e0e7c..d91feb6bd3b 100644 --- a/wasmbinding/bindings/msg.go +++ b/wasmbinding/bindings/msg.go @@ -15,8 +15,6 @@ type OsmosisMsg struct { /// that they are the admin of. /// Currently, the burn from address must be the admin contract. BurnTokens *BurnTokens `json:"burn_tokens,omitempty"` - /// Swap over one or more pools - Swap *SwapMsg `json:"swap,omitempty"` } // CreateDenom creates a new factory denom, of denomination: @@ -48,9 +46,3 @@ type BurnTokens struct { // BurnFromAddress must be set to "" for now. BurnFromAddress string `json:"burn_from_address"` } - -type SwapMsg struct { - First Swap `json:"first"` - Route []Step `json:"route"` - Amount SwapAmountWithLimit `json:"amount"` -} diff --git a/wasmbinding/bindings/pool.go b/wasmbinding/bindings/pool.go deleted file mode 100644 index e7c78b8c40d..00000000000 --- a/wasmbinding/bindings/pool.go +++ /dev/null @@ -1,8 +0,0 @@ -package bindings - -import sdk "github.com/cosmos/cosmos-sdk/types" - -type PoolAssets struct { - Assets []sdk.Coin - Shares sdk.Coin -} diff --git a/wasmbinding/bindings/query.go b/wasmbinding/bindings/query.go index e2bc9f70c3f..de6c96fec4f 100644 --- a/wasmbinding/bindings/query.go +++ b/wasmbinding/bindings/query.go @@ -1,24 +1,11 @@ package bindings -import ( - wasmvmtypes "github.com/CosmWasm/wasmvm/types" -) - // OsmosisQuery contains osmosis custom queries. // See https://github.com/osmosis-labs/osmosis-bindings/blob/main/packages/bindings/src/query.rs type OsmosisQuery struct { /// Given a subdenom minted by a contract via `OsmosisMsg::MintTokens`, /// returns the full denom as used by `BankMsg::Send`. FullDenom *FullDenom `json:"full_denom,omitempty"` - /// For a given pool ID, list all tokens traded on it with current liquidity (spot). - /// As well as the total number of LP shares and their denom. - PoolState *PoolState `json:"pool_state,omitempty"` - /// Return current spot price swapping In for Out on given pool ID. - /// Warning: this can easily be manipulated via sandwich attacks, do not use as price oracle. - /// We will add TWAP for more robust price feed. - SpotPrice *SpotPrice `json:"spot_price,omitempty"` - /// Return current spot price swapping In for Out on given pool ID. - EstimateSwap *EstimateSwap `json:"estimate_swap,omitempty"` /// Returns the admin of a denom, if the denom is a Token Factory denom. DenomAdmin *DenomAdmin `json:"denom_admin,omitempty"` } @@ -36,66 +23,6 @@ type DenomAdminResponse struct { Admin string `json:"admin"` } -type PoolState struct { - PoolId uint64 `json:"id"` -} - -type SpotPrice struct { - Swap Swap `json:"swap"` - WithSwapFee bool `json:"with_swap_fee"` -} - -type EstimateSwap struct { - Sender string `json:"sender"` - First Swap `json:"first"` - Route []Step `json:"route"` - Amount SwapAmount `json:"amount"` -} - -type ArithmeticTwap struct { - PoolId uint64 `json:"id"` - QuoteAssetDenom string `json:"quote_asset_denom"` - BaseAssetDenom string `json:"base_asset_denom"` - // NOTE: StartTime is expected to be in Unix time milliseconds. - StartTime int64 `json:"start_time"` - // NOTE: EndTime is expected to be in Unix time milliseconds. - EndTime int64 `json:"end_time"` -} - -type ArithmeticTwapToNow struct { - PoolId uint64 `json:"id"` - QuoteAssetDenom string `json:"quote_asset_denom"` - BaseAssetDenom string `json:"base_asset_denom"` - // NOTE: StartTime is expected to be in Unix time milliseconds. - StartTime int64 `json:"start_time"` -} - -func (e *EstimateSwap) ToSwapMsg() *SwapMsg { - return &SwapMsg{ - First: e.First, - Route: e.Route, - Amount: e.Amount.Unlimited(), - } -} - type FullDenomResponse struct { Denom string `json:"denom"` } - -type PoolStateResponse struct { - /// The various assets that be swapped. Including current liquidity. - Assets []wasmvmtypes.Coin `json:"assets"` - /// The number of LP shares and their amount - Shares wasmvmtypes.Coin `json:"shares"` -} - -type SpotPriceResponse struct { - /// How many output we would get for 1 input - Price string `json:"price"` -} - -type EstimatePriceResponse struct { - // If you query with SwapAmount::Input, this is SwapAmount::Output. - // If you query with SwapAmount::Output, this is SwapAmount::Input. - Amount SwapAmount `json:"swap_amount"` -} diff --git a/wasmbinding/bindings/types.go b/wasmbinding/bindings/types.go deleted file mode 100644 index 2f3d89a48f3..00000000000 --- a/wasmbinding/bindings/types.go +++ /dev/null @@ -1,74 +0,0 @@ -package bindings - -import ( - "math" - - sdk "github.com/cosmos/cosmos-sdk/types" -) - -type Swap struct { - PoolId uint64 `json:"pool_id"` - DenomIn string `json:"denom_in"` - DenomOut string `json:"denom_out"` -} - -type Step struct { - PoolId uint64 `json:"pool_id"` - DenomOut string `json:"denom_out"` -} - -type SwapAmount struct { - In *sdk.Int `json:"in,omitempty"` - Out *sdk.Int `json:"out,omitempty"` -} - -// This returns SwapAmountWithLimit with the largest possible limits (that will never be hit) -func (s SwapAmount) Unlimited() SwapAmountWithLimit { - if s.In != nil { - return SwapAmountWithLimit{ - ExactIn: &ExactIn{ - Input: *s.In, - MinOutput: sdk.NewInt(1), - }, - } - } - if s.Out != nil { - return SwapAmountWithLimit{ - ExactOut: &ExactOut{ - Output: *s.Out, - MaxInput: sdk.NewInt(math.MaxInt64), - }, - } - } - panic("Must define In or Out") -} - -type SwapAmountWithLimit struct { - ExactIn *ExactIn `json:"exact_in,omitempty"` - ExactOut *ExactOut `json:"exact_out,omitempty"` -} - -// This returns the amount without min/max to use as simpler argument -func (s SwapAmountWithLimit) RemoveLimit() SwapAmount { - if s.ExactIn != nil { - return SwapAmount{ - In: &s.ExactIn.Input, - } - } - if s.ExactOut != nil { - return SwapAmount{ - Out: &s.ExactOut.Output, - } - } - panic("Must define ExactIn or ExactOut") -} - -type ExactIn struct { - Input sdk.Int `json:"input"` - MinOutput sdk.Int `json:"min_output"` -} - -type ExactOut struct { - MaxInput sdk.Int `json:"max_input"` - Output sdk.Int `json:"output"` -} diff --git a/wasmbinding/bindings/types_test.go b/wasmbinding/bindings/types_test.go deleted file mode 100644 index 859c264d54b..00000000000 --- a/wasmbinding/bindings/types_test.go +++ /dev/null @@ -1,179 +0,0 @@ -package bindings - -import ( - "encoding/json" - "testing" - - sdk "github.com/cosmos/cosmos-sdk/types" - "github.com/stretchr/testify/assert" - "github.com/stretchr/testify/require" -) - -var ( - swapJson = []byte("{ \"pool_id\": 1, \"denom_in\": \"denomIn\", \"denom_out\": \"denomOut\" }") - swap = Swap{ - PoolId: 1, - DenomIn: "denomIn", - DenomOut: "denomOut", - } - stepJson = []byte("{ \"pool_id\": 2, \"denom_out\": \"denomOut\" }") - step = Step{ - PoolId: 2, - DenomOut: "denomOut", - } - swapAmountInJson = []byte("{ \"in\": \"123\", \"out\": null }") - in = sdk.NewInt(123) - swapAmountIn = SwapAmount{ - In: &in, - } - swapAmountOutJson = []byte("{ \"out\": \"456\" }") - out = sdk.NewInt(456) - swapAmountOut = SwapAmount{ - Out: &out, - } - exactIn = ExactIn{ - Input: sdk.NewInt(789), - MinOutput: sdk.NewInt(101112), - } - exactOut = ExactOut{ - MaxInput: sdk.NewInt(131415), - Output: sdk.NewInt(161718), - } - swapAmountExactInJson = []byte("{ \"exact_in\": { \"input\": \"789\", \"min_output\": \"101112\" } }") - swapAmountExactIn = SwapAmountWithLimit{ - ExactIn: &exactIn, - } - swapAmountExactOutJson = []byte("{ \"exact_in\": null, \"exact_out\": { \"max_input\": \"131415\", \"output\": \"161718\" } }") - swapAmountExactOut = SwapAmountWithLimit{ - ExactOut: &exactOut, - } -) - -func TestTypesEncodeDecode(t *testing.T) { - // Swap - // Marshal - bzSwap, err := json.Marshal(swap) - require.NoError(t, err) - // Unmarshal - var swap1 Swap - err = json.Unmarshal(bzSwap, &swap1) - require.NoError(t, err) - // Check - assert.Equal(t, swap, swap1) - - // Step - // Marshal - bzStep, err := json.Marshal(step) - require.NoError(t, err) - // Unmarshal - var step1 Step - err = json.Unmarshal(bzStep, &step1) - require.NoError(t, err) - // Check - assert.Equal(t, step, step1) - - // SwapAmount - // Marshal - bzSwapAmount, err := json.Marshal(swapAmountOut) - require.NoError(t, err) - // Unmarshal - var swapAmount1 SwapAmount - err = json.Unmarshal(bzSwapAmount, &swapAmount1) - require.NoError(t, err) - // Check - assert.Equal(t, swapAmountOut, swapAmount1) - - // SwapAmount in - // Marshal - bzSwapAmount2, err := json.Marshal(swapAmountIn) - require.NoError(t, err) - // Unmarshal - var swapAmount2 SwapAmount - err = json.Unmarshal(bzSwapAmount2, &swapAmount2) - require.NoError(t, err) - // Check - assert.Equal(t, swapAmountIn, swapAmount2) - - // SwapAmount out - // Marshal - bzSwapAmount3, err := json.Marshal(swapAmountOut) - require.NoError(t, err) - // Unmarshal - var swapAmount3 SwapAmount - err = json.Unmarshal(bzSwapAmount3, &swapAmount3) - require.NoError(t, err) - // Check - assert.Equal(t, swapAmountOut, swapAmount3) - - // SwapAmount exact in - // Marshal - bzSwapAmountWithLimit1, err := json.Marshal(swapAmountExactIn) - require.NoError(t, err) - // Unmarshal - var swapAmountWithLimit1 SwapAmountWithLimit - err = json.Unmarshal(bzSwapAmountWithLimit1, &swapAmountWithLimit1) - require.NoError(t, err) - // Check - assert.Equal(t, swapAmountExactIn, swapAmountWithLimit1) - - // SwapAmount exact out - // Marshal - bzSwapAmountWithLimit2, err := json.Marshal(swapAmountExactOut) - require.NoError(t, err) - // Unmarshal - var swapAmountWithLimit2 SwapAmountWithLimit - err = json.Unmarshal(bzSwapAmountWithLimit2, &swapAmountWithLimit2) - require.NoError(t, err) - // Check - assert.Equal(t, swapAmountExactOut, swapAmountWithLimit2) -} - -func TestTypesDecode(t *testing.T) { - // Swap - // Unmarshal - var swap1 Swap - err := json.Unmarshal([]byte(swapJson), &swap1) - require.NoError(t, err) - // Check - assert.Equal(t, swap, swap1) - - // Step - // Unmarshal - var step1 Step - err = json.Unmarshal(stepJson, &step1) - require.NoError(t, err) - // Check - assert.Equal(t, step, step1) - - // SwapAmount in - // Unmarshal - var swapAmount1 SwapAmount - err = json.Unmarshal(swapAmountInJson, &swapAmount1) - require.NoError(t, err) - // Check - assert.Equal(t, swapAmountIn, swapAmount1) - - // SwapAmount out - // Unmarshal - var swapAmount2 SwapAmount - err = json.Unmarshal(swapAmountOutJson, &swapAmount2) - require.NoError(t, err) - // Check - assert.Equal(t, swapAmountOut, swapAmount2) - - // SwapAmount exact in - // Unmarshal - var swapAmountWithLimit1 SwapAmountWithLimit - err = json.Unmarshal(swapAmountExactInJson, &swapAmountWithLimit1) - require.NoError(t, err) - // Check - assert.Equal(t, swapAmountExactIn, swapAmountWithLimit1) - - // SwapAmount exact out - // Unmarshal - var swapAmountWithLimit2 SwapAmountWithLimit - err = json.Unmarshal(swapAmountExactOutJson, &swapAmountWithLimit2) - require.NoError(t, err) - // Check - assert.Equal(t, swapAmountExactOut, swapAmountWithLimit2) -} diff --git a/wasmbinding/message_plugin.go b/wasmbinding/message_plugin.go index c57452ae1e6..4d6b775e38b 100644 --- a/wasmbinding/message_plugin.go +++ b/wasmbinding/message_plugin.go @@ -10,20 +10,17 @@ import ( bankkeeper "github.com/cosmos/cosmos-sdk/x/bank/keeper" "github.com/osmosis-labs/osmosis/v13/wasmbinding/bindings" - gammkeeper "github.com/osmosis-labs/osmosis/v13/x/gamm/keeper" - gammtypes "github.com/osmosis-labs/osmosis/v13/x/gamm/types" tokenfactorykeeper "github.com/osmosis-labs/osmosis/v13/x/tokenfactory/keeper" tokenfactorytypes "github.com/osmosis-labs/osmosis/v13/x/tokenfactory/types" ) // CustomMessageDecorator returns decorator for custom CosmWasm bindings messages -func CustomMessageDecorator(gammKeeper *gammkeeper.Keeper, bank *bankkeeper.BaseKeeper, tokenFactory *tokenfactorykeeper.Keeper) func(wasmkeeper.Messenger) wasmkeeper.Messenger { +func CustomMessageDecorator(bank *bankkeeper.BaseKeeper, tokenFactory *tokenfactorykeeper.Keeper) func(wasmkeeper.Messenger) wasmkeeper.Messenger { return func(old wasmkeeper.Messenger) wasmkeeper.Messenger { return &CustomMessenger{ wrapped: old, bank: bank, - gammKeeper: gammKeeper, tokenFactory: tokenFactory, } } @@ -32,7 +29,6 @@ func CustomMessageDecorator(gammKeeper *gammkeeper.Keeper, bank *bankkeeper.Base type CustomMessenger struct { wrapped wasmkeeper.Messenger bank *bankkeeper.BaseKeeper - gammKeeper *gammkeeper.Keeper tokenFactory *tokenfactorykeeper.Keeper } @@ -59,9 +55,6 @@ func (m *CustomMessenger) DispatchMsg(ctx sdk.Context, contractAddr sdk.AccAddre if contractMsg.BurnTokens != nil { return m.burnTokens(ctx, contractAddr, contractMsg.BurnTokens) } - if contractMsg.Swap != nil { - return m.swapTokens(ctx, contractAddr, contractMsg.Swap) - } } return m.wrapped.DispatchMsg(ctx, contractAddr, contractIBCPortID, msg) } @@ -203,75 +196,6 @@ func PerformBurn(f *tokenfactorykeeper.Keeper, ctx sdk.Context, contractAddr sdk return nil } -// swapTokens swaps one denom for another. -func (m *CustomMessenger) swapTokens(ctx sdk.Context, contractAddr sdk.AccAddress, swap *bindings.SwapMsg) ([]sdk.Event, [][]byte, error) { - _, err := PerformSwap(m.gammKeeper, ctx, contractAddr, swap) - if err != nil { - return nil, nil, sdkerrors.Wrap(err, "perform swap") - } - return nil, nil, nil -} - -// PerformSwap can be used both for the real swap, and the EstimateSwap query -func PerformSwap(keeper *gammkeeper.Keeper, ctx sdk.Context, contractAddr sdk.AccAddress, swap *bindings.SwapMsg) (*bindings.SwapAmount, error) { - if swap == nil { - return nil, wasmvmtypes.InvalidRequest{Err: "gamm perform swap null swap"} - } - if swap.Amount.ExactIn != nil { - routes := []gammtypes.SwapAmountInRoute{{ - PoolId: swap.First.PoolId, - TokenOutDenom: swap.First.DenomOut, - }} - for _, step := range swap.Route { - routes = append(routes, gammtypes.SwapAmountInRoute{ - PoolId: step.PoolId, - 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 - tokenOutAmount, err := keeper.MultihopSwapExactAmountIn(ctx, contractAddr, routes, tokenIn, tokenOutMinAmount) - if err != nil { - return nil, sdkerrors.Wrap(err, "gamm perform swap exact amount in") - } - return &bindings.SwapAmount{Out: &tokenOutAmount}, nil - } else if swap.Amount.ExactOut != nil { - routes := []gammtypes.SwapAmountOutRoute{{ - PoolId: swap.First.PoolId, - TokenInDenom: swap.First.DenomIn, - }} - output := swap.First.DenomOut - for _, step := range swap.Route { - routes = append(routes, gammtypes.SwapAmountOutRoute{ - PoolId: step.PoolId, - TokenInDenom: output, - }) - 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, - } - tokenInAmount, err := keeper.MultihopSwapExactAmountOut(ctx, contractAddr, routes, tokenInMaxAmount, tokenOut) - if err != nil { - return nil, sdkerrors.Wrap(err, "gamm perform swap exact amount out") - } - return &bindings.SwapAmount{In: &tokenInAmount}, nil - } else { - return nil, wasmvmtypes.UnsupportedRequest{Kind: "must support either Swap.ExactIn or Swap.ExactOut"} - } -} - // GetFullDenom is a function, not method, so the message_plugin can use it func GetFullDenom(contract string, subDenom string) (string, error) { // Address validation diff --git a/wasmbinding/queries.go b/wasmbinding/queries.go index 41882d0a7c5..d03475897a8 100644 --- a/wasmbinding/queries.go +++ b/wasmbinding/queries.go @@ -2,30 +2,20 @@ package wasmbinding import ( "fmt" - "time" - wasmvmtypes "github.com/CosmWasm/wasmvm/types" sdk "github.com/cosmos/cosmos-sdk/types" - sdkerrors "github.com/cosmos/cosmos-sdk/types/errors" "github.com/osmosis-labs/osmosis/v13/wasmbinding/bindings" - gammkeeper "github.com/osmosis-labs/osmosis/v13/x/gamm/keeper" - gammtypes "github.com/osmosis-labs/osmosis/v13/x/gamm/types" tokenfactorykeeper "github.com/osmosis-labs/osmosis/v13/x/tokenfactory/keeper" - twapkeeper "github.com/osmosis-labs/osmosis/v13/x/twap" ) type QueryPlugin struct { - gammKeeper *gammkeeper.Keeper - twapKeeper *twapkeeper.Keeper tokenFactoryKeeper *tokenfactorykeeper.Keeper } // NewQueryPlugin returns a reference to a new QueryPlugin. -func NewQueryPlugin(gk *gammkeeper.Keeper, tk *twapkeeper.Keeper, tfk *tokenfactorykeeper.Keeper) *QueryPlugin { +func NewQueryPlugin(tfk *tokenfactorykeeper.Keeper) *QueryPlugin { return &QueryPlugin{ - gammKeeper: gk, - twapKeeper: tk, tokenFactoryKeeper: tfk, } } @@ -39,108 +29,3 @@ func (qp QueryPlugin) GetDenomAdmin(ctx sdk.Context, denom string) (*bindings.De return &bindings.DenomAdminResponse{Admin: metadata.Admin}, nil } - -// GetPoolState is a query to get pool liquidity and amount of each denoms' pool shares. -func (qp QueryPlugin) GetPoolState(ctx sdk.Context, poolID uint64) (*bindings.PoolAssets, error) { - poolData, err := qp.gammKeeper.GetPoolAndPoke(ctx, poolID) - if err != nil { - return nil, sdkerrors.Wrap(err, "gamm get pool") - } - - return &bindings.PoolAssets{ - Assets: poolData.GetTotalPoolLiquidity(ctx), - Shares: sdk.Coin{ - Denom: gammtypes.GetPoolShareDenom(poolID), - Amount: poolData.GetTotalShares(), - }, - }, nil -} - -// GetSpotPrice is a query to get spot price of denoms. -func (qp QueryPlugin) GetSpotPrice(ctx sdk.Context, spotPrice *bindings.SpotPrice) (*sdk.Dec, error) { - if spotPrice == nil { - return nil, wasmvmtypes.InvalidRequest{Err: "gamm spot price null"} - } - - poolId := spotPrice.Swap.PoolId - baseAsset := spotPrice.Swap.DenomOut - quoteAsset := spotPrice.Swap.DenomIn - withSwapFee := spotPrice.WithSwapFee - - price, err := qp.gammKeeper.CalculateSpotPrice(ctx, poolId, quoteAsset, baseAsset) - if err != nil { - return nil, sdkerrors.Wrap(err, "gamm get spot price") - } - - if withSwapFee { - poolData, err := qp.gammKeeper.GetPoolAndPoke(ctx, poolId) - if err != nil { - return nil, sdkerrors.Wrap(err, "gamm get pool") - } - - price = price.Mul(sdk.OneDec().Sub(poolData.GetSwapFee(ctx))) - } - - return &price, nil -} - -// EstimateSwap validates each denom (in / out) and performs a swap. -func (qp QueryPlugin) EstimateSwap(ctx sdk.Context, estimateSwap *bindings.EstimateSwap) (*bindings.SwapAmount, error) { - if estimateSwap == nil { - return nil, wasmvmtypes.InvalidRequest{Err: "gamm estimate swap null"} - } - if err := sdk.ValidateDenom(estimateSwap.First.DenomIn); err != nil { - return nil, sdkerrors.Wrap(err, "gamm estimate swap denom in") - } - if err := sdk.ValidateDenom(estimateSwap.First.DenomOut); err != nil { - return nil, sdkerrors.Wrap(err, "gamm estimate swap denom out") - } - senderAddr, err := sdk.AccAddressFromBech32(estimateSwap.Sender) - if err != nil { - return nil, sdkerrors.Wrap(err, "gamm estimate swap sender address") - } - - if estimateSwap.Amount == (bindings.SwapAmount{}) { - return nil, wasmvmtypes.InvalidRequest{Err: "gamm estimate swap empty swap"} - } - - estimate, err := PerformSwap(qp.gammKeeper, ctx, senderAddr, estimateSwap.ToSwapMsg()) - return estimate, err -} - -func (qp QueryPlugin) ArithmeticTwap(ctx sdk.Context, arithmeticTwap *bindings.ArithmeticTwap) (*sdk.Dec, error) { - if arithmeticTwap == nil { - return nil, wasmvmtypes.InvalidRequest{Err: "gamm arithmetic twap null"} - } - - poolId := arithmeticTwap.PoolId - quoteAssetDenom := arithmeticTwap.QuoteAssetDenom - baseAssetDenom := arithmeticTwap.BaseAssetDenom - startTime := time.UnixMilli(arithmeticTwap.StartTime) - endTime := time.UnixMilli(arithmeticTwap.EndTime) - - twap, err := qp.twapKeeper.GetArithmeticTwap(ctx, poolId, baseAssetDenom, quoteAssetDenom, startTime, endTime) - if err != nil { - return nil, sdkerrors.Wrap(err, "gamm arithmetic twap") - } - - return &twap, nil -} - -func (qp QueryPlugin) ArithmeticTwapToNow(ctx sdk.Context, arithmeticTwap *bindings.ArithmeticTwapToNow) (*sdk.Dec, error) { - if arithmeticTwap == nil { - return nil, wasmvmtypes.InvalidRequest{Err: "gamm arithmetic twap null"} - } - - poolId := arithmeticTwap.PoolId - quoteAssetDenom := arithmeticTwap.QuoteAssetDenom - baseAssetDenom := arithmeticTwap.BaseAssetDenom - startTime := time.UnixMilli(arithmeticTwap.StartTime) - - twap, err := qp.twapKeeper.GetArithmeticTwapToNow(ctx, poolId, baseAssetDenom, quoteAssetDenom, startTime) - if err != nil { - return nil, sdkerrors.Wrap(err, "gamm arithmetic twap") - } - - return &twap, nil -} diff --git a/wasmbinding/query_plugin.go b/wasmbinding/query_plugin.go index 1016043b47f..aa1626d060e 100644 --- a/wasmbinding/query_plugin.go +++ b/wasmbinding/query_plugin.go @@ -86,57 +86,6 @@ func CustomQuerier(qp *QueryPlugin) func(ctx sdk.Context, request json.RawMessag return bz, nil - case contractQuery.PoolState != nil: - poolId := contractQuery.PoolState.PoolId - - state, err := qp.GetPoolState(ctx, poolId) - if err != nil { - return nil, sdkerrors.Wrap(err, "osmo pool state query") - } - - assets := ConvertSdkCoinsToWasmCoins(state.Assets) - shares := ConvertSdkCoinToWasmCoin(state.Shares) - - res := bindings.PoolStateResponse{ - Assets: assets, - Shares: shares, - } - - bz, err := json.Marshal(res) - if err != nil { - return nil, sdkerrors.Wrap(err, "osmo pool state query response") - } - - return bz, nil - - case contractQuery.SpotPrice != nil: - spotPrice, err := qp.GetSpotPrice(ctx, contractQuery.SpotPrice) - if err != nil { - return nil, sdkerrors.Wrap(err, "osmo spot price query") - } - - res := bindings.SpotPriceResponse{Price: spotPrice.String()} - bz, err := json.Marshal(res) - if err != nil { - return nil, sdkerrors.Wrap(err, "osmo spot price query response") - } - - return bz, nil - - case contractQuery.EstimateSwap != nil: - swapAmount, err := qp.EstimateSwap(ctx, contractQuery.EstimateSwap) - if err != nil { - return nil, sdkerrors.Wrap(err, "osmo estimate swap query") - } - - res := bindings.EstimatePriceResponse{Amount: *swapAmount} - bz, err := json.Marshal(res) - if err != nil { - return nil, sdkerrors.Wrap(err, "osmo estimate swap query response") - } - - return bz, nil - default: return nil, wasmvmtypes.UnsupportedRequest{Kind: "unknown osmosis query variant"} } diff --git a/wasmbinding/test/custom_msg_test.go b/wasmbinding/test/custom_msg_test.go index 9b49239c8e9..cf9e46dca35 100644 --- a/wasmbinding/test/custom_msg_test.go +++ b/wasmbinding/test/custom_msg_test.go @@ -240,381 +240,6 @@ type BaseState struct { RegenPool uint64 } -func TestSwapMsg(t *testing.T) { - // table tests with this setup - cases := []struct { - name string - msg func(BaseState) *bindings.SwapMsg - expectErr bool - initFunds sdk.Coin - finalFunds []sdk.Coin - }{ - { - name: "exact in: simple swap works", - msg: func(state BaseState) *bindings.SwapMsg { - return &bindings.SwapMsg{ - First: bindings.Swap{ - PoolId: state.StarPool, - DenomIn: "uosmo", - DenomOut: "ustar", - }, - // Note: you must use empty array, not nil, for valid Rust JSON - Route: []bindings.Step{}, - Amount: bindings.SwapAmountWithLimit{ - ExactIn: &bindings.ExactIn{ - Input: sdk.NewInt(12000000), - MinOutput: sdk.NewInt(5000000), - }, - }, - } - }, - initFunds: sdk.NewInt64Coin("uosmo", 13000000), - finalFunds: []sdk.Coin{ - sdk.NewInt64Coin("uosmo", 1000000), - sdk.NewInt64Coin("ustar", 120000000), - }, - }, - { - name: "exact in: price too low", - msg: func(state BaseState) *bindings.SwapMsg { - return &bindings.SwapMsg{ - First: bindings.Swap{ - PoolId: state.StarPool, - DenomIn: "uosmo", - DenomOut: "ustar", - }, - // Note: you must use empty array, not nil, for valid Rust JSON - Route: []bindings.Step{}, - Amount: bindings.SwapAmountWithLimit{ - ExactIn: &bindings.ExactIn{ - Input: sdk.NewInt(12000000), - MinOutput: sdk.NewInt(555000000), - }, - }, - } - }, - initFunds: sdk.NewInt64Coin("uosmo", 13000000), - expectErr: true, - }, - { - name: "exact in: not enough funds to swap", - msg: func(state BaseState) *bindings.SwapMsg { - return &bindings.SwapMsg{ - First: bindings.Swap{ - PoolId: state.StarPool, - DenomIn: "uosmo", - DenomOut: "ustar", - }, - // Note: you must use empty array, not nil, for valid Rust JSON - Route: []bindings.Step{}, - Amount: bindings.SwapAmountWithLimit{ - ExactIn: &bindings.ExactIn{ - Input: sdk.NewInt(12000000), - MinOutput: sdk.NewInt(5000000), - }, - }, - } - }, - initFunds: sdk.NewInt64Coin("uosmo", 7000000), - expectErr: true, - }, - { - name: "exact in: invalidPool", - msg: func(state BaseState) *bindings.SwapMsg { - return &bindings.SwapMsg{ - First: bindings.Swap{ - PoolId: state.StarPool, - DenomIn: "uosmo", - DenomOut: "uatom", - }, - // Note: you must use empty array, not nil, for valid Rust JSON - Route: []bindings.Step{}, - Amount: bindings.SwapAmountWithLimit{ - ExactIn: &bindings.ExactIn{ - Input: sdk.NewInt(12000000), - MinOutput: sdk.NewInt(100000), - }, - }, - } - }, - initFunds: sdk.NewInt64Coin("uosmo", 13000000), - expectErr: true, - }, - - // FIXME: this panics in GAMM module !?! hits a known TODO - // https://github.com/osmosis-labs/osmosis/blob/a380ab2fcd39fb94c2b10411e07daf664911257a/osmomath/math.go#L47-L51 - //"exact out: panics if too much swapped": { - // msg: func(state BaseState) *bindings.SwapMsg { - // return &bindings.SwapMsg{ - // First: bindings.Swap{ - // PoolId: state.StarPool, - // DenomIn: "uosmo", - // DenomOut: "ustar", - // }, - // // Note: you must use empty array, not nil, for valid Rust JSON - // Route: []bindings.Step{}, - // Amount: bindings.SwapAmountWithLimit{ - // ExactOut: &bindings.ExactOut{ - // MaxInput: sdk.NewInt(22000000), - // Output: sdk.NewInt(120000000), - // }, - // }, - // } - // }, - // initFunds: sdk.NewInt64Coin("uosmo", 15000000), - // finalFunds: []sdk.Coin{ - // sdk.NewInt64Coin("uosmo", 3000000), - // sdk.NewInt64Coin("ustar", 120000000), - // }, - //}, - { - name: "exact out: simple swap works", - msg: func(state BaseState) *bindings.SwapMsg { - return &bindings.SwapMsg{ - First: bindings.Swap{ - PoolId: state.AtomPool, - DenomIn: "uosmo", - DenomOut: "uatom", - }, - // Note: you must use empty array, not nil, for valid Rust JSON - Route: []bindings.Step{}, - Amount: bindings.SwapAmountWithLimit{ - ExactOut: &bindings.ExactOut{ - // 12 OSMO * 6 ATOM == 18 OSMO * 4 ATOM (+6 OSMO, -2 ATOM) - MaxInput: sdk.NewInt(7000000), - Output: sdk.NewInt(2000000), - }, - }, - } - }, - initFunds: sdk.NewInt64Coin("uosmo", 8000000), - finalFunds: []sdk.Coin{ - sdk.NewInt64Coin("uatom", 2000000), - sdk.NewInt64Coin("uosmo", 2000000), - }, - }, - { - name: "exact in: 2 step multi-hop", - msg: func(state BaseState) *bindings.SwapMsg { - return &bindings.SwapMsg{ - First: bindings.Swap{ - PoolId: state.StarPool, - DenomIn: "ustar", - DenomOut: "uosmo", - }, - Route: []bindings.Step{{ - PoolId: state.AtomPool, - DenomOut: "uatom", - }}, - Amount: bindings.SwapAmountWithLimit{ - ExactIn: &bindings.ExactIn{ - Input: sdk.NewInt(240000000), - MinOutput: sdk.NewInt(1999000), - }, - }, - } - }, - initFunds: sdk.NewInt64Coin("ustar", 240000000), - finalFunds: []sdk.Coin{ - // 240 STAR -> 6 OSMO - // 6 OSMO -> 2 ATOM (with minor rounding) - sdk.NewInt64Coin("uatom", 1999999), - }, - }, - { - name: "exact out: 2 step multi-hop", - msg: func(state BaseState) *bindings.SwapMsg { - return &bindings.SwapMsg{ - First: bindings.Swap{ - PoolId: state.AtomPool, - DenomIn: "uosmo", - DenomOut: "uatom", - }, - Route: []bindings.Step{{ - PoolId: state.RegenPool, - DenomOut: "uregen", - }}, - Amount: bindings.SwapAmountWithLimit{ - ExactOut: &bindings.ExactOut{ - MaxInput: sdk.NewInt(2000000), - Output: sdk.NewInt(12000000 - 12), - }, - }, - } - }, - initFunds: sdk.NewInt64Coin("uosmo", 2000000), - finalFunds: []sdk.Coin{ - // 2 OSMO -> 1.2 ATOM - // 1.2 ATOM -> 12 REGEN (with minor rounding) - sdk.NewInt64Coin("uosmo", 2), - sdk.NewInt64Coin("uregen", 12000000-12), - }, - }, - // FIXME: this panics in GAMM module !?! hits a known TODO - // https://github.com/osmosis-labs/osmosis/blob/a380ab2fcd39fb94c2b10411e07daf664911257a/osmomath/math.go#L47-L51 - // { - // name: "exact out: panics on math power stuff", - // msg: func(state BaseState) *bindings.SwapMsg { - // return &bindings.SwapMsg{ - // First: bindings.Swap{ - // PoolId: state.StarPool, - // DenomIn: "ustar", - // DenomOut: "uosmo", - // }, - // Route: []bindings.Step{{ - // PoolId: state.AtomPool, - // DenomOut: "uatom", - // }}, - // Amount: bindings.SwapAmountWithLimit{ - // ExactOut: &bindings.ExactOut{ - // MaxInput: sdk.NewInt(240005000), - // Output: sdk.NewInt(2000000), - // }, - // }, - // } - // }, - // initFunds: sdk.NewInt64Coin("ustar", 240005000), - // finalFunds: []sdk.Coin{ - // // 240 STAR -> 6 OSMO - // // 6 OSMO -> 2 ATOM (with minor rounding) - // sdk.NewInt64Coin("uatom", 2000000), - // sdk.NewInt64Coin("ustar", 5000), - // }, - // }, - { - name: "exact in: 3 step multi-hop", - msg: func(state BaseState) *bindings.SwapMsg { - return &bindings.SwapMsg{ - First: bindings.Swap{ - PoolId: state.StarPool, - DenomIn: "ustar", - DenomOut: "uosmo", - }, - Route: []bindings.Step{{ - PoolId: state.AtomPool, - DenomOut: "uatom", - }, { - PoolId: state.RegenPool, - DenomOut: "uregen", - }}, - Amount: bindings.SwapAmountWithLimit{ - ExactIn: &bindings.ExactIn{ - Input: sdk.NewInt(240000000), - MinOutput: sdk.NewInt(23900000), - }, - }, - } - }, - initFunds: sdk.NewInt64Coin("ustar", 240000000), - finalFunds: []sdk.Coin{ - // 240 STAR -> 6 OSMO - // 6 OSMO -> 2 ATOM - // 2 ATOM -> 24 REGEN (with minor rounding) - sdk.NewInt64Coin("uregen", 23999990), - }, - }, - { - name: "exact out: 3 step multi-hop", - msg: func(state BaseState) *bindings.SwapMsg { - return &bindings.SwapMsg{ - First: bindings.Swap{ - PoolId: state.StarPool, - DenomIn: "ustar", - DenomOut: "uosmo", - }, - Route: []bindings.Step{{ - PoolId: state.AtomPool, - DenomOut: "uatom", - }, { - PoolId: state.RegenPool, - DenomOut: "uregen", - }}, - Amount: bindings.SwapAmountWithLimit{ - ExactOut: &bindings.ExactOut{ - MaxInput: sdk.NewInt(50000000), - Output: sdk.NewInt(12000000), - }, - }, - } - }, - initFunds: sdk.NewInt64Coin("ustar", 50000000), - finalFunds: []sdk.Coin{ - // ~48 STAR -> 2 OSMO - // 2 OSMO -> .857 ATOM - // .857 ATOM -> 12 REGEN (with minor rounding) - sdk.NewInt64Coin("uregen", 12000000), - sdk.NewInt64Coin("ustar", 1999971), - }, - }, - } - for _, tc := range cases { - tc := tc - t.Run(tc.name, func(t *testing.T) { - creator := RandomAccountAddress() - osmosis, ctx := SetupCustomApp(t, creator) - state := prepareSwapState(t, ctx, osmosis) - - trader := RandomAccountAddress() - fundAccount(t, ctx, osmosis, trader, []sdk.Coin{tc.initFunds}) - reflect := instantiateReflectContract(t, ctx, osmosis, trader) - require.NotEmpty(t, reflect) - - msg := bindings.OsmosisMsg{Swap: tc.msg(state)} - err := executeCustom(t, ctx, osmosis, reflect, trader, msg, tc.initFunds) - if tc.expectErr { - require.Error(t, err) - } else { - require.NoError(t, err) - balances := osmosis.BankKeeper.GetAllBalances(ctx, reflect) - // uncomment these to debug any confusing results (show balances, not (*big.Int)(0x140005e51e0)) - // fmt.Printf("Expected: %s\n", tc.finalFunds) - // fmt.Printf("Got: %s\n", balances) - require.EqualValues(t, tc.finalFunds, balances) - } - }) - } -} - -// test setup for each run through the table test above -func prepareSwapState(t *testing.T, ctx sdk.Context, osmosis *app.OsmosisApp) BaseState { - actor := RandomAccountAddress() - - swapperFunds := sdk.NewCoins( - sdk.NewInt64Coin("uatom", 333000000), - sdk.NewInt64Coin("uosmo", 555000000+3*poolFee), - sdk.NewInt64Coin("uregen", 777000000), - sdk.NewInt64Coin("ustar", 999000000), - ) - fundAccount(t, ctx, osmosis, actor, swapperFunds) - - // 20 star to 1 osmo - funds1 := []sdk.Coin{ - sdk.NewInt64Coin("uosmo", 12000000), - sdk.NewInt64Coin("ustar", 240000000), - } - starPool := preparePool(t, ctx, osmosis, actor, funds1) - - // 2 osmo to 1 atom - funds2 := []sdk.Coin{ - sdk.NewInt64Coin("uatom", 6000000), - sdk.NewInt64Coin("uosmo", 12000000), - } - atomPool := preparePool(t, ctx, osmosis, actor, funds2) - - // 16 regen to 1 atom - funds3 := []sdk.Coin{ - sdk.NewInt64Coin("uatom", 6000000), - sdk.NewInt64Coin("uregen", 96000000), - } - regenPool := preparePool(t, ctx, osmosis, actor, funds3) - - return BaseState{ - StarPool: starPool, - AtomPool: atomPool, - RegenPool: regenPool, - } -} - type ReflectExec struct { ReflectMsg *ReflectMsgs `json:"reflect_msg,omitempty"` ReflectSubMsg *ReflectSubMsgs `json:"reflect_sub_msg,omitempty"` diff --git a/wasmbinding/test/custom_query_test.go b/wasmbinding/test/custom_query_test.go index e9cb1b278e6..c6bd6a912e2 100644 --- a/wasmbinding/test/custom_query_test.go +++ b/wasmbinding/test/custom_query_test.go @@ -4,7 +4,6 @@ import ( "encoding/json" "fmt" "os" - "strconv" "testing" "github.com/stretchr/testify/require" @@ -16,7 +15,6 @@ import ( sdk "github.com/cosmos/cosmos-sdk/types" "github.com/osmosis-labs/osmosis/v13/app" - "github.com/osmosis-labs/osmosis/v13/wasmbinding" "github.com/osmosis-labs/osmosis/v13/wasmbinding/bindings" "github.com/osmosis-labs/osmosis/v13/x/gamm/pool-models/balancer" ) @@ -63,200 +61,6 @@ func TestQueryFullDenom(t *testing.T) { require.EqualValues(t, expected, resp.Denom) } -func TestQueryPool(t *testing.T) { - actor := RandomAccountAddress() - osmosis, ctx := SetupCustomApp(t, actor) - - fundAccount(t, ctx, osmosis, actor, defaultFunds) - - poolFunds := []sdk.Coin{ - sdk.NewInt64Coin("uosmo", 12000000), - sdk.NewInt64Coin("ustar", 240000000), - } - // 2 star to 1 osmo - starPool := preparePool(t, ctx, osmosis, actor, poolFunds) - - pool2Funds := []sdk.Coin{ - sdk.NewInt64Coin("uatom", 6000000), - sdk.NewInt64Coin("uosmo", 12000000), - } - // 2 star to 1 osmo - atomPool := preparePool(t, ctx, osmosis, actor, pool2Funds) - - reflect := instantiateReflectContract(t, ctx, osmosis, actor) - require.NotEmpty(t, reflect) - - // query pool state - query := bindings.OsmosisQuery{ - PoolState: &bindings.PoolState{PoolId: starPool}, - } - resp := bindings.PoolStateResponse{} - queryCustom(t, ctx, osmosis, reflect, query, &resp) - expected := wasmbinding.ConvertSdkCoinsToWasmCoins(poolFunds) - require.EqualValues(t, expected, resp.Assets) - assertValidShares(t, resp.Shares, starPool) - - // query second pool state - query = bindings.OsmosisQuery{ - PoolState: &bindings.PoolState{PoolId: atomPool}, - } - resp = bindings.PoolStateResponse{} - queryCustom(t, ctx, osmosis, reflect, query, &resp) - expected = wasmbinding.ConvertSdkCoinsToWasmCoins(pool2Funds) - require.EqualValues(t, expected, resp.Assets) - assertValidShares(t, resp.Shares, atomPool) -} - -func TestQuerySpotPrice(t *testing.T) { - actor := RandomAccountAddress() - osmosis, ctx := SetupCustomApp(t, actor) - swapFee := 0. // FIXME: Set / support an actual fee - epsilon := 1e-6 - - fundAccount(t, ctx, osmosis, actor, defaultFunds) - - poolFunds := []sdk.Coin{ - sdk.NewInt64Coin("uosmo", 12000000), - sdk.NewInt64Coin("ustar", 240000000), - } - // 20 star to 1 osmo - starPool := preparePool(t, ctx, osmosis, actor, poolFunds) - - reflect := instantiateReflectContract(t, ctx, osmosis, actor) - require.NotEmpty(t, reflect) - - // query spot price - query := bindings.OsmosisQuery{ - SpotPrice: &bindings.SpotPrice{ - Swap: bindings.Swap{ - PoolId: starPool, - DenomIn: "ustar", - DenomOut: "uosmo", - }, - WithSwapFee: false, - }, - } - resp := bindings.SpotPriceResponse{} - queryCustom(t, ctx, osmosis, reflect, query, &resp) - - price, err := strconv.ParseFloat(resp.Price, 64) - require.NoError(t, err) - - uosmo, err := poolFunds[0].Amount.ToDec().Float64() - require.NoError(t, err) - ustar, err := poolFunds[1].Amount.ToDec().Float64() - require.NoError(t, err) - - expected := ustar / uosmo - require.InEpsilonf(t, expected, price, epsilon, fmt.Sprintf("Outside of tolerance (%f)", epsilon)) - - // and the reverse conversion (with swap fee) - // query spot price - query = bindings.OsmosisQuery{ - SpotPrice: &bindings.SpotPrice{ - Swap: bindings.Swap{ - PoolId: starPool, - DenomIn: "uosmo", - DenomOut: "ustar", - }, - WithSwapFee: true, - }, - } - resp = bindings.SpotPriceResponse{} - queryCustom(t, ctx, osmosis, reflect, query, &resp) - - price, err = strconv.ParseFloat(resp.Price, 32) - require.NoError(t, err) - - expected = 1. / expected - require.InEpsilonf(t, expected+swapFee, price, epsilon, fmt.Sprintf("Outside of tolerance (%f)", epsilon)) -} - -func TestQueryEstimateSwap(t *testing.T) { - actor := RandomAccountAddress() - osmosis, ctx := SetupCustomApp(t, actor) - epsilon := 2e-3 - - fundAccount(t, ctx, osmosis, actor, defaultFunds) - - poolFunds := []sdk.Coin{ - sdk.NewInt64Coin("uosmo", 12000000), - sdk.NewInt64Coin("ustar", 240000000), - } - // 2 star to 1 osmo - starPool := preparePool(t, ctx, osmosis, actor, poolFunds) - - reflect := instantiateReflectContract(t, ctx, osmosis, actor) - require.NotEmpty(t, reflect) - - // The contract/sender needs to have funds for estimating the price - fundAccount(t, ctx, osmosis, reflect, defaultFunds) - - // Estimate swap rate - uosmo, err := poolFunds[0].Amount.ToDec().Float64() - require.NoError(t, err) - ustar, err := poolFunds[1].Amount.ToDec().Float64() - require.NoError(t, err) - swapRate := ustar / uosmo - - // Query estimate cost (Exact in. No route) - amountIn := sdk.NewInt(10000) - query := bindings.OsmosisQuery{ - EstimateSwap: &bindings.EstimateSwap{ - Sender: reflect.String(), - First: bindings.Swap{ - PoolId: starPool, - DenomIn: "uosmo", - DenomOut: "ustar", - }, - Route: []bindings.Step{}, - Amount: bindings.SwapAmount{ - In: &amountIn, - }, - }, - } - resp := bindings.EstimatePriceResponse{} - queryCustom(t, ctx, osmosis, reflect, query, &resp) - require.NotNil(t, resp.Amount.Out) - require.Nil(t, resp.Amount.In) - cost, err := (*resp.Amount.Out).ToDec().Float64() - require.NoError(t, err) - - amount, err := amountIn.ToDec().Float64() - require.NoError(t, err) - expected := amount * swapRate // out - require.InEpsilonf(t, expected, cost, epsilon, fmt.Sprintf("Outside of tolerance (%f)", epsilon)) - - // And the other way around - // Query estimate cost (Exact out. No route) - amountOut := sdk.NewInt(10000) - query = bindings.OsmosisQuery{ - EstimateSwap: &bindings.EstimateSwap{ - Sender: reflect.String(), - First: bindings.Swap{ - PoolId: starPool, - DenomIn: "uosmo", - DenomOut: "ustar", - }, - Route: []bindings.Step{}, - Amount: bindings.SwapAmount{ - Out: &amountOut, - }, - }, - } - resp = bindings.EstimatePriceResponse{} - queryCustom(t, ctx, osmosis, reflect, query, &resp) - require.NotNil(t, resp.Amount.In) - require.Nil(t, resp.Amount.Out) - cost, err = (*resp.Amount.In).ToDec().Float64() - require.NoError(t, err) - - amount, err = amountOut.ToDec().Float64() - require.NoError(t, err) - expected = amount * 1. / swapRate - require.InEpsilonf(t, expected, cost, epsilon, fmt.Sprintf("Outside of tolerance (%f)", epsilon)) -} - type ReflectQuery struct { Chain *ChainRequest `json:"chain,omitempty"` } diff --git a/wasmbinding/test/messages_test.go b/wasmbinding/test/messages_test.go index 1851cff40f5..7db69dcc92f 100644 --- a/wasmbinding/test/messages_test.go +++ b/wasmbinding/test/messages_test.go @@ -2,7 +2,6 @@ package wasmbinding import ( "fmt" - "math" "testing" sdk "github.com/cosmos/cosmos-sdk/types" @@ -11,7 +10,6 @@ import ( "github.com/osmosis-labs/osmosis/v13/wasmbinding/bindings" "github.com/osmosis-labs/osmosis/v13/x/tokenfactory/types" - "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" ) @@ -414,418 +412,3 @@ func TestBurn(t *testing.T) { }) } } - -func TestSwap(t *testing.T) { - actor := RandomAccountAddress() - osmosis, ctx := SetupCustomApp(t, actor) - epsilon := 1e-3 - - fundAccount(t, ctx, osmosis, actor, defaultFunds) - - poolFunds := []sdk.Coin{ - sdk.NewInt64Coin("uosmo", 12_000_000), - sdk.NewInt64Coin("ustar", 240_000_000), - } - // 20 star to 1 osmo - starPool := preparePool(t, ctx, osmosis, actor, poolFunds) - - // Estimate swap rate - uosmo := poolFunds[0].Amount.ToDec().MustFloat64() - ustar := poolFunds[1].Amount.ToDec().MustFloat64() - swapRate := ustar / uosmo - - amountIn := bindings.ExactIn{ - Input: sdk.NewInt(10000), - MinOutput: sdk.OneInt(), - } - zeroAmountIn := amountIn - zeroAmountIn.Input = sdk.ZeroInt() - negativeAmountIn := amountIn - negativeAmountIn.Input = negativeAmountIn.Input.Neg() - - amountOut := bindings.ExactOut{ - MaxInput: sdk.NewInt(math.MaxInt64), - Output: sdk.NewInt(10000), - } - zeroAmountOut := amountOut - zeroAmountOut.Output = sdk.ZeroInt() - negativeAmountOut := amountOut - negativeAmountOut.Output = negativeAmountOut.Output.Neg() - - amount := amountIn.Input.ToDec().MustFloat64() - starAmount := sdk.NewInt(int64(amount * swapRate)) - - starSwapAmount := bindings.SwapAmount{Out: &starAmount} - - specs := map[string]struct { - swap *bindings.SwapMsg - expCost *bindings.SwapAmount - expErr bool - }{ - "valid swap (exact in)": { - swap: &bindings.SwapMsg{ - First: bindings.Swap{ - PoolId: starPool, - DenomIn: "uosmo", - DenomOut: "ustar", - }, - Route: nil, - Amount: bindings.SwapAmountWithLimit{ - ExactIn: &amountIn, - }, - }, - expCost: &starSwapAmount, - }, - "non-existent pool id": { - swap: &bindings.SwapMsg{ - First: bindings.Swap{ - PoolId: starPool + 4, - DenomIn: "uosmo", - DenomOut: "ustar", - }, - Route: nil, - Amount: bindings.SwapAmountWithLimit{ - ExactIn: &amountIn, - }, - }, - expErr: true, - }, - "zero pool id": { - swap: &bindings.SwapMsg{ - First: bindings.Swap{ - PoolId: 0, - DenomIn: "uosmo", - DenomOut: "ustar", - }, - Route: nil, - Amount: bindings.SwapAmountWithLimit{ - ExactIn: &amountIn, - }, - }, - expErr: true, - }, - "invalid denom in": { - swap: &bindings.SwapMsg{ - First: bindings.Swap{ - PoolId: starPool, - DenomIn: "invalid", - DenomOut: "ustar", - }, - Route: nil, - Amount: bindings.SwapAmountWithLimit{ - ExactIn: &amountIn, - }, - }, - expErr: true, - }, - "empty denom in": { - swap: &bindings.SwapMsg{ - First: bindings.Swap{ - PoolId: starPool, - DenomIn: "", - DenomOut: "ustar", - }, - Route: nil, - Amount: bindings.SwapAmountWithLimit{ - ExactIn: &amountIn, - }, - }, - expErr: true, - }, - "invalid denom out": { - swap: &bindings.SwapMsg{ - First: bindings.Swap{ - PoolId: starPool, - DenomIn: "ustar", - DenomOut: "invalid", - }, - Route: nil, - Amount: bindings.SwapAmountWithLimit{ - ExactIn: &amountIn, - }, - }, - expErr: true, - }, - "empty denom out": { - swap: &bindings.SwapMsg{ - First: bindings.Swap{ - PoolId: starPool, - DenomIn: "ustar", - DenomOut: "", - }, - Route: nil, - Amount: bindings.SwapAmountWithLimit{ - ExactIn: &amountIn, - }, - }, - expErr: true, - }, - "null swap": { - swap: nil, - expErr: true, - }, - "empty swap amount": { - swap: &bindings.SwapMsg{ - First: bindings.Swap{ - PoolId: starPool, - DenomIn: "ustar", - DenomOut: "", - }, - Route: nil, - Amount: bindings.SwapAmountWithLimit{}, - }, - expErr: true, - }, - "zero amount in": { - swap: &bindings.SwapMsg{ - First: bindings.Swap{ - PoolId: starPool, - DenomIn: "uosmo", - DenomOut: "ustar", - }, - Route: nil, - Amount: bindings.SwapAmountWithLimit{ - ExactIn: &zeroAmountIn, - }, - }, - expErr: true, - }, - "zero amount out": { - swap: &bindings.SwapMsg{ - First: bindings.Swap{ - PoolId: starPool, - DenomIn: "uosmo", - DenomOut: "ustar", - }, - Route: nil, - Amount: bindings.SwapAmountWithLimit{ - ExactOut: &zeroAmountOut, - }, - }, - expErr: true, - }, - "negative amount in": { - swap: &bindings.SwapMsg{ - First: bindings.Swap{ - PoolId: starPool, - DenomIn: "uosmo", - DenomOut: "ustar", - }, - Route: nil, - Amount: bindings.SwapAmountWithLimit{ - ExactIn: &negativeAmountIn, - }, - }, - expErr: true, - }, - "negative amount out": { - swap: &bindings.SwapMsg{ - First: bindings.Swap{ - PoolId: starPool, - DenomIn: "uosmo", - DenomOut: "ustar", - }, - Route: nil, - Amount: bindings.SwapAmountWithLimit{ - ExactOut: &negativeAmountOut, - }, - }, - expErr: true, - }, - } - for name, spec := range specs { - t.Run(name, func(t *testing.T) { - // when - gotAmount, gotErr := wasmbinding.PerformSwap(osmosis.GAMMKeeper, ctx, actor, spec.swap) - // then - if spec.expErr { - require.Error(t, gotErr) - return - } - require.NoError(t, gotErr) - assert.InEpsilonf(t, (*spec.expCost.Out).ToDec().MustFloat64(), (*gotAmount.Out).ToDec().MustFloat64(), epsilon, "exp %s but got %s", spec.expCost.Out.String(), gotAmount.Out.String()) - }) - } -} - -func TestSwapMultiHop(t *testing.T) { - actor := RandomAccountAddress() - osmosis, ctx := SetupCustomApp(t, actor) - epsilon := 1e-3 - - fundAccount(t, ctx, osmosis, actor, defaultFunds) - - poolFunds := []sdk.Coin{ - sdk.NewInt64Coin("uosmo", 12_000_000), - sdk.NewInt64Coin("ustar", 240_000_000), - } - // 20 star to 1 osmo - starPool := preparePool(t, ctx, osmosis, actor, poolFunds) - - // 2 osmo to 1 atom - poolFunds2 := []sdk.Coin{ - sdk.NewInt64Coin("uatom", 6_000_000), - sdk.NewInt64Coin("uosmo", 12_000_000), - } - atomPool := preparePool(t, ctx, osmosis, actor, poolFunds2) - - amountIn := bindings.ExactIn{ - Input: sdk.NewInt(1_000_000), - MinOutput: sdk.NewInt(20_000), - } - - // Multi-hop - // Estimate 1st swap rate - uosmo := poolFunds[0].Amount.ToDec().MustFloat64() - ustar := poolFunds[1].Amount.ToDec().MustFloat64() - expectedOut1 := uosmo - uosmo*ustar/(ustar+amountIn.Input.ToDec().MustFloat64()) - - // Estimate 2nd swap rate - uatom2 := poolFunds2[0].Amount.ToDec().MustFloat64() - uosmo2 := poolFunds2[1].Amount.ToDec().MustFloat64() - expectedOut2 := uatom2 - uosmo2*uatom2/(uosmo2+expectedOut1) - - atomAmount := sdk.NewInt(int64(expectedOut2)) - atomSwapAmount := bindings.SwapAmount{Out: &atomAmount} - - specs := map[string]struct { - swap *bindings.SwapMsg - expCost *bindings.SwapAmount - expErr bool - }{ - "valid swap (exact in, 2 step multi-hop)": { - swap: &bindings.SwapMsg{ - First: bindings.Swap{ - PoolId: starPool, - DenomIn: "ustar", - DenomOut: "uosmo", - }, - Route: []bindings.Step{{ - PoolId: atomPool, - DenomOut: "uatom", - }}, - Amount: bindings.SwapAmountWithLimit{ - ExactIn: &amountIn, - }, - }, - expCost: &atomSwapAmount, - }, - "non-existent step pool id": { - swap: &bindings.SwapMsg{ - First: bindings.Swap{ - PoolId: starPool, - DenomIn: "ustar", - DenomOut: "uosmo", - }, - Route: []bindings.Step{{ - PoolId: atomPool + 2, - DenomOut: "uatom", - }}, - Amount: bindings.SwapAmountWithLimit{ - ExactIn: &amountIn, - }, - }, - expErr: true, - }, - "zero step pool id": { - swap: &bindings.SwapMsg{ - First: bindings.Swap{ - PoolId: starPool, - DenomIn: "ustar", - DenomOut: "uosmo", - }, - Route: []bindings.Step{{ - PoolId: 0, - DenomOut: "uatom", - }}, - Amount: bindings.SwapAmountWithLimit{ - ExactIn: &amountIn, - }, - }, - expErr: true, - }, - "wrong step denom out": { - swap: &bindings.SwapMsg{ - First: bindings.Swap{ - PoolId: starPool, - DenomIn: "ustar", - DenomOut: "uosmo", - }, - Route: []bindings.Step{{ - PoolId: atomPool, - DenomOut: "ATOM", - }}, - Amount: bindings.SwapAmountWithLimit{ - ExactIn: &amountIn, - }, - }, - expErr: true, - }, - "self-swap not allowed": { - swap: &bindings.SwapMsg{ - First: bindings.Swap{ - PoolId: starPool, - DenomIn: "ustar", - DenomOut: "uosmo", - }, - Route: []bindings.Step{{ - PoolId: atomPool, - DenomOut: "uosmo", // this is same as the input (output of first swap) - }}, - Amount: bindings.SwapAmountWithLimit{ - ExactIn: &amountIn, - }, - }, - expErr: true, - }, - "invalid step denom out": { - swap: &bindings.SwapMsg{ - First: bindings.Swap{ - PoolId: starPool, - DenomIn: "ustar", - DenomOut: "uosmo", - }, - Route: []bindings.Step{{ - PoolId: atomPool, - DenomOut: "invalid", - }}, - Amount: bindings.SwapAmountWithLimit{ - ExactIn: &amountIn, - }, - }, - expErr: true, - }, - "empty step denom out": { - swap: &bindings.SwapMsg{ - First: bindings.Swap{ - PoolId: starPool, - DenomIn: "ustar", - DenomOut: "uosmo", - }, - Route: []bindings.Step{{ - PoolId: atomPool, - DenomOut: "", - }}, - Amount: bindings.SwapAmountWithLimit{ - ExactIn: &amountIn, - }, - }, - expErr: true, - }, - } - for name, spec := range specs { - t.Run(name, func(t *testing.T) { - // use scratch context to avoid interference between tests - subCtx, _ := ctx.CacheContext() - // when - gotAmount, gotErr := wasmbinding.PerformSwap(osmosis.GAMMKeeper, subCtx, actor, spec.swap) - // then - if spec.expErr { - require.Error(t, gotErr) - return - } - require.NoError(t, gotErr) - assert.InEpsilonf(t, (*spec.expCost.Out).ToDec().MustFloat64(), (*gotAmount.Out).ToDec().MustFloat64(), epsilon, "exp %s but got %s", spec.expCost.Out.String(), gotAmount.Out.String()) - }) - } -} diff --git a/wasmbinding/test/queries_test.go b/wasmbinding/test/queries_test.go index 334418a17d6..90b05128838 100644 --- a/wasmbinding/test/queries_test.go +++ b/wasmbinding/test/queries_test.go @@ -10,7 +10,6 @@ import ( sdk "github.com/cosmos/cosmos-sdk/types" "github.com/osmosis-labs/osmosis/v13/wasmbinding" - "github.com/osmosis-labs/osmosis/v13/wasmbinding/bindings" ) func TestFullDenom(t *testing.T) { @@ -78,7 +77,7 @@ func TestDenomAdmin(t *testing.T) { require.NoError(t, err) require.NotEmpty(t, tfDenom) - queryPlugin := wasmbinding.NewQueryPlugin(app.GAMMKeeper, app.TwapKeeper, app.TokenFactoryKeeper) + queryPlugin := wasmbinding.NewQueryPlugin(app.TokenFactoryKeeper) testCases := []struct { name string @@ -114,428 +113,3 @@ func TestDenomAdmin(t *testing.T) { }) } } - -func TestPoolState(t *testing.T) { - actor := RandomAccountAddress() - osmosis, ctx := SetupCustomApp(t, actor) - - fundAccount(t, ctx, osmosis, actor, defaultFunds) - - poolFunds := []sdk.Coin{ - sdk.NewInt64Coin("uosmo", 12000000), - sdk.NewInt64Coin("ustar", 240000000), - } - // 20 star to 1 osmo - starPool := preparePool(t, ctx, osmosis, actor, poolFunds) - - // FIXME: Derive / obtain these values - starSharesDenom := fmt.Sprintf("gamm/pool/%d", starPool) - starSharedAmount, _ := sdk.NewIntFromString("100_000_000_000_000_000_000") - - queryPlugin := wasmbinding.NewQueryPlugin(osmosis.GAMMKeeper, osmosis.TwapKeeper, osmosis.TokenFactoryKeeper) - - specs := map[string]struct { - poolId uint64 - expPoolState *bindings.PoolAssets - expErr bool - }{ - "existent pool id": { - poolId: starPool, - expPoolState: &bindings.PoolAssets{ - Assets: poolFunds, - Shares: sdk.NewCoin(starSharesDenom, starSharedAmount), - }, - }, - "non-existent pool id": { - poolId: starPool + 1, - expErr: true, - }, - "zero pool id": { - poolId: 0, - expErr: true, - }, - } - for name, spec := range specs { - t.Run(name, func(t *testing.T) { - // when - gotPoolState, gotErr := queryPlugin.GetPoolState(ctx, spec.poolId) - // then - if spec.expErr { - require.Error(t, gotErr) - return - } - require.NoError(t, gotErr) - assert.Equal(t, spec.expPoolState, gotPoolState, "exp %s but got %s", spec.expPoolState, gotPoolState) - }) - } -} - -func TestSpotPrice(t *testing.T) { - actor := RandomAccountAddress() - swapFee := 0. // FIXME: Set / support an actual fee - epsilon := 1e-6 - osmosis, ctx := SetupCustomApp(t, actor) - - fundAccount(t, ctx, osmosis, actor, defaultFunds) - - poolFunds := []sdk.Coin{ - sdk.NewInt64Coin("uosmo", 12000000), - sdk.NewInt64Coin("ustar", 240000000), - } - // 20 star to 1 osmo - starPool := preparePool(t, ctx, osmosis, actor, poolFunds) - - uosmo := poolFunds[0].Amount.ToDec().MustFloat64() - ustar := poolFunds[1].Amount.ToDec().MustFloat64() - - starPrice := sdk.MustNewDecFromStr(fmt.Sprintf("%f", uosmo/ustar)) - starFee := sdk.MustNewDecFromStr(fmt.Sprintf("%f", swapFee)) - starPriceWithFee := starPrice.Add(starFee) - - queryPlugin := wasmbinding.NewQueryPlugin(osmosis.GAMMKeeper, osmosis.TwapKeeper, osmosis.TokenFactoryKeeper) - - specs := map[string]struct { - spotPrice *bindings.SpotPrice - expPrice *sdk.Dec - expErr bool - }{ - "valid spot price": { - spotPrice: &bindings.SpotPrice{ - Swap: bindings.Swap{ - PoolId: starPool, - DenomIn: "uosmo", - DenomOut: "ustar", - }, - WithSwapFee: false, - }, - expPrice: &starPrice, - }, - "valid spot price with fee": { - spotPrice: &bindings.SpotPrice{ - Swap: bindings.Swap{ - PoolId: starPool, - DenomIn: "uosmo", - DenomOut: "ustar", - }, - WithSwapFee: true, - }, - expPrice: &starPriceWithFee, - }, - "non-existent pool id": { - spotPrice: &bindings.SpotPrice{ - Swap: bindings.Swap{ - PoolId: starPool + 2, - DenomIn: "uosmo", - DenomOut: "ustar", - }, - WithSwapFee: false, - }, - expErr: true, - }, - "zero pool id": { - spotPrice: &bindings.SpotPrice{ - Swap: bindings.Swap{ - PoolId: 0, - DenomIn: "uosmo", - DenomOut: "ustar", - }, - WithSwapFee: false, - }, - expErr: true, - }, - "invalid denom in": { - spotPrice: &bindings.SpotPrice{ - Swap: bindings.Swap{ - PoolId: starPool, - DenomIn: "invalid", - DenomOut: "ustar", - }, - WithSwapFee: false, - }, - expErr: true, - }, - "empty denom in": { - spotPrice: &bindings.SpotPrice{ - Swap: bindings.Swap{ - PoolId: starPool, - DenomIn: "", - DenomOut: "ustar", - }, - WithSwapFee: false, - }, - expErr: true, - }, - "invalid denom out": { - spotPrice: &bindings.SpotPrice{ - Swap: bindings.Swap{ - PoolId: starPool, - DenomIn: "uosmo", - DenomOut: "invalid", - }, - WithSwapFee: false, - }, - expErr: true, - }, - "empty denom out": { - spotPrice: &bindings.SpotPrice{ - Swap: bindings.Swap{ - PoolId: starPool, - DenomIn: "uosmo", - DenomOut: "", - }, - WithSwapFee: false, - }, - expErr: true, - }, - "null spot price": { - spotPrice: nil, - expErr: true, - }, - } - for name, spec := range specs { - t.Run(name, func(t *testing.T) { - // when - gotPrice, gotErr := queryPlugin.GetSpotPrice(ctx, spec.spotPrice) - // then - if spec.expErr { - require.Error(t, gotErr) - return - } - require.NoError(t, gotErr) - assert.InEpsilonf(t, spec.expPrice.MustFloat64(), gotPrice.MustFloat64(), epsilon, "exp %s but got %s", spec.expPrice.String(), gotPrice.String()) - }) - } -} - -func TestEstimateSwap(t *testing.T) { - actor := RandomAccountAddress() - osmosis, ctx := SetupCustomApp(t, actor) - epsilon := 1e-3 - - fundAccount(t, ctx, osmosis, actor, defaultFunds) - - poolFunds := []sdk.Coin{ - sdk.NewInt64Coin("uosmo", 12000000), - sdk.NewInt64Coin("ustar", 240000000), - } - // 20 star to 1 osmo - starPool := preparePool(t, ctx, osmosis, actor, poolFunds) - - // Estimate swap rate - uosmo := poolFunds[0].Amount.ToDec().MustFloat64() - ustar := poolFunds[1].Amount.ToDec().MustFloat64() - swapRate := ustar / uosmo - - amountIn := sdk.NewInt(10000) - zeroAmount := sdk.ZeroInt() - negativeAmount := amountIn.Neg() - - amount := amountIn.ToDec().MustFloat64() - starAmount := sdk.NewInt(int64(amount * swapRate)) - - starSwapAmount := bindings.SwapAmount{Out: &starAmount} - - queryPlugin := wasmbinding.NewQueryPlugin(osmosis.GAMMKeeper, osmosis.TwapKeeper, osmosis.TokenFactoryKeeper) - - specs := map[string]struct { - estimateSwap *bindings.EstimateSwap - expCost *bindings.SwapAmount - expErr bool - }{ - "valid estimate swap (exact in)": { - estimateSwap: &bindings.EstimateSwap{ - Sender: actor.String(), - First: bindings.Swap{ - PoolId: starPool, - DenomIn: "uosmo", - DenomOut: "ustar", - }, - Route: nil, - Amount: bindings.SwapAmount{ - In: &amountIn, - }, - }, - expCost: &starSwapAmount, - }, - "non-existent pool id": { - estimateSwap: &bindings.EstimateSwap{ - Sender: actor.String(), - First: bindings.Swap{ - PoolId: starPool + 3, - DenomIn: "uosmo", - DenomOut: "ustar", - }, - Route: nil, - Amount: bindings.SwapAmount{ - In: &amountIn, - }, - }, - expErr: true, - }, - "zero pool id": { - estimateSwap: &bindings.EstimateSwap{ - Sender: actor.String(), - First: bindings.Swap{ - PoolId: 0, - DenomIn: "uosmo", - DenomOut: "ustar", - }, - Route: nil, - Amount: bindings.SwapAmount{ - In: &amountIn, - }, - }, - expErr: true, - }, - "invalid denom in": { - estimateSwap: &bindings.EstimateSwap{ - Sender: actor.String(), - First: bindings.Swap{ - PoolId: starPool, - DenomIn: "invalid", - DenomOut: "ustar", - }, - Route: nil, - Amount: bindings.SwapAmount{ - In: &amountIn, - }, - }, - expErr: true, - }, - "empty denom in": { - estimateSwap: &bindings.EstimateSwap{ - Sender: actor.String(), - First: bindings.Swap{ - PoolId: starPool, - DenomIn: "", - DenomOut: "ustar", - }, - Route: nil, - Amount: bindings.SwapAmount{ - In: &amountIn, - }, - }, - expErr: true, - }, - "invalid denom out": { - estimateSwap: &bindings.EstimateSwap{ - Sender: actor.String(), - First: bindings.Swap{ - PoolId: starPool, - DenomIn: "ustar", - DenomOut: "invalid", - }, - Route: nil, - Amount: bindings.SwapAmount{ - In: &amountIn, - }, - }, - expErr: true, - }, - "empty denom out": { - estimateSwap: &bindings.EstimateSwap{ - Sender: actor.String(), - First: bindings.Swap{ - PoolId: starPool, - DenomIn: "ustar", - DenomOut: "", - }, - Route: nil, - Amount: bindings.SwapAmount{ - In: &amountIn, - }, - }, - expErr: true, - }, - "null estimate swap": { - estimateSwap: nil, - expErr: true, - }, - "empty swap amount": { - estimateSwap: &bindings.EstimateSwap{ - Sender: actor.String(), - First: bindings.Swap{ - PoolId: starPool, - DenomIn: "uosmo", - DenomOut: "ustar", - }, - Route: nil, - Amount: bindings.SwapAmount{}, - }, - expErr: true, - }, - "zero amount in": { - estimateSwap: &bindings.EstimateSwap{ - Sender: actor.String(), - First: bindings.Swap{ - PoolId: starPool, - DenomIn: "uosmo", - DenomOut: "ustar", - }, - Route: nil, - Amount: bindings.SwapAmount{ - In: &zeroAmount, - }, - }, - expErr: true, - }, - "zero amount out": { - estimateSwap: &bindings.EstimateSwap{ - Sender: actor.String(), - First: bindings.Swap{ - PoolId: starPool, - DenomIn: "uosmo", - DenomOut: "ustar", - }, - Route: nil, - Amount: bindings.SwapAmount{ - Out: &zeroAmount, - }, - }, - expErr: true, - }, - "negative amount in": { - estimateSwap: &bindings.EstimateSwap{ - Sender: actor.String(), - First: bindings.Swap{ - PoolId: starPool, - DenomIn: "uosmo", - DenomOut: "ustar", - }, - Route: nil, - Amount: bindings.SwapAmount{ - In: &negativeAmount, - }, - }, - expErr: true, - }, - "negative amount out": { - estimateSwap: &bindings.EstimateSwap{ - Sender: actor.String(), - First: bindings.Swap{ - PoolId: starPool, - DenomIn: "uosmo", - DenomOut: "ustar", - }, - Route: nil, - Amount: bindings.SwapAmount{ - Out: &negativeAmount, - }, - }, - expErr: true, - }, - } - for name, spec := range specs { - t.Run(name, func(t *testing.T) { - // when - gotCost, gotErr := queryPlugin.EstimateSwap(ctx, spec.estimateSwap) - // then - if spec.expErr { - require.Error(t, gotErr) - return - } - require.NoError(t, gotErr) - assert.InEpsilonf(t, (*spec.expCost.Out).ToDec().MustFloat64(), (*gotCost.Out).ToDec().MustFloat64(), epsilon, "exp %s but got %s", spec.expCost.Out.String(), gotCost.Out.String()) - }) - } -} diff --git a/wasmbinding/testdata/download_releases.sh b/wasmbinding/testdata/download_releases.sh deleted file mode 100755 index 89d26ab6b55..00000000000 --- a/wasmbinding/testdata/download_releases.sh +++ /dev/null @@ -1,24 +0,0 @@ -#!/bin/bash -set -o errexit -o nounset -o pipefail -command -v shellcheck > /dev/null && shellcheck "$0" - -if [ $# -ne 1 ]; then - echo "Usage: ./download_releases.sh RELEASE_TAG" - exit 1 -fi - -tag="v1.0.0-beta6" - -for contract in hackatom reflect; do - url="https://github.com/CosmWasm/cosmwasm/releases/download/$tag/${contract}.wasm" - echo "Downloading $url ..." - wget -O "${contract}.wasm" "$url" -done - -tag="$1" -url="https://github.com/osmosis-labs/osmosis-bindings/releases/download/$tag/osmo_reflect.wasm" -echo "Downloading $url ..." -wget -O "osmo_reflect.wasm" "$url" - -rm -f version.txt -echo "$tag" >version.txt \ No newline at end of file diff --git a/wasmbinding/testdata/reflect.wasm b/wasmbinding/testdata/reflect.wasm deleted file mode 100644 index dadaadf7cc3..00000000000 Binary files a/wasmbinding/testdata/reflect.wasm and /dev/null differ diff --git a/wasmbinding/testdata/version.txt b/wasmbinding/testdata/version.txt deleted file mode 100644 index dc6b1f0b600..00000000000 --- a/wasmbinding/testdata/version.txt +++ /dev/null @@ -1 +0,0 @@ -v0.6.0 7414d1b diff --git a/wasmbinding/wasm.go b/wasmbinding/wasm.go index 0d67c535449..91f9abba39f 100644 --- a/wasmbinding/wasm.go +++ b/wasmbinding/wasm.go @@ -9,24 +9,20 @@ import ( bankkeeper "github.com/cosmos/cosmos-sdk/x/bank/keeper" - gammkeeper "github.com/osmosis-labs/osmosis/v13/x/gamm/keeper" tokenfactorykeeper "github.com/osmosis-labs/osmosis/v13/x/tokenfactory/keeper" - twap "github.com/osmosis-labs/osmosis/v13/x/twap" ) func RegisterCustomPlugins( - gammKeeper *gammkeeper.Keeper, bank *bankkeeper.BaseKeeper, - twap *twap.Keeper, tokenFactory *tokenfactorykeeper.Keeper, ) []wasmkeeper.Option { - wasmQueryPlugin := NewQueryPlugin(gammKeeper, twap, tokenFactory) + wasmQueryPlugin := NewQueryPlugin(tokenFactory) queryPluginOpt := wasmkeeper.WithQueryPlugins(&wasmkeeper.QueryPlugins{ Custom: CustomQuerier(wasmQueryPlugin), }) messengerDecoratorOpt := wasmkeeper.WithMessageHandlerDecorator( - CustomMessageDecorator(gammKeeper, bank, tokenFactory), + CustomMessageDecorator(bank, tokenFactory), ) return []wasm.Option{