-
Notifications
You must be signed in to change notification settings - Fork 1
/
keeper.go
375 lines (315 loc) · 13.5 KB
/
keeper.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
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
package keeper
import (
"fmt"
"strconv"
gogotypes "github.com/gogo/protobuf/types"
"cosmossdk.io/core/store"
errorsmod "cosmossdk.io/errors"
"cosmossdk.io/log"
sdkmath "cosmossdk.io/math"
"github.com/cosmos/cosmos-sdk/codec"
sdk "github.com/cosmos/cosmos-sdk/types"
paramstypes "github.com/cosmos/cosmos-sdk/x/params/types"
"github.com/Canto-Network/Canto/v7/x/coinswap/types"
)
// Keeper of the coinswap store
type Keeper struct {
cdc codec.BinaryCodec
storeService store.KVStoreService
bk types.BankKeeper
ak types.AccountKeeper
paramSpace paramstypes.Subspace
feeCollectorName string
blockedAddrs map[string]bool
// the address capable of executing a MsgUpdateParams message. Typically, this
// should be the x/gov module account.
authority string
}
// NewKeeper returns a coinswap keeper. It handles:
// - creating new ModuleAccounts for each trading pair
// - burning and minting liquidity coins
// - sending to and from ModuleAccounts
func NewKeeper(
cdc codec.BinaryCodec,
storeService store.KVStoreService,
paramSpace paramstypes.Subspace,
bk types.BankKeeper,
ak types.AccountKeeper,
blockedAddrs map[string]bool,
feeCollectorName string,
authority string,
) Keeper {
// ensure coinswap module account is set
if addr := ak.GetModuleAddress(types.ModuleName); addr == nil {
panic(fmt.Sprintf("%s module account has not been set", types.ModuleName))
}
// set KeyTable if it has not already been set
if !paramSpace.HasKeyTable() {
paramSpace = paramSpace.WithKeyTable(types.ParamKeyTable())
}
return Keeper{
storeService: storeService,
bk: bk,
ak: ak,
cdc: cdc,
paramSpace: paramSpace,
blockedAddrs: blockedAddrs,
feeCollectorName: feeCollectorName,
authority: authority,
}
}
// GetAuthority returns the x/coinswap module's authority.
func (k Keeper) GetAuthority() string {
return k.authority
}
// Logger returns a module-specific logger.
func (k Keeper) Logger(ctx sdk.Context) log.Logger {
return ctx.Logger().With("module", fmt.Sprintf("x/%s", types.ModuleName))
}
// Swap execute swap order in specified pool
func (k Keeper) Swap(ctx sdk.Context, msg *types.MsgSwapOrder) error {
var amount sdkmath.Int
var err error
standardDenom, err := k.GetStandardDenom(ctx)
if err != nil {
return err
}
isDoubleSwap := (msg.Input.Coin.Denom != standardDenom) && (msg.Output.Coin.Denom != standardDenom)
if isDoubleSwap {
return errorsmod.Wrapf(types.ErrNotContainStandardDenom, "unsupported swap: standard coin must be in either Input or Output")
}
if msg.IsBuyOrder {
amount, err = k.TradeInputForExactOutput(ctx, msg.Input, msg.Output)
} else {
amount, err = k.TradeExactInputForOutput(ctx, msg.Input, msg.Output)
}
if err != nil {
return err
}
ctx.EventManager().EmitEvent(
sdk.NewEvent(
types.EventTypeSwap,
sdk.NewAttribute(types.AttributeValueAmount, amount.String()),
sdk.NewAttribute(types.AttributeValueSender, msg.Input.Address),
sdk.NewAttribute(types.AttributeValueRecipient, msg.Output.Address),
sdk.NewAttribute(types.AttributeValueIsBuyOrder, strconv.FormatBool(msg.IsBuyOrder)),
sdk.NewAttribute(types.AttributeValueTokenPair, types.GetTokenPairByDenom(msg.Input.Coin.Denom, msg.Output.Coin.Denom)),
),
)
return nil
}
// AddLiquidity adds liquidity to the specified pool
func (k Keeper) AddLiquidity(ctx sdk.Context, msg *types.MsgAddLiquidity) (sdk.Coin, error) {
standardDenom, err := k.GetStandardDenom(ctx)
if err != nil {
return sdk.Coin{}, err
}
if standardDenom == msg.MaxToken.Denom {
return sdk.Coin{}, errorsmod.Wrapf(types.ErrInvalidDenom,
"MaxToken: %s should not be StandardDenom", msg.MaxToken.String())
}
params := k.GetParams(ctx)
if !params.MaxSwapAmount.AmountOf(msg.MaxToken.Denom).IsPositive() {
return sdk.Coin{}, errorsmod.Wrapf(types.ErrInvalidDenom,
"MaxToken %s is not registered in max swap amount", msg.MaxToken.Denom)
}
var mintLiquidityAmt sdkmath.Int
var depositToken sdk.Coin
var standardCoin = sdk.NewCoin(standardDenom, msg.ExactStandardAmt)
poolId := types.GetPoolId(msg.MaxToken.Denom)
pool, exists := k.GetPool(ctx, poolId)
sender, err := sdk.AccAddressFromBech32(msg.Sender)
if err != nil {
return sdk.Coin{}, err
}
// calculate amount of UNI to be minted for sender
// and coin amount to be deposited
if !exists {
// deduct the user's fee for creating a Liquidity pool
if err := k.DeductPoolCreationFee(ctx, sender); err != nil {
return sdk.Coin{}, err
}
mintLiquidityAmt = msg.ExactStandardAmt
if mintLiquidityAmt.GT(params.MaxStandardCoinPerPool) {
return sdk.Coin{}, errorsmod.Wrap(types.ErrMaxedStandardDenom, fmt.Sprintf("liquidity amount not met, max standard coin amount: no bigger than %s, actual: %s", params.MaxStandardCoinPerPool.String(), mintLiquidityAmt.String()))
}
if mintLiquidityAmt.LT(msg.MinLiquidity) {
return sdk.Coin{}, errorsmod.Wrap(types.ErrConstraintNotMet, fmt.Sprintf("liquidity amount not met, user expected: no less than %s, actual: %s", msg.MinLiquidity.String(), mintLiquidityAmt.String()))
}
depositToken = sdk.NewCoin(msg.MaxToken.Denom, msg.MaxToken.Amount)
pool = k.CreatePool(ctx, msg.MaxToken.Denom)
} else {
balances, err := k.GetPoolBalances(ctx, pool.EscrowAddress)
if err != nil {
return sdk.Coin{}, err
}
standardReserveAmt := balances.AmountOf(standardDenom)
tokenReserveAmt := balances.AmountOf(msg.MaxToken.Denom)
liquidity := k.bk.GetSupply(ctx, pool.LptDenom).Amount
if liquidity.Equal(sdkmath.ZeroInt()) {
// pool exists, but it is empty
// same with initial liquidity provide
mintLiquidityAmt = msg.ExactStandardAmt
if mintLiquidityAmt.GT(params.MaxStandardCoinPerPool) {
return sdk.Coin{}, errorsmod.Wrap(types.ErrMaxedStandardDenom, fmt.Sprintf("liquidity amount not met, max standard coin amount: no bigger than %s, actual: %s", params.MaxStandardCoinPerPool.String(), mintLiquidityAmt.String()))
}
if mintLiquidityAmt.LT(msg.MinLiquidity) {
return sdk.Coin{}, errorsmod.Wrap(types.ErrConstraintNotMet, fmt.Sprintf("liquidity amount not met, user expected: no less than %s, actual: %s", msg.MinLiquidity.String(), mintLiquidityAmt.String()))
}
depositToken = sdk.NewCoin(msg.MaxToken.Denom, msg.MaxToken.Amount)
} else {
if standardReserveAmt.GTE(params.MaxStandardCoinPerPool) {
return sdk.Coin{}, errorsmod.Wrap(types.ErrMaxedStandardDenom, fmt.Sprintf("pool standard coin is maxed out: %s", params.MaxStandardCoinPerPool.String()))
}
maxStandardInputAmt := sdkmath.MinInt(msg.ExactStandardAmt, params.MaxStandardCoinPerPool.Sub(standardReserveAmt))
mintLiquidityAmt = (liquidity.Mul(maxStandardInputAmt)).Quo(standardReserveAmt)
if mintLiquidityAmt.LT(msg.MinLiquidity) {
return sdk.Coin{}, errorsmod.Wrap(types.ErrConstraintNotMet, fmt.Sprintf("liquidity amount not met, user expected: no less than %s, actual: %s", msg.MinLiquidity.String(), mintLiquidityAmt.String()))
}
depositAmt := (tokenReserveAmt.Mul(maxStandardInputAmt)).Quo(standardReserveAmt).AddRaw(1)
depositToken = sdk.NewCoin(msg.MaxToken.Denom, depositAmt)
standardCoin = sdk.NewCoin(standardDenom, maxStandardInputAmt)
if depositAmt.GT(msg.MaxToken.Amount) {
return sdk.Coin{}, errorsmod.Wrap(types.ErrConstraintNotMet, fmt.Sprintf("token amount not met, user expected: no more than %s, actual: %s", msg.MaxToken.String(), depositToken.String()))
}
}
}
reservePoolAddress, err := sdk.AccAddressFromBech32(pool.EscrowAddress)
if err != nil {
return sdk.Coin{}, err
}
ctx.EventManager().EmitEvent(
sdk.NewEvent(
types.EventTypeAddLiquidity,
sdk.NewAttribute(types.AttributeValueSender, msg.Sender),
sdk.NewAttribute(types.AttributeValueTokenPair, types.GetTokenPairByDenom(msg.MaxToken.Denom, standardDenom)),
),
)
return k.addLiquidity(ctx, sender, reservePoolAddress, standardCoin, depositToken, pool.LptDenom, mintLiquidityAmt)
}
func (k Keeper) addLiquidity(ctx sdk.Context,
sender sdk.AccAddress,
reservePoolAddress sdk.AccAddress,
standardCoin, token sdk.Coin,
lptDenom string,
mintLiquidityAmt sdkmath.Int,
) (sdk.Coin, error) {
depositedTokens := sdk.NewCoins(standardCoin, token)
// transfer deposited token into coinswaps Account
if err := k.bk.SendCoins(ctx, sender, reservePoolAddress, depositedTokens); err != nil {
return sdk.Coin{}, err
}
mintToken := sdk.NewCoin(lptDenom, mintLiquidityAmt)
mintTokens := sdk.NewCoins(mintToken)
if err := k.bk.MintCoins(ctx, types.ModuleName, mintTokens); err != nil {
return sdk.Coin{}, err
}
if err := k.bk.SendCoinsFromModuleToAccount(ctx, types.ModuleName, sender, mintTokens); err != nil {
return sdk.Coin{}, err
}
return mintToken, nil
}
// RemoveLiquidity removes liquidity from the specified pool
func (k Keeper) RemoveLiquidity(ctx sdk.Context, msg *types.MsgRemoveLiquidity) (sdk.Coins, error) {
standardDenom, err := k.GetStandardDenom(ctx)
if err != nil {
return nil, err
}
pool, exists := k.GetPoolByLptDenom(ctx, msg.WithdrawLiquidity.Denom)
if !exists {
return nil, errorsmod.Wrapf(types.ErrReservePoolNotExists, "liquidity pool token: %s", msg.WithdrawLiquidity.Denom)
}
balances, err := k.GetPoolBalances(ctx, pool.EscrowAddress)
if err != nil {
return nil, err
}
lptDenom := msg.WithdrawLiquidity.Denom
minTokenDenom := pool.CounterpartyDenom
standardReserveAmt := balances.AmountOf(standardDenom)
tokenReserveAmt := balances.AmountOf(minTokenDenom)
liquidityReserve := k.bk.GetSupply(ctx, lptDenom).Amount
if standardReserveAmt.LT(msg.MinStandardAmt) {
return nil, errorsmod.Wrap(types.ErrInsufficientFunds, fmt.Sprintf("insufficient %s funds, user expected: %s, actual: %s", standardDenom, msg.MinStandardAmt.String(), standardReserveAmt.String()))
}
if tokenReserveAmt.LT(msg.MinToken) {
return nil, errorsmod.Wrap(types.ErrInsufficientFunds, fmt.Sprintf("insufficient %s funds, user expected: %s, actual: %s", minTokenDenom, msg.MinToken.String(), tokenReserveAmt.String()))
}
if liquidityReserve.LT(msg.WithdrawLiquidity.Amount) {
return nil, errorsmod.Wrap(types.ErrInsufficientFunds, fmt.Sprintf("insufficient %s funds, user expected: %s, actual: %s", lptDenom, msg.WithdrawLiquidity.Amount.String(), liquidityReserve.String()))
}
// calculate amount of UNI to be burned for sender
// and coin amount to be returned
standardWithdrawAmt := msg.WithdrawLiquidity.Amount.Mul(standardReserveAmt).Quo(liquidityReserve)
tokenWithdrawnAmt := msg.WithdrawLiquidity.Amount.Mul(tokenReserveAmt).Quo(liquidityReserve)
standardWithdrawCoin := sdk.NewCoin(standardDenom, standardWithdrawAmt)
tokenWithdrawCoin := sdk.NewCoin(minTokenDenom, tokenWithdrawnAmt)
deductUniCoin := msg.WithdrawLiquidity
if standardWithdrawCoin.Amount.LT(msg.MinStandardAmt) {
return nil, errorsmod.Wrap(types.ErrConstraintNotMet, fmt.Sprintf("iris amount not met, user expected: no less than %s, actual: %s", sdk.NewCoin(standardDenom, msg.MinStandardAmt).String(), standardWithdrawCoin.String()))
}
if tokenWithdrawCoin.Amount.LT(msg.MinToken) {
return nil, errorsmod.Wrap(types.ErrConstraintNotMet, fmt.Sprintf("token amount not met, user expected: no less than %s, actual: %s", sdk.NewCoin(minTokenDenom, msg.MinToken).String(), tokenWithdrawCoin.String()))
}
ctx.EventManager().EmitEvent(
sdk.NewEvent(
types.EventTypeRemoveLiquidity,
sdk.NewAttribute(types.AttributeValueSender, msg.Sender),
sdk.NewAttribute(types.AttributeValueTokenPair, types.GetTokenPairByDenom(minTokenDenom, standardDenom)),
),
)
sender, err := sdk.AccAddressFromBech32(msg.Sender)
if err != nil {
return nil, err
}
poolAddr, err := sdk.AccAddressFromBech32(pool.EscrowAddress)
if err != nil {
return nil, err
}
return k.removeLiquidity(ctx, poolAddr, sender, deductUniCoin, standardWithdrawCoin, tokenWithdrawCoin)
}
func (k Keeper) removeLiquidity(ctx sdk.Context, poolAddr, sender sdk.AccAddress, deductUniCoin, standardWithdrawCoin, tokenWithdrawCoin sdk.Coin) (sdk.Coins, error) {
deltaCoins := sdk.NewCoins(deductUniCoin)
// send liquidity vouchers to be burned from sender account to module account
if err := k.bk.SendCoinsFromAccountToModule(ctx, sender, types.ModuleName, deltaCoins); err != nil {
return nil, err
}
// burn liquidity vouchers of reserve pool from module account
if err := k.bk.BurnCoins(ctx, types.ModuleName, deltaCoins); err != nil {
return nil, err
}
// transfer withdrawn liquidity from coinswap reserve pool account to sender account
coins := sdk.NewCoins(standardWithdrawCoin, tokenWithdrawCoin)
return coins, k.bk.SendCoins(ctx, poolAddr, sender, coins)
}
// GetParams gets the parameters for the coinswap module.
func (k Keeper) GetParams(ctx sdk.Context) types.Params {
var swapParams types.Params
k.paramSpace.GetParamSet(ctx, &swapParams)
return swapParams
}
// SetParams sets the parameters for the coinswap module.
func (k Keeper) SetParams(ctx sdk.Context, params types.Params) {
k.paramSpace.SetParamSet(ctx, ¶ms)
}
// SetStandardDenom sets the standard denom for the coinswap module.
func (k Keeper) SetStandardDenom(ctx sdk.Context, denom string) error {
store := k.storeService.OpenKVStore(ctx)
denomWrap := gogotypes.StringValue{Value: denom}
bz := k.cdc.MustMarshal(&denomWrap)
err := store.Set(types.KeyStandardDenom, bz)
if err != nil {
return err
}
return nil
}
// GetStandardDenom returns the standard denom of the coinswap module.
func (k Keeper) GetStandardDenom(ctx sdk.Context) (string, error) {
store := k.storeService.OpenKVStore(ctx)
bz, err := store.Get(types.KeyStandardDenom)
if len(bz) == 0 {
return "", err
}
var denomWrap = gogotypes.StringValue{}
k.cdc.MustUnmarshal(bz, &denomWrap)
return denomWrap.Value, nil
}