adapters

package
v0.6.9 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Oct 3, 2019 License: MIT Imports: 29 Imported by: 0

Documentation

Overview

Package adapters contain the core adapters used by the Chainlink node.

Bridge

The Bridge adapter is used to send and receive data to and from external adapters. The adapter will POST to the target adapter URL with an "id" field for the TaskRunID and a "data" field. For example:

{"id": "b8004e2989e24e1d8e4449afad2eb480", "data": {}}

Compare

The Compare adapter is used to compare the previous task's result against a specified value. Just like an `if` statement, the compare adapter will save `true` or `false` in the task run's result.

{ "type": "Compare", "params": {"operator": "eq", "value": "Hello" }}

HTTPGet

The HTTPGet adapter is used to grab the JSON data from the given URL.

{ "type": "HTTPGet", "params": {"get": "https://some-api-example.net/api" }}

HTTPPost

Sends a POST request to the specified URL and will return the response.

{ "type": "HTTPPost", "params": {"post": "https://weiwatchers.com/api" }}

JSONParse

The JSONParse adapter will obtain the value(s) for the given field(s).

{ "type": "JSONParse", "params": {"path": ["someField"] }}

EthBool

The EthBool adapter will take the given values and format them for the Ethereum blockchain in boolean value.

{ "type": "EthBool" }

EthBytes32

The EthBytes32 adapter will take the given values and format them for the Ethereum blockchain.

{ "type": "EthBytes32" }

EthInt256

The EthInt256 adapter will take a given signed 256 bit integer and format it to hex for the Ethereum blockchain.

{ "type": "EthInt256" }

EthUint256

The EthUint256 adapter will take a given 256 bit integer and format it in hex for the Ethereum blockchain.

{ "type": "EthUint256" }

EthTx

The EthTx adapter will write the data to the given address and functionSelector.

{
  "type": "EthTx", "params": {
    "address": "0x0000000000000000000000000000000000000000",
    "functionSelector": "0xffffffff"
  }
}

Multiplier

The Multiplier adapter multiplies the given input value times another specified value.

{ "type": "Multiply", "params": {"times": 100 }}

Random

Random adapter generates a number between 0 and 2**256-1 WARNING: The random adapter as implemented is not verifiable. Outputs from this adapters are not verifiable onchain as a fairly-drawn random samples. As a result, the oracle potentially has complete discretion to instead deliberately choose values with favorable onchain outcomes. Don't use it for a lottery, for instance, unless you fully trust the oracle not to pick its own tickets. We intend to either improve it in the future, or introduce a verifiable alternative. For now it is provided as an alternative to making web requests for random numbers, which is similarly unverifiable and has additional possible points of failure.

{ "type": "Random" }

EthTxABIEncode

The EthTxABIEncode adapter serializes the contents of a json object as transaction data calling an arbitrary function of a smart contract. See https://solidity.readthedocs.io/en/v0.5.11/abi-spec.html#formal-specification-of-the-encoding for the serialization format. We currently support all types that solidity contracts as of solc v0.5.11 can decode, i.e. address, bool, bytes1, ..., bytes32, int8, ..., int256, uint8, ..., uint256, arrays (e.g. address[2]), bytes (variable length), string (variable length), and slices (e.g. uint256[] or address[2][]).

The ABI of the function to be called is specified in the functionABI field, using the ABI JSON format used by solc and vyper. For example,

{
  "type": "EthTxABIEncode",
  "functionABI": {
    "name": "example"
    "inputs": [
      {"name": "x", "type": "uint256"},
      {"name": "y", "type": "bool[2][]"}
      {"name": "z", "type": "string"}
    ]
  },
  "address": "0xdeadbeefdeadbeefdeadbeefdeadbeefdeadbeef"
}

will encode a transaction to a function example(uint256 x, bool[2][] y, string z) for a contract at address 0xdeadbeefdeadbeefdeadbeefdeadbeefdeadbeef.

