Git Product home page Git Product logo

chainlink-samples's Introduction

Sample Smart Contracts for Kaleido Chainlink Service

Kaleido's Chainlink service supports the popular Chainlink decentralized oracle technology. The service makes it trivial to create a permissioned Chainlink network attached to a Kaleido blockchain environment.

The project demonstrates the smart contract code a customer has to write in order to utilize the oracle service after Chainlink has been provisioned in Kaleido.

Sample code for both v0.4 and v0.5 Solidity are provided.

Pre-requisites

Before running the sample, make sure the following tools are installed:

Testing the Samples

Here is an overview of the steps

  1. Create a Ethereum node and a Chainlink node in your Kaleido environment.
  2. Create a job in the Chainlink node.
  3. Set 4 env variables to connect your environments:
JOB_ID
ETH_URL
LINK_ADDRESS
ORACLE_ADDRESS
  1. Run npm i and then truffle test

Let's break it down.

Create a Job in the Chainlink node

After adding a Chainlink service to a Kaleido environment, click the service to open the dashboard. Take note of the essential information displayed in order to run the sample:

  • LINK CONTRACT ADDRESS: the contract address for the Link token. This is required to deploy the Chainlink client contract, but no LINK payment is necessary to use the oracle service in a Kaleido environment
  • ORACLE CONTRACT ADDRESS: the contract address for the Oracle. This is the central component that make Chainlink work. The Chainlink client contract in the sample must be told where the Oracle contract is in order to request for a job run and be called back once the job run completes.

Click the OPEN CHAINLINK WEB UI button to open the Chainlink Operator UI. Use the username and password displayed in the service dashboard to log in.

After logging in, click "New Job" button, and paste the JSON content from the file chainlink-sample/jobs/eth-price.json, and click Create Job.

Copy the value of the Job ID that got generated by Chainlink, and set an environment variable in the command line JOB_ID with this value.

Configuring Kaleido Ethereum Node URL

The sample truffle test will deploy the client smart contract to the target Kaleido environment. Truffle needs to be told where the node is.

All Kaleido blockchain nodes are protected by strong authentication credentials. In the Kaleido console UI, find a node owned by you, find the ... menu on the right hand side and click Connect. Then select the button on the left side for Native JSON/RPC. You will be prompted to generate a pair of strong credentials. Once generated, a dashboard page is displayed for different ways to connect to the Kaleido node. Find the full URL on the bottom of the page under the label CONNECTION URL. Click Copy.

Set environment variable ETH_URL to the value just copied above. This will be used by truffle-config.js to point truffle tests to the Kaleido node.

Set LinkToken and Oracle Contract Addresses

The Chainlink client smart contract requires the addresses of the LinkToken and Oracle contracts to function. These values are displayed in the Chainlink service dashboard in the Kaleido console UI as discussed above.

Copy those values and set them to environment variables LINK_ADDRESS and ORACLE_ADDRESS respectively.

Run the Chainlink Jobs

The Chainlink Job can be triggered by the client smart contract via the Oracle contract. Details of how the mechanism works can be found in our blog post.

The client smart contract can be written in Solidity v0.4 or v0.5. Samples for both versions have been provided, in directories v0.4 and v0.5 respectively.

Go to the directory for the version of Solidity you would like to use, and install the depdencies first:

npm i

You can then run the sample by launching the truffle test:

$ truffle test
Using network 'development'.


Compiling your contracts...
===========================
✔ Fetching solc version list from solc-bin. Attempt #1
✔ Downloading compiler. Attempt #1.
> Compiling ./contracts/EthPrice.sol
> Compiling ./contracts/Migrations.sol
> Compiling chainlink/contracts/Chainlink.sol
> Compiling chainlink/contracts/ChainlinkClient.sol
> Compiling chainlink/contracts/interfaces/ChainlinkRequestInterface.sol
> Compiling chainlink/contracts/interfaces/ENSInterface.sol
> Compiling chainlink/contracts/interfaces/LinkTokenInterface.sol
> Compiling chainlink/contracts/interfaces/PointerInterface.sol
> Compiling chainlink/contracts/vendor/Buffer.sol
> Compiling chainlink/contracts/vendor/CBOR.sol
> Compiling chainlink/contracts/vendor/ENSResolver.sol
> Compiling chainlink/contracts/vendor/Ownable.sol
> Compiling chainlink/contracts/vendor/SafeMath.sol


  Contract: EthPrice
    Solidity 0.4 based EthPrice contract
      ✓ deploys the EthPrice contract (167ms)
      ✓ Calling requestEthereumPrice() with job ID 4149eac8974b4daabadc11ce4eb3da03 (9380ms)


  2 passing (10s)


To verify that the job has been successfully triggered and executed, go to the Chainlink Operator UI and check the Job runs entries. A successful run should have a status of Complete.

Congratulations! You have successfully executed the Chainlink sample application! To get further details of integrating Chainlink to your application, visit the Kaleido Samples Gallery repository.

Got any comments about how the Chainlink service in Kaleido can be further improved? Or if your project has a need to talk with the outside world from your smart contracts and you would like to understand better how Chainlink can help you, please feel free to contact us at [email protected].

chainlink-samples's People

Contributors

jimthematrix avatar patrickalphac 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.