-
Notifications
You must be signed in to change notification settings - Fork 371
/
swap.go
122 lines (97 loc) · 3.79 KB
/
swap.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
package keeper
import (
"fmt"
"github.com/kava-labs/kava/x/swap/types"
errorsmod "cosmossdk.io/errors"
sdk "github.com/cosmos/cosmos-sdk/types"
)
// 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 errorsmod.Wrapf(types.ErrInsufficientLiquidity, "swap output rounds to zero, increase input amount")
}
priceChange := sdk.NewDecFromInt(swapOutput.Amount).Quo(sdk.NewDecFromInt(coinB.Amount))
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 errorsmod.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 := sdk.NewDecFromInt(coinA.Amount).Quo(sdk.NewDecFromInt(swapInput.Sub(feePaid).Amount))
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, errorsmod.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 errorsmod.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
}