Actions

External Initiators: Difference between revisions

From Chainlink Community Wiki

No edit summary
No edit summary
 
(7 intermediate revisions by the same user not shown)
Line 1: Line 1:
=External Initiators+
=External Initiators=
[https://docs.chain.link/chainlink-nodes/external-initiators/building-external-initiators/ Official Chainlink Docs for External Initiators]
[https://docs.chain.link/chainlink-nodes/external-initiators/building-external-initiators/ Official Chainlink Docs for External Initiators]


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


'''String 01:'''  <code>FeedName</code>
'''String01:'''  <code>FeedName</code>


'''String 02:'''  <code>FeedID</code>
'''String02:'''  <code>FeedID</code>


'''Uint:'''  <code>01234567.89</code>
'''Decimal01:'''  <code>1234567.89</code>


----
----
==Create External Initiator==
==Create External Initiator==
We need to add the following line to our <code>.env</code> file before we can actually use External Initiators:
<pre style="white-space:pre-wrap; width:100%; border:1px solid lightgrey; background:black; color:white;">FEATURE_EXTERNAL_INITIATORS=true</pre>
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)
In order to create an external initiator, we need to get access to the Chainlink command line (CLI)


Line 22: Line 31:
<code>docker exec -it $CHAINLINK_CONTAINER_NAME /bin.bash</code>
<code>docker exec -it $CHAINLINK_CONTAINER_NAME /bin.bash</code>


You will be prompted to log in.
Once attached to the container, we will need to login.
 
<code>chainlink admin login</code>


Your login credentials are the same email address and password combination you use to login to the web UI
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.
Once authenticated, we can create our external initiator.
<code>chainlink initiators create $EXTERNAL_INITIATOR_NAME $EXTERNAL_INITIATOR_URL</code>
<code>chainlink initiators create $EXTERNAL_INITIATOR_NAME $EXTERNAL_INITIATOR_URL</code>


In the above example command, the <code>$EXTERNAL_INITIATOR_NAME</code> would be whatever you want it to be.
In the above example command, the <code>$EXTERNAL_INITIATOR_NAME</code> 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.
 
*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
 
<code>http://172.17.0.1:8080/jobs</code>
 
 
 
----
==Create Job==
Example job spec:
 
<pre style="white-space:pre-wrap; width:100%; border:1px solid lightgrey; background:black; color:white;">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
"""
</pre>
 
 
----
==Testing the External Initiator==
The below should provide you with a semi-working example.
 
'''Create External Initiator:'''
 
<code>chainlink initiators create ExternalInitiatorExample01 http://172.17.0.1:8080/jobs</code>
 
'''Create the Chainlink Job:'''
 
<pre style="white-space:pre-wrap; width:100%; border:1px solid lightgrey; background:black; color:white;">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
"""
</pre>
 
'''Send a Test Request:'''
 
<code>curl -b cookiefile -X POST -H "Content-Type: application/json" --data '{"string1":"myVal1","string2":"myVal2","num1":123}' http://localhost:6688/v2/jobs/%s/runs</code>
 
Note: How to get cookiefile
 
<code>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<code>

Latest revision as of 21:15, 9 January 2023

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

We need to add the following line to our .env file before we can actually use External Initiators:

FEATURE_EXTERNAL_INITIATORS=true

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

Once attached to the container, we will need to login.

chainlink admin login

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