-
Notifications
You must be signed in to change notification settings - Fork 48
/
abci.go
195 lines (176 loc) · 5.65 KB
/
abci.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
package keeper
import (
"encoding/json"
"strings"
"time"
"cosmossdk.io/math"
"github.com/cosmos/cosmos-sdk/telemetry"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/elys-network/elys/x/amm/types"
)
func (k Keeper) GetStackedSlippage(ctx sdk.Context, poolId uint64) sdk.Dec {
pool, found := k.GetPool(ctx, poolId)
if !found {
return sdk.ZeroDec()
}
snapshot := k.GetPoolSnapshotOrSet(ctx, pool)
return pool.StackedRatioFromSnapshot(ctx, k.oracleKeeper, &snapshot)
}
func (k Keeper) ApplySwapRequest(ctx sdk.Context, msg sdk.Msg) error {
switch msg := msg.(type) {
case *types.MsgSwapExactAmountIn:
sender, err := sdk.AccAddressFromBech32(msg.Sender)
if err != nil {
return err
}
recipient, err := sdk.AccAddressFromBech32(msg.Recipient)
if err != nil {
recipient = sender
}
_, _, _, err = k.RouteExactAmountIn(ctx, sender, recipient, msg.Routes, msg.TokenIn, math.Int(msg.TokenOutMinAmount), msg.Discount)
if err != nil {
return err
}
return nil
case *types.MsgSwapExactAmountOut:
sender, err := sdk.AccAddressFromBech32(msg.Sender)
if err != nil {
return err
}
recipient, err := sdk.AccAddressFromBech32(msg.Recipient)
if err != nil {
recipient = sender
}
_, _, _, err = k.RouteExactAmountOut(ctx, sender, recipient, msg.Routes, msg.TokenInMaxAmount, msg.TokenOut, msg.Discount)
if err != nil {
return err
}
return nil
default:
return types.ErrInvalidSwapMsgType
}
}
func (k Keeper) DeleteSwapRequest(ctx sdk.Context, msg sdk.Msg, index uint64) {
switch msg := msg.(type) {
case *types.MsgSwapExactAmountIn:
k.DeleteSwapExactAmountInRequest(ctx, msg, index)
case *types.MsgSwapExactAmountOut:
k.DeleteSwapExactAmountOutRequest(ctx, msg, index)
}
}
func (k Keeper) SelectOneSwapRequest(ctx sdk.Context, sprefix []byte) (sdk.Msg, uint64) {
msg1, index := k.GetFirstSwapExactAmountInRequest(ctx, sprefix)
if index != 0 {
return msg1, index
}
msg2, index := k.GetFirstSwapExactAmountOutRequest(ctx, sprefix)
return msg2, index
}
func (k Keeper) SelectReverseSwapRequest(ctx sdk.Context, msg sdk.Msg) (sdk.Msg, uint64) {
sprefix := []byte{}
switch msg := msg.(type) {
case *types.MsgSwapExactAmountIn:
sprefix = types.TKeyPrefixSwapExactAmountInPrefix(msg)
case *types.MsgSwapExactAmountOut:
sprefix = types.TKeyPrefixSwapExactAmountOutPrefix(msg)
}
split := strings.Split(string(sprefix), "/")
for i, j := 0, len(split)-1; i < j; i, j = i+1, j-1 {
split[i], split[j] = split[j], split[i]
}
rprefix := strings.Join(split, "/")
return k.SelectOneSwapRequest(ctx, []byte(rprefix))
}
func (k Keeper) FirstPoolId(msg sdk.Msg) uint64 {
switch msg := msg.(type) {
case *types.MsgSwapExactAmountIn:
return types.FirstPoolIdFromSwapExactAmountIn(msg)
case *types.MsgSwapExactAmountOut:
return types.FirstPoolIdFromSwapExactAmountOut(msg)
}
return 0
}
func (k Keeper) ExecuteSwapRequests(ctx sdk.Context) []sdk.Msg {
// Algorithm
// - Select a random swap request
// - Try execution on cache context, and check stacked slippage
// - Check if opposite direction request exists (Same pool id with opposite in/out tokens)
// - If opposite direction request exists, try execution on cache context, and check stacked slippage
// - Apply the swap request which as lower stacked slippage
// - If one of the swaps fail, not apply any changes and remove the swap request
// - Repeat the process until the swap requests run-out
requests := []sdk.Msg{}
for {
var index1, index2 uint64
var msg1, msg2 sdk.Msg
msg1, index1 = k.SelectOneSwapRequest(ctx, []byte{})
if index1 == 0 {
break
}
msg2, index2 = k.SelectReverseSwapRequest(ctx, msg1)
if index2 == 0 {
cachedCtx, write := ctx.CacheContext()
err := k.ApplySwapRequest(cachedCtx, msg1)
if err == nil {
write()
}
// remove msg1 from the store
k.DeleteSwapRequest(ctx, msg1, index1)
requests = append(requests, msg1)
continue
}
poolId := k.FirstPoolId(msg1)
cachedCtx1, write1 := ctx.CacheContext()
err1 := k.ApplySwapRequest(cachedCtx1, msg1)
stackedSlippage1 := k.GetStackedSlippage(cachedCtx1, poolId)
cachedCtx2, write2 := ctx.CacheContext()
err2 := k.ApplySwapRequest(cachedCtx2, msg2)
stackedSlippage2 := k.GetStackedSlippage(cachedCtx2, poolId)
if err1 == nil && err2 == nil {
if stackedSlippage1.LT(stackedSlippage2) {
write1()
// remove msg1 from the store
k.DeleteSwapRequest(ctx, msg1, index1)
requests = append(requests, msg1)
} else {
write2()
// remove msg2 from the store
k.DeleteSwapRequest(ctx, msg2, index2)
requests = append(requests, msg2)
}
} else if err1 == nil {
// remove msg2 from the store
k.DeleteSwapRequest(ctx, msg2, index2)
requests = append(requests, msg2)
} else if err2 == nil {
// remove msg1 from the store
k.DeleteSwapRequest(ctx, msg1, index1)
requests = append(requests, msg1)
} else {
// remove both msg1, msg2 messages
k.DeleteSwapRequest(ctx, msg1, index1)
k.DeleteSwapRequest(ctx, msg2, index2)
requests = append(requests, msg1, msg2)
}
}
return requests
}
func (k Keeper) ClearOutdatedSlippageTrack(ctx sdk.Context) {
params := k.GetParams(ctx)
tracks := k.AllSlippageTracks(ctx)
for _, track := range tracks {
if track.Timestamp+params.SlippageTrackDuration < uint64(ctx.BlockTime().Unix()) {
k.DeleteSlippageTrack(ctx, track)
}
}
}
// EndBlocker of amm module
func (k Keeper) EndBlocker(ctx sdk.Context) {
defer telemetry.ModuleMeasureSince(types.ModuleName, time.Now(), telemetry.MetricKeyBeginBlocker)
msgs := k.ExecuteSwapRequests(ctx)
if len(msgs) > 0 {
bz, _ := json.Marshal(msgs)
k.Logger(ctx).Debug("Executed swap requests: " + string(bz))
}
k.ClearOutdatedSlippageTrack(ctx)
}