Now that you have your local Chainlink node set up and running, it’s time to start developing your own smart contracts. In this chapter, we’ll introduce you to the basics of smart contract development, including the Solidity programming language, the structure of smart contracts, and the Ethereum Virtual Machine (EVM).
What is a Smart Contract?
A smart contract is a self-executing contract with the terms of the agreement between buyer and seller being directly written into lines of code. The code and the agreements contained therein are transparent and immutable, meaning they are open for anyone to see and can’t be altered. Smart contracts are stored on a blockchain, which is a decentralized database that allows them to be securely and transparently executed without the need for a third party.
Smart contracts are often used to facilitate, verify, and enforce the negotiation or performance of a contract. They can be used for a wide range of applications, including financial transactions, supply chain management, and voting systems.
The Solidity Programming Language
Smart contracts are written in a programming language called Solidity. Solidity is a high-level, object-oriented language that is similar to JavaScript and was specifically designed for writing smart contracts on Ethereum.
Here is a simple example of a Solidity contract that defines a simple “Hello World” message:
pragma solidity ^0.5.0;
contract HelloWorld {
string public message;
constructor() public {
message = "Hello, World!";
}
}
This contract defines a string variable called “message” and a constructor function that initializes the value of “message” to “Hello, World!”. The “public” keyword makes the “message” variable visible to external contracts and users.
The Structure of Smart Contracts
Smart contracts typically have a specific structure, with different sections for defining variables, functions, and other elements. Here is the general structure of a Solidity contract:
pragma solidity ^0.5.0;
contract MyContract {
// Variables
string public message;
// Functions
function setMessage(string _message) public {
message = _message;
}
function getMessage() public view returns (string) {
return message;
}
}
In this example, the contract defines two functions: “setMessage” and “getMessage”. The “setMessage” function allows external contracts and users to set the value of the “message” variable, while the “getMessage” function allows them to retrieve the value of the “message” variable. The “public” keyword makes both functions visible and callable from external contracts and users. The “view” keyword specifies that the “getMessage” function does not modify any state variables and can be called without requiring a transaction. The “returns” keyword specifies the data type of the value returned by the function.
The Ethereum Virtual Machine (EVM)
The Ethereum Virtual Machine (EVM) is the runtime environment for executing smart contracts on the Ethereum blockchain. When a smart contract is deployed to the Ethereum network, it is compiled into bytecode and stored on the blockchain. The EVM then executes the bytecode whenever a contract function is called.
The EVM is a stack-based virtual machine, meaning it executes code by pushing and popping values from a stack. It also has a memory area and a storage area for storing data. The memory area is temporary and is reset after each contract call, while the storage area is permanent and persists between contract calls.
The EVM has its own internal currency called Ether, which is used to pay for the execution of contract code. When a contract function is called, the caller must include a payment of Ether to cover the cost of executing the code. The cost of executing code is measured in “gas”, which is a unit of measurement for the amount of computational work required to execute a contract.
Conclusion
In this chapter, we’ve introduced you to the basics of smart contract development, including the Solidity programming language, the structure of smart contracts, and the Ethereum Virtual Machine (EVM). In the next chapter, we’ll show you how to write and deploy a simple Chainlink contract using these concepts.
Exercises
To review these concepts, we will go through a series of exercises designed to test your understanding and apply what you have learned.
What is the purpose of the “pragma” directive in Solidity?
The “pragma” directive is used to specify the version of Solidity that the contract is written in. It helps to ensure that the contract is compatible with the version of Solidity that is being used to compile it.
What is the purpose of the “public” keyword in Solidity?
The “public” keyword is used to make variables and functions visible and callable from external contracts and users.
What is the purpose of the “view” keyword in Solidity?
The “view” keyword is used to specify that a function does not modify any state variables and can be called without requiring a transaction.
What is the purpose of the “returns” keyword in Solidity?
The “returns” keyword is used to specify the data type of the value returned by a function.
What are the differences between the memory area and the storage area in the Ethereum Virtual Machine (EVM)?
The memory area is temporary and is reset after each contract call, while the storage area is permanent and persists between contract calls. The memory area is used to store data that is only needed for the duration of a contract call, while the storage area is used to store data that needs to be persisted between contract calls.