In this workshop, we will build a price forecast to help inform the direction of a trade transaction.
Chainlink functions enables you to leverage the power of a decentralized oracle network (DON) to execute external function calls (off-chain) to inform on-chain interactions.
Chainlink is able to execute a user-defined function via a DON, which comes to consensus on the results and reports the median result to the requesting contract via a callback function.
Chainlink Functions enables you to securely share secrets with the DON. Secrets are encrypted with a password.
yarn set:pw
Once the ecrpytion key is created with your desired password, you can safely share your secrets with the DON, which requires multiple nodes to decrypt with consensus.
We may now safely store environment variables without worrying about them being exposed, since they will be encrypted with your desired password.
These variables will be stored in a file called .env.enc
.
yarn set:env
After running the command, you'll be prompted to enter the following for each variable to be encrypted:
-
Name: used to identify the variable.
-
Value: your (unencrypted) environment variable (secret).
For this demonstration, you will need to add the following to your encrypted environment variables:
OPENAI_KEY
PRIVATE_KEY
ETHERSCAN_API_KEY
Before deploying, it's useful to simulate the execution of your function to ensure the output of your function execution is as expected.
You may simulate your function using the command below.
yarn simulate
For full details on creating HTTP requestion via functions, read our API Reference Documentation.
yarn deploy
Note: ensure you have updated the deployment script to align with your target blockchain configurations.
Fund a new Functions billing subscription via the Chainlink Functions UI and add your deployed Consumer Contract as a an authorized consumer to your subscription OR do so programmatically, as follows:
npx hardhat func-sub-create --network ethereumSepolia --amount <LINK_AMOUNT> --contract <CONSUMER_ADDRESS>
Functions enable you to make requests via the consumer contract. Before requesting, make sure you have successfully compiled your FunctionConsumer Contract, otherwise the request will fail to process.
You may do this programmatically with:
npx hardhat func-request --network <NETWORK_NAME> --contract <CONSUMER_ADDRESS> --subid <SUBSCRIPTION_ID>
You will see a confirmation request, so hit Y
and press enter.
Once the request is fulfilled the console will show the response (decoded into the relevant return type) from the execution of your custom JS script.
You are also able to query the response that was stored in your Functions Consumer contract either through the Functions UI or programmatically as follows:
npx hardhat func-read --contract <CONSUMER_ADDRESS> --network <NETWORK_NAME>