πŸ“š
Open Polkadot Bootcamp
  • πŸ“š About the Bootcamp
    • πŸ“– Additional Resources
    • πŸ‘ Ask For Support
  • πŸ“– Curriculum
  • πŸ“•Rust Programming Language
    • Basic Rust
      • Introduction to Rust
        • πŸ§‘β€πŸ’» Excercises
      • Common Programming Concepts
        • πŸ§‘β€πŸ’» Excercises
      • Program Life Cycle
        • πŸ§‘β€πŸ’» Excercises
      • Ownership & Borrow Checker
      • Common Data Structures
    • Advanced Rust
      • Generic types, Trait extension and Advanced types
      • Lifetime Notation
      • Smart pointers & Macros
      • Common design patterns in Rust
      • Package management & How to structure your Rust project
      • Overview of the Rust ecosystem
  • πŸ“˜Building a blockchain with Polkadot SDK
    • Polkadot
      • Additional Reads
        • Why do you want to build a blockchain on Polkadot?
        • Understanding the sharded network design of Polkadot
      • Development on Polkadot
    • Polkadot SDK
      • Substrate
        • Create a new blockchain
          • πŸ§‘β€πŸ’» Exercise: Clone the minimal template
          • Understanding the architecture
          • Break down the node architecture
          • Introducing to Pop CLI tool
        • Adding a custom logic to runtime
          • πŸ§‘β€πŸ’» Exercise: Rust State Machine
          • Components of a Pallet
          • Hooks
          • Weights & Benchmarking
          • Extensions
            • Signed Extensions
            • Transaction Extensions
        • Common runtime modules
          • πŸ“•Example: System Pallet
          • πŸ“•Example: Contracts Pallet
          • πŸ“•Example: Assets Pallet
          • πŸ“•Example: Utility Pallet
        • Runtime API and RPC
        • Runtime upgrade
        • Bump Polkadot SDK versions
      • Cumulus
        • Introduction to Cumulus
          • Parachain from scratch
          • πŸ§‘β€πŸ’» Exercise: Build a parachain from scratch
        • Running a local relaychain network
          • Register & reserve a parachain
          • Launch the network & run a collator node
          • Launch the network with Pop CLI
        • Agile Coretime
    • Polkadot Hub
  • πŸ“’Smart Contract Development
    • Introduction
      • Introduction to PolkaVM
      • Getting started with Solidity development
      • Solidity File Structure
      • Contract Structure
    • Basic Solidity
      • Value types
      • Reference Types
      • Mapping Types
      • Simple Storage
    • Advanced Solidity
      • Units
      • Global Variables
      • Expression and Control Structures
      • Advanced Storage
      • Contract Tests
      • Contracts
Powered by GitBook
On this page
  • INT / UINT
  • BOOL
  • ADDRESS
  • BYTES
  • Default values
  • CONTRACT
  • ENUM
  • TYPE
  • FUNCTION
  1. Smart Contract Development
  2. Basic Solidity

Value types

INT / UINT

uint is short for unsigned integer, and you can choose the size from uint8 to uint256

  • uint8 starts from 0 to 2 ** 8 - 1

  • uint16 starts from 0 to 2 ** 16 - 1 ...

  • uint256 starts from 0 to 2 ** 256 - 1

uint8 public u8 = 1;
uint256 public u256 = 456;
uint public u = 123; // uint is short hand for uint256

int is short for integer, and you can choose the size from int8 to int256

  • int8 starts from -2 ** 7 to 2 ** 7 - 1

  • int16 starts from -2 ** 15 to 2 ** 15 - 1 ...

  • int128 starts from -2 ** 127 to 2 ** 127 - 1

  • int256 starts from -2 ** 255 to 2 ** 255 - 1

int8 public i8 = -1;
int256 public i256 = 456;
int public i = -123; // int is short hand for int256

int and uint operators:

  • Comparisons: <=, <, ==, !=, >=, > (returns bool)

  • Bit operations: &, |, ^ (bitwise exclusive hoαΊ·c), ~ (bitwise negation)

  • Shifts: << (left shift), >> (right shift)

  • Addition, Subtraction and Multiplication: +, -, negative - (as in signed integer), *, /, % (modulo), ** (exponentiation)

For a type integer variable X, you can use type(X).min and type(X).max to access smallest and biggest value respectively for that type.

// minimum and maximum of int type: 
int public minInt = type(int).min;
int public maxInt = type(int).max;

// minimum and maximum of uint type:
uint public minUint = type(uint).min;
uint public maxUint = type(uint).max;

BOOL

bool means Boolean and has 2 possible values which are true and false

bool public trueVar = true;
bool public falseVar = false;

Operators:

  • ! (logical negation)

  • && (logical conjunction, β€œand”)

  • || (logical disjunction, β€œor”)

  • == (equality)

  • != (inequality)

The operators || and && apply the common short-circuiting rules. This means that in the expression f(x) || g(y), if f(x) evaluates to true, g(y) will not be evaluated even if it may have side-effects.

ADDRESS

  • address is a special data type in Solidity that allows storing 20 bytes (size) of the address of an EVM account

  • address payable similar to address but adds 2 more methods transfer and send

address public exampleAddress = 0xCA35b7d915458EF540aDe6068dFe2F44E8fa733c;
address payable public examplePayableAddress = payable(0xCA35b7d915458EF540aDe6068dFe2F44E8fa733c);

function getAddressBalance() public view returns (uint) {
    // Returns the balance of the exampleAddress
    return exampleAddress.balance;
}

function sendViaTransfer() public payable {
    // This function is no longer recommended for sending Ether.
    examplePayableAddress.transfer(msg.value);
}

function sendViaSend() public payable {
    // Send returns a boolean value indicating success or failure.
    // This function is not recommended for sending Ether.
    bool sent = examplePayableAddress.send(msg.value);
    require(sent, "Failed to send Ether");
}

function sendViaCall() public payable {
    // Send Ether to the wallet
    (bool sent, bytes memory data) = examplePayableAddress.call{value: msg.value}("");
    require(sent, "Failed to send Ether");
}

BYTES

In Solidity, the byte data type represents a sequence of bytes. Solidity has two types of bytes:

  • Fixed-size byte arrays

  • Dynamic-size byte arrays

The bytes keyword in Solidity represents a dynamic array of bytes. Essentially, it is a shorthand for byte[].

bytes1 a = 0xb5; //  [10110101]
bytes1 b = 0x56; //  [01010110]
bytes c = "abc"; //  [01100001, 01100010, 01100011]

Default values

Declared variables without value assignment will have its default values.

bool public defaultBool; // false
uint public defaultUint; // 0
int public defaultInt; // 0
address public defaultAddr; // 0x0000000000000000000000000000000000000000
bytes1 public defaultByte; // 0x00

CONTRACT

contract is used to declare a contract in Solidity.

contract HelloWorld {}

contract can also inherit from another contract using the keyword is

contract Mercedes is Car {}

ENUM

Enums are one way to create a user-defined type in Solidity. They are explicitly convertible to and from all integer types but implicit conversion is not allowed. The explicit conversion from integer checks at runtime that the value lies inside the range of the enum and causes a Panic error otherwise. Enums require at least one member, and its default value when declared is the first member. Enums cannot have more than 256 members.

The data representation is the same as for enums in C: The options are represented by subsequent unsigned integer values starting from 0.

Using type(NameOfEnum).min and type(NameOfEnum).max you can get the smallest and respectively largest value of the given enum.

// SPDX-License-Identifier: MIT
pragma solidity ^0.8.20;

contract Enum {
    // Enum representing shipping status
    enum Status {
        Pending,
        Shipped,
        Accepted,
        Rejected,
        Canceled
    }

    // Default value is the first element listed in
    // definition of the type, in this case "Pending"
    Status public status;

    // Returns uint
    // Pending  - 0
    // Shipped  - 1
    // Accepted - 2
    // Rejected - 3
    // Canceled - 4
    function get() public view returns (Status) {
        return status;
    }

    // Update status by passing uint into input
    function set(Status _status) public {
        status = _status;
    }

    // You can update to a specific enum like this
    function cancel() public {
        status = Status.Canceled;
    }

    // delete resets the enum to its first value, 0
    function reset() public {
        delete status;
    }
}

TYPE

A user-defined value type allows creating a zero cost abstraction over an elementary value type. This is similar to an alias, but with stricter type requirements.

A user-defined value type is defined using type C is V, where C is the name of the newly introduced type and V has to be a built-in value type (the β€œunderlying type”)

// SPDX-License-Identifier: GPL-3.0
pragma solidity ^0.8.8;

// Represent a 18 decimal, 256 bit wide fixed point type using a user-defined value type.
type UFixed256x18 is uint256;

FUNCTION

function keyword is used to declare a function in Solidity.

We can declare a function like below:

contract Counter {
    uint public count;

    // Function to view count variable
    function get() public view returns (uint) {
        return count;
    }
}
PreviousBasic SolidityNextReference Types

Last updated 5 months ago

πŸ“’