Upon use, the json input to an EthTxABIEncode task is expected to have a corresponding map of argument names to compatible data in its `result` field such as

{
  "result": {
    "x": "680564733841876926926749227234810109236",
    "y": [[true, false], [false, false]],
    "z": "hello world! привет мир!"
  }
}

which will result in a call to `example(uint256,bool[2][],string)` at address 0xdeadbeefdeadbeefdeadbeefdeadbeefdeadbeef, with arguments

example(
  680564733841876926926749227234810109236,
  [[true, false], [false, false]],
  "hello world! привет мир!"
)

The result from EthTxABIEncode is the hash of the resulting transaction, if it was successfully transmitted, or an error if not.

ABI types must be represented in JSON as follows:

address:
  - a hexstring containing at most 20 bytes, e.g.
    "0xdeadbeefdeadbeefdeadbeefdeadbeefdeadbeef"
bool:
  - true or false
bytes<n> (where 1 <= n <= 32):
  - a hexstring containing exactly n bytes, e.g.
    "0xdeadbeef" for a bytes8
  - an array of numbers containing exactly n bytes, e.g.
    [1,2,3,4,5,6,7,8,9,10] for a bytes10
int<n> (where n ∈ {8, 16, 24, ..., 256}):
  - a decimal number, e.g. from "-128" to "127" for an int8
  - a positive hex number, e.g. "0xdeadbeef" for an int64
  - for n <= 48, a number, e.g. 4294967296 for an int40
    (for larger n, json numbers aren't suitable due to them commonly
    being interpreted as doubles which cannot represent all integers above
    2**53)
uint<n> (where n ∈ {8, 16, 24, ..., 256}):
  - a decimal number, e.g. from "0" to "255" for an uint8
  - a positive hex number, e.g. "0xdeadbeef" for an uint64
  - for n <= 48, a number, e.g. 4294967296 for an uint40
    (for larger n, json numbers aren't suitable due to them commonly
    being interpreted as doubles which cannot represent all integers above
    2**53)
arrays:
  - a json array of the appropriate length, e.g. ["0x1", "-1"] for int8[2]
==============
bytes:
  - a hexstring of variable length, e.g. "0xdeadbeefc0ffee"
  - an array of numbers of variable length, e.g.
    [1,2,1,2,1,2]
string:
  - a utf8 string, e.g. "hello world! привет мир!"
slice:
  - an array of variable length, e.g. ["0x1", "-2", 3] for
    an int128[]

Index

Constants

View Source
const (
	// DataFormatBytes instructs the EthTx Adapter to treat the input value as a
	// bytes string, rather than a hexadecimal encoded bytes32
	DataFormatBytes = "bytes"
)

Variables

View Source
var (
	// TaskTypeCopy is the identifier for the Copy adapter.
	TaskTypeCopy = models.MustNewTaskType("copy")
	// TaskTypeEthBool is the identifier for the EthBool adapter.
	TaskTypeEthBool = models.MustNewTaskType("ethbool")
	// 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")
	// TaskTypeEthTxABIEncode is the identifier for the EthTxABIEncode adapter.
	TaskTypeEthTxABIEncode = models.MustNewTaskType("ethtxabiencode")
	// 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")
	// TaskTypeRandom is the identifier for the Random adapter.
	TaskTypeRandom = models.MustNewTaskType("random")
	// TaskTypeCompare is the identifier for the Compare adapter.
	TaskTypeCompare = models.MustNewTaskType("compare")
)
View Source
var (
	ErrResultNotNumber      = errors.New("The result was not a number")
	ErrValueNotNumber       = errors.New("The value was not a number")
	ErrOperatorNotSpecified = errors.New("Operator not specified")
	ErrValueNotSpecified    = errors.New("Value not specified")
)

Functions

func IsClientEmptyError added in v0.6.4

func IsClientEmptyError(err error) bool

Parity light clients can return an EmptyResponse error when they don't have access to the transaction in the mempool. If we wait long enough it should eventually return a transaction receipt.

