2 min read

Primitives Overview

Introduction

Solidity is a high-level programming language used for writing smart contracts on the Ethereum blockchain. Solidity provides a wide range of data types and primitives that are used to define variables, functions, and other elements in a smart contract. In this blog post, we will describe the Solidity 0.8.16 primitives and provide an example smart contract that uses them.

Solidity Primitives Overview

By understanding these Solidity 0.8.16 primitives, you can write more complex smart contracts that store and manipulate data on the Ethereum blockchain.

// SPDX-License-Identifier: MIT
// Primitives.sol
pragma solidity ^0.8.16;

contract Primitives {
    bool public storedBool;
    int public storedInt;
    uint public storedUint;
    address public storedAddress;
    bytes public storedBytes;
    string public storedString;
    enum State { PENDING, APPROVED, REJECTED }
    State public currentState;
    mapping(address => uint) public balances;
}

bool: A boolean value can be either true or falseBooleans are used to express logical conditions and are often used in control structures such as ifwhile, and for statements.

int / uint: Signed and unsigned integers of various sizes, from 8 bits to 256 bits. Integers are used to represent numerical values and are often used in arithmetic operations. Unsigned Integers represent only positive values.

address: A 20-byte Ethereum Address. Addresses are used to represent Ethereum accounts and are often used in transactions and smart contract interactions.

bytes: A variable-length byte array. Bytes are used to represent arbitrary binary data and can be of any length.

string: A UTF-8 encoded string. Strings are used to represent human-readable text and are often used in user interfaces and message passing.

enum: A user-defined enumerated type. Enums are used to define a set of named values and are often used to represent state or status information in smart contracts.

mapping: A key-value store that maps one type to another. Mappings are used to store and retrieve data in a smart contract and are often used to implement data structures such as arrays and dictionaries.

The constructor Function initializes the variables with default values. The getBalance() function is a view function that returns the balance of a specified account. The approve() and reject() functions are functions that set the currentState variable to the APPROVED and REJECTED values, respectively.

Default Values

Below are the default values for each primitive data type.

// SPDX-License-Identifier: MIT
// Primitives.sol
pragma solidity ^0.8.16;

contract Primitives {
  // public variables have automatic getter functions
  bool public storedBool;  // default: false
  int public storedInt;    // default: 0 
  uint public storedUint;  // default: 0
  address public storedAddress; // default address: 0x0000000000000000000000000000000000000000
  bytes public storedBytes;     // default: 0x
  string public storedString;   // default: empty stiring aka ""
  mapping(address => uint) public balances;   // 0x0..00 address => 0 uint

  enum State { 
		PENDING, 
		APPROVED, REJECTED 
	} // creates enum structure
	
  State public state; // creates instance of enum
}

Testing

If you'd like to test the primitives, be sure to check out the following two tutorials:

It's suggested to go through both to understand how the basics of the Ethereum development environments.

Conclusion

In this blog post, we described the Solidity 0.8.16 primitives and their primitives and provided an example smart contract that uses them. Additionally, we created tests using Hardhat and Foundry.

By understanding the basics of the language primitives, you can begin to write more complex smart contracts that store and manipulate data on the Ethereum blockchain. In addition, we can learn the workflow for more complex projects by understanding how to use the basics of testing and interacting with the two dominant Ethereum development environments.

Feedback

Have feedback? Found something that needs to be updated, accurate, or explained?

Join our discord and share what can be improved.

Any and all feedback is welcomed.