Skip to content

Commit

Permalink
Add trade functions to Keeper and refactor Swap
Browse files Browse the repository at this point in the history
function
  • Loading branch information
Dreamer committed Nov 6, 2023
1 parent 2a726b5 commit 3bad579
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 19 deletions.
35 changes: 16 additions & 19 deletions modules/coinswap/keeper/keeper.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ type Keeper struct {
feeCollectorName string
authority string
blockedAddrs map[string]bool
tradeFuncs map[types.TradeType]types.TradeFunc
}

// NewKeeper returns a coinswap keeper. It handles:
Expand All @@ -44,7 +45,7 @@ func NewKeeper(
panic(fmt.Sprintf("%s module account has not been set", types.ModuleName))
}

return Keeper{
k := Keeper{
storeKey: key,
bk: bk,
ak: ak,
Expand All @@ -53,6 +54,13 @@ func NewKeeper(
feeCollectorName: feeCollectorName,
authority: authority,
}
k.tradeFuncs = map[types.TradeType]types.TradeFunc{
types.Buy: k.TradeInputForExactOutput,
types.Sell: k.TradeExactInputForOutput,
types.BuyDouble: k.doubleTradeInputForExactOutput,
types.SellDouble: k.doubleTradeExactInputForOutput,
}
return k
}

// Logger returns a module-specific logger.
Expand All @@ -62,22 +70,12 @@ func (k Keeper) Logger(ctx sdk.Context) log.Logger {

// Swap execute swap order in specified pool
func (k Keeper) Swap(ctx sdk.Context, msg *types.MsgSwapOrder) (sdk.Coin, error) {
var amount sdk.Coin
var err error

standardDenom := k.GetStandardDenom(ctx)
isDoubleSwap := (msg.Input.Coin.Denom != standardDenom) &&
(msg.Output.Coin.Denom != standardDenom)

if msg.IsBuyOrder && isDoubleSwap {
amount, err = k.doubleTradeInputForExactOutput(ctx, msg.Input, msg.Output)
} else if msg.IsBuyOrder && !isDoubleSwap {
amount, err = k.TradeInputForExactOutput(ctx, msg.Input, msg.Output)
} else if !msg.IsBuyOrder && isDoubleSwap {
amount, err = k.doubleTradeExactInputForOutput(ctx, msg.Input, msg.Output)
} else if !msg.IsBuyOrder && !isDoubleSwap {
amount, err = k.TradeExactInputForOutput(ctx, msg.Input, msg.Output)
}
tradeType := types.GetTradeType(msg.IsBuyOrder,
k.GetStandardDenom(ctx),
msg.Input.Coin.Denom,
msg.Output.Coin.Denom,
)
amount, err := k.tradeFuncs[tradeType](ctx, msg.Input, msg.Output)
if err != nil {
return amount, err
}
Expand All @@ -95,7 +93,6 @@ func (k Keeper) Swap(ctx sdk.Context, msg *types.MsgSwapOrder) (sdk.Coin, error)
),
),
)

return amount, nil
}

Expand Down Expand Up @@ -650,4 +647,4 @@ func (k Keeper) removeUnilateralLiquidity(ctx sdk.Context,
coins := sdk.NewCoins(sdk.NewCoin(targetTokenDenom, targetTokenAmtAfterFee))

return coins, k.bk.SendCoins(ctx, poolAddr, sender, coins)
}
}
34 changes: 34 additions & 0 deletions modules/coinswap/types/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,40 @@ import (
sdk "github.com/cosmos/cosmos-sdk/types"
)

const (
Buy TradeType = "buy"
Sell TradeType = "sell"
BuyDouble TradeType = "buyDouble"
SellDouble TradeType = "SellDouble"
)

type TradeType string
type TradeFunc func(ctx sdk.Context, input Input, output Output) (sdk.Coin, error)

// GetTradeType returns the trade type based on the given parameters.
//
// Parameters:
// - isBuyOrder: a boolean indicating whether the order is a buy order.
// - standardDenom: the standard denomination.
// - inputDenom: the input denomination.
// - outputDenom: the output denomination.
//
// Returns:
// - TradeType: the trade type based on the given parameters.
func GetTradeType(isBuyOrder bool, standardDenom, inputDenom, outputDenom string) TradeType {
isDoubleSwap := (inputDenom != standardDenom) && (outputDenom != standardDenom)
if isBuyOrder && isDoubleSwap {
return BuyDouble
}
if isBuyOrder && !isDoubleSwap {
return Buy
}
if isDoubleSwap {
return SellDouble
}
return Sell
}

// GetReservePoolAddr returns the pool address for the provided liquidity denomination.
func GetReservePoolAddr(lptDenom string) sdk.AccAddress {
return sdk.AccAddress(crypto.AddressHash([]byte(lptDenom)))
Expand Down

0 comments on commit 3bad579

Please sign in to comment.