-
Notifications
You must be signed in to change notification settings - Fork 627
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
Custom CosmWasm Integration #1135
Changes from 37 commits
e0328b2
2d3bb29
042e6de
91d75c5
c6ed0f9
d315407
c15dcee
1353907
4f6cf0d
3116a19
56bb64b
904e96c
20d4c7f
d1a0399
ac9b93a
2a21647
797f559
e1ea3a6
00b5070
e54749c
6b354dd
825d67b
61cfb64
37830f1
8586062
12627c1
4ed654b
ada0b18
c4464e2
02cdd8c
e6c6000
17a32d9
3391ca9
15804aa
fe90366
471b92c
98cf636
6a47da2
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -123,6 +123,8 @@ import ( | |
bech32ibckeeper "github.com/osmosis-labs/bech32-ibc/x/bech32ibc/keeper" | ||
bech32ibctypes "github.com/osmosis-labs/bech32-ibc/x/bech32ibc/types" | ||
bech32ics20keeper "github.com/osmosis-labs/bech32-ibc/x/bech32ics20/keeper" | ||
|
||
owasm "github.com/osmosis-labs/osmosis/v7/app/wasm" | ||
) | ||
|
||
type appKeepers struct { | ||
|
@@ -367,7 +369,10 @@ func (app *OsmosisApp) InitNormalKeepers( | |
|
||
// The last arguments can contain custom message handlers, and custom query handlers, | ||
// if we want to allow any custom callbacks | ||
supportedFeatures := "iterator,staking,stargate" | ||
supportedFeatures := "iterator,staking,stargate,osmosis" | ||
|
||
wasmOpts = append(owasm.RegisterCustomPlugins(app.GAMMKeeper, app.BankKeeper), wasmOpts...) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. And here we register the custom plugins to handle those messages |
||
|
||
wasmKeeper := wasm.NewKeeper( | ||
appCodec, | ||
keys[wasm.StoreKey], | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
# CosmWasm support | ||
|
||
This package contains CosmWasm integration points. | ||
|
||
This package provides first class support for: | ||
|
||
* Queries | ||
* Denoms | ||
* Pools | ||
* Prices | ||
* Messages / Execution | ||
* Mint | ||
* Swap | ||
|
||
### Command line interface (CLI) | ||
|
||
* Commands | ||
|
||
```sh | ||
osmosisd tx wasm -h | ||
``` | ||
|
||
* Query | ||
|
||
```sh | ||
osmosisd query wasm -h | ||
``` | ||
|
||
### Tests | ||
|
||
This contains a few high level tests that `x/wasm` is properly integrated. | ||
|
||
Since the code tested is not in this repo, and we are just testing the application | ||
integration (app.go), I figured this is the most suitable location for it. |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
package wasmbindings | ||
|
||
type OsmosisMsg struct { | ||
// /// Contracts can mint native tokens that have an auto-generated denom | ||
// /// namespaced under the contract's address. A contract may create any number | ||
// /// of independent sub-denoms. | ||
// MintTokens *MintTokens `json:"mint_tokens,omitempty"` | ||
/// Swap over one or more pools | ||
Swap *SwapMsg `json:"swap,omitempty"` | ||
} | ||
|
||
// type MintTokens struct { | ||
// /// Must be 2-32 alphanumeric characters | ||
// SubDenom string `json:"sub_denom"` | ||
// Amount sdk.Int `json:"amount"` | ||
// Recipient string `json:"recipient"` | ||
// } | ||
|
||
type SwapMsg struct { | ||
First Swap `json:"first"` | ||
Route []Step `json:"route"` | ||
Amount SwapAmountWithLimit `json:"amount"` | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
package wasmbindings | ||
|
||
import sdk "github.com/cosmos/cosmos-sdk/types" | ||
|
||
type PoolAssets struct { | ||
Assets []sdk.Coin | ||
Shares sdk.Coin | ||
} | ||
Comment on lines
+5
to
+8
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. As part of the refactor, we're trying to convert a lot of instances of There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We can gladly rename everything. Changing the type I easy and this is only used when querying the gamm, so please do suggest new names. We use the Rust definitions as the canonical source for all types that hit the contract boundary, and this is more permanent (breaking API changes), so maybe you can double-check we use the proper names there first. Types that are only used within this package can easily be renamed by anyone There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Awesome! Will review those in more depth then |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
package wasmbindings | ||
|
||
import ( | ||
wasmvmtypes "github.com/CosmWasm/wasmvm/types" | ||
) | ||
|
||
// OsmosisQuery contains osmosis custom queries. | ||
// See https://github.com/confio/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"` | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Should we rename these types to have a Query suffix? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is that the norm across the project? If not, I would leave it as it is. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. That is the norm for all of the golang queries, I don't know whats the norm for the cosmwasm side There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We typically label them the same name as the field. Again, as long as the json stays the same, you can gladly rename the types as you wish in another PR. |
||
/// 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"` | ||
} | ||
|
||
// type FullDenom struct { | ||
// Contract string `json:"contract"` | ||
// SubDenom string `json:"sub_denom"` | ||
// } | ||
|
||
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"` | ||
} | ||
|
||
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"` | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
package wasmbindings | ||
|
||
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"` | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We add the extra feature here, so contract including osmo-bindings are supported here.