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

added wasm bindings to fetch price per asset #839

Merged
merged 16 commits into from
Sep 1, 2023
2 changes: 1 addition & 1 deletion app/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -842,7 +842,7 @@ func New(
}
supportedFeatures := "iterator,staking,stargate,comdex,cosmwasm_1_1"

wasmOpts = append(cwasm.RegisterCustomPlugins(&app.LockerKeeper, &app.TokenmintKeeper, &app.AssetKeeper, &app.Rewardskeeper, &app.CollectorKeeper, &app.LiquidationKeeper, &app.AuctionKeeper, &app.EsmKeeper, &app.VaultKeeper, &app.LendKeeper, &app.LiquidityKeeper), wasmOpts...)
wasmOpts = append(cwasm.RegisterCustomPlugins(&app.LockerKeeper, &app.TokenmintKeeper, &app.AssetKeeper, &app.Rewardskeeper, &app.CollectorKeeper, &app.LiquidationKeeper, &app.AuctionKeeper, &app.EsmKeeper, &app.VaultKeeper, &app.LendKeeper, &app.LiquidityKeeper, &app.MarketKeeper), wasmOpts...)

app.WasmKeeper = wasmkeeper.NewKeeper(
app.cdc,
Expand Down
9 changes: 9 additions & 0 deletions app/wasm/bindings/query.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ type ComdexQuery struct {
CheckBorrowed *CheckBorrowed `json:"check_borrowed,omitempty"`
CheckLiquidityProvided *CheckLiquidityProvided `json:"check_liquidity_provided,omitempty"`
GetPoolByApp *GetPoolByApp `json:"get_pool_by_app,omitempty"`
GetAssetPrice *GetAssetPrice `json:"Get_Asset_Price,omitempty"`
}

type AppData struct {
Expand Down Expand Up @@ -291,3 +292,11 @@ type GetPoolByApp struct {
type GetPoolByAppResponse struct {
Pools []uint64 `json:"pools"`
}

type GetAssetPrice struct {
AssetID uint64 `json:"asset_id"`
}

type GetAssetPriceResponse struct {
Price uint64 `json:"price"`
}
15 changes: 13 additions & 2 deletions app/wasm/queries.go
Original file line number Diff line number Diff line change
@@ -1,18 +1,18 @@
package wasm

import (
sdk "github.com/cosmos/cosmos-sdk/types"

assetKeeper "github.com/comdex-official/comdex/x/asset/keeper"
collectorkeeper "github.com/comdex-official/comdex/x/collector/keeper"
esmKeeper "github.com/comdex-official/comdex/x/esm/keeper"
lendKeeper "github.com/comdex-official/comdex/x/lend/keeper"
liquidationKeeper "github.com/comdex-official/comdex/x/liquidation/keeper"
liquidityKeeper "github.com/comdex-official/comdex/x/liquidity/keeper"
lockerkeeper "github.com/comdex-official/comdex/x/locker/keeper"
marketKeeper "github.com/comdex-official/comdex/x/market/keeper"
rewardsKeeper "github.com/comdex-official/comdex/x/rewards/keeper"
tokenMintKeeper "github.com/comdex-official/comdex/x/tokenmint/keeper"
vaultKeeper "github.com/comdex-official/comdex/x/vault/keeper"
sdk "github.com/cosmos/cosmos-sdk/types"
)

type QueryPlugin struct {
Expand All @@ -26,6 +26,7 @@ type QueryPlugin struct {
vaultKeeper *vaultKeeper.Keeper
lendKeeper *lendKeeper.Keeper
liquidityKeeper *liquidityKeeper.Keeper
marketKeeper *marketKeeper.Keeper
}

func NewQueryPlugin(
Expand All @@ -39,6 +40,7 @@ func NewQueryPlugin(
vaultKeeper *vaultKeeper.Keeper,
lendKeeper *lendKeeper.Keeper,
liquidityKeeper *liquidityKeeper.Keeper,
marketKeeper *marketKeeper.Keeper,
) *QueryPlugin {
return &QueryPlugin{
assetKeeper: assetKeeper,
Expand All @@ -51,6 +53,7 @@ func NewQueryPlugin(
vaultKeeper: vaultKeeper,
lendKeeper: lendKeeper,
liquidityKeeper: liquidityKeeper,
marketKeeper: marketKeeper,
}
}

Expand Down Expand Up @@ -203,3 +206,11 @@ func (qp QueryPlugin) WasmGetPools(ctx sdk.Context, appID uint64) (pools []uint6
}
return pools
}

func (qp QueryPlugin) WasmGetAssetPrice(ctx sdk.Context, assetID uint64) (twa uint64, found bool) {
assetTwa, found := qp.marketKeeper.GetTwa(ctx, assetID)
if found && assetTwa.IsPriceActive{
return assetTwa.Twa, true
}
return 0, false
}
11 changes: 11 additions & 0 deletions app/wasm/query_plugin.go
Original file line number Diff line number Diff line change
Expand Up @@ -355,6 +355,17 @@ func CustomQuerier(queryPlugin *QueryPlugin) func(ctx sdk.Context, request json.
return nil, sdkerrors.Wrap(err, "GetPoolByApp query response")
}
return bz, nil
} else if comdexQuery.GetAssetPrice != nil {
assetID := comdexQuery.GetAssetPrice.AssetID
assetPrice, _ := queryPlugin.WasmGetAssetPrice(ctx, assetID)
res := bindings.GetAssetPriceResponse{
Price: assetPrice,
}
bz, err := json.Marshal(res)
if err != nil {
return nil, sdkerrors.Wrap(err, "GetAssetPrice query response")
}
return bz, nil
}
return nil, wasmvmtypes.UnsupportedRequest{Kind: "unknown App Data query variant"}
}
Expand Down
27 changes: 18 additions & 9 deletions app/wasm/test/messages_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,8 @@ func TestWhitelistAssetLocker(t *testing.T) {
&comdex.EsmKeeper,
&comdex.VaultKeeper,
&comdex.LendKeeper,
&comdex.LiquidityKeeper)
&comdex.LiquidityKeeper,
&comdex.MarketKeeper)
for _, tc := range []struct {
name string
msg *bindings.MsgWhiteListAssetLocker
Expand Down Expand Up @@ -65,7 +66,8 @@ func TestAddMsgAddExtendedPairsVault(t *testing.T) {
&comdex.EsmKeeper,
&comdex.VaultKeeper,
&comdex.LendKeeper,
&comdex.LiquidityKeeper)
&comdex.LiquidityKeeper,
&comdex.MarketKeeper)
for _, tc := range []struct {
name string
msg *bindings.MsgAddExtendedPairsVault
Expand Down Expand Up @@ -122,7 +124,8 @@ func TestMsgSetCollectorLookupTable(t *testing.T) {
&comdex.EsmKeeper,
&comdex.VaultKeeper,
&comdex.LendKeeper,
&comdex.LiquidityKeeper)
&comdex.LiquidityKeeper,
&comdex.MarketKeeper)
for _, tc := range []struct {
name string
msg *bindings.MsgSetCollectorLookupTable
Expand Down Expand Up @@ -173,7 +176,8 @@ func TestMsgSetAuctionMappingForApp(t *testing.T) {
&comdex.EsmKeeper,
&comdex.VaultKeeper,
&comdex.LendKeeper,
&comdex.LiquidityKeeper)
&comdex.LiquidityKeeper,
&comdex.MarketKeeper)
for _, tc := range []struct {
name string
msg *bindings.MsgSetAuctionMappingForApp
Expand Down Expand Up @@ -223,7 +227,8 @@ func TestMsgUpdateCollectorLookupTable(t *testing.T) {
&comdex.EsmKeeper,
&comdex.VaultKeeper,
&comdex.LendKeeper,
&comdex.LiquidityKeeper)
&comdex.LiquidityKeeper,
&comdex.MarketKeeper)
for _, tc := range []struct {
name string
msg *bindings.MsgUpdateCollectorLookupTable
Expand Down Expand Up @@ -274,7 +279,8 @@ func TestMsgUpdatePairsVault(t *testing.T) {
&comdex.EsmKeeper,
&comdex.VaultKeeper,
&comdex.LendKeeper,
&comdex.LiquidityKeeper)
&comdex.LiquidityKeeper,
&comdex.MarketKeeper)
for _, tc := range []struct {
name string
msg *bindings.MsgUpdatePairsVault
Expand Down Expand Up @@ -329,7 +335,8 @@ func TestMsgWhitelistAppIDLiquidation(t *testing.T) {
&comdex.EsmKeeper,
&comdex.VaultKeeper,
&comdex.LendKeeper,
&comdex.LiquidityKeeper)
&comdex.LiquidityKeeper,
&comdex.MarketKeeper)
for _, tc := range []struct {
name string
msg *bindings.MsgWhitelistAppIDLiquidation
Expand Down Expand Up @@ -376,7 +383,8 @@ func TestMsgRemoveWhitelistAppIDLiquidation(t *testing.T) {
&comdex.EsmKeeper,
&comdex.VaultKeeper,
&comdex.LendKeeper,
&comdex.LiquidityKeeper)
&comdex.LiquidityKeeper,
&comdex.MarketKeeper)
for _, tc := range []struct {
name string
msg *bindings.MsgRemoveWhitelistAppIDLiquidation
Expand Down Expand Up @@ -419,7 +427,8 @@ func TestMsgAddAuctionParams(t *testing.T) {
&comdex.EsmKeeper,
&comdex.VaultKeeper,
&comdex.LendKeeper,
&comdex.LiquidityKeeper)
&comdex.LiquidityKeeper,
&comdex.MarketKeeper)
for _, tc := range []struct {
name string
msg *bindings.MsgAddAuctionParams
Expand Down
4 changes: 3 additions & 1 deletion app/wasm/wasm.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import (
rewardsKeeper "github.com/comdex-official/comdex/x/rewards/keeper"
tokenMintkeeper "github.com/comdex-official/comdex/x/tokenmint/keeper"
vaultKeeper "github.com/comdex-official/comdex/x/vault/keeper"
marketKeeper "github.com/comdex-official/comdex/x/market/keeper"
)

func RegisterCustomPlugins(
Expand All @@ -29,8 +30,9 @@ func RegisterCustomPlugins(
vault *vaultKeeper.Keeper,
lend *lendKeeper.Keeper,
liquidity *liquidityKeeper.Keeper,
market *marketKeeper.Keeper,
) []wasmkeeper.Option {
comdexQueryPlugin := NewQueryPlugin(asset, locker, tokenMint, rewards, collector, liquidation, esm, vault, lend, liquidity)
comdexQueryPlugin := NewQueryPlugin(asset, locker, tokenMint, rewards, collector, liquidation, esm, vault, lend, liquidity, market)

appDataQueryPluginOpt := wasmkeeper.WithQueryPlugins(&wasmkeeper.QueryPlugins{
Custom: CustomQuerier(comdexQueryPlugin),
Expand Down
Loading
Loading