mirror of
https://github.com/0glabs/0g-chain.git
synced 2024-11-10 18:15:19 +00:00
ffef832d45
- Upgrade cosmos-sdk to v0.44.5 from v0.39.2 - Add Legacy Tx Endpoint for backwards compatibility - Add IBC v1.2.3 Support Co-authored-by: DracoLi <draco@dracoli.com> Co-authored-by: drklee3 <derrick@dlee.dev> Co-authored-by: denalimarsh <denalimarsh@gmail.com> Co-authored-by: Draco Li <draco@kava.io> Co-authored-by: Nick DeLuca <nickdeluca08@gmail.com> Co-authored-by: Kevin Davis <karzak@users.noreply.github.com> Co-authored-by: Denali Marsh <denali@kava.io>
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.NewPoolRecordFromPool(pool))
|
|
|
|
if err := k.bankKeeper.SendCoinsFromAccountToModule(ctx, requester, types.ModuleAccountName, sdk.NewCoins(swapInput)); err != nil {
|
|
return err
|
|
}
|
|
|
|
if err := k.bankKeeper.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
|
|
}
|