func IsClientRetriable

func IsClientRetriable(err error) bool

IsClientRetriable does its best effort to see if an error indicates one that might have a different outcome if we retried the operation

Types

type BaseAdapter

type BaseAdapter interface {
	Perform(models.RunResult, *store.Store) models.RunResult
}

BaseAdapter is the minimum interface required to create an adapter. Only core adapters have this minimum requirement.

type Bridge

type Bridge struct {
	*models.BridgeType
	Params *models.JSON
}

Bridge adapter is responsible for connecting the task pipeline to external adapters, allowing for custom computations to be executed and included in runs.

func (*Bridge) Perform

func (ba *Bridge) Perform(input models.RunResult, store *store.Store) models.RunResult

Perform sends a POST request containing the JSON of the input RunResult to the external adapter specified in the BridgeType. It records the RunResult returned to it, and optionally marks the RunResult pending.

If the Perform is resumed with a pending RunResult, the RunResult is marked not pending and the RunResult is returned.

type Compare added in v0.6.7

type Compare struct {
	Operator string `json:"operator"`
	Value    string `json:"value"`
}

Compare adapter type takes an Operator and a Value field to compare to the previous task's Result.

func (*Compare) Perform added in v0.6.7

func (c *Compare) Perform(input models.RunResult, _ *store.Store) models.RunResult

Perform uses the Operator to check the run's result against the specified Value.

type Copy

type Copy struct {
	CopyPath JSONPath `json:"copyPath"`
}

Copy obj keys refers to which value to copy inside `data`, each obj value refers to where to copy the value to inside `data`

func (*Copy) Perform

func (c *Copy) Perform(input models.RunResult, store *store.Store) models.RunResult

Perform returns the copied values from the desired mapping within the `data` JSON object

type EthBool

type EthBool struct{}

EthBool holds no fields

func (*EthBool) Perform

func (*EthBool) Perform(input models.RunResult, _ *store.Store) models.RunResult

Perform returns the abi encoding for a boolean

For example, after converting the result false to hex encoded Ethereum ABI, it would be: "0x0000000000000000000000000000000000000000000000000000000000000000"

type EthBytes32

type EthBytes32 struct{}

EthBytes32 holds no fields.

func (*EthBytes32) Perform

func (*EthBytes32) Perform(input models.RunResult, _ *store.Store) models.RunResult

Perform returns the hex value of the first 32 bytes of a string so that it is in the proper format to be written to the blockchain.

For example, after converting the string "16800.01" to hex encoded Ethereum ABI, it would be: "0x31363830302e3031000000000000000000000000000000000000000000000000"

type EthInt256

type EthInt256 struct{}

EthInt256 holds no fields

func (*EthInt256) Perform

func (*EthInt256) Perform(input models.RunResult, _ *store.Store) models.RunResult

Perform returns the hex value of a given string so that it is in the proper format to be written to the blockchain.

For example, after converting the string "-123.99" to hex encoded Ethereum ABI, it would be: "0xffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff85"

type EthTx

type EthTx struct {
	Address          common.Address          `json:"address"`
	FunctionSelector models.FunctionSelector `json:"functionSelector"`
	DataPrefix       hexutil.Bytes           `json:"dataPrefix"`
	DataFormat       string                  `json:"format"`
	GasPrice         *models.Big             `json:"gasPrice" gorm:"type:numeric"`
	GasLimit         uint64                  `json:"gasLimit"`
}

EthTx holds the Address to send the result to and the FunctionSelector to execute.

func (*EthTx) Perform

func (etx *EthTx) Perform(input models.RunResult, store *strpkg.Store) models.RunResult

Perform creates the run result for the transaction if the existing run result is not currently pending. Then it confirms the transaction was confirmed on the blockchain.

type EthTxABIEncode added in v0.6.9

