The Ultimate Guide to Learn Solidity

Solidity language information in one blog

Param_eth
Coinmonks
Published in
6 min readDec 3, 2022

--

solidity cheatsheet:

Solidity

Solidity is a high-level, contract-oriented programming language used for writing smart contracts that run on the Ethereum platform. Solidity was developed specifically for writing smart contracts on the Ethereum platform, and is designed to be easy to learn and use for developers with experience in object-oriented programming languages.

In simple terms, Solidity allows developers to write self-executing contracts that run on the Ethereum blockchain. These contracts can be used to create decentralized applications (DApps) that can run on the Ethereum network, allowing users to interact with the blockchain and securely store and transfer digital assets.

Solidity is a statically-typed language, meaning that variable types must be declared before they can be used. It supports various data types, including integers, booleans, and strings, as well as complex data structures such as arrays and mappings. Solidity also includes support for inheritance, allowing developers to create complex, modular contracts that can be easily extended and modified.

Solidity variables:

In the Solidity programming language, variables are used to store data values that can be used within a contract. Solidity is a statically-typed language, meaning that variables must be declared with a specific type before they can be used.

There are several different types of variables that can be used in Solidity, including:

  • bool: This type represents a boolean value (true or false).
  • int and uint: These types represent signed and unsigned integers, respectively. The size of the integer (8 bits, 16 bits, etc.) must be specified when declaring the variable.
  • address: This type represents an Ethereum address.
  • bytes: This type represents a sequence of bytes. The size of the bytes must be specified when declaring the variable.
  • string: This type represents a string of Unicode characters.

Here is an example of declaring and initializing some variables in Solidity:

bool success = true;
int8 number = -5;
uint256 count = 100;
address owner = 0x1234567890abcdef;
bytes32 data = 0xabcdef0123456789abcdef0123456789;
string message = "Hello, world!";

six variables are declared and initialized with different data types. The success variable is a boolean, the number variable is a signed 8-bit integer, the count variable is an unsigned 256-bit integer, the owner variable is an Ethereum address, the data variable is a sequence of 32 bytes, and the message variable is a string.

Overall, variables are an important part of programming in Solidity. They allow developers to store and manipulate data within a contract, making it possible to build complex, dynamic applications on the Ethereum platform.

Bytes in solidity

The bytes data type is similar to the string data type, but unlike strings, which are encoded using a specific character encoding (such as ASCII or UTF-8), the bytes data type is a raw sequence of bytes and does not have a specific encoding.

To declare a variable of the bytes data type, you can use the bytes keyword followed by the variable name, like this:

bytes myBytes;

You can also specify the length of the byte array using the bytesN syntax, where N is the length of the byte array in bytes. For example:

bytes32 myBytes32; // Declares a byte array with 32 bytes

Once a bytes variable has been declared, you can assign values to it using the hex keyword, followed by the hexadecimal representation of the byte array. For example:

myBytes = hex"01020304"; // Assigns the byte array [0x01, 0x02, 0x03, 0x04] to myBytes

In addition to the hex keyword, the bytes data type also supports the abi.encode and abi.encodePacked functions, which can be used to encode data in a specific format for use in function calls or other contexts.

Msg.sender and Msg.value

msg.sender refers to the address of the account that called the current function. This can be used, for example, to determine the caller's identity or to implement access control checks.

msg.value refers to the value of the transaction, in wei, that called the current function. This can be used to check the amount of Ether sent with the transaction, or to transfer the value to another account.

Here is an example of how msg.sender and msg.value can be used in a Solidity contract:

contract ExampleContract {
// Store the address of the contract owner
address public owner;
constructor() public {
// Set the contract owner to the caller of the constructor
owner = msg.sender;
}
function withdrawFunds(uint amount) public {
// Check that the caller is the contract owner
require(msg.sender == owner, "Only the contract owner can withdraw funds");
// Check that the caller sent at least the amount they are trying to withdraw
require(msg.value >= amount, "Insufficient funds");
// Transfer the amount to the caller
msg.sender.transfer(amount);
}
}

the withdrawFunds function uses msg.sender to check that the caller is the contract owner, and uses msg.value to check that the caller sent at least the amount they are trying to withdraw. If the checks pass, the function uses msg.sender.transfer to transfer the specified amount of Ether to the caller.

msg.sender and msg.value are useful built-in variables that provide information about the transaction or message that called the current function in a Solidity contract. They can be used to implement access control checks, transfer values, and more.

functions in solidity

Functions in Solidity have the following general syntax:

function functionName(parameter1, parameter2, ...) visibility_modifier returns (return_type1, return_type2, ...) {
// Function body
}

Here, functionName is the name of the function, parameter1, parameter2, etc. are the function's parameters, visibility_modifier is a keyword that determines whether the function is public or private, and return_type1, return_type2, etc. are the types of the values that the function returns.

For example, the following code defines a public function named addNumbers that takes two uint (unsigned integer) parameters and returns a single uint value:

function addNumbers(uint a, uint b) public returns (uint) {
return a + b;
}

Functions can be called from within the contract using the functionName(arg1, arg2, ...) syntax, or from outside the contract by sending a transaction to the contract's address.

Modifiers are applied to functions using the modifier keyword, and can be used to implement access control, argument validation, and other functionality.

Inheritance in solidity

Inheritance is useful because it allows developers to create complex, modular contracts that can be easily extended and modified. Instead of copying and pasting code from one contract to another, a derived contract can simply inherit the code and behavior of the base contract, and add its own customizations as needed.

To implement inheritance in Solidity, the is keyword is used in the derived contract to specify the base contract that it inherits from. For example:

contract BaseContract {
// Code and behavior defined in the base contract
}

contract DerivedContract is BaseContract {
// Code and behavior defined in the derived contract, which
// inherits from the base contract
}

in this example, the DerivedContract contract inherits from the BaseContract contract. This means that the DerivedContract contract will have access to all of the code and behavior defined in the BaseContract contract, as well as any code and behavior it defines itself.

Varibale Visibility

A public variable is visible from outside the contract, and can be accessed by calling the contract's public functions. For example:

contract ExampleContract {
// Public variable
uint public variable;

function setVariable(uint value) public {
variable = value;
}

function getVariable() public view returns (uint) {
return variable;
}
}

the variable variable is marked as public, which means it can be accessed from outside the contract. The setVariable function allows the variable to be set to a new value, and the getVariable function allows the variable to be read by calling the contract's public functions.

A private variable, on the other hand, is not visible from outside the contract, and can only be accessed from within the contract. For example:

contract ExampleContract {
// Private variable
uint private variable;

function setVariable(uint value) public {
variable = value;
}

function getVariable() public view returns (uint) {
// Access the private variable within the contract
return variable;
}
}

the variable variable is marked as private, which means it can only be accessed from within the contract. The setVariable function can still set the variable to a new value, but the getVariable function cannot return the variable value to the caller, because it is marked as private.

Thank you Guys

Follow me on Twitter: @Param_eth

New to trading? Try crypto trading bots or copy trading

Join Coinmonks Telegram Channel and Youtube Channel get daily Crypto News

Also, Read

--

--

Param_eth
Coinmonks

I help peoples learn web3 development through my blogs.