Skip to content

Commit

Permalink
Add direct swap in wasm binding
Browse files Browse the repository at this point in the history
  • Loading branch information
Victor118 committed Nov 29, 2024
1 parent f1591c7 commit 787df24
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 1 deletion.
39 changes: 39 additions & 0 deletions x/tokenfactory/bindings/message_plugin.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,9 @@ func (m *CustomMessenger) DispatchMsg(ctx sdk.Context, contractAddr sdk.AccAddre
if contractMsg.CreatePool != nil {
return m.createPool(ctx, contractAddr, contractMsg.CreatePool)
}
if contractMsg.DirectSwap != nil {
return m.directSwap(ctx, contractAddr, contractMsg.DirectSwap)
}
}
return m.wrapped.DispatchMsg(ctx, contractAddr, contractIBCPortID, msg)
}
Expand Down Expand Up @@ -114,6 +117,42 @@ func PerformCreateStakedrop(f *tokenfactorykeeper.Keeper, b bankkeeper.Keeper, c
return resp.Marshal()
}

// createDenom creates a new token denom
func (m *CustomMessenger) directSwap(ctx sdk.Context, contractAddr sdk.AccAddress, directSwap *bindingstypes.DirectSwap) ([]sdk.Event, [][]byte, [][]*types.Any, error) {
bz, err := PerformDirectSwap(m.liquidity, m.bank, ctx, contractAddr, directSwap)
if err != nil {
return nil, nil, nil, errorsmod.Wrap(err, "perform create pool")
}
// TODO: double check how this is all encoded to the contract
return nil, [][]byte{bz}, nil, nil
}

// PerformCreateStakedrop is used with createStakedrop to create a stakedrop; validates the msgCreateStakedrop.
func PerformDirectSwap(f *liquiditykeeper.Keeper, b bankkeeper.Keeper, ctx sdk.Context, contractAddr sdk.AccAddress, directSwap *bindingstypes.DirectSwap) ([]byte, error) {
if directSwap == nil {
return nil, wasmvmtypes.InvalidRequest{Err: "direct swap null direct swap"}
}

msgServer := liquiditykeeper.NewMsgServerImpl(*f)

msgDirectSwap := liquiditytypes.NewMsgDirectSwap(contractAddr, directSwap.PoolId, 1, directSwap.OfferCoin, directSwap.DemandCoinDenom, directSwap.OrderPrice)

if err := msgDirectSwap.ValidateBasic(); err != nil {
return nil, errorsmod.Wrap(err, "failed validating MsgCreateStakedrop")
}

// Create denom
resp, err := msgServer.DirectSwap(
ctx,
msgDirectSwap,
)
if err != nil {
return nil, errorsmod.Wrap(err, "creating stakedrop")
}

return resp.Marshal()
}

// createDenom creates a new token denom
func (m *CustomMessenger) createPool(ctx sdk.Context, contractAddr sdk.AccAddress, createPool *bindingstypes.CreatePool) ([]sdk.Event, [][]byte, [][]*types.Any, error) {
bz, err := PerformCreatePool(m.liquidity, m.bank, ctx, contractAddr, createPool)
Expand Down
17 changes: 16 additions & 1 deletion x/tokenfactory/bindings/types/msg.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
package types

import "cosmossdk.io/math"
import (
"cosmossdk.io/math"
"github.com/cosmos/cosmos-sdk/types"
)

type WasmMsg struct {
/// Contracts can create denoms, namespaced under the contract's address.
Expand All @@ -23,6 +26,8 @@ type WasmMsg struct {
CreateStakedrop *CreateStakedrop `json:"create_stakedrop,omitempty"`

CreatePool *CreatePool `json:"create_pool,omitempty"`

DirectSwap *DirectSwap `json:"direct_swap,omitempty"`
}

type CreateStakedrop struct {
Expand Down Expand Up @@ -84,3 +89,13 @@ type CreatePool struct {
Amount2 math.Int `json:"amount2"`
Denom2 string `json:"denom2"`
}

type DirectSwap struct {
SwapRequesterAddress string `json:"swap_requester_address"`
PoolId uint64 `json:"pool_id"`
SwapTypeId uint32 `json:"swap_type_id"`
OfferCoin types.Coin `json:"offer_coin"`
DemandCoinDenom string `json:"demand_coin_denom"`
OfferCoinFee types.Coin `json:"offer_coin_fee"`
OrderPrice math.LegacyDec `json:"order_price"`
}

0 comments on commit 787df24

Please sign in to comment.