This is behind the programming language for Web3
The concept of so-called smart contracts has been around since 1994. Nick Szabo compared them to digital vending machines at the time. Basically, smart contracts are just programs – code and data on a blockchain. They are executed when previously defined conditions are met. They are typically used to automate the execution of an agreement so that everyone involved knows the outcome immediately, without delays or the need for a middleman. Entire processes can also be automated with the help of smart contracts. If the conditions defined in a smart contract are met, the next action is triggered.
For example, Anna could sign a contract with Johannes that he would take on the role of photographer at her wedding for an amount of 0.2 Eth. You record the terms of the contract in a smart contract written in Solidity on the blockchain:
pragma solidity ^0.8.10;
contract WeddingPhotos {
address payable public seller;
address payable public buyer;
event PurchaseConfirmed();
event ItemReceived();
function confirmPurchase() public {
// ...
}
}
A smart contract is not controlled by people, but has its own internal code. Because it is on the blockchain, it cannot be tampered with and there is no room for interpretation. If Johannes properly does his job to take photos at Anna’s wedding, the contract will be carried out and money will be transferred to Johannes. There are many other potential use cases such as decentralized Web3 apps, decentralized financial products or trading in digital assets in the form of so-called non-fungible tokens, or NFTs for short.
Solidity was launched in 2014. It is a so-called high-level programming language for writing smart contracts with a syntax that is similar to languages such as C ++ or TypeScript and JavaScript. Solidity is compiled, statically typed and borrows from structures from object-oriented programming. Solidity is mostly mentioned in the same breath as Ethereum, but is also supported by other EVM-compatible platforms, for example Tron, Tendermint or Kin.
To write your first smart contract, you have to create a file with the extension .sol in the editor of your choice. The Solidity project on GitHub recommends Remix, a browser-based IDE to get you started. Here you can write and manage your first Smart Contracts with Solidity without local installation.
That pragma
-Keyword signals to the compiler which version of Solidity should be used.
pragma solidity ^0.8.10;
contract MyContract {
string public greeting = "Moini Friends";
address payable public seller;
address payable public buyer;
struct Order {
string description;
bool completed;
}
function confirmOrder() public {
buyer = payable(msg.sender);
}
constructor(string memory _text) {
greeting = _text;
}
}
The keyword contract
defines the details of your smart contract. That contract
-Keyword is similar to a class in JavaScript, TypeScript or Python. Variables or a so-called state are defined within the curly brackets. Their value can change during the life cycle of the contract. Solidity has a special data type called address
which is used to store an Ethereum address. With the help of struct
-Keywords you can define more complex data types and thus represent the different entities of your smart contract. With the help of function
-Keywords can be used to define functions that can be used to change the state of the smart contract.
Every smart contract programmed in Solidity also has one constructor
. A constructor
is a special, optionally definable function that initializes so-called state variables of a smart contract. If this is not explicitly defined, the smart contract still has a default constructor. after the constructor
Function has been executed, the final code is deployed on the blockchain.
After you have defined the smart contract, it must be compiled so that you can use it programmatically with an API of your choice. If you want to instantiate and manage your smart contracts in JavaScript, you can use ether.js or Web3.js, for example.
If you want to delve deeper into the subject after this brief overview, you will find it in Project on GitHub more information and on the associated website a detailed tutorial.