External Initiators
From Chainlink Community Wiki
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
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