-
Notifications
You must be signed in to change notification settings - Fork 18
/
result.go
143 lines (124 loc) · 4.49 KB
/
result.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
// Copyright (C) 2023 Gobalsky Labs Limited
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as
// published by the Free Software Foundation, either version 3 of the
// License, or (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.
package ethcall
import (
"bytes"
"context"
"encoding/json"
"fmt"
"strconv"
"text/scanner"
ethcallcommon "code.vegaprotocol.io/vega/core/datasource/external/ethcall/common"
"github.com/PaesslerAG/gval"
"github.com/PaesslerAG/jsonpath"
)
type Result struct {
Bytes []byte
Values []any
Normalised map[string]string
PassesFilters bool
}
func NewResult(spec ethcallcommon.Spec, bytes []byte) (Result, error) {
call, err := NewCall(spec)
if err != nil {
return Result{}, fmt.Errorf("failed to create result: %w", err)
}
return newResult(call, bytes)
}
func newResult(call Call, bytes []byte) (Result, error) {
values, err := call.abi.Unpack(call.method, bytes)
if err != nil {
return Result{}, fmt.Errorf("failed to unpack contract call result: %w", err)
}
normalised, err := normaliseValues(values, call.spec.Normalisers)
if err != nil {
return Result{}, fmt.Errorf("failed to normalise contract call result: %w", err)
}
passesFilters, err := call.filters.Match(normalised)
if err != nil {
return Result{}, fmt.Errorf("error evaluating filters: %w", err)
}
return Result{
Bytes: bytes,
Values: values,
Normalised: normalised,
PassesFilters: passesFilters,
}, nil
}
func normaliseValues(values []any, rules map[string]string) (map[string]string, error) {
// The data in 'values' is relatively well typed, after being unpacked by the ABI.
// For example, a uint256 will be a big.Int, structs returned from the contract call
// will be anonymous go structs rather than a string map etc.. In order to fish data
// out with jsonpath it need to be simple lists, maps, strings and numbers; so we
// serialise to json and then deserialise to an into an []interface{}.
valuesJson, err := json.Marshal(values)
if err != nil {
return nil, fmt.Errorf("unable to serialse values: %v", err)
}
valuesSimple := []interface{}{}
d := json.NewDecoder(bytes.NewBuffer(valuesJson))
// Keep numbers as a json.Number, which holds the original string representation
// otherwise all numbers get cast to float64 which is no good for e.g uint256.
d.UseNumber()
err = d.Decode(&valuesSimple)
if err != nil {
return nil, fmt.Errorf("unable to deserialse values: %v", err)
}
res := make(map[string]string)
for key, path := range rules {
value, err := myJSONPathGet(path, valuesSimple)
if err != nil {
return nil, fmt.Errorf("unable to normalise key %v: %v", key, err)
}
switch v := value.(type) {
case json.Number:
res[key] = v.String()
case int64:
// all of the numbers in the json from the ethereum call result will be
// json.Number and handled above; this case is just for the corner case
// where someone specifies a number as a static value in the json path itself
res[key] = strconv.FormatInt(v, 10)
case string:
res[key] = v
case bool:
res[key] = strconv.FormatBool(v)
default:
return nil, fmt.Errorf("unable to normalise key %v of type %T", key, value)
}
}
return res, nil
}
// myJSONPathGet works exactly like jsonpath.Get(path, values), except that any numbers found
// are returned as int64 rather than float64 which is the default.
// ** in the path expression itself, not the json being queried **
// Evaluation will fail if numbers in the path are not integers.
func myJSONPathGet(path string, values []interface{}) (interface{}, error) {
baselang := jsonpath.Language()
mylang := gval.PrefixExtension(scanner.Int, parseNumberAsInt64)
lang := gval.NewLanguage(baselang, mylang)
eval, err := lang.NewEvaluable(path)
if err != nil {
return nil, err
}
value, err := eval(context.Background(), values)
return value, err
}
func parseNumberAsInt64(c context.Context, p *gval.Parser) (gval.Evaluable, error) {
n, err := strconv.ParseInt(p.TokenText(), 10, 64)
if err != nil {
return nil, err
}
return p.Const(n), nil
}