/
common.go
619 lines (532 loc) · 16.4 KB
/
common.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
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
package models
import (
"database/sql/driver"
"encoding/json"
"fmt"
"net/url"
"strings"
"time"
"github.com/pkg/errors"
"github.com/smartcontractkit/chainlink/core/assets"
"github.com/araddon/dateparse"
"github.com/ethereum/go-ethereum/common"
"github.com/fxamacker/cbor/v2"
"github.com/robfig/cron/v3"
"github.com/tidwall/gjson"
"github.com/tidwall/sjson"
)
// CronParser is the global parser for crontabs.
// It accepts the standard 5 field cron syntax as well as an optional 6th field
// at the front to represent seconds.
var CronParser cron.Parser
func init() {
cronParserSpec := cron.SecondOptional | cron.Minute | cron.Hour | cron.Dom | cron.Month | cron.Dow | cron.Descriptor
CronParser = cron.NewParser(cronParserSpec)
}
// RunStatus is a string that represents the run status
type RunStatus string
const (
// RunStatusUnstarted is the default state of any run status.
RunStatusUnstarted = RunStatus("unstarted")
// RunStatusInProgress is used for when a run is actively being executed.
RunStatusInProgress = RunStatus("in_progress")
// RunStatusPendingIncomingConfirmations is used for when a run is awaiting for incoming block confirmations
// e.g. waiting for the log event to be N blocks deep
RunStatusPendingIncomingConfirmations = RunStatus("pending_incoming_confirmations")
// RunStatusPendingConnection states that the run is waiting on a connection to the block chain.
RunStatusPendingConnection = RunStatus("pending_connection")
// RunStatusPendingBridge is used for when a run is waiting on the completion
// of another event.
RunStatusPendingBridge = RunStatus("pending_bridge")
// RunStatusPendingSleep is used for when a run is waiting on a sleep function to finish.
RunStatusPendingSleep = RunStatus("pending_sleep")
// RunStatusPendingOutgoingConfirmations is used for when a run is waiting for outgoing block confirmations
// e.g. we have sent a transaction using ethtx and are now waiting for it to be N blocks deep
RunStatusPendingOutgoingConfirmations = RunStatus("pending_outgoing_confirmations")
// RunStatusErrored is used for when a run has errored and will not complete.
RunStatusErrored = RunStatus("errored")
// RunStatusCompleted is used for when a run has successfully completed execution.
RunStatusCompleted = RunStatus("completed")
// RunStatusCancelled is used to indicate a run is no longer desired.
RunStatusCancelled = RunStatus("cancelled")
)
// Unstarted returns true if the status is the initial state.
func (s RunStatus) Unstarted() bool {
return s == RunStatusUnstarted
}
// PendingBridge returns true if the status is pending_bridge.
func (s RunStatus) PendingBridge() bool {
return s == RunStatusPendingBridge
}
// PendingIncomingConfirmations returns true if the status is pending_incoming_confirmations.
func (s RunStatus) PendingIncomingConfirmations() bool {
return s == RunStatusPendingIncomingConfirmations
}
// PendingConnection returns true if the status is pending_connection.
func (s RunStatus) PendingConnection() bool {
return s == RunStatusPendingConnection
}
// PendingSleep returns true if the status is pending_sleep.
func (s RunStatus) PendingSleep() bool {
return s == RunStatusPendingSleep
}
// PendingOutgoingConfirmations returns true if the status is pending_incoming_confirmations.
func (s RunStatus) PendingOutgoingConfirmations() bool {
return s == RunStatusPendingOutgoingConfirmations
}
// Completed returns true if the status is RunStatusCompleted.
func (s RunStatus) Completed() bool {
return s == RunStatusCompleted
}
// Cancelled returns true if the status is RunStatusCancelled.
func (s RunStatus) Cancelled() bool {
return s == RunStatusCancelled
}
// Errored returns true if the status is RunStatusErrored.
func (s RunStatus) Errored() bool {
return s == RunStatusErrored
}
// Pending returns true if the status is pending external or confirmations.
func (s RunStatus) Pending() bool {
return s.PendingBridge() || s.PendingIncomingConfirmations() || s.PendingOutgoingConfirmations() || s.PendingSleep() || s.PendingConnection()
}
// Finished returns true if the status is final and can't be changed.
func (s RunStatus) Finished() bool {
return s.Completed() || s.Errored() || s.Cancelled()
}
// Runnable returns true if the status is ready to be run.
func (s RunStatus) Runnable() bool {
return !s.Errored() && !s.Pending()
}
// CanStart returns true if the run is ready to begin processed.
func (s RunStatus) CanStart() bool {
return s.Pending() || s.Unstarted()
}
// Value returns this instance serialized for database storage.
func (s RunStatus) Value() (driver.Value, error) {
return string(s), nil
}
// Scan reads the database value and returns an instance.
func (s *RunStatus) Scan(value interface{}) error {
switch v := value.(type) {
case []byte:
*s = RunStatus(string(v))
case string:
*s = RunStatus(v)
default:
return fmt.Errorf("unable to convert %#v of %T to RunStatus", value, value)
}
return nil
}
// JSON stores the json types string, number, bool, and null.
// Arrays and Objects are returned as their raw json types.
type JSON struct {
gjson.Result
}
// Value returns this instance serialized for database storage.
func (j JSON) Value() (driver.Value, error) {
s := j.Bytes()
if len(s) == 0 {
return nil, nil
}
return s, nil
}
// Scan reads the database value and returns an instance.
func (j *JSON) Scan(value interface{}) error {
switch v := value.(type) {
case string:
*j = JSON{Result: gjson.Parse(v)}
case []byte:
*j = JSON{Result: gjson.ParseBytes(v)}
default:
return fmt.Errorf("unable to convert %v of %T to JSON", value, value)
}
return nil
}
// ParseJSON attempts to coerce the input byte array into valid JSON
// and parse it into a JSON object.
func ParseJSON(b []byte) (JSON, error) {
var j JSON
str := string(b)
if len(str) == 0 {
return j, nil
}
return j, json.Unmarshal([]byte(str), &j)
}
// UnmarshalJSON parses the JSON bytes and stores in the *JSON pointer.
func (j *JSON) UnmarshalJSON(b []byte) error {
str := string(b)
if !gjson.Valid(str) {
return fmt.Errorf("invalid JSON: %v", str)
}
*j = JSON{gjson.Parse(str)}
return nil
}
// MarshalJSON returns the JSON data if it already exists, returns
// an empty JSON object as bytes if not.
func (j JSON) MarshalJSON() ([]byte, error) {
if j.Exists() {
return j.Bytes(), nil
}
return []byte("{}"), nil
}
// Bytes returns the raw JSON.
func (j JSON) Bytes() []byte {
return []byte(j.String())
}
// AsMap returns j as a map
func (j JSON) AsMap() (map[string]interface{}, error) {
output := make(map[string]interface{})
switch v := j.Result.Value().(type) {
case map[string]interface{}:
for key, value := range v {
output[key] = value
}
case nil:
default:
return nil, errors.New("can only add to JSON objects or null")
}
return output, nil
}
// mapToJSON returns m as a JSON object, or errors
func mapToJSON(m map[string]interface{}) (JSON, error) {
bytes, err := json.Marshal(m)
if err != nil {
return JSON{}, err
}
return JSON{Result: gjson.ParseBytes(bytes)}, nil
}
// Add returns a new instance of JSON with the new value added.
func (j JSON) Add(insertKey string, insertValue interface{}) (JSON, error) {
return j.MultiAdd(KV{insertKey: insertValue})
}
// KV represents a key/value pair to be added to a JSON object
type KV map[string]interface{}
// MultiAdd returns a new instance of j with the new values added.
func (j JSON) MultiAdd(keyValues KV) (JSON, error) {
output, err := j.AsMap()
if err != nil {
return JSON{}, err
}
for key, value := range keyValues {
output[key] = value
}
return mapToJSON(output)
}
// Delete returns a new instance of JSON with the specified key removed.
func (j JSON) Delete(key string) (JSON, error) {
js, err := sjson.Delete(j.String(), key)
if err != nil {
return j, err
}
return ParseJSON([]byte(js))
}
// CBOR returns a bytes array of the JSON map or array encoded to CBOR.
func (j JSON) CBOR() ([]byte, error) {
switch v := j.Result.Value().(type) {
case map[string]interface{}, []interface{}, nil:
return cbor.Marshal(v)
default:
var b []byte
return b, fmt.Errorf("unable to coerce JSON to CBOR for type %T", v)
}
}
// WebURL contains the URL of the endpoint.
type WebURL url.URL
// UnmarshalJSON parses the raw URL stored in JSON-encoded
// data to a URL structure and sets it to the URL field.
func (w *WebURL) UnmarshalJSON(j []byte) error {
var v string
err := json.Unmarshal(j, &v)
if err != nil {
return err
}
// handle no url case
if len(v) == 0 {
return nil
}
u, err := url.ParseRequestURI(v)
if err != nil {
return err
}
*w = WebURL(*u)
return nil
}
// MarshalJSON returns the JSON-encoded string of the given data.
func (w WebURL) MarshalJSON() ([]byte, error) {
return json.Marshal(w.String())
}
// String delegates to the wrapped URL struct or an empty string when it is nil
func (w WebURL) String() string {
url := url.URL(w)
return url.String()
}
// Value returns this instance serialized for database storage.
func (w WebURL) Value() (driver.Value, error) {
return w.String(), nil
}
// Scan reads the database value and returns an instance.
func (w *WebURL) Scan(value interface{}) error {
s, ok := value.(string)
if !ok {
return fmt.Errorf("unable to convert %v of %T to WebURL", value, value)
}
u, err := url.ParseRequestURI(s)
if err != nil {
return err
}
*w = WebURL(*u)
return nil
}
// AnyTime holds a common field for time, and serializes it as
// a json number.
type AnyTime struct {
time.Time
Valid bool
}
// NewAnyTime creates a new Time.
func NewAnyTime(t time.Time) AnyTime {
return AnyTime{Time: t, Valid: true}
}
// MarshalJSON implements json.Marshaler.
// It will encode null if this time is null.
func (t AnyTime) MarshalJSON() ([]byte, error) {
if !t.Valid {
return []byte("null"), nil
}
return t.Time.UTC().MarshalJSON()
}
// UnmarshalJSON parses the raw time stored in JSON-encoded
// data and stores it to the Time field.
func (t *AnyTime) UnmarshalJSON(b []byte) error {
var str string
var n json.Number
if err := json.Unmarshal(b, &n); err == nil {
str = n.String()
} else if err := json.Unmarshal(b, &str); err != nil {
return err
}
if len(str) == 0 {
t.Valid = false
return nil
}
newTime, err := dateparse.ParseAny(str)
t.Time = newTime.UTC()
t.Valid = true
return err
}
// MarshalText returns null if not set, or the time.
func (t AnyTime) MarshalText() ([]byte, error) {
if !t.Valid {
return []byte("null"), nil
}
return t.Time.MarshalText()
}
// UnmarshalText parses null or a valid time.
func (t *AnyTime) UnmarshalText(text []byte) error {
str := string(text)
if str == "" || str == "null" {
t.Valid = false
return nil
}
if err := t.Time.UnmarshalText(text); err != nil {
return err
}
t.Valid = true
return nil
}
// Value returns this instance serialized for database storage.
func (t AnyTime) Value() (driver.Value, error) {
if !t.Valid {
return nil, nil
}
return t.Time, nil
}
// Scan reads the database value and returns an instance.
func (t *AnyTime) Scan(value interface{}) error {
switch temp := value.(type) {
case time.Time:
t.Time = temp.UTC()
t.Valid = true
return nil
case nil:
t.Valid = false
return nil
default:
return fmt.Errorf("unable to convert %v of %T to Time", value, value)
}
}
// Cron holds the string that will represent the spec of the cron-job.
type Cron string
// UnmarshalJSON parses the raw spec stored in JSON-encoded
// data and stores it to the Cron string.
func (c *Cron) UnmarshalJSON(b []byte) error {
var s string
err := json.Unmarshal(b, &s)
if err != nil {
return fmt.Errorf("Cron: %v", err)
}
if s == "" {
return nil
}
if !strings.HasPrefix(s, "CRON_TZ=") {
return errors.New("Cron: specs must specify a time zone using CRON_TZ, e.g. 'CRON_TZ=UTC 5 * * * *'")
}
_, err = CronParser.Parse(s)
if err != nil {
return fmt.Errorf("Cron: %v", err)
}
*c = Cron(s)
return nil
}
// String returns the current Cron spec string.
func (c Cron) String() string {
return string(c)
}
// Duration is a non-negative time duration.
type Duration struct{ d time.Duration }
func MakeDuration(d time.Duration) (Duration, error) {
if d < time.Duration(0) {
return Duration{}, fmt.Errorf("cannot make negative time duration: %s", d)
}
return Duration{d: d}, nil
}
func MustMakeDuration(d time.Duration) Duration {
rv, err := MakeDuration(d)
if err != nil {
panic(err)
}
return rv
}
// Duration returns the value as the standard time.Duration value.
func (d Duration) Duration() time.Duration {
return d.d
}
// Before returns the time d units before time t
func (d Duration) Before(t time.Time) time.Time {
return t.Add(-d.Duration())
}
// Shorter returns true if and only if d is shorter than od.
func (d Duration) Shorter(od Duration) bool { return d.d < od.d }
// IsInstant is true if and only if d is of duration 0
func (d Duration) IsInstant() bool { return d.d == 0 }
// String returns a string representing the duration in the form "72h3m0.5s".
// Leading zero units are omitted. As a special case, durations less than one
// second format use a smaller unit (milli-, micro-, or nanoseconds) to ensure
// that the leading digit is non-zero. The zero duration formats as 0s.
func (d Duration) String() string {
return d.Duration().String()
}
// MarshalJSON implements the json.Marshaler interface.
func (d Duration) MarshalJSON() ([]byte, error) {
return json.Marshal(d.String())
}
// UnmarshalJSON implements the json.Unmarshaler interface.
func (d *Duration) UnmarshalJSON(input []byte) error {
var txt string
err := json.Unmarshal(input, &txt)
if err != nil {
return err
}
v, err := time.ParseDuration(string(txt))
if err != nil {
return err
}
*d, err = MakeDuration(v)
if err != nil {
return err
}
return nil
}
func (d *Duration) Scan(v interface{}) (err error) {
switch tv := v.(type) {
case int64:
*d, err = MakeDuration(time.Duration(tv))
return err
default:
return errors.Errorf(`don't know how to parse "%s" of type %T as a `+
`models.Duration`, tv, tv)
}
}
func (d Duration) Value() (driver.Value, error) {
return int64(d.d), nil
}
// WithdrawalRequest request to withdraw LINK.
type WithdrawalRequest struct {
DestinationAddress common.Address `json:"address"`
ContractAddress common.Address `json:"contractAddress"`
Amount *assets.Link `json:"amount"`
}
// SendEtherRequest represents a request to transfer ETH.
type SendEtherRequest struct {
DestinationAddress common.Address `json:"address"`
FromAddress common.Address `json:"from"`
Amount *assets.Eth `json:"amount"`
}
// CreateKeyRequest represents a request to add an ethereum key.
type CreateKeyRequest struct {
CurrentPassword string `json:"current_password"`
}
// AddressCollection is an array of common.Address
// serializable to and from a database.
type AddressCollection []common.Address
// ToStrings returns this address collection as an array of strings.
func (r AddressCollection) ToStrings() []string {
// Unable to convert copy-free without unsafe:
// https://stackoverflow.com/a/48554123/639773
converted := make([]string, len(r))
for i, e := range r {
converted[i] = e.Hex()
}
return converted
}
// Value returns the string value to be written to the database.
func (r AddressCollection) Value() (driver.Value, error) {
return strings.Join(r.ToStrings(), ","), nil
}
// Scan parses the database value as a string.
func (r *AddressCollection) Scan(value interface{}) error {
str, ok := value.(string)
if !ok {
return fmt.Errorf("unable to convert %v of %T to AddressCollection", value, value)
}
if len(str) == 0 {
return nil
}
arr := strings.Split(str, ",")
collection := make(AddressCollection, len(arr))
for i, a := range arr {
collection[i] = common.HexToAddress(a)
}
*r = collection
return nil
}
// Configuration stores key value pairs for overriding global configuration
type Configuration struct {
ID int64 `gorm:"primary_key"`
Name string `gorm:"not null;unique;index"`
Value string `gorm:"not null"`
CreatedAt time.Time
UpdatedAt time.Time
DeletedAt *time.Time
}
// Merge returns a new map with all keys merged from right to left
func Merge(inputs ...JSON) (JSON, error) {
output := make(map[string]interface{})
for _, input := range inputs {
switch v := input.Result.Value().(type) {
case map[string]interface{}:
for key, value := range v {
output[key] = value
}
case nil:
default:
return JSON{}, errors.New("can only merge JSON objects")
}
}
bytes, err := json.Marshal(output)
if err != nil {
return JSON{}, err
}
return JSON{Result: gjson.ParseBytes(bytes)}, nil
}