-
Notifications
You must be signed in to change notification settings - Fork 0
/
routes.go
270 lines (218 loc) · 7.61 KB
/
routes.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
package types
import (
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/osmosis-labs/osmosis/osmoutils"
authtypes "github.com/cosmos/cosmos-sdk/x/auth/types"
banktypes "github.com/cosmos/cosmos-sdk/x/bank/types"
)
// AccountI defines the account contract that must be fulfilled when
// creating a x/gamm keeper.
type AccountI interface {
GetModuleAccount(ctx sdk.Context, moduleName string) authtypes.ModuleAccountI
NewAccount(sdk.Context, authtypes.AccountI) authtypes.AccountI
GetAccount(ctx sdk.Context, addr sdk.AccAddress) authtypes.AccountI
SetAccount(ctx sdk.Context, acc authtypes.AccountI)
}
// BankI defines the banking contract that must be fulfilled when
// creating a x/gamm keeper.
type BankI interface {
SendCoins(ctx sdk.Context, fromAddr sdk.AccAddress, toAddr sdk.AccAddress, amt sdk.Coins) error
SetDenomMetaData(ctx sdk.Context, denomMetaData banktypes.Metadata)
}
// CommunityPoolI defines the contract needed to be fulfilled for distribution keeper.
type CommunityPoolI interface {
FundCommunityPool(ctx sdk.Context, amount sdk.Coins, sender sdk.AccAddress) error
}
// PoolModuleI is the interface that must be fulfillled by the module
// storing and containing the pools.
type PoolModuleI interface {
InitializePool(ctx sdk.Context, pool PoolI, creatorAddress sdk.AccAddress) error
GetPool(ctx sdk.Context, poolId uint64) (PoolI, error)
GetPools(ctx sdk.Context) ([]PoolI, error)
GetPoolDenoms(ctx sdk.Context, poolId uint64) (denoms []string, err error)
CalculateSpotPrice(
ctx sdk.Context,
poolId uint64,
quoteAssetDenom string,
baseAssetDenom string,
) (price sdk.Dec, err error)
SwapExactAmountIn(
ctx sdk.Context,
sender sdk.AccAddress,
pool PoolI,
tokenIn sdk.Coin,
tokenOutDenom string,
tokenOutMinAmount sdk.Int,
spreadFactor sdk.Dec,
) (sdk.Int, error)
// 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.
CalcOutAmtGivenIn(
ctx sdk.Context,
poolI PoolI,
tokenIn sdk.Coin,
tokenOutDenom string,
spreadFactor sdk.Dec,
) (tokenOut sdk.Coin, err error)
SwapExactAmountOut(
ctx sdk.Context,
sender sdk.AccAddress,
pool PoolI,
tokenInDenom string,
tokenInMaxAmount sdk.Int,
tokenOut sdk.Coin,
spreadFactor sdk.Dec,
) (tokenInAmount sdk.Int, err error)
// 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.
CalcInAmtGivenOut(
ctx sdk.Context,
poolI PoolI,
tokenOut sdk.Coin,
tokenInDenom string,
spreadFactor sdk.Dec,
) (tokenIn sdk.Coin, err error)
// GetTotalPoolLiquidity returns the coins in the pool owned by all LPs
GetTotalPoolLiquidity(ctx sdk.Context, poolId uint64) (sdk.Coins, error)
// ValidatePermissionlessPoolCreationEnabled returns nil if permissionless pool creation in the module is enabled.
// Otherwise, returns an error.
ValidatePermissionlessPoolCreationEnabled(ctx sdk.Context) error
// GetTotalLiquidity returns the total liquidity of all the pools in the module.
GetTotalLiquidity(ctx sdk.Context) (sdk.Coins, error)
}
type PoolIncentivesKeeperI interface {
IsPoolIncentivized(ctx sdk.Context, poolId uint64) bool
}
type MultihopRoute interface {
Length() int
PoolIds() []uint64
IntermediateDenoms() []string
}
type SwapAmountInRoutes []SwapAmountInRoute
func (routes SwapAmountInRoutes) Validate() error {
if len(routes) == 0 {
return ErrEmptyRoutes
}
for _, route := range routes {
err := sdk.ValidateDenom(route.TokenOutDenom)
if err != nil {
return err
}
}
return nil
}
func (routes SwapAmountInRoutes) IntermediateDenoms() []string {
if len(routes) < 2 {
return nil
}
intermediateDenoms := make([]string, 0, len(routes)-1)
for _, route := range routes[:len(routes)-1] {
intermediateDenoms = append(intermediateDenoms, route.TokenOutDenom)
}
return intermediateDenoms
}
func (routes SwapAmountInRoutes) PoolIds() []uint64 {
poolIds := make([]uint64, 0, len(routes))
for _, route := range routes {
poolIds = append(poolIds, route.PoolId)
}
return poolIds
}
func (routes SwapAmountInRoutes) Length() int {
return len(routes)
}
type SwapAmountOutRoutes []SwapAmountOutRoute
func (routes SwapAmountOutRoutes) Validate() error {
if len(routes) == 0 {
return ErrEmptyRoutes
}
for _, route := range routes {
err := sdk.ValidateDenom(route.TokenInDenom)
if err != nil {
return err
}
}
return nil
}
func (routes SwapAmountOutRoutes) IntermediateDenoms() []string {
if len(routes) < 2 {
return nil
}
intermediateDenoms := make([]string, 0, len(routes)-1)
for _, route := range routes[1:] {
intermediateDenoms = append(intermediateDenoms, route.TokenInDenom)
}
return intermediateDenoms
}
func (routes SwapAmountOutRoutes) PoolIds() []uint64 {
poolIds := make([]uint64, 0, len(routes))
for _, route := range routes {
poolIds = append(poolIds, route.PoolId)
}
return poolIds
}
func (routes SwapAmountOutRoutes) Length() int {
return len(routes)
}
// ValidateSwapAmountInSplitRoute validates a slice of SwapAmountInSplitRoute.
//
// returns an error if any of the following are true:
// - the slice is empty
// - any SwapAmountInRoute in the slice is invalid
// - the last TokenOutDenom of any SwapAmountInRoute in the slice does not match the TokenOutDenom of the previous SwapAmountInRoute in the slice
// - there are duplicate SwapAmountInRoutes in the slice
func ValidateSwapAmountInSplitRoute(splitRoutes []SwapAmountInSplitRoute) error {
if len(splitRoutes) == 0 {
return ErrEmptyRoutes
}
// validate every multihop path
previousLastDenomOut := ""
multihopRoutes := make([]SwapAmountInRoutes, 0, len(splitRoutes))
for _, splitRoute := range splitRoutes {
multihopRoute := splitRoute.Pools
err := SwapAmountInRoutes(multihopRoute).Validate()
if err != nil {
return err
}
lastDenomOut := multihopRoute[len(multihopRoute)-1].TokenOutDenom
if previousLastDenomOut != "" && lastDenomOut != previousLastDenomOut {
return InvalidFinalTokenOutError{TokenOutGivenA: previousLastDenomOut, TokenOutGivenB: lastDenomOut}
}
previousLastDenomOut = lastDenomOut
multihopRoutes = append(multihopRoutes, multihopRoute)
}
if osmoutils.ContainsDuplicateDeepEqual(multihopRoutes) {
return ErrDuplicateRoutesNotAllowed
}
return nil
}
// ValidateSwapAmountOutSplitRoute validates a slice of SwapAmountOutSplitRoute and returns an error if any of the following are true:
// - the slice is empty
// - any SwapAmountOutRoute in the slice is invalid
// - the first TokenInDenom of any SwapAmountOutRoute in the slice does not match the TokenInDenom of the previous SwapAmountOutRoute in the slice
// - there are duplicate SwapAmountOutRoutes in the slice
func ValidateSwapAmountOutSplitRoute(splitRoutes []SwapAmountOutSplitRoute) error {
if len(splitRoutes) == 0 {
return ErrEmptyRoutes
}
// validate every multihop path
previousFirstDenomIn := ""
multihopRoutes := make([]SwapAmountOutRoutes, 0, len(splitRoutes))
for _, splitRoute := range splitRoutes {
multihopRoute := splitRoute.Pools
err := SwapAmountOutRoutes(multihopRoute).Validate()
if err != nil {
return err
}
firstDenomIn := multihopRoute[0].TokenInDenom
if previousFirstDenomIn != "" && firstDenomIn != previousFirstDenomIn {
return InvalidFinalTokenOutError{TokenOutGivenA: previousFirstDenomIn, TokenOutGivenB: firstDenomIn}
}
previousFirstDenomIn = firstDenomIn
multihopRoutes = append(multihopRoutes, multihopRoute)
}
if osmoutils.ContainsDuplicateDeepEqual(multihopRoutes) {
return ErrDuplicateRoutesNotAllowed
}
return nil
}