-
Notifications
You must be signed in to change notification settings - Fork 558
/
routes.go
169 lines (133 loc) · 4.34 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
package types
import (
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/osmosis-labs/osmosis/osmoutils"
)
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
}