type EthTxABIEncode struct {
	// Ethereum address of the contract this task calls
	Address common.Address `json:"address"`
	// ABI of contract function this task calls
	FunctionABI abi.Method  `json:"functionABI"`
	GasPrice    *models.Big `json:"gasPrice" gorm:"type:numeric"`
	GasLimit    uint64      `json:"gasLimit"`
}

EthTxABIEncode holds the Address to send the result to and the FunctionABI to use for encoding arguments.

func (*EthTxABIEncode) Perform added in v0.6.9

func (etx *EthTxABIEncode) Perform(
	input models.RunResult, store *strpkg.Store) models.RunResult

Perform creates the run result for the transaction if the existing run result is not currently pending. Then it confirms the transaction was confirmed on the blockchain.

func (*EthTxABIEncode) UnmarshalJSON added in v0.6.9

func (etx *EthTxABIEncode) UnmarshalJSON(data []byte) error

UnmarshalJSON for custom JSON unmarshal that is strict, i.e. doesn't accept spurious fields. (In particular, we wan't to ensure that we don't get spurious fields in the FunctionABI, so that users don't get any wrong ideas about what parts of the ABI we use for encoding data.)

type EthUint256

type EthUint256 struct{}

EthUint256 holds no fields.

func (*EthUint256) Perform

func (*EthUint256) Perform(input models.RunResult, _ *store.Store) models.RunResult

Perform returns the hex value of a given string so that it is in the proper format to be written to the blockchain.

For example, after converting the string "123.99" to hex encoded Ethereum ABI, it would be: "0x000000000000000000000000000000000000000000000000000000000000007b"

type ExtendedPath

type ExtendedPath []string

ExtendedPath is the path to append to a base URL

func (*ExtendedPath) UnmarshalJSON

func (ep *ExtendedPath) UnmarshalJSON(input []byte) error

UnmarshalJSON implements the Unmarshaler interface

type HTTPGet

type HTTPGet struct {
	URL          models.WebURL   `json:"url"`
	GET          models.WebURL   `json:"get"`
	Headers      http.Header     `json:"headers"`
	QueryParams  QueryParameters `json:"queryParams"`
	ExtendedPath ExtendedPath    `json:"extPath"`
}

HTTPGet requires a URL which is used for a GET request when the adapter is called.

func (*HTTPGet) GetRequest

func (hga *HTTPGet) GetRequest() (*http.Request, error)

GetRequest returns the HTTP request including query parameters and headers

func (*HTTPGet) GetURL

func (hga *HTTPGet) GetURL() string

GetURL retrieves the GET field if set otherwise returns the URL field

func (*HTTPGet) Perform

func (hga *HTTPGet) Perform(input models.RunResult, store *store.Store) models.RunResult

Perform ensures that the adapter's URL responds to a GET request without errors and returns the response body as the "value" field of the result.

type HTTPPost

type HTTPPost struct {
	URL          models.WebURL   `json:"url"`
	POST         models.WebURL   `json:"post"`
	Headers      http.Header     `json:"headers"`
	QueryParams  QueryParameters `json:"queryParams"`
	Body         *string         `json:"body,omitempty"`
	ExtendedPath ExtendedPath    `json:"extPath"`
}

HTTPPost requires a URL which is used for a POST request when the adapter is called.

func (*HTTPPost) GetRequest

func (hpa *HTTPPost) GetRequest(body string) (*http.Request, error)

GetRequest takes the request body and returns the HTTP request including query parameters and headers.

HTTPPost's Body parameter overrides the given argument if present.

func (*HTTPPost) GetURL

func (hpa *HTTPPost) GetURL() string

GetURL retrieves the POST field if set otherwise returns the URL field

func (*HTTPPost) Perform

func (hpa *HTTPPost) Perform(input models.RunResult, store *store.Store) models.RunResult

Perform ensures that the adapter's URL responds to a POST request without errors and returns the response body as the "value" field of the result.

type JSONParse

type JSONParse struct {
	Path JSONPath `json:"path"`
}

JSONParse holds a path to the desired field in a JSON object, made up of an array of strings.

