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

feat: add the custom handlers for cosmwasm #1997

Merged
merged 30 commits into from
May 25, 2023
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
Show all changes
30 commits
Select commit Hold shift + click to select a range
a98974f
WIP: add the custom handlers for cosmwasm
gsk967 Apr 14, 2023
67ad3e7
Merge branch 'main' into sai/wasm_handler
gsk967 Apr 14, 2023
3046ee0
fix: fix the lint
gsk967 Apr 14, 2023
6642c3d
Merge branch 'main' into sai/wasm_handler
gsk967 Apr 17, 2023
8c2df41
review: address the pr comments
gsk967 Apr 17, 2023
ac87616
rename MsgSupplyCollaterize to Collateral
gsk967 Apr 18, 2023
c5288ac
Merge branch 'main' into sai/wasm_handler
gsk967 Apr 19, 2023
13404a4
Merge branch 'main' into sai/wasm_handler
gsk967 Apr 24, 2023
ab7156b
Merge branch 'main' into sai/wasm_handler
gsk967 Apr 25, 2023
a3c878d
Merge branch 'main' into sai/wasm_handler
gsk967 Apr 26, 2023
ded7f34
Merge branch 'main' into sai/wasm_handler
gsk967 Apr 26, 2023
73729d8
tests: cw20 integration test (#2007)
gsk967 May 1, 2023
b8d9379
Merge branch 'main' into sai/wasm_handler
gsk967 May 1, 2023
964521e
Merge branch 'main' into sai/wasm_handler
gsk967 May 2, 2023
3767abc
Merge branch 'main' into sai/wasm_handler
gsk967 May 4, 2023
c5f579d
fix: fix the supply collateral in wasm leverage tx
gsk967 May 5, 2023
ed5dca8
Merge branch 'main' into sai/wasm_handler
gsk967 May 5, 2023
8330260
fix: fix the leverage custom msg execute by wasm
gsk967 May 5, 2023
bb55e58
Merge remote-tracking branch 'origin' into sai/wasm_handler
gsk967 May 5, 2023
b44ba19
Merge remote-tracking branch 'origin/sai/wasm_handler' into sai/wasm_…
gsk967 May 5, 2023
2832c7c
Merge branch 'main' into sai/wasm_handler
gsk967 May 8, 2023
22654fe
Merge branch 'main' into sai/wasm_handler
gsk967 May 10, 2023
cca9e21
Merge branch 'main' into sai/wasm_handler
gsk967 May 15, 2023
21de827
Merge branch 'main' into sai/wasm_handler
gsk967 May 17, 2023
79c7f37
Merge branch 'main' into sai/wasm_handler
gsk967 May 19, 2023
27fd80b
Merge branch 'main' into sai/wasm_handler
gsk967 May 20, 2023
8245c72
Merge branch 'main' into sai/wasm_handler
gsk967 May 22, 2023
785f190
Merge branch 'main' into sai/wasm_handler
gsk967 May 22, 2023
ae810d6
Merge branch 'main' into sai/wasm_handler
gsk967 May 24, 2023
1ef846e
chore: remove types using from umee wasm queries
gsk967 May 24, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions app/experimental_appconfig.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ import (
servertypes "github.com/cosmos/cosmos-sdk/server/types"
storetypes "github.com/cosmos/cosmos-sdk/store/types"
govv1beta1 "github.com/cosmos/cosmos-sdk/x/gov/types/v1beta1"

umeewasm "github.com/umee-network/umee/v4/app/wasm"
)

const (
Expand Down Expand Up @@ -71,6 +73,9 @@ func (app *UmeeApp) customKeepers(

app.wasmCfg = wasmConfig

// Registering the custom plugins (leverage and oracle queries and msgs)
wasmOpts = append(umeewasm.RegisterCustomPlugins(app.LeverageKeeper, app.OracleKeeper), wasmOpts...)

// The last arguments can contain custom message handlers, and custom query handlers,
// if we want to allow any custom callbacks
availableCapabilities := "iterator,staking,stargate,cosmwasm_1_1"
Expand Down
31 changes: 31 additions & 0 deletions app/wasm/custom_plugins.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package wasm

import (
"github.com/CosmWasm/wasmd/x/wasm"
wasmkeeper "github.com/CosmWasm/wasmd/x/wasm/keeper"

"github.com/umee-network/umee/v4/app/wasm/msg"
"github.com/umee-network/umee/v4/app/wasm/query"
leveragekeeper "github.com/umee-network/umee/v4/x/leverage/keeper"
oraclekeeper "github.com/umee-network/umee/v4/x/oracle/keeper"
)

// RegisterCustomPlugins expose the queries and msgs of native modules to wasm.
func RegisterCustomPlugins(
leverageKeeper leveragekeeper.Keeper,
oracleKeeper oraclekeeper.Keeper,
) []wasmkeeper.Option {
wasmQueryPlugin := query.NewQueryPlugin(leverageKeeper, oracleKeeper)
queryPluginOpt := wasmkeeper.WithQueryPlugins(&wasmkeeper.QueryPlugins{
Custom: wasmQueryPlugin.CustomQuerier(),
})

messagePluginOpt := wasmkeeper.WithMessageHandlerDecorator(func(old wasmkeeper.Messenger) wasmkeeper.Messenger {
return msg.NewMessagePlugin(leverageKeeper)
})

return []wasm.Option{
queryPluginOpt,
messagePluginOpt,
}
}
88 changes: 88 additions & 0 deletions app/wasm/msg/handler_leverage.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
package msg

import (
"context"

"github.com/gogo/protobuf/proto"

lvtypes "github.com/umee-network/umee/v4/x/leverage/types"
)

// HandleSupply handles the Supply value of an address.
func (m UmeeMsg) HandleSupply(
ctx context.Context,
s lvtypes.MsgServer,
) (proto.Message, error) {
req := &lvtypes.MsgSupply{Supplier: m.Supply.Supplier, Asset: m.Supply.Asset}
return s.Supply(ctx, req)
}

// HandleWithdraw handles the Withdraw value of an address.
func (m UmeeMsg) HandleWithdraw(
ctx context.Context,
s lvtypes.MsgServer,
) (proto.Message, error) {
req := &lvtypes.MsgWithdraw{Supplier: m.Withdraw.Supplier, Asset: m.Withdraw.Asset}
return s.Withdraw(ctx, req)
}

// HandleCollateralize handles the enable selected uTokens as collateral.
func (m UmeeMsg) HandleCollateralize(
ctx context.Context,
s lvtypes.MsgServer,
) (proto.Message, error) {
req := &lvtypes.MsgCollateralize{Borrower: m.Collateralize.Borrower, Asset: m.Collateralize.Asset}
return s.Collateralize(ctx, req)
}

// HandleDecollateralize handles the disable amount of an selected uTokens
// as collateral.
func (m UmeeMsg) HandleDecollateralize(
ctx context.Context,
s lvtypes.MsgServer,
) (proto.Message, error) {
req := &lvtypes.MsgDecollateralize{Borrower: m.Decollateralize.Borrower, Asset: m.Decollateralize.Asset}
return s.Decollateralize(ctx, req)
}

// HandleBorrow handles the borrowing coins from the capital facility.
func (m UmeeMsg) HandleBorrow(
ctx context.Context,
s lvtypes.MsgServer,
) (proto.Message, error) {
req := &lvtypes.MsgBorrow{Borrower: m.Borrow.Borrower, Asset: m.Borrow.Asset}
return s.Borrow(ctx, req)
}

// HandleRepay handles repaying borrowed coins to the capital facility.
func (m UmeeMsg) HandleRepay(
ctx context.Context,
s lvtypes.MsgServer,
) (proto.Message, error) {
req := &lvtypes.MsgRepay{Borrower: m.Repay.Borrower, Asset: m.Repay.Asset}
return s.Repay(ctx, req)
}

// HandleLiquidate handles the repaying a different user's borrowed coins
// to the capital facility in exchange for some of their collateral.
func (m UmeeMsg) HandleLiquidate(
ctx context.Context,
s lvtypes.MsgServer,
) (proto.Message, error) {
req := &lvtypes.MsgLiquidate{
Liquidator: m.Liquidate.Liquidator,
Borrower: m.Liquidate.Borrower,
Repayment: m.Liquidate.Repayment,
RewardDenom: m.Liquidate.RewardDenom,
}
return s.Liquidate(ctx, req)
}

// HandleSupplyCollateral handles the supply the assets and collateral their assets.
func (m UmeeMsg) HandleSupplyCollateral(
ctx context.Context,
s lvtypes.MsgServer,
) (proto.Message, error) {
req := &lvtypes.MsgSupplyCollateral{Supplier: m.SupplyCollateralize.Supplier, Asset: m.Supply.Asset}
gsk967 marked this conversation as resolved.
Show resolved Hide resolved
return s.SupplyCollateral(ctx, req)
}
73 changes: 73 additions & 0 deletions app/wasm/msg/plugin.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
package msg

import (
"encoding/json"

sdkerrors "cosmossdk.io/errors"
wasmkeeper "github.com/CosmWasm/wasmd/x/wasm/keeper"
wasmvmtypes "github.com/CosmWasm/wasmvm/types"
sdk "github.com/cosmos/cosmos-sdk/types"

lvkeeper "github.com/umee-network/umee/v4/x/leverage/keeper"
lvtypes "github.com/umee-network/umee/v4/x/leverage/types"
)

// Plugin wraps the msg plugin with Messengers.
type Plugin struct {
lvMsgServer lvtypes.MsgServer
wrapped wasmkeeper.Messenger
}

func NewMessagePlugin(leverageKeeper lvkeeper.Keeper) *Plugin {
return &Plugin{
lvMsgServer: lvkeeper.NewMsgServerImpl(leverageKeeper),
}
}

// DispatchCustomMsg responsible for handling custom messages (umee native messages).
func (plugin *Plugin) DispatchCustomMsg(ctx sdk.Context, rawMsg json.RawMessage) error {
var smartcontractMessage UmeeMsg
if err := json.Unmarshal(rawMsg, &smartcontractMessage); err != nil {
return sdkerrors.Wrap(err, "umee custom msg")
}

var err error
sdkCtx := sdk.WrapSDKContext(ctx)

switch smartcontractMessage.AssignedMsg {
case AssignedMsgSupply:
_, err = smartcontractMessage.HandleSupply(sdkCtx, plugin.lvMsgServer)
case AssignedMsgWithdraw:
_, err = smartcontractMessage.HandleWithdraw(sdkCtx, plugin.lvMsgServer)
case AssignedMsgCollateralize:
_, err = smartcontractMessage.HandleCollateralize(sdkCtx, plugin.lvMsgServer)
case AssignedMsgDecollateralize:
_, err = smartcontractMessage.HandleDecollateralize(sdkCtx, plugin.lvMsgServer)
case AssignedMsgBorrow:
_, err = smartcontractMessage.HandleBorrow(sdkCtx, plugin.lvMsgServer)
case AssignedMsgRepay:
_, err = smartcontractMessage.HandleRepay(sdkCtx, plugin.lvMsgServer)
case AssignedMsgLiquidate:
_, err = smartcontractMessage.HandleLiquidate(sdkCtx, plugin.lvMsgServer)
case AssignedMsgSupplyCollateralize:
gsk967 marked this conversation as resolved.
Show resolved Hide resolved
_, err = smartcontractMessage.HandleSupplyCollateral(sdkCtx, plugin.lvMsgServer)

default:
err = wasmvmtypes.UnsupportedRequest{Kind: "invalid assigned umee msg"}
}

return err
}

// DispatchMsg encodes the wasmVM message and dispatches it.
func (plugin *Plugin) DispatchMsg(
ctx sdk.Context,
contractAddr sdk.AccAddress,
contractIBCPortID string,
msg wasmvmtypes.CosmosMsg,
) (events []sdk.Event, data [][]byte, err error) {
if msg.Custom != nil {
return nil, nil, plugin.DispatchCustomMsg(ctx, msg.Custom)
}
return plugin.wrapped.DispatchMsg(ctx, contractAddr, contractIBCPortID, msg)
}
60 changes: 60 additions & 0 deletions app/wasm/msg/types.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
package msg

import (
lvtypes "github.com/umee-network/umee/v4/x/leverage/types"
)

// AssignedMsg defines the msg to be called.
type AssignedMsg uint16

const (
// AssignedMsgSupply represents the call to supply coins to the capital facility.
AssignedMsgSupply AssignedMsg = iota + 0
gsk967 marked this conversation as resolved.
Show resolved Hide resolved
// AssignedMsgWithdraw represents the call to withdraw previously loaned coins
// from the capital facility.
AssignedMsgWithdraw
// AssignedMsgCollateralize represents the call to enable an amount of
// selected uTokens as collateral.
AssignedMsgCollateralize
// AssignedMsgDecollateralize represents the call to disable amount of
// an selected uTokens as collateral.
AssignedMsgDecollateralize
// AssignedMsgBorrow represents the call to borrowing coins from the
// capital facility.
AssignedMsgBorrow
// AssignedMsgRepay represents the call to repaying borrowed coins to
// the capital facility.
AssignedMsgRepay
// AssignedMsgLiquidate represents the call to repaying a different user's
// borrowed coins to the capital facility in exchange for some of their
// collateral.
AssignedMsgLiquidate
// AssignedMsgSupplyCollateralize represents the call to supply and collateralize their assets.
AssignedMsgSupplyCollateralize
gsk967 marked this conversation as resolved.
Show resolved Hide resolved
AssignedMsgMaxWithdraw
)

// UmeeMsg wraps all the messages availables for cosmwasm smartcontracts.
type UmeeMsg struct {
// Mandatory field to determine which msg to call.
AssignedMsg AssignedMsg `json:"assigned_msg"`
// Used to supply coins to the capital facility.
Supply *lvtypes.MsgSupply `json:"supply,omitempty"`
// Used to withdraw previously loaned coins from the capital facility.
Withdraw *lvtypes.MsgWithdraw `json:"withdraw,omitempty"`
// Used to enable an amount of selected uTokens as collateral.
Collateralize *lvtypes.MsgCollateralize `json:"collateralize,omitempty"`
// Used to disable amount of an selected uTokens as collateral.
Decollateralize *lvtypes.MsgDecollateralize `json:"decollateralize,omitempty"`
// Used to borrowing coins from the capital facility.
Borrow *lvtypes.MsgBorrow `json:"borrow,omitempty"`
// Used to repaying borrowed coins to the capital facility.
Repay *lvtypes.MsgRepay `json:"repay,omitempty"`
// Used to repaying a different user's borrowed coins
// to the capital facility in exchange for some of their collateral.
Liquidate *lvtypes.MsgLiquidate `json:"liquidate,omitempty"`
// Used to do supply and collateralize their assets.
SupplyCollateralize *lvtypes.MsgSupplyCollateral `json:"supply_collateralize,omitempty"`
gsk967 marked this conversation as resolved.
Show resolved Hide resolved
// Used to do withdraw maximum assets by supplier.
AssignedMsgMaxWithdraw *lvtypes.MsgMaxWithdraw `json:"max_withdraw,omitempty"`
}
77 changes: 77 additions & 0 deletions app/wasm/query/handle_leverage.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
package query

import (
"context"

"github.com/gogo/protobuf/proto"

lvtypes "github.com/umee-network/umee/v4/x/leverage/types"
)

// HandleLeverageParams handles the get the x/leverage module's parameters.
func (q UmeeQuery) HandleLeverageParams(
ctx context.Context,
qs lvtypes.QueryServer,
) (proto.Message, error) {
return qs.Params(ctx, &lvtypes.QueryParams{})
}

// HandleRegisteredTokens handles the get all registered tokens query and response.
func (q UmeeQuery) HandleRegisteredTokens(
ctx context.Context,
qs lvtypes.QueryServer,
) (proto.Message, error) {
return qs.RegisteredTokens(ctx, &lvtypes.QueryRegisteredTokens{})
}

// HandleMarketSummary queries a base asset's current borrowing and supplying conditions.
func (q UmeeQuery) HandleMarketSummary(
ctx context.Context,
qs lvtypes.QueryServer,
) (proto.Message, error) {
return qs.MarketSummary(ctx, &lvtypes.QueryMarketSummary{Denom: q.MarketSummary.Denom})
}

// HandleAccountBalances queries an account's current supply, collateral, and borrow positions.
func (q UmeeQuery) HandleAccountBalances(
ctx context.Context,
qs lvtypes.QueryServer,
) (proto.Message, error) {
req := &lvtypes.QueryAccountBalances{Address: q.AccountBalances.Address}
return qs.AccountBalances(ctx, req)
}

// HandleAccountSummary queries USD values representing an account's total
// positions and borrowing limits. It requires oracle prices to return successfully.
func (q UmeeQuery) HandleAccountSummary(
ctx context.Context,
qs lvtypes.QueryServer,
) (proto.Message, error) {
req := &lvtypes.QueryAccountSummary{Address: q.AccountSummary.Address}
return qs.AccountSummary(ctx, req)
}

// HandleLiquidationTargets queries a list of all borrower account addresses eligible for liquidation.
func (q UmeeQuery) HandleLiquidationTargets(
ctx context.Context,
qs lvtypes.QueryServer,
) (proto.Message, error) {
return qs.LiquidationTargets(ctx, &lvtypes.QueryLiquidationTargets{})
}

// HandleBadDebts queries bad debts.
func (q UmeeQuery) HandleBadDebts(
ctx context.Context,
qs lvtypes.QueryServer,
) (proto.Message, error) {
return qs.BadDebts(ctx, &lvtypes.QueryBadDebts{})
}

// HandleBadDebts queries bad debts.
func (q UmeeQuery) HandleMaxWithdraw(
ctx context.Context,
qs lvtypes.QueryServer,
) (proto.Message, error) {
req := &lvtypes.QueryMaxWithdraw{Address: q.MaxWithdraw.Address, Denom: q.MaxWithdraw.Denom}
return qs.MaxWithdraw(ctx, req)
}
Loading