Actions

External Initiators

From Chainlink Community Wiki

Revision as of 21:08, 9 January 2023 by Devtrace (talk | contribs)

External Initiators

Official Chainlink Docs for External Initiators


Example Details for the Document

To help get a better understanding of how external initiators work, we will run through an example in the doc.

The example is writing three pieces of information on-chain; two strings and an uint.

String01: FeedName

String02: FeedID

Decimal01: 1234567.89


Create External Initiator

Due to the fact that the job spec requires an external initiator name, we must create the external initiator before we create and deploy the Chainlink job.

Note: The name you give the external initiator will be used to call the external initiator in the job spec we deploy.

In order to create an external initiator, we need to get access to the Chainlink command line (CLI)

We'll accomplish this by entering into our Chainlink docker container

docker exec -it $CHAINLINK_CONTAINER_NAME /bin.bash

You will be prompted to log in.

Your login credentials are the same email address and password combination you use to login to the web UI

Once authenticated, we can create our external initiator.

chainlink initiators create $EXTERNAL_INITIATOR_NAME $EXTERNAL_INITIATOR_URL

In the above example command, the $EXTERNAL_INITIATOR_NAME would be whatever you want it to be.

  • Whatever name you give it, will appear as the name in the database, so I recommend you name it something specific to the job.

Lastly, in the above example, the URL you provide is going to be the http patch to your Chainlink node's job endpoint

http://172.17.0.1:8080/jobs



Create Job

Example job spec:

type            = "webhook"
name            = "External Initiator Example 01"
schemaVersion   = 1
externalInitiators = [
  { name = "$EXTERNAL_INITIATOR_NAME", spec = "{}" }
]
observationSource   = """
    data         [type="jsonparse" data="$(jobRun.requestBody)"]

    encode_tx    [type="ethabiencode"
          abi="myFunc(string string1, string string2, uint256 num1)"
          data="{ \\"String01\\": $(data.string1), \\"String02\\": $(data.string2), \\"Decimal01\\": $(data.num1) }"]

    submit_tx    [type="ethtx"
          to="$CONTRACT_ADDRESS"
          data="$(encode_tx)"
          failOnRevert="true"]

    data -> encode_tx -> submit_tx
"""



Testing the External Initiator

The below should provide you with a semi-working example.

Create External Initiator:

chainlink initiators create ExternalInitiatorExample01 http://172.17.0.1:8080/jobs

Create the Chainlink Job:

type            = "webhook"
name            = "External Initiator Example 01"
schemaVersion   = 1
externalInitiators = [
  { name = "ExternalInitiatorExample01", spec = "{}" }
]
observationSource   = """
    data         [type="jsonparse" data="$(jobRun.requestBody)"]

    encode_tx    [type="ethabiencode"
          abi="myFunc(string string1, string string2, uint256 num1)"
          data="{ \\"String01\\": $(data.string1), \\"String02\\": $(data.string2), \\"Decimal01\\": $(data.num1) }"]

    submit_tx    [type="ethtx"
          to="$CONTRACT_ADDRESS"
          data="$(encode_tx)"
          failOnRevert="true"]

    data -> encode_tx -> submit_tx
"""

Send a Test Request: curl -b cookiefile -X POST -H "Content-Type: application/json" --data '{"string1":"myVal1","string2":"myVal2","num1":123}' http://localhost:6688/v2/jobs/%s/runs

Note: How to get cookiefile

curl -k -s -c cookiefile -X POST -H 'Content-Type: application/json' -d '{"email":"YOUR_EMAIL", "password":"YOUR_PASSWORD"}' https://NODE_IP_OR_HOSTNAME:6689/sessions