-
Notifications
You must be signed in to change notification settings - Fork 0
/
mappers.go
259 lines (225 loc) · 7.44 KB
/
mappers.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
package dezswap
import (
"fmt"
"sort"
"strings"
"github.com/dezswap/cosmwasm-etl/parser"
"github.com/dezswap/cosmwasm-etl/pkg/eventlog"
ds "github.com/dezswap/cosmwasm-etl/pkg/rules/dezswap"
"github.com/pkg/errors"
)
var _ parser.Mapper = &createPairMapper{}
var _ parser.Mapper = &transferMapper{}
var _ parser.Mapper = &wasmTransferMapper{}
type mapperMixin struct{}
type createPairMapper struct{ mapperMixin }
type initialProvideMapper struct{ mapperMixin }
type transferMapperMixin struct {
mapperMixin
}
type transferMapper struct {
mixin transferMapperMixin
pairSet map[string]parser.Pair
}
type wasmTransferMapper struct {
mixin transferMapperMixin
pairSet map[string]parser.Pair
}
// match implements mapper
func (m *createPairMapper) MatchedToParsedTx(res eventlog.MatchedResult, optionals ...interface{}) (*parser.ParsedTx, error) {
if err := m.mapperMixin.checkResult(res, ds.CreatePairMatchedLen); err != nil {
return nil, errors.Wrap(err, "createPairMapper.MatchedToParsedTx")
}
sortResult(res)
assets := strings.Split(res[ds.FactoryPairIdx].Value, "-")
if len(assets) != 2 {
msg := fmt.Sprintf("expected assets length(%d)", 2)
return nil, errors.New(msg)
}
return &parser.ParsedTx{
Type: parser.CreatePair,
Sender: "",
ContractAddr: res[ds.FactoryPairAddrIdx].Value,
Assets: [2]parser.Asset{
{Addr: assets[0]},
{Addr: assets[1]},
},
LpAddr: res[ds.FactoryLpAddrIdx].Value,
LpAmount: "",
}, nil
}
// match implements mapper
func (m *wasmTransferMapper) MatchedToParsedTx(res eventlog.MatchedResult, optionals ...interface{}) (*parser.ParsedTx, error) {
sortResult(res)
action := res[ds.WasmCommonTransferActionIdx]
switch action.Value {
case ds.WasmTransferAction:
return m.v1MatchedToParsedTx(res, optionals...)
case ds.WasmTransferFromAction:
return m.v2MatchedToParsedTx(res, optionals...)
}
msg := fmt.Sprintf("expected action(%s) or (%s)", ds.WasmTransferAction, ds.WasmTransferFromAction)
return nil, errors.New(msg)
}
func (m *wasmTransferMapper) v1MatchedToParsedTx(res eventlog.MatchedResult, _ ...interface{}) (*parser.ParsedTx, error) {
if err := m.mixin.checkResult(res, ds.WasmV1TransferMatchedLen); err != nil {
return nil, errors.Wrap(err, "wasmTransferMapper.v1MatchedToParsedTx")
}
from := res[ds.WasmTransferFromIdx].Value
to := res[ds.WasmTransferToIdx].Value
pair, fromPair, err := m.mixin.pairBy(m.pairSet, from, to)
if err != nil {
if strings.Contains(err.Error(), "no pair") {
return nil, nil
}
return nil, errors.Wrap(err, "wasmTransferMapper.v1MatchedToParsedTx")
}
return m.matchedToParsedTx(
pair, from, to, res[ds.WasmCommonTransferCw20AddrIdx].Value, res[ds.WasmTransferAmountIdx].Value, fromPair,
)
}
func (m *wasmTransferMapper) v2MatchedToParsedTx(res eventlog.MatchedResult, _ ...interface{}) (*parser.ParsedTx, error) {
if err := m.mixin.checkResult(res, ds.WasmV2TransferMatchedLen); err != nil {
return nil, errors.Wrap(err, "wasmTransferMapper.v2MatchedToParsedTx")
}
from := res[ds.WasmTransferFromFromIdx].Value
to := res[ds.WasmTransferFromToIdx].Value
pair, fromPair, err := m.mixin.pairBy(m.pairSet, from, to)
if err != nil {
if strings.Contains(err.Error(), "no pair") {
return nil, nil
}
return nil, errors.Wrap(err, "wasmTransferMapper.v2MatchedToParsedTx")
}
return m.matchedToParsedTx(
pair, from, to, res[ds.WasmCommonTransferCw20AddrIdx].Value, res[ds.WasmTransferFromAmountIdx].Value, fromPair,
)
}
// match implements mapper
func (m *transferMapper) MatchedToParsedTx(res eventlog.MatchedResult, optionals ...interface{}) (*parser.ParsedTx, error) {
if err := m.mixin.checkResult(res, ds.TransferMatchedLen); err != nil {
return nil, errors.Wrap(err, "transferMapper.MatchedToParsedTx")
}
from := res[ds.TransferSenderIdx].Value
to := res[ds.TransferRecipientIdx].Value
pair, fromPair, err := m.mixin.pairBy(m.pairSet, from, to)
if err != nil {
if strings.Contains(err.Error(), "no pair") {
return nil, nil
}
return nil, errors.Wrap(err, "transferMapper.MatchedToParsedTx")
}
assets := [2]parser.Asset{
{Addr: pair.Assets[0]},
{Addr: pair.Assets[1]},
}
amountStrs := strings.Split(res[ds.TransferAmountIdx].Value, ",")
if len(amountStrs) == 0 {
return nil, errors.New("empty amount or wrong format(amounts separated by ,)")
}
for _, amountStr := range amountStrs {
asset, err := parser.GetAssetFromAmountAssetString(amountStr)
if err != nil {
return nil, errors.Wrap(err, "transferMapper.MatchedToParsedTx")
}
idx := parser.IndexOf(pair.Assets, asset.Addr)
if idx == -1 {
msg := fmt.Sprintf("wrong asset(%s), pair(%s) assets(%s)", asset.Addr, pair.ContractAddr, pair.Assets)
return nil, errors.New(msg)
}
if fromPair {
asset.Amount = fmt.Sprintf("-%s", asset.Amount)
}
assets[idx] = asset
}
return &parser.ParsedTx{
Type: parser.Transfer,
Sender: res[ds.TransferSenderIdx].Value,
ContractAddr: pair.ContractAddr,
Assets: assets,
LpAddr: "",
LpAmount: "",
Meta: map[string]interface{}{
"recipient": to,
},
}, nil
}
func (m *initialProvideMapper) MatchedToParsedTx(res eventlog.MatchedResult, optionals ...interface{}) (*parser.ParsedTx, error) {
if err := m.checkResult(res, ds.PairInitialProvideMatchedLen); err != nil {
return nil, errors.Wrap(err, "initialProvideMapper.MatchedToParsedTx")
}
return &parser.ParsedTx{
Type: parser.InitialProvide,
Sender: "",
ContractAddr: res[ds.PairInitialProvideToIdx].Value,
Assets: [2]parser.Asset{{}, {}},
LpAddr: res[ds.PairInitialProvideAddrIdx].Value,
LpAmount: res[ds.PairInitialProvideAmountIdx].Value,
Meta: nil,
}, nil
}
func (*mapperMixin) checkResult(res eventlog.MatchedResult, expectedLen int) error {
if len(res) != expectedLen {
msg := fmt.Sprintf("expected results length(%d)", expectedLen)
return errors.New(msg)
}
return nil
}
func (*wasmTransferMapper) matchedToParsedTx(pair *parser.Pair, from, to, targetToken, amount string, isFromPair bool) (*parser.ParsedTx, error) {
assets := [2]parser.Asset{
{Addr: pair.Assets[0]},
{Addr: pair.Assets[1]},
}
idx := parser.IndexOf(pair.Assets, targetToken)
if idx == -1 {
msg := fmt.Sprintf("wrong asset(%s), pair(%s) assets(%s)", targetToken, pair.ContractAddr, pair.Assets)
return nil, errors.New(msg)
}
if isFromPair {
assets[idx].Amount = "-" + amount
} else {
assets[idx].Amount = amount
}
return &parser.ParsedTx{
Type: parser.Transfer,
Sender: from,
ContractAddr: pair.ContractAddr,
Assets: assets,
LpAddr: "",
LpAmount: "",
Meta: map[string]interface{}{
"recipient": to,
},
}, nil
}
func (*transferMapperMixin) pairBy(pairSet map[string]parser.Pair, from, to string) (*parser.Pair, bool, error) {
fromPair, fromOk := pairSet[from]
toPair, toOk := pairSet[to]
if !fromOk && !toOk {
msg := fmt.Sprintf("transferMapperMixin.pairBy no pair for (%s, %s)", from, to)
return nil, fromOk, errors.New(msg)
}
if fromOk {
return &fromPair, fromOk, nil
}
return &toPair, fromOk, nil
}
// sortResult sorts the result by key split by "_contract_address"
// @param res will be sorted
func sortResult(res eventlog.MatchedResult) {
const sortSplitter = "_contract_address"
sort := func(from, to int) {
target := res[from:to]
sort.Slice(target, func(i, j int) bool {
return target[i].Key < target[j].Key
})
}
prev := 0
for idx, v := range res {
if v.Key == sortSplitter {
sort(prev, idx)
prev = idx
}
}
sort(prev, len(res))
}