-
Notifications
You must be signed in to change notification settings - Fork 19
/
spec.go
203 lines (177 loc) · 5.94 KB
/
spec.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
// 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 common
import (
"encoding/base64"
"encoding/hex"
"errors"
"fmt"
"sort"
"strings"
"code.vegaprotocol.io/vega/core/datasource/common"
vegapb "code.vegaprotocol.io/vega/protos/vega"
"google.golang.org/protobuf/types/known/structpb"
)
var (
ErrCallSpecIsNil = errors.New("ethereum call spec proto is nil")
ErrInvalidEthereumAbi = errors.New("is not a valid ethereum abi definition")
ErrInvalidCallTrigger = errors.New("ethereum call trigger not valid")
ErrInvalidCallArgs = errors.New("ethereum call args not valid")
ErrInvalidFilters = errors.New("ethereum call filters not valid")
)
type Spec struct {
Address string
AbiJson []byte
Method string
ArgsJson []string
Trigger Trigger
RequiredConfirmations uint64
Normalisers map[string]string
Filters common.SpecFilters
SourceChainID uint64
}
func SpecFromProto(proto *vegapb.EthCallSpec) (Spec, error) {
if proto == nil {
return Spec{}, ErrCallSpecIsNil
}
trigger, err := TriggerFromProto(proto.Trigger)
if err != nil {
return Spec{}, errors.Join(ErrInvalidCallTrigger, err)
}
filters := common.SpecFiltersFromProto(proto.Filters)
abiBytes := []byte(proto.Abi)
jsonArgs := []string{}
for _, protoArg := range proto.Args {
// special case for string starting with 0x? and are 32bytes long.
// this is to match the ethereum bytes32 type
// down the line we are trying to call the contract with strings
// instead here we want to encode the value into a base64 string
// which gets unwrapped into a [32]byte by the ethereum library
var isBytes bool
var jsonArg []byte
stringValue := protoArg.GetStringValue()
if stringValue != "" && strings.HasPrefix(stringValue, "0x") {
bytes, err := hex.DecodeString(stringValue[2:])
if err == nil && len(bytes) == 32 {
base64Value := base64.StdEncoding.EncodeToString(bytes)
jsonArg = []byte(fmt.Sprintf("\"%v\"", base64Value))
isBytes = true
}
}
if !isBytes {
jsonArg, err = protoArg.MarshalJSON()
if err != nil {
return Spec{}, errors.Join(ErrInvalidCallArgs, err)
}
}
jsonArgs = append(jsonArgs, string(jsonArg))
}
normalisers := map[string]string{}
for _, v := range proto.Normalisers {
normalisers[v.Name] = v.Expression
}
return Spec{
Address: proto.Address,
AbiJson: abiBytes,
Method: proto.Method,
ArgsJson: jsonArgs,
Trigger: trigger,
RequiredConfirmations: proto.RequiredConfirmations,
Filters: filters,
Normalisers: normalisers,
SourceChainID: proto.SourceChainId,
}, nil
}
func (s Spec) IntoProto() (*vegapb.EthCallSpec, error) {
argsPBValue := []*structpb.Value{}
for _, arg := range s.ArgsJson {
v := structpb.Value{}
err := v.UnmarshalJSON([]byte(arg))
if err != nil {
return nil, fmt.Errorf("failed to unmarshal arg json '%s': %w", arg, err)
}
argsPBValue = append(argsPBValue, &v)
}
normalisers := []*vegapb.Normaliser{}
for k, v := range s.Normalisers {
n := vegapb.Normaliser{
Name: k,
Expression: v,
}
normalisers = append(normalisers, &n)
}
sort.Slice(normalisers, func(i, j int) bool { return normalisers[i].Name < normalisers[j].Name })
return &vegapb.EthCallSpec{
Address: s.Address,
Abi: string(s.AbiJson),
Method: s.Method,
Args: argsPBValue,
Trigger: s.Trigger.IntoTriggerProto(),
RequiredConfirmations: s.RequiredConfirmations,
Filters: s.Filters.IntoProto(),
Normalisers: normalisers,
SourceChainId: s.SourceChainID,
}, nil
}
func (s Spec) ToDefinitionProto() (*vegapb.DataSourceDefinition, error) {
eth, err := s.IntoProto()
if err != nil {
return nil, fmt.Errorf("failed to convert to eth oracle proto: %w", err)
}
return &vegapb.DataSourceDefinition{
SourceType: &vegapb.DataSourceDefinition_External{
External: &vegapb.DataSourceDefinitionExternal{
SourceType: &vegapb.DataSourceDefinitionExternal_EthOracle{
EthOracle: eth,
},
},
},
}, nil
}
func (s Spec) GetFilters() []*common.SpecFilter {
return s.Filters
}
func (s Spec) String() string {
return fmt.Sprintf("ethcallspec(%v, %v, %v, %v, %v, %v, %v)",
s.Address, s.AbiJson, s.Method, s.ArgsJson, s.Trigger, s.RequiredConfirmations, s.Filters)
}
// Whats the need for this deep clone?
func (s Spec) DeepClone() common.DataSourceType {
clonedNormalisers := make(map[string]string)
for key, value := range s.Normalisers {
clonedNormalisers[key] = value
}
return Spec{
Address: s.Address,
AbiJson: s.AbiJson,
Method: s.Method,
ArgsJson: append([]string(nil), s.ArgsJson...),
Trigger: s.Trigger,
RequiredConfirmations: s.RequiredConfirmations,
Filters: append(common.SpecFilters(nil), s.Filters...),
Normalisers: clonedNormalisers,
SourceChainID: s.SourceChainID,
}
}
func (s Spec) IsZero() bool {
return s.Address == "" &&
s.Method == "" &&
s.Trigger == nil &&
s.RequiredConfirmations == 0 &&
len(s.AbiJson) == 0 &&
len(s.ArgsJson) == 0 &&
len(s.Filters) == 0 &&
len(s.Normalisers) == 0
}