Solidity is an object-oriented, high-level programming language used primarily for writing smart contracts on blockchain platforms, most notably Ethereum. Influenced by C++, it is designed to make it easier to build decentralized applications (DApps) and execute smart contracts securely.
Smart contracts are self-executing transactions that run on the blockchain, automatically enforcing and executing the terms of a contract when predefined conditions are met. This eliminates the need for intermediaries, making transactions more secure, transparent, and efficient.
For example, consider a basic smart contract that allows users to store and retrieve a number:
// SPDX-License-Identifier: GPL-3.0pragma solidity >=0.7.0 <0.9.0;/*** @title DataStorage* @dev Store & retrieve an unsigned integer value*/contract DataStorage {uint256 private storedNumber;/*** @dev Save a value to the storedNumber variable* @param num the value to be saved*/function save(uint256 num) public {storedNumber = num;}/*** @dev Retrieve the stored value* @return the value of 'storedNumber'*/function get() public view returns (uint256) {return storedNumber;}}
Let's breakdown the code written above:
Lines 3: We set the version of the solidity compiler to be used. It ensures that the code is compiled with a Solidity version between 0.7.0
(inclusive) and 0.9.0
.
Line 9: We declare the start of the contract named DataStorage
. Contracts in solidity are similar to classes in other programming languages.
Line 10: We declare a state variable named storedNumber
of type uint256
(unsigned 256-bit integer). The private
keyword means that this variable can only be accessed from within the contract.
Line 16: We declare a function named save
that takes a single parameter num
of type uint256
. The public
keyword means this function can be called from outside the contract.
Line 17: We assign the value of num
to the state variable storedNumber
.
Line 24: We declare a function named get
that returns a uint256
value. The public
keyword means this function can be called from outside the contract. The view
keyword indicates that this function does not modify the state of the contract.
Line 25: We return the value of the state variable storedNumber
.
While smart contracts are powerful, they are limited to the data available on the blockchain.
However, many applications require external data, such as real-time prices, weather conditions, or sports scores. This is where oracles come into play. Oracles act as bridges between the on-chain world of smart contracts and off-chain external data, allowing smart contracts to interact with real-world information.
In context of Web3 and blockchain technology, an oracle is a third-party service that provides smart contracts with external information. Since smart contracts operate within the blockchain environment, they typically don’t have direct access to real world data such as stock prices, weather conditions, or sport scores.
Oracles bridge this gap by fetching and delivering off-chain data to on-chain smart contracts.
Depending on the information they deliver, oracles can be categorized into the following types:
Price oracles: These provide real-time price feeds for assets such as cryptocurrencies, stocks, and commodities.
Event oracles: Supply information about specific events, such as election outcomes, sports scores, or weather conditions.
Randomness oracles: Generate random numbers securely for applications like gaming and lotteries.
Custom oracles: Fetch custom data based on specific requirements defined by smart contracts.
To integrate oracles into Solidity contracts, developers typically use oracle services like Chainlink, which provide reliable and tamper-proof data feeds. The smart contract makes a request to the oracle, which then fetches the required data and returns it to the contract. This process involves:
Creating a Request: The smart contract sends a request to the oracle service for specific data.
Processing the Request: The oracle fetches the requested data from external sources.
Returning the Data: The oracle sends the retrieved data back to the smart contract, which then uses it to execute its predefined logic.
Solidity enables the creation of autonomous smart contracts on platforms like ethereum, powering various decentralized applications (DApps). However, smart contracts often need external data, which oracles provide by bridging on-chain contracts with off-chain information sources. Oracles are categorized into price, event, randomness, and custom oracles, enriching smart contracts’ capabilities for real-world interactions. As blockchain technology advances, oracles become pivotal in expanding DApp functionalities, ensuring secure and decentralized data access, and accelerating the adoption of Web3 technologies.
What is the primary purpose of a smart contract in Solidity?
To store and execute predefined conditions autonomously on the blockchain
To monitor blockchain transactions
To provide real-time data feeds to applications
To manage cryptocurrency wallets
Free Resources