Introducing Chainlink, a decentralized solution to connect blockchain to external data

Tram Ho

1. What is Oracle? Limitations of oracle concentration

Oracle is a Greek term that refers to people who are able to communicate with supernatural forces, invisible or predicting the future. More simply, we call the universe prophet 😄

The term Oracle in the blockchain refers to a system that provides external data to smart contracts in the blockchain network. Through the above explanation, we see more or less the meaning is also related to the original meaning.

Currently, many Oracle services and systems have been introduced such as Provable , Augur , Ramp , etc. Basically, Oracle categories are divided into 2 types:

  • Oracle focused (Centrailized Oracle)
  • Decentralized Oracle

Limitations of oracle concentration

Oracle focuses on a single point of failure (Single Point of Failure), the data put into the smart contract can be completely changed and counterfeit, thus losing the trust and decentralization of the blockchain network.

2. What is ChainLink?

Chainlink is simply a decentralized oracle network, ensuring the reliability and integrity of the external data source put into the blockchain through consensus mechanisms. The ChainLink network consists of multiple nodes (oracle), which avoids the single point of failure that concentrated oracles encounter.

3. Architecture of ChainLink

In this section, we will explore Chainlink architecture with the Ethereum network

Chainlink architecture consists of 2 main parts:

  • The blockchain part (on-chain): User smart contract (User-SC), chainlink smartcontract (Chainlink-SC or Oracle Contract).
  • Off-chain blockchain network (off-chain): Chainlink Node (Oracle) and External API.

User-SC : Smart contracts are usually written in Solidity or Vyper, containing application logic.

Oracle contract : Smart contract on Ethereum, provided by Oracle node. Oracle contract is responsible for receiving request from user contract and transferring to oracle nodes for further processing.

Chainlink Node : As nodes in the Chainlink network, each node will have a core part containing the operation logic and the adapter part to help retrieve data from external APIs (get, post, …)

External API : External websites and data storage services.

4. Data flow

Data query

  1. The user contract will send a transaction to the Oracle contract (the transaction will consist of a number of LINK tokens and query request data).
  2. Oracle smart contract after getting enough LINK tokens from user contract (usually 1 LINK / request) will forward the query to the oracle nodes in the Chainlink network.

Returns data

  1. Once the oracle node has retrieved data from the API via the adapter, the oracle node will return the results to the oracle contract.
  2. The Oracle contract returns the result to the user contract along with paying a LINK token amount to the oracle node as a reward.

5. Chainlink network

Chainlink is a decentrailized network with many oracle nodes. To ensure that the data provided from the API is not tampered with or manipulated, an oracle contract mechanism should be chosen to select the appropriate value to return to the user contract from the return value of different oracle nodes. data from various sources).

Also, one problem that needs to be addressed is freeloading . Freeloading refers to the fact that oracle nodes copy the results of other oracle nodes without the effort of querying external APIs.

To solve the above problems, Chainlink proposed two solutions: In-contract aggregation and Medium-term strategy .

ETH / USD exchange rate is derived from at least 14 oracle nodes with different values

On-chain solution: In-contract aggregation

The idea of ​​this solution is that the oracle nodes will not immediately return the results to the oracle contract but encode the results to send to the oracle contract and then send the plaintext of the following query result. This will solve the relatively radical freeloading problem. Oracle contract will be the place to give the final result to return to the user contract.

Advantages : Simple, flexible (flexible) and reliable (due to the logic implemented on the blockchain network).

Cons : Need to make multiple transactions => costly transactions.

Off-chain solution: Medium-term strategy

The off-chain solution is to overcome the weakness of the on-chain solution, in order to save transaction fees (only need 1 transaction). The consolidation and unification of the results will be done with oracle nodes on the Chainlink network. The idea in this method is that each oracle nodes have a signature piece (Schnorr algorithm) to sign to choose the return value for the oracle contract.

Advantages : Only one transaction is needed, saving transaction fees.

Cons : Unable to completely solve the problem of freeloading.

To understand more details of the above solutions, you can read the White paper of Chainlink.

6. Write smart contracts

In this section, we will try to learn how to write smart contracts with Solidity using Chainlink to get the ETH / USD exchange rate.

Below is the source code of the smart contract

We will explain the meaning of the code bit by bit

  • First two lines to declare the soliity version and import the ChainlinkClient library.
  • Our contract is named Evi and inherits from the ChainlinkClient contract (so we can use chainlink).
  • Two variables ORACLE_PRICE and JOB_ID_PRICE are the addresses of oracle contract respectively to send requests to and JOB_ID.
  • The constructor uses setPublicChainlinkToken() ‘s setPublicChainlinkToken() function to get to only issue the LINK token on the network we deploy the contract (in this part we use testnet Ropsten).
  • The variable etherPrice will save the ETH / USD exchange rate when returned by the oracle contract.
  • The variable payment = 1 * LỊNK means 1 Token LINK (the amount of tokens paid to the oracle contract each time the data is requested).
  • The queryPrice function is responsible for making a request to the Oracle contract (specifying an API url, a data field).
  • The fullfullPrice function receives the result returned from the oracle contract, the function uses the modifier recordChainlinkFulfillment to ensure a valid result. The returned result is also saved to the etherPrice variable.

Demo on Remix

We made the above source code copy into the Remix IDE to conduct a demo.

Step 1 : Compile and deploy the contract on Ropsten network (here I use the compiler 0.5.12)

Step 2 : After deploying, we will send the LINK token to the contract (as a fee to send the oracle contract later). If the account does not have a LINK token, we can faucet at

Step 3 : Call the queryPrice function on Remix to get the ETH / USD exchange rate

Step 4 : After the transaction is successful, we take the value of the variable etherPrice . The return value has been multiplied by 100 to integer because solidity does not currently support floating-point numbers. The install times 100 by the return value from the API is done at the following command.

Finally, we get the ETH / USD rate at the time of writing is 1 ETH = 151.26 USD


Chia sẻ bài viết ngay

Nguồn bài viết : Viblo