func (*JSONParse) Perform

func (jpa *JSONParse) Perform(input models.RunResult, _ *store.Store) models.RunResult

Perform returns the value associated to the desired field for a given JSON object.

For example, if the JSON data looks like this:

{
  "data": [
    {"last": "1111"},
    {"last": "2222"}
  ]
}

Then ["0","last"] would be the path, and "111" would be the returned value

type JSONPath

type JSONPath []string

JSONPath is a path to a value in a JSON object

func (*JSONPath) UnmarshalJSON

func (jp *JSONPath) UnmarshalJSON(b []byte) error

UnmarshalJSON implements the Unmarshaler interface

type Multiplier

type Multiplier float64

Multiplier represents the number to multiply by in Multiply adapter.

func (*Multiplier) UnmarshalJSON

func (m *Multiplier) UnmarshalJSON(input []byte) error

UnmarshalJSON implements json.Unmarshaler.

type Multiply

type Multiply struct {
	Times *Multiplier `json:"times"`
}

Multiply holds the a number to multiply the given value by.

func (*Multiply) Perform

func (ma *Multiply) Perform(input models.RunResult, _ *store.Store) models.RunResult

Perform returns the input's "result" field, multiplied times the adapter's "times" field.

For example, if input value is "99.994" and the adapter's "times" is set to "100", the result's value will be "9999.4".

type NoOp

type NoOp struct{}

NoOp adapter type holds no fields

func (*NoOp) Perform

func (noa *NoOp) Perform(input models.RunResult, _ *store.Store) models.RunResult

Perform returns the empty RunResult

type NoOpPend

type NoOpPend struct{}

NoOpPend adapter type holds no fields

func (*NoOpPend) Perform

func (noa *NoOpPend) Perform(input models.RunResult, _ *store.Store) models.RunResult

Perform on this adapter type returns an empty RunResult with an added field for the status to indicate the task is Pending.

type PipelineAdapter

type PipelineAdapter struct {
	BaseAdapter
	// contains filtered or unexported fields
}

PipelineAdapter wraps a BaseAdapter with requirements for execution in the pipeline.

func For

func For(task models.TaskSpec, store *store.Store) (*PipelineAdapter, error)

For determines the adapter type to use for a given task.

func (PipelineAdapter) MinConfs

func (p PipelineAdapter) MinConfs() uint32

MinConfs returns the private attribute

func (PipelineAdapter) MinContractPayment

func (p PipelineAdapter) MinContractPayment() *assets.Link

MinContractPayment returns the private attribute

type QueryParameters

type QueryParameters url.Values

QueryParameters are the keys and values to append to the URL

func (*QueryParameters) UnmarshalJSON

func (qp *QueryParameters) UnmarshalJSON(input []byte) error

UnmarshalJSON implements the Unmarshaler interface

type Random added in v0.6.4

type Random struct{}

Random adapter type holds no fields

func (*Random) Perform added in v0.6.4

func (ra *Random) Perform(input models.RunResult, _ *store.Store) models.RunResult

Perform returns a random uint256 number in 0 | 2**256-1 range

type Sleep

type Sleep struct {
	Until models.AnyTime `json:"until"`
}

Sleep adapter allows a job to do nothing for some amount of wall time.

func (*Sleep) Duration

func (adapter *Sleep) Duration() time.Duration

Duration returns the amount of sleeping this task should be paused for.

func (*Sleep) Perform

func (adapter *Sleep) Perform(input models.RunResult, str *store.Store) models.RunResult

Perform returns the input RunResult after waiting for the specified Until parameter.

type Wasm

type Wasm struct {
	WasmT string `json:"wasmt"`
}

Wasm represents a wasm binary encoded as base64 or wasm encoded as text (a lisp like language).

func (*Wasm) Perform

func (wasm *Wasm) Perform(input models.RunResult, _ *store.Store) models.RunResult

Perform ships the wasm representation to the SGX enclave where it is evaluated.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL