Digital Communication


Digital Communication

Solidity: the language of smart contracts


Solidity allows you to program sophisticated smart contracts on the Ethereum blockchain. It offers unique interesting approaches that set it apart from other languages.

What is Solidity?

Solidity is a high-level programming language for creating smart contracts on the Ethereum blockchain. Smart contracts are self-executing contracts that automate the exchange of assets between parties. The particularity is that no intermediary is required to ensure compliance with this contract.

Solidity source code is compiled into byte code and made available on the Ethereum blockchain as a smart contract. The smart contract can then be executed from each node in the network, but the state is recorded on the blockchain. Here is an example of a simple contract for a distributor of non-fungible tokens (non-fungible tokens or NFT in English):

pragma Solidity 0.8.7;

contract NFTVendingMachine {

  // Declare state variables
  address public owner;
  mapping (address => uint) public nftBalance;

  // Run on deployment
  constructor() {
    owner = msg.sender;
    nftBalance[address(this)] = 100;

  // Allow the owner to restock the NFT balance
  function restock(uint amount) public {
    require(msg.sender == owner, "Only the owner can restock.");
    nftBalance[address(this)] += amount;

  // Allow anyone to purchase NFTs
  function purchase(uint amount) public payable {
    require(msg.value >= amount * 1 ether, "You must pay at least 1 ETH per NFT");
    require(nftBalance[address(this)] >= amount, "Not enough NFTs in stock to complete this purchase");
    nftBalance[address(this)] -= amount;
    nftBalance[msg.sender] += amount;


What applications can Solidity be used for?

Solidity was specially designed for creation of decentralized applications, dApp (“distributed applications”), which are operated on Ethereum virtual machines (EVM). Smart contracts are particularly suitable for managing digital assets, creating decentralized stock markets and implementing voting systems.

Web hosting with personal advisor!

Powerful, flexible and efficient web hosting with email, personal advisor and domain included 1time year !


SSL certificate

24/7 support

Decentralized Finance (DeFi)

Solidity is used for the development of DeFi applications such as decentralized stock markets, credit and rental platforms, predictive markets and cryptocurrencies. DeFi has grown to become one of the preferred applications for blockchain technology. Solidity has thus become one of the essential tools for setting up DeFi applications in the Ethereum network.

Non-fungible tokens

The non-fungible token (JNF) has enjoyed great popularity since 2020. NFTs are unique digital assets recorded on the blockchain. It is therefore, for example,digital artwork, sports memorabilia, or gaming industry artifacts. Solidity is used to create smart contracts based on NFTs.

Supply chain management

Thanks to Solidity, smart contracts can be generated for the control and management of supply chains. They are used to automate various supply chain processes including tracking stock movement, checking product authenticity and executing payments between parties.

Voting systems

With Solidity you can generate smart contracts that implement secure and transparent voting systems on the blockchain. Contracts can be used to ensure that votes have been counted and that the electoral process is fair and transparent.

What are the advantages and disadvantages of Solidity?

Overall, Solidity is a powerful language for creating smart contracts on the Ethereum blockchain. Like all technologies, however, Solidity has specific advantages and disadvantages that developers should be aware of when creating smart contracts. Regardless, developing secure smart contracts requires a certain level of technical knowledge and rigor.

We take the example of a smart contract acting like a black hole : any ether sent to the contract will be irremediably “absorbed”. There is no way to withdraw ether:

// SPDX-License-Identifier: GPL-3.0
pragma solidity >= 0.9.0;

// This contract swallows all Ether sent to it
contract Blackhole {
  event Received(address, uint);
  receive() external payable {
      emit Received(msg.sender, msg.value);


Advantages of Solidity

  • Flexibility : Solidity is a general-purpose language. It allows you to develop very different smart contracts for a multitude of applications.
  • Security : Solidity was developed with security in mind. The language contains features such as access controls, exception handling, and failure mechanisms that help developers write secure contracts.
  • Compatibility with Ethereum : Solidity is currently the priority language for creating smart contracts on the Ethereum blockchain.
  • Large community : a large community of blockchain developers has formed around Solidity. It is home to many resources to learn and solve your problems.

Disadvantages of Solidity

  • Learning curve : Solidity presents a relatively steep learning curve for developers new to blockchain and smart contract creation.
  • Unalterability : as soon as a smart contract is made available on the blockchain, it can no longer be modified. When writing and testing, developers must therefore be extremely vigilant.
  • Lack of formal verification : Solidity does not have any built-in tools to perform formal code checks. This means that developers are reliant on external tools to ensure the accuracy of their contracts.
  • Limited tools : Solidity’s ecosystem of tools is still relatively underdeveloped. This creates potential problems with integrated development environments (IDEs), testing frameworks, and other development tools.

What does Solidity’s basic syntax look like?

Solidity is a object-oriented language, specialized for smart contracts, which was influenced by JavaScript, Python and C++. The language’s syntax is similar to JavaScript while displaying interesting unique features.

Variables in Solidity

At first glance, variables in Solidity seem to work like they do in similar languages. However, a notable difference exists: the Ethereum virtual machine (or Ethereum Virtual Machine (EVM) in English) is used as the execution environment. All the Operations performed in the EVM, as well as data logging consume a given volume of “gas”. This implies that when programming you need to think about, where appropriate, the most efficient way possible to implement an operation.

In addition to the “normal” variables, Solidity has constants that must be defined during compilation. Constants require less gas than recording:

// Regular variable can be declared without defining
int a;
// Constant needs to be defined at declaration
int constant b = 51;


“Immutable” variables behave similarly. They also consume less gas and cannot be changed after assignment. Unlike “constant” variables, the assignment must take place during execution.

Solidity ordering instructions

As an imperative programming language, Solidity supports known command statements, such as branches and loops. Here is the code allowing you to choose the larger of two numbers (between “a” and “b”):

int largerNumber = 0;
// If-else statement
if (a > b) {
  largerNumber = a;
} else {
    largerNumber = b;


There “for” loop in Solidity corresponds to the known syntax of JavaScript or C++:

// Loop 10 times
for (int i = 0; i < 10; i++) {
  // …


There “while” loop also works as usual. Here we combine a stop condition with a digital counter variable:

bool continueLoop = true;
int counter = 0;
// Loop at most 10 times
while (continueLoop && counter < 10) {
  // …


Simple types in Solidity

Solidity is a standardized static type language which supports types generally existing in programming languages. Simple types that represent individual values ​​include Booleans, numbers, and strings.

In Solidity, Booleans represent the values ​​“true” and “false”. They can be combined with corresponding known Boolean operators and used in “if” statements:

bool paymentReceived = true;
bool itemsStocked = true;
bool continueTransaction = paymentReceived && itemsStocked;

if (continueTransaction) {
  // ...


Solidity is compatible with a wide spectrum of numeric types. With integers, a difference is made between signed numbers (“int”) and unsigned numbers (“uint”), the latter however having to be only positive. Additionally, the range of a digit is determined in 8-bit increments, from “int8” to “int16” to “int265”.

uint8 smallNumber = 120;
int8 negativeNumber = -125;

int8 result = smallNumber + negativeNumber;
assert(result == -5)


Strings are mainly used in Solidity when generating status messages. The language is compatible with single and double quotesas well as with Unicode characters:

string message="Hello World";
string success = unicode"Transfer sent ";


Functions in Solidity

As in most programming languages, functions are an integral part of Solidity. Defining a function is reminiscent of JavaScript, but the argument types must be specified explicitly. Furthermore, the keyword “returns” is used to characterize the types of return values:

// Define a function
function addNumbers(int a, int b) returns (int) {
  return a + b;


L’calling a function proceeds as follows:

// Call the function
int result = addNumbers(2, 3);


Curiously, the return values ​​can also be named, similar to named arguments. In this case, it is enough to assign the corresponding variables in the body of the function. It is then no longer necessary to make an explicit return using “return”:

function divideNumbers(int dividend, int divisor) returns (int quotient) {
  quotient = dividend / divisor;
  // No `return` necessary


As with “constant” or “immutable” variables, in Solidity, the functions can be marked as non-modifying. The keywords “view” and “pure” are then used. A “view” function does not modify the state while the “pure” function also ensures that it does not read state variables.

Smart contracts in Solidity

Besides the common types, Solidity also knows a few types specific to smart contracts. The base type is “address” and represents Ethereum addresses. “Payable” type addresses can benefit from ether transfers. “Payable” type addresses implement the “balance()” and “transfer()” methods.

// Get address of this contract
address mine = address(this);
// Get payable external address
address payable other = payable(0x123);
// Transfer if balances fulfill conditions
if (other.balance < 10 && mine.balance >= 100) {


Based on the “address” type, the “contract” type acts as a central language element. Contracts are more or less similar to classes in object-oriented programming languages. Contracts thus group together state data and functions, and protect them from the outside world. They are compatible with several inheritances, as is the case in Python or C++.

The contracts generally begin with a “pragma” line which indicates the authorized version of Solidity, followed by the definition itself:

// Make sure Solidity version matches
pragma Solidity >=0.7.1 <0.9.0;

// Contract definition
contract Purchase {
  // Public state variables
  address seller;
  address buyer;
  // View-function
  function getSeller() external view returns (address) {
    return seller;


Smart contracts can define state data and functions. Just like in C++ and Java, you can define one access level among the three available:

  • “public”: possibility ofread and write access to the variable from the contract. Additionally, a “view” function is automatically generated as a means of gaining read access from the outside.
  • “internal”: the variable is protected against external access. Access is allowed for reading and writing from a contract and from a legacy contract.
  • “private”: similar to the “internal” level, but does not include no access from legacy contracts.

Functions can also be defined as “external”. An “external” function acts as an integral part of the contract interface and is used for access from the outside. The important “receive” function, which allows you to receive ether, is a well-known example:

// Define without `function` keyword
receive() external payable {
  // Handle Ether


Modifiers in Solidity

Modifiers are an interesting language element in Solidity. The function is reminiscent of Python’s decorators. As with the latter, modifiers are used to modify the call of a function. They are frequently used to ensure that a condition is met before a function is executed:

contract Sale {
  uint price;
  address payable owner;

  modifier onlyOwner {
    // Will throw error if called by anyone other than the owner
      msg.sender == owner,
      "Only owner can call this function."
    // The wrapped function's body is inserted here
  // `onlyOwner` wraps `changePrice`
  function changePrice(uint newPrice) public onlyOwner {
    // We'll only get here if the owner called this function
    price = newPrice;


Transaction management with Solidity

Solidity has an integrated transaction management system. This ensures that a ether transfer was executed in full or not at all. To do this, the language uses the special keyword “revert” which triggers the repetition (roll-back) of a transaction. The “error” keyword allows you to define your own error codes:

// Custom error definition
error InsufficientPayment(uint256 paid, uint256 required);
// Contract representing a sale
contract Sale {
  uint price;
  // Purchase if enough ether transferred
  function purchase() public payable {
    if (msg.value < price) {
      revert InsufficientPayment(msg.value, price);
    // Complete purchase


Another “frequent pattern”: the “require()” function. Its use is similar to that of the “revert” function:

// Using `require()` function
if (!condition) revert("Error message");
// Equivalent to
require(condition, "Error message");


Télécharger notre livre blanc

Comment construire une stratégie de marketing digital ?

Le guide indispensable pour promouvoir votre marque en ligne

En savoir plus

Souhaitez vous Booster votre Business?

écrivez-nous et restez en contact


© 2024 AMZ DIGICOM All Rights Reserved