-
Notifications
You must be signed in to change notification settings - Fork 1.7k
/
Copy pathadapter.go
131 lines (122 loc) · 4.23 KB
/
adapter.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
package adapters
import (
"encoding/json"
"fmt"
"github.com/smartcontractkit/chainlink/store"
"github.com/smartcontractkit/chainlink/store/models"
)
var (
// TaskTypeCopy is the identifier for the Copy adapter.
TaskTypeCopy = models.MustNewTaskType("copy")
// TaskTypeEthBytes32 is the identifier for the EthBytes32 adapter.
TaskTypeEthBytes32 = models.MustNewTaskType("ethbytes32")
// TaskTypeEthInt256 is the identifier for the EthInt256 adapter.
TaskTypeEthInt256 = models.MustNewTaskType("ethint256")
// TaskTypeEthUint256 is the identifier for the EthUint256 adapter.
TaskTypeEthUint256 = models.MustNewTaskType("ethuint256")
// TaskTypeEthTx is the identifier for the EthTx adapter.
TaskTypeEthTx = models.MustNewTaskType("ethtx")
// TaskTypeHTTPGet is the identifier for the HTTPGet adapter.
TaskTypeHTTPGet = models.MustNewTaskType("httpget")
// TaskTypeHTTPPost is the identifier for the HTTPPost adapter.
TaskTypeHTTPPost = models.MustNewTaskType("httppost")
// TaskTypeJSONParse is the identifier for the JSONParse adapter.
TaskTypeJSONParse = models.MustNewTaskType("jsonparse")
// TaskTypeMultiply is the identifier for the Multiply adapter.
TaskTypeMultiply = models.MustNewTaskType("multiply")
// TaskTypeNoOp is the identifier for the NoOp adapter.
TaskTypeNoOp = models.MustNewTaskType("noop")
// TaskTypeNoOpPend is the identifier for the NoOpPend adapter.
TaskTypeNoOpPend = models.MustNewTaskType("nooppend")
// TaskTypeSleep is the identifier for the Sleep adapter.
TaskTypeSleep = models.MustNewTaskType("sleep")
// TaskTypeWasm is the wasm interpereter adapter
TaskTypeWasm = models.MustNewTaskType("wasm")
)
// Adapter interface applies to all core adapters.
// Each implementation must return a RunResult.
type Adapter interface {
Perform(models.RunResult, *store.Store) models.RunResult
}
// AdapterWithMinConfs is the interface required for an adapter to be run in
// the job pipeline. In addition to the Adapter interface, implementers must
// specify the number of confirmations required before the Adapter can be run.
type AdapterWithMinConfs interface {
Adapter
MinConfs() uint64
}
// MinConfsWrappedAdapter allows for an adapter to be wrapped so that it meets
// the AdapterWithMinConfsInterface.
type MinConfsWrappedAdapter struct {
Adapter
ConfiguredConfirmations uint64
}
// MinConfs specifies the number of block confirmations
// needed to run the adapter.
func (wa MinConfsWrappedAdapter) MinConfs() uint64 {
return wa.ConfiguredConfirmations
}
// For determines the adapter type to use for a given task.
func For(task models.TaskSpec, store *store.Store) (AdapterWithMinConfs, error) {
var ac Adapter
var err error
switch task.Type {
case TaskTypeCopy:
ac = &Copy{}
err = unmarshalParams(task.Params, ac)
case TaskTypeEthBytes32:
ac = &EthBytes32{}
err = unmarshalParams(task.Params, ac)
case TaskTypeEthInt256:
ac = &EthInt256{}
err = unmarshalParams(task.Params, ac)
case TaskTypeEthUint256:
ac = &EthUint256{}
err = unmarshalParams(task.Params, ac)
case TaskTypeEthTx:
ac = &EthTx{}
err = unmarshalParams(task.Params, ac)
case TaskTypeHTTPGet:
ac = &HTTPGet{}
err = unmarshalParams(task.Params, ac)
case TaskTypeHTTPPost:
ac = &HTTPPost{}
err = unmarshalParams(task.Params, ac)
case TaskTypeJSONParse:
ac = &JSONParse{}
err = unmarshalParams(task.Params, ac)
case TaskTypeMultiply:
ac = &Multiply{}
err = unmarshalParams(task.Params, ac)
case TaskTypeNoOp:
ac = &NoOp{}
err = unmarshalParams(task.Params, ac)
case TaskTypeNoOpPend:
ac = &NoOpPend{}
err = unmarshalParams(task.Params, ac)
case TaskTypeSleep:
ac = &Sleep{}
err = unmarshalParams(task.Params, ac)
case TaskTypeWasm:
ac = &Wasm{}
err = unmarshalParams(task.Params, ac)
default:
bt, err := store.FindBridge(task.Type.String())
if err != nil {
return nil, fmt.Errorf("%s is not a supported adapter type", task.Type)
}
return &Bridge{BridgeType: bt, Params: &task.Params}, nil
}
wa := MinConfsWrappedAdapter{
Adapter: ac,
ConfiguredConfirmations: store.Config.MinIncomingConfirmations,
}
return wa, err
}
func unmarshalParams(params models.JSON, dst interface{}) error {
bytes, err := params.MarshalJSON()
if err != nil {
return err
}
return json.Unmarshal(bytes, dst)
}