Lesson 6 of 14
In Progress

Writing and Deploying a Simple Chainlink Contract

In this chapter, we’ll show you how to write and deploy a simple Chainlink contract using the concepts introduced in the previous chapter. We’ll use the Solidity programming language and the Ethereum Virtual Machine (EVM) to create a contract that retrieves data from an external source and stores it on the blockchain.

To follow along with this chapter, you’ll need to have a local Chainlink node running and connected to the Ethereum network. You’ll also need to have an Ethereum wallet app or command-line tool for deploying your contract and interacting with it.

Write the Contract

The first step in writing a Chainlink contract is to define the contract structure and specify the variables and functions that it will use. Here is a simple example of a Chainlink contract that retrieves the current price of Ethereum from an external API and stores it on the blockchain:

pragma solidity ^0.5.0;

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

contract EthereumPrice {
    ChainlinkClient public chainlink;
    uint256 public price;

    constructor(ChainlinkClient _chainlink) public {
        chainlink = _chainlink;
    }

    function updatePrice() public {
        chainlink.requestData(chainlink.ORACLE_ADDRESS, jobId);
    }

    function onData(bytes32 _jobId, uint256 _price) public {
        require(_jobId == jobId, "Invalid job ID");
        price = _price;
    }
}

This contract imports the ChainlinkClient contract and defines two variables: “chainlink” and “price”. The “chainlink” variable is an instance of the ChainlinkClient contract that is used to interact with the Chainlink network, while the “price” variable stores the current price of Ethereum.

The contract also defines two functions: “updatePrice” and “onData”. The “updatePrice” function sends a data request to the Chainlink Oracle contract, asking it to retrieve the current price of Ethereum from an external API. The “onData” function is called when the data request is fulfilled and stores the retrieved price in the “price” variable.

Compile the Contract

Once you have written your contract, the next step is to compile it. To compile the contract, you’ll need to use a Solidity compiler, such as Remix or Solc.

To compile the contract using Remix, open Remix in your web browser and paste the contract code into the left-hand pane. Then, click the “Compile” button in the top menu. This will compile the contract and display the compiled bytecode in the right-hand pane.

To compile the contract using Solc, open a terminal window and enter the following command:

solc <contract_file>.sol --bin

This will compile the contract and output the compiled bytecode to the terminal.

Deploy the Contract

Once you have compiled the contract, the next step is to deploy it to the Ethereum network. To deploy the contract, you’ll need to use an Ethereum wallet app or command-line tool, such as Mist or web3.

To deploy the contract using Mist, open Mist and select the “Contracts” tab. Then, click the “Deploy Contract” button and paste the compiled bytecode into the “Bytecode” field. Specify the contract name and any constructor arguments, if applicable. Then, click the “Deploy” button to deploy the contract to the Ethereum network.

To deploy the contract using web3, open a terminal window and enter the following command:

web3.eth.sendTransaction({
    from: <sender_address>,
    data: '<compiled_bytecode>',
    gas: '<gas_limit>'
})

Replace <sender_address> with the address of the Ethereum account that will be sending the transaction, <compiled_bytecode> with the compiled bytecode of the contract, and <gas_limit> with the maximum amount of gas that you are willing to spend on the transaction.

Interact with the Contract

Once you have deployed the contract, you can interact with it using an Ethereum wallet app or command-line tool.

To interact with the contract using Mist, open Mist and select the “Contracts” tab. Then, click the “Watch Contract” button and enter the contract address, name, and ABI (Application Binary Interface). This will add the contract to your list of watched contracts and allow you to call its functions and view its variables.

To interact with the contract using web3, open a terminal window and enter the following command:

web3.eth.contract(<abi>).at(<contract_address>).<function_name>()

Replace <abi> with the ABI of the contract, <contract_address> with the address of the contract, and <function_name> with the name of the function that you want to call.

Conclusion

In this chapter, we’ve shown you how to write and deploy a simple Chainlink contract that retrieves data from an external source and stores it on the blockchain. With this knowledge, you’ll be ready to start developing and deploying your own Chainlink contracts.

Exercises

To review these concepts, we will go through a series of exercises designed to test your understanding and apply what you have learned.

Write a Solidity contract that defines a function called “setMessage” that takes in a string parameter called “_message” and sets it to a public variable called “message”.

pragma solidity ^0.5.0;

contract SetMessage {
    string public message;

    function setMessage(string _message) public {
        message = _message;
    }
}

Write a Solidity contract that defines a function called “getMessage” that returns the value of the “message” variable. Make sure the function is marked as “view” and has a “returns” statement specifying the data type of the returned value.

pragma solidity ^0.5.0;

contract GetMessage {
    string public message;

    function setMessage(string _message) public {
        message = _message;
    }

    function getMessage() public view returns (string) {
        return message;
    }
}

Write a Solidity contract that defines a function called “updatePrice” that sends a data request to the Chainlink Database contract to retrieve the current price of Ethereum. Make sure the function takes in a “bytes32” parameter called “_jobId” and passes it to the “requestData” function.

pragma solidity ^0.5.0;

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

contract EthereumPrice {
    ChainlinkClient public chainlink;
    uint256 public price;

    constructor(ChainlinkClient _chainlink) public {
        chainlink = _chainlink;
    }

    function updatePrice(bytes32 _jobId) public {
        chainlink.requestData(chainlink.ORACLE_ADDRESS, _jobId);
    }
}

Write a Solidity contract that defines a function called “addNumbers” that takes in two “uint256” parameters called “a” and “b” and returns their sum.

pragma solidity ^0.5.0;

contract AddNumbers {
    function addNumbers(uint256 a, uint256 b) public pure returns (uint256) {
        return a + b;
    }
}

Write a Solidity contract that defines a function called “subtractNumbers” that takes in two “uint256” parameters called “a” and “b” and returns their difference.

pragma solidity ^0.5.0;

contract SubtractNumbers {
    function subtractNumbers(uint256 a, uint256 b) public pure returns (uint256) {
        return a - b;
    }
}