Value types
INT / UINT
uint
is short for unsigned integer
, and you can choose the size from uint8
to uint256
uint8
starts from0
to2 ** 8 - 1
uint16
starts from0
to2 ** 16 - 1
...uint256
starts from0
to2 ** 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
to2 ** 7 - 1
int16
starts from-2 ** 15
to2 ** 15 - 1
...int128
starts from-2 ** 127
to2 ** 127 - 1
int256
starts from-2 ** 255
to2 ** 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 insigned 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 accountaddress payable
similar toaddress
but adds 2 more methodstransfer
andsend
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;
}
}
Last updated