Chainlink node deployment tutorial

Time:2020-9-5

Chainlink node deployment tutorial

This paper mainly refers to the official documents of chainlink( https://docs.chain.link/docs/running-a-chainlink-node )If you can read English, you are recommended to read the official documents directly.

Installation required components

The server environment is ubuntu1804. We take the ropsten test network as an example. To build the main Ethernet network, you only need to modify the relevant configuration options.

Docker installation reference document

https://docs.docker.com/engine/install/ubuntu/

Add the current user to the docker user group

sudo usermod -aG docker $USER
exit
#Re login

PostgresSQL

Chainlink node uses postgressql as the only storage option after version 0.8. Postgressql database can be built by itself or provided by cloud service providers, as long as the database access interface can be provided.

Local installation reference:

https://www.postgresql.org/download/linux/ubuntu/

Ethereum client

The chainlink node connects with the Ethereum client node, obtains the user Oracle request on the chain through the connected Ethereum client, and writes the obtained data to the chain by submitting the transaction. Therefore, the chainlink node must rely on an Ethereum client node to function. You can build your own Ethereum client node, or you can choose the service provided by the Ethereum RPC service provider. You only need a websocket link to provide it to the chainlink node.

In the document of chainlink( https://docs.chain.link/docs/run-an-ethereum-client )It provides a method to build local Ethereum client nodes, and provides several external Ethereum API services for you to choose from. Due to the high hardware requirements of Ethereum client nodes, in this paper, we choose the free API provided by infra as the Ethereum client selection of chainlink node.

Deploy chainlink node

Create a chainlink directory

Ropsten: mkdir ~/.chainlink-ropsten

Configure environment variables

Ropsten:

echo "ROOT=/chainlink
LOG_LEVEL=debug
ETH_CHAIN_ID=3
MIN_OUTGOING_CONFIRMATIONS=2
LINK_CONTRACT_ADDRESS=0x20fE562d797A42Dcb3399062AE9546cd06f63280
CHAINLINK_TLS_PORT=0
SECURE_COOKIES=false
GAS_UPDATER_ENABLED=true
ALLOW_ORIGINS=*
ETH_URL=wss://ropsten.infura.io/ws/v3/<your-api-key>
DATABASE_URL=postgresql://postgres:[email protected]:5432/postgres
DATABASE_TIMEOUT=0" > ~/.chainlink-ropsten/.env

MIN_OUTGOING_CONFIRMATIONS: the minimum number of acknowledgement blocks required by a node to issue transactions. Before the minimum number of acknowledgement blocks is reached, the same run will not start the next task. If not set, the default value is 12.

LINK_CONTRACT_ADDRESS: the release address of the link token. Here are the link distribution addresses of the test network and the main network. You can configure it according to your own network. If not configured, the default is the link distribution address of the main network.

GAS_UPDATER_ENABLED: if set to true, the node will increase the gas cost and re commit when the transaction fails to commit. The default is false.

ETH_URL: address of Ethereum client websocket RPC interface. You can choose to build your own Ethereum client or a third-party service, as long as you provide the RPC interface address of websocket protocol.

DATABASE_URL: chainlink node needs to use postgressql database for persistent storage of some data. Similarly, you can choose to build your own database or use a third-party database service. You only need to provide the database address to allow the node to read and write data.

DATABASE_TIMEOUT: database connection timeout wait time. The default is 500ms.

Start node

cd ~/.chainlink-ropsten && docker run -p 6688:6688 -v ~/.chainlink-ropsten:/chainlink -it --env-file=.env smartcontract/chainlink local n

For the first time, the administrator is required to input a password, which is the keystore password of the node account. This password must be kept in mind. If it is lost, there is no way to retrieve it, and the funds managed by the private key will also be lost. Each time you start, the administrator is asked to enter this password to unlock the keystore. The node needs to sign the transaction through the private key of the keystore and submit it to the blockchain.

Next, the node administrator will be required to enter a pair of email accounts and passwords to log in to the node management page.

Test node

Log in to the browser management page

Open the IP: 6688 address in the browser to see the login entry of the chainlink node management page. With the email account and password that we fill in when starting the node, you can log in to the management interface.

Let’s go first**Configuration**Tab, you’ll find oneACCOUNT_ADDRESSThis address is the address corresponding to the private key of the keystore password that we set when starting the node for the first time. Before the node provides services, it needs to transfer this address to eth for submitting transactions.

Since we are using the ropsten test network, we can copy this address into the ropsten faucet to obtain the test eth. If you are an application serving Ethernet, you need to manually transfer the account address to eth.

Create Oracle contract

Oracle contract is the bridgehead of chainlink node on the blockchain. It communicates with the user contract directly, receives the Oracle request from the user, and writes the request result to the user’s contract through the callback function registered by the user.

The Oracle contract does not need to be written by ourselves. We can directly deploy the open source Oracle contract code of chainlink. If you deploy with remix, you only need to create a new file containing the following two lines of code:

pragma solidity 0.6.6;

import "https://github.com/smartcontractkit/chainlink/evm-contracts/src/v0.6/Oracle.sol";

It should be noted that the Oracle contract will temporarily accept the link fee paid by the user to initiate the chainlink request. Therefore, it is necessary to keep the private key of the contract owner properly.

After the Oracle contract is deployed, open permissions should be given to the address of the node to allow the node to commit transactions.

Chainlink node deployment tutorial

Create jobs

Follow the example in the document to add a job to the node.

Open the jobs tab of the node management interface, click new job to add a new job, copy the JSON of each type of job to the text box, and replace the JSON in JSON with the Oracle contract address just deployedYOUR_ORACLE_CONTRACT_ADDRESSField. Each job created successfully will generate a job ID. all jobs can be found in the jobs tab.

Test node availability

Next, let’s test the availability of our deployed chainlink service.

pragma solidity ^0.6.0;

import "https://raw.githubusercontent.com/smartcontractkit/chainlink/develop/evm-contracts/src/v0.6/ChainlinkClient.sol";

contract APIConsumer is ChainlinkClient {
  
    uint256 public ethereumPrice;
    
    address private oracle;
    bytes32 private jobId;
    uint256 private fee;
    
    constructor() public {
        setPublicChainlinkToken();
    //Fill in the Oracle address we just deployed
        oracle = 0x5F66a231a29CE1513dc6c16407fDCe9D0aEE2cB0;
    //Fill in the address we just created
        jobId = "4b9b6e7d89154a8d855affed3985aafd";
    //The minimum fee paid to Oracle can be found in the configuration page minimum_ CONTRACT_ View the payment field
        fee = 1 * 10 ** 18; // 1 LINK
    }
    
    function requestEthereumPrice() public returns (bytes32 requestId) 
    {
        Chainlink.Request memory request = buildChainlinkRequest(jobId, address(this), this.fulfill.selector);
        
        //Set the API address to be requested
        request.add("get", "https://min-api.cryptocompare.com/data/price?fsym=ETH&tsyms=USD");
        
        //The resolution path of the API return result
        request.add("path", "USD");
        
        //Multiply the result to remove the decimal point
        request.addInt("times", 100);
        
        //Send request
        return sendChainlinkRequestTo(oracle, request, fee);
    }
    
    function fulfill(bytes32 _requestId, uint256 _price) public recordChainlinkFulfillment(_requestId)
    {
        ethereumPrice = _price;
    }
}

After deploying the user contract, transfer to link to the user contract, and callrequestEthereumPriceMethod, we can test whether our chainlink Oracle node can work normally.

Join the chainlink developer community

Chainlink node deployment tutorial