Giter VIP home page Giter VIP logo

functions-workshop's Introduction

Block Magic Functions Workshop

In this workshop, we will build a price forecast to help inform the direction of a trade transaction.

Getting Started

Overview of Functions

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.


Functions Workflow

1. Setup Environment Variables

Create Password

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.

Store Variables

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

2. Simulate Functions

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.

3. Deploy Consumer

yarn deploy

Note: ensure you have updated the deployment script to align with your target blockchain configurations.

4. Create Subscription

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>

5. Make Requests

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.

6. Make Queries

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>

functions-workshop's People

Contributors

bunsdev avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.