Solidity Complete Fundamentals Cheat Sheet

luislucena16

Luis Lucena

Posted on September 1, 2022

Solidity Complete Fundamentals Cheat Sheet
  • What is Solidity?

It is a contract-oriented high level language for implementing smart contracts. It was influenced by C++, JavaScript and Python and is designed to target the EVM.

  • Contracts

Contracts in solidity are similar to classes in object oriented language.
A contract can be created using "new" keyword.

contract L {
function add(unit_p, unit_q) returns(unit){
return_p+_q;
}
}
contract M{
address p;
function f(unit_p){
p = new L();
}
}
Enter fullscreen mode Exit fullscreen mode
  • Smart Contracts

It is a computer protocol which is used to streamline the process of contracts by digitally enforcing, verifying and managing them.

  • Remix, Solium & Doxity
- Remix: It is a browser based IDE with integrated compiler and solidity run-time environment without server side components.
Enter fullscreen mode Exit fullscreen mode
- Solium: A linter to identity and fix style security issues in solidity.
Enter fullscreen mode Exit fullscreen mode
- Doxity: Used in solidity as a documentation generator.
Enter fullscreen mode Exit fullscreen mode
  • Solograph & Assembly
- Solograph: Used to visualize solidity control flow and highlight potential security vulnerabilities.
Enter fullscreen mode Exit fullscreen mode
- Solidity Assembly: An assembly language which is used without solidity & inline assembly inside solidity source code.
Enter fullscreen mode Exit fullscreen mode
  • Gas & Block Gas Limit
- Gas: A measurement roughly equivalent to the computational steps.
Enter fullscreen mode Exit fullscreen mode
- Block Gas limit: It is used to control the amount of gas consumed during the transactions.
Enter fullscreen mode Exit fullscreen mode
  • Solidity Compiler
        Contract Definition
                |
         Solidity Compiler
                |
      Byte code - Application Binary Interface (ABI)
Enter fullscreen mode Exit fullscreen mode
  • ABI & WEI
- ABI: A data encoding scheme called "Application Binary Interface" (ABI) is used in for working with smart contracts.
Enter fullscreen mode Exit fullscreen mode
- WEI: The smallest denomination or base unit of Ether.
Enter fullscreen mode Exit fullscreen mode
  • Global Variables
- block.blockhash(uint numberOfBlock)returns(bytes32): hash function of the given block which works for the 256 most recent blocks excluding the current block.
Enter fullscreen mode Exit fullscreen mode
- block.coinbase(address): shows miner's address of current block.
Enter fullscreen mode Exit fullscreen mode
- block.numer(uint): number of current block.
Enter fullscreen mode Exit fullscreen mode
- block.gaslimit(uint): gaslimit of the current block.
Enter fullscreen mode Exit fullscreen mode
- block.timestamp(uint): timestamp of current block.
Enter fullscreen mode Exit fullscreen mode
- msg.gas(uint): the gas that remains.
Enter fullscreen mode Exit fullscreen mode
- msg.value(uint): the amount of WEI sent along with the message.
Enter fullscreen mode Exit fullscreen mode
- msg.sender(address): address of the message sender(current call).
Enter fullscreen mode Exit fullscreen mode
- msg.sig(bytes4): first four bytes of the call data.
Enter fullscreen mode Exit fullscreen mode
- now (uint): timestamp for the current block..
Enter fullscreen mode Exit fullscreen mode
- tx.origin(address): the sender of the transaction(whole call chain).
Enter fullscreen mode Exit fullscreen mode
  • Pragma
Used to specify certain conditions under which the source files can or cannot run.
Enter fullscreen mode Exit fullscreen mode
Example:
- pragma solidity ^0.8.15
       This code compiles with a compiler function >=0.8.15
- A pseudocode example for pragma syntax
       'pragma' Identifier([^;]+)';'
Enter fullscreen mode Exit fullscreen mode
  • Interface
Interface in solidity are defined as contractsm but the function bodies are omitted for the functions.
Example:
pragma solidity ^0.22
interfaceToken
{
       function transfer(address recipient, uint amount);
}
Enter fullscreen mode Exit fullscreen mode
  • Data Types
- Boolean:
true or false
Logical:!(Logical negation), && (AND), || (OR)
Comparisons:==(equality),!=(inequality)
Enter fullscreen mode Exit fullscreen mode
- Integer: Unsigned Integer, and Signed integer Comparisons:<=(Less than or equal to), <(Less than),==(equal to),!=(Not equal to),>=(Greater than or equal to),>(Greater than) Arithmetic Operators: + (Addition), - (Subtraction), Unary —, Unary +, * (Multiplication), / (Division), ** (Exponential), << (Left shift), >>(Right shift)
Enter fullscreen mode Exit fullscreen mode
- Address: Holds an Ethereum address(20 byte value).
Operators: Comparisons: <=, <, ==, !=, >= and >
Balance:
- <address>.balance(uint256): balance of address in WEI
Transfer and send:
- <address>. transfer(uint256 amount): send given amount of Wei to Address, throws on failure.
- <addres>.send(uint256 amount)returns(bool): send given amount of Wei to Address, returns false on failure.
Enter fullscreen mode Exit fullscreen mode
  • Global functions:
- keccak256(...)returns(bytes32)-computes the ethereum SHA-3 hash associated with the arguments.
Enter fullscreen mode Exit fullscreen mode
- SHA256(..)returns(bytes32) — Computes the SHA-256 argument hash.
Enter fullscreen mode Exit fullscreen mode
- mulmod( uint a, uint b, uint c)returns(uint): It computes (a*b)/c, if the multiplication gets executed using arbitrary precision, thus not wrapping around 2**256
Enter fullscreen mode Exit fullscreen mode
- addmod(uint p, unint q, uint m)returns(uint): Computes (p+q)/m
Enter fullscreen mode Exit fullscreen mode
- ecrecover(bytes32 _hash, uint8 _v, bytes32 _r, bytes32 _s)
returns (address): recovers the address linked with the public key and if an error occurs, zero is returned.
Enter fullscreen mode Exit fullscreen mode
- ripemd160(...)returns(bytes20): computes the RIPEMD-160
(tightly packed) argument hash.
Enter fullscreen mode Exit fullscreen mode
- <address>.balance(uint256): Returns the balance of the specified address in WEI.
Enter fullscreen mode Exit fullscreen mode
- <address>.send(uint256 amount)returns(bool): It sends the specified number of WEI to the address given, on failure it returns false as an output.
Enter fullscreen mode Exit fullscreen mode
- <address>.transfer(uint256 amount): Sends specified number of WEI to the particular address, on failure throws.
Enter fullscreen mode Exit fullscreen mode
  • Access Modifiers:
- Public: Accessible from the current contract, inherited contracts externally.
Enter fullscreen mode Exit fullscreen mode
- Private: Accessible only form the current contract.
Enter fullscreen mode Exit fullscreen mode
- Internal: Accessible only from current contract and contracts inheriting from it.
Enter fullscreen mode Exit fullscreen mode
- External: Can be accessed externally only.
Enter fullscreen mode Exit fullscreen mode
  • Important Terms
- Voting: When the contract is quiet complex, it uses voting contract, it shows how delegated voting can be done so that vote counting is automatic and completely transparent at the same time.
Enter fullscreen mode Exit fullscreen mode
- Delegate call: It is a reusable library code that can be applied to a contracts storage in solidity.
Enter fullscreen mode Exit fullscreen mode
- Logs: A feature called logs is used in solidity to implement events.
Enter fullscreen mode Exit fullscreen mode
- NPM: It is a convenient and portable way to install Solidity compiler.
Enter fullscreen mode Exit fullscreen mode
- Truffle: It is a test bed that will allow to easily test and compile the smart contract.
Enter fullscreen mode Exit fullscreen mode
- Inheritance: In solidity inheritance is more syntactic. In the final compilation the compiler copies the parent class members to create the byte-code of the derived contract with the copied members.
Enter fullscreen mode Exit fullscreen mode
- This: This is a keyword that refers to the instance of the contract where the call is made.
Enter fullscreen mode Exit fullscreen mode
- msg.sender: This function refers to the address where the contract is being called from.
Enter fullscreen mode Exit fullscreen mode
- Pure: It is a modifier which assures not to be read from or modified.
Enter fullscreen mode Exit fullscreen mode
- View: In solidity, view is a function that promises to not modify the state of the smart contract.
Enter fullscreen mode Exit fullscreen mode
- Suicide: It is a alias for self destruct function, that is used to destroy the current contract.
Enter fullscreen mode Exit fullscreen mode
- Delete: Delete is used to delete all elements in an array.
Enter fullscreen mode Exit fullscreen mode
- Block.coinbase(address): It refers to the minter's address of the current block.
Enter fullscreen mode Exit fullscreen mode
- Address(contractVar).send(amount): If a built-in send function needs to be used, it can be accessed using this function.
Enter fullscreen mode Exit fullscreen mode
- Super: It is used to refer the contract that is higher by one level in the inheritance hierarchy.
Enter fullscreen mode Exit fullscreen mode
  • Transaction Example

Image description

I hope this can be of service to you ❤️
Any feedback is welcome :)
Enter fullscreen mode Exit fullscreen mode

tw: @_luisald
website: Luis Lucena

💖 💪 🙅 🚩
luislucena16
Luis Lucena

Posted on September 1, 2022

Join Our Newsletter. No Spam, Only the good stuff.

Sign up to receive the latest update from our blog.

Related

Solidity Complete Fundamentals Cheat Sheet
javascript Solidity Complete Fundamentals Cheat Sheet

September 1, 2022

The Hyperverse
javascript The Hyperverse

May 16, 2022