-
Notifications
You must be signed in to change notification settings - Fork 13
/
routable_transmuter_pool.go
123 lines (99 loc) · 4.3 KB
/
routable_transmuter_pool.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
package pools
import (
"fmt"
"cosmossdk.io/math"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/osmosis-labs/sqs/domain"
"github.com/osmosis-labs/sqs/sqsdomain"
"github.com/osmosis-labs/osmosis/osmomath"
cwpoolmodel "github.com/osmosis-labs/osmosis/v21/x/cosmwasmpool/model"
poolmanagertypes "github.com/osmosis-labs/osmosis/v21/x/poolmanager/types"
)
var _ sqsdomain.RoutablePool = &routableTransmuterPoolImpl{}
type routableTransmuterPoolImpl struct {
ChainPool *cwpoolmodel.CosmWasmPool "json:\"pool\""
Balances sdk.Coins "json:\"balances\""
TokenOutDenom string "json:\"token_out_denom\""
TakerFee osmomath.Dec "json:\"taker_fee\""
SpreadFactor osmomath.Dec "json:\"spread_factor\""
}
// GetId implements sqsdomain.RoutablePool.
func (r *routableTransmuterPoolImpl) GetId() uint64 {
return r.ChainPool.PoolId
}
// GetPoolDenoms implements sqsdomain.RoutablePool.
func (r *routableTransmuterPoolImpl) GetPoolDenoms() []string {
return r.Balances.Denoms()
}
// GetType implements sqsdomain.RoutablePool.
func (*routableTransmuterPoolImpl) GetType() poolmanagertypes.PoolType {
return poolmanagertypes.CosmWasm
}
// GetSpreadFactor implements sqsdomain.RoutablePool.
func (r *routableTransmuterPoolImpl) GetSpreadFactor() math.LegacyDec {
return r.SpreadFactor
}
// CalculateTokenOutByTokenIn implements sqsdomain.RoutablePool.
// It calculates the amount of token out given the amount of token in for a transmuter pool.
// Transmuter pool allows no slippage swaps. It just returns the same amount of token out as token in
// Returns error if:
// - the underlying chain pool set on the routable pool is not of transmuter type
// - the token in amount is greater than the balance of the token in
// - the token in amount is greater than the balance of the token out
func (r *routableTransmuterPoolImpl) CalculateTokenOutByTokenIn(tokenIn sdk.Coin) (sdk.Coin, error) {
poolType := r.GetType()
// Esnure that the pool is concentrated
if poolType != poolmanagertypes.CosmWasm {
return sdk.Coin{}, domain.InvalidPoolTypeError{PoolType: int32(poolType)}
}
balances := r.Balances
// Validate token in balance
if err := validateBalance(tokenIn.Amount, balances, tokenIn.Denom); err != nil {
return sdk.Coin{}, err
}
// Validate token out balance
if err := validateBalance(tokenIn.Amount, balances, r.TokenOutDenom); err != nil {
return sdk.Coin{}, err
}
// No slippage swaps - just return the same amount of token out as token in
// as long as there is enough liquidity in the pool.
return sdk.NewCoin(r.TokenOutDenom, tokenIn.Amount), nil
}
// GetTokenOutDenom implements RoutablePool.
func (r *routableTransmuterPoolImpl) GetTokenOutDenom() string {
return r.TokenOutDenom
}
// String implements sqsdomain.RoutablePool.
func (r *routableTransmuterPoolImpl) String() string {
return fmt.Sprintf("pool (%d), pool type (%d), pool denoms (%v), token out (%s)", r.ChainPool.PoolId, poolmanagertypes.CosmWasm, r.GetPoolDenoms(), r.TokenOutDenom)
}
// ChargeTakerFeeExactIn implements sqsdomain.RoutablePool.
// Returns tokenInAmount and does not charge any fee for transmuter pools.
func (r *routableTransmuterPoolImpl) ChargeTakerFeeExactIn(tokenIn sdk.Coin) (inAmountAfterFee sdk.Coin) {
return tokenIn
}
// validateBalance validates that the balance of the denom to validate is greater than the token in amount.
// Returns nil on success, error otherwise.
func validateBalance(tokenInAmount osmomath.Int, balances sdk.Coins, denomToValidate string) error {
balanceToValidate := balances.AmountOf(denomToValidate)
if tokenInAmount.GT(balanceToValidate) {
return domain.TransmuterInsufficientBalanceError{
Denom: denomToValidate,
BalanceAmount: balanceToValidate.String(),
Amount: tokenInAmount.String(),
}
}
return nil
}
// GetTakerFee implements sqsdomain.RoutablePool.
func (r *routableTransmuterPoolImpl) GetTakerFee() math.LegacyDec {
return r.TakerFee
}
// SetTokenOutDenom implements sqsdomain.RoutablePool.
func (r *routableTransmuterPoolImpl) SetTokenOutDenom(tokenOutDenom string) {
r.TokenOutDenom = tokenOutDenom
}
// CalcSpotPrice implements sqsdomain.RoutablePool.
func (r *routableTransmuterPoolImpl) CalcSpotPrice(baseDenom string, quoteDenom string) (osmomath.BigDec, error) {
return osmomath.OneBigDec(), nil
}