mirror of
https://github.com/0glabs/0g-chain.git
synced 2024-12-27 00:35:18 +00:00
20437a91fb
* add message types for swaps * add tx client commands * add test coverage for swap message deadlines * start handler swap tests, export handler result message event into private method, add stubbed keeper methods * add initial swap implementation to get handler tests passing; adds event specific for trades * add handler acceptance test for slippage in exact input and exact output swaps * implement slippage limit for swap keeper methods * add tests to ensure a user can only swap spendable coins * test pool not found, panic on invalid pool, and panic when module account does not have enough funds * validate that the exact output when using for exact swaps is less than the pool liquidity * nit: long line * add validation that swap output is greater than zero * add rest txs for swap messages * nit: lints * dry up swap keeper methods * from pr feedback - spelling and increase clairty around the output amount of a swap rounding to zero
123 lines
3.8 KiB
Go
123 lines
3.8 KiB
Go
package keeper
|
|
|
|
import (
|
|
"fmt"
|
|
|
|
"github.com/kava-labs/kava/x/swap/types"
|
|
|
|
sdk "github.com/cosmos/cosmos-sdk/types"
|
|
sdkerrors "github.com/cosmos/cosmos-sdk/types/errors"
|
|
)
|
|
|
|
// SwapExactForTokens swaps an exact coin a input for a coin b output
|
|
func (k *Keeper) SwapExactForTokens(ctx sdk.Context, requester sdk.AccAddress, exactCoinA, coinB sdk.Coin, slippageLimit sdk.Dec) error {
|
|
poolID, pool, err := k.loadPool(ctx, exactCoinA.Denom, coinB.Denom)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
swapOutput, feePaid := pool.SwapWithExactInput(exactCoinA, k.GetSwapFee(ctx))
|
|
if swapOutput.IsZero() {
|
|
return sdkerrors.Wrapf(types.ErrInsufficientLiquidity, "swap output rounds to zero, increase input amount")
|
|
}
|
|
|
|
priceChange := swapOutput.Amount.ToDec().Quo(coinB.Amount.ToDec())
|
|
if err := k.assertSlippageWithinLimit(priceChange, slippageLimit); err != nil {
|
|
return err
|
|
}
|
|
|
|
if err := k.commitSwap(ctx, poolID, pool, requester, exactCoinA, swapOutput, feePaid, "input"); err != nil {
|
|
return err
|
|
}
|
|
|
|
return nil
|
|
}
|
|
|
|
// SwapForExactTokens swaps a coin a input for an exact coin b output
|
|
func (k *Keeper) SwapForExactTokens(ctx sdk.Context, requester sdk.AccAddress, coinA, exactCoinB sdk.Coin, slippageLimit sdk.Dec) error {
|
|
poolID, pool, err := k.loadPool(ctx, coinA.Denom, exactCoinB.Denom)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
if exactCoinB.Amount.GTE(pool.Reserves().AmountOf(exactCoinB.Denom)) {
|
|
return sdkerrors.Wrapf(
|
|
types.ErrInsufficientLiquidity,
|
|
"output %s >= pool reserves %s", exactCoinB.Amount.String(), pool.Reserves().AmountOf(exactCoinB.Denom).String(),
|
|
)
|
|
}
|
|
|
|
swapInput, feePaid := pool.SwapWithExactOutput(exactCoinB, k.GetSwapFee(ctx))
|
|
|
|
priceChange := coinA.Amount.ToDec().Quo(swapInput.Sub(feePaid).Amount.ToDec())
|
|
if err := k.assertSlippageWithinLimit(priceChange, slippageLimit); err != nil {
|
|
return err
|
|
}
|
|
|
|
if err := k.commitSwap(ctx, poolID, pool, requester, swapInput, exactCoinB, feePaid, "output"); err != nil {
|
|
return err
|
|
}
|
|
|
|
return nil
|
|
}
|
|
|
|
func (k Keeper) loadPool(ctx sdk.Context, denomA string, denomB string) (string, *types.DenominatedPool, error) {
|
|
poolID := types.PoolID(denomA, denomB)
|
|
|
|
poolRecord, found := k.GetPool(ctx, poolID)
|
|
if !found {
|
|
return poolID, nil, sdkerrors.Wrapf(types.ErrInvalidPool, "pool %s not found", poolID)
|
|
}
|
|
|
|
pool, err := types.NewDenominatedPoolWithExistingShares(poolRecord.Reserves(), poolRecord.TotalShares)
|
|
if err != nil {
|
|
panic(fmt.Sprintf("invalid pool %s: %s", poolID, err))
|
|
}
|
|
|
|
return poolID, pool, nil
|
|
}
|
|
|
|
func (k Keeper) assertSlippageWithinLimit(priceChange sdk.Dec, slippageLimit sdk.Dec) error {
|
|
slippage := sdk.OneDec().Sub(priceChange)
|
|
if slippage.GT(slippageLimit) {
|
|
return sdkerrors.Wrapf(types.ErrSlippageExceeded, "slippage %s > limit %s", slippage, slippageLimit)
|
|
}
|
|
|
|
return nil
|
|
}
|
|
|
|
func (k Keeper) commitSwap(
|
|
ctx sdk.Context,
|
|
poolID string,
|
|
pool *types.DenominatedPool,
|
|
requester sdk.AccAddress,
|
|
swapInput sdk.Coin,
|
|
swapOutput sdk.Coin,
|
|
feePaid sdk.Coin,
|
|
exactDirection string,
|
|
) error {
|
|
k.SetPool(ctx, types.NewPoolRecord(pool))
|
|
|
|
if err := k.supplyKeeper.SendCoinsFromAccountToModule(ctx, requester, types.ModuleAccountName, sdk.NewCoins(swapInput)); err != nil {
|
|
return err
|
|
}
|
|
|
|
if err := k.supplyKeeper.SendCoinsFromModuleToAccount(ctx, types.ModuleAccountName, requester, sdk.NewCoins(swapOutput)); err != nil {
|
|
panic(err)
|
|
}
|
|
|
|
ctx.EventManager().EmitEvent(
|
|
sdk.NewEvent(
|
|
types.EventTypeSwapTrade,
|
|
sdk.NewAttribute(types.AttributeKeyPoolID, poolID),
|
|
sdk.NewAttribute(types.AttributeKeyRequester, requester.String()),
|
|
sdk.NewAttribute(types.AttributeKeySwapInput, swapInput.String()),
|
|
sdk.NewAttribute(types.AttributeKeySwapOutput, swapOutput.String()),
|
|
sdk.NewAttribute(types.AttributeKeyFeePaid, feePaid.String()),
|
|
sdk.NewAttribute(types.AttributeKeyExactDirection, exactDirection),
|
|
),
|
|
)
|
|
|
|
return nil
|
|
}
|