-
Notifications
You must be signed in to change notification settings - Fork 556
/
swap.go
207 lines (179 loc) · 6.36 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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
package keeper
import (
"errors"
"fmt"
errorsmod "cosmossdk.io/errors"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/osmosis-labs/osmosis/v17/x/gamm/types"
"github.com/osmosis-labs/osmosis/v17/x/poolmanager/events"
poolmanagertypes "github.com/osmosis-labs/osmosis/v17/x/poolmanager/types"
)
// swapExactAmountIn is an internal method for swapping an exact amount of tokens
// as input to a pool, using the provided spreadFactor. This is intended to allow
// different spread factors as determined by multi-hops, or when recovering from
// chain liveness failures.
// TODO: investigate if spreadFactor can be unexported
// https://github.com/osmosis-labs/osmosis/issues/3130
func (k Keeper) SwapExactAmountIn(
ctx sdk.Context,
sender sdk.AccAddress,
pool poolmanagertypes.PoolI,
tokenIn sdk.Coin,
tokenOutDenom string,
tokenOutMinAmount sdk.Int,
spreadFactor sdk.Dec,
) (tokenOutAmount sdk.Int, err error) {
if tokenIn.Denom == tokenOutDenom {
return sdk.Int{}, errors.New("cannot trade same denomination in and out")
}
poolSpreadFactor := pool.GetSpreadFactor(ctx)
if spreadFactor.LT(poolSpreadFactor.QuoInt64(2)) {
return sdk.Int{}, fmt.Errorf("given spread factor (%s) must be greater than or equal to half of the pool's spread factor (%s)", spreadFactor, poolSpreadFactor)
}
tokensIn := sdk.Coins{tokenIn}
defer func() {
if r := recover(); r != nil {
tokenOutAmount = sdk.Int{}
err = fmt.Errorf("function swapExactAmountIn failed due to internal reason: %v", r)
}
}()
cfmmPool, err := asCFMMPool(pool)
if err != nil {
return sdk.Int{}, err
}
// Executes the swap in the pool and stores the output. Updates pool assets but
// does not actually transfer any tokens to or from the pool.
tokenOutCoin, err := cfmmPool.SwapOutAmtGivenIn(ctx, tokensIn, tokenOutDenom, spreadFactor)
if err != nil {
return sdk.Int{}, err
}
tokenOutAmount = tokenOutCoin.Amount
if !tokenOutAmount.IsPositive() {
return sdk.Int{}, errorsmod.Wrapf(types.ErrInvalidMathApprox, "token amount must be positive")
}
if tokenOutAmount.LT(tokenOutMinAmount) {
return sdk.Int{}, errorsmod.Wrapf(types.ErrLimitMinAmount, "%s token is lesser than min amount", tokenOutDenom)
}
// Settles balances between the tx sender and the pool to match the swap that was executed earlier.
// Also emits swap event and updates related liquidity metrics
if err := k.updatePoolForSwap(ctx, pool, sender, tokenIn, tokenOutCoin); err != nil {
return sdk.Int{}, err
}
return tokenOutAmount, nil
}
// SwapExactAmountOut is a method for swapping to get an exact number of tokens out of a pool,
// using the provided spreadFactor.
// This is intended to allow different spread factors as determined by multi-hops,
// or when recovering from chain liveness failures.
func (k Keeper) SwapExactAmountOut(
ctx sdk.Context,
sender sdk.AccAddress,
pool poolmanagertypes.PoolI,
tokenInDenom string,
tokenInMaxAmount sdk.Int,
tokenOut sdk.Coin,
spreadFactor sdk.Dec,
) (tokenInAmount sdk.Int, err error) {
if tokenInDenom == tokenOut.Denom {
return sdk.Int{}, errors.New("cannot trade same denomination in and out")
}
defer func() {
if r := recover(); r != nil {
tokenInAmount = sdk.Int{}
err = fmt.Errorf("function swapExactAmountOut failed due to internal reason: %v", r)
}
}()
liquidity, err := k.GetTotalPoolLiquidity(ctx, pool.GetId())
if err != nil {
return sdk.Int{}, err
}
poolOutBal := liquidity.AmountOf(tokenOut.Denom)
if tokenOut.Amount.GTE(poolOutBal) {
return sdk.Int{}, errorsmod.Wrapf(types.ErrTooManyTokensOut,
"can't get more tokens out than there are tokens in the pool")
}
cfmmPool, err := asCFMMPool(pool)
if err != nil {
return sdk.Int{}, err
}
tokenIn, err := cfmmPool.SwapInAmtGivenOut(ctx, sdk.Coins{tokenOut}, tokenInDenom, spreadFactor)
if err != nil {
return sdk.Int{}, err
}
tokenInAmount = tokenIn.Amount
if tokenInAmount.LTE(sdk.ZeroInt()) {
return sdk.Int{}, errorsmod.Wrapf(types.ErrInvalidMathApprox, "token amount is zero or negative")
}
if tokenInAmount.GT(tokenInMaxAmount) {
return sdk.Int{}, errorsmod.Wrapf(types.ErrLimitMaxAmount, "Swap requires %s, which is greater than the amount %s", tokenIn, tokenInMaxAmount)
}
err = k.updatePoolForSwap(ctx, pool, sender, tokenIn, tokenOut)
if err != nil {
return sdk.Int{}, err
}
return tokenInAmount, nil
}
// CalcOutAmtGivenIn calculates the amount of tokenOut given tokenIn and the pool's current state.
// Returns error if the given pool is not a CFMM pool. Returns error on internal calculations.
func (k Keeper) CalcOutAmtGivenIn(
ctx sdk.Context,
poolI poolmanagertypes.PoolI,
tokenIn sdk.Coin,
tokenOutDenom string,
spreadFactor sdk.Dec,
) (tokenOut sdk.Coin, err error) {
cfmmPool, err := asCFMMPool(poolI)
if err != nil {
return sdk.Coin{}, err
}
return cfmmPool.CalcOutAmtGivenIn(ctx, sdk.NewCoins(tokenIn), tokenOutDenom, spreadFactor)
}
// CalcInAmtGivenOut calculates the amount of tokenIn given tokenOut and the pool's current state.
// Returns error if the given pool is not a CFMM pool. Returns error on internal calculations.
func (k Keeper) CalcInAmtGivenOut(
ctx sdk.Context,
poolI poolmanagertypes.PoolI,
tokenOut sdk.Coin,
tokenInDenom string,
spreadFactor sdk.Dec,
) (tokenIn sdk.Coin, err error) {
cfmmPool, err := asCFMMPool(poolI)
if err != nil {
return sdk.Coin{}, err
}
return cfmmPool.CalcInAmtGivenOut(ctx, sdk.NewCoins(tokenOut), tokenInDenom, spreadFactor)
}
// updatePoolForSwap takes a pool, sender, and tokenIn, tokenOut amounts
// It then updates the pool's balances to the new reserve amounts, and
// sends the in tokens from the sender to the pool, and the out tokens from the pool to the sender.
func (k Keeper) updatePoolForSwap(
ctx sdk.Context,
pool poolmanagertypes.PoolI,
sender sdk.AccAddress,
tokenIn sdk.Coin,
tokenOut sdk.Coin,
) error {
tokensIn := sdk.Coins{tokenIn}
tokensOut := sdk.Coins{tokenOut}
err := k.setPool(ctx, pool)
if err != nil {
return err
}
err = k.bankKeeper.SendCoins(ctx, sender, pool.GetAddress(), sdk.Coins{
tokenIn,
})
if err != nil {
return err
}
err = k.bankKeeper.SendCoins(ctx, pool.GetAddress(), sender, sdk.Coins{
tokenOut,
})
if err != nil {
return err
}
events.EmitSwapEvent(ctx, sender, pool.GetId(), tokensIn, tokensOut)
k.hooks.AfterCFMMSwap(ctx, sender, pool.GetId(), tokensIn, tokensOut)
k.RecordTotalLiquidityIncrease(ctx, tokensIn)
k.RecordTotalLiquidityDecrease(ctx, tokensOut)
return err
}