solidity notes
Hello World
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.1;
contract HelloWorld {
string public greet = "Hello World!";
}
Source file does NOT compile with a compiler earlier than version 0.8.1
and starting from version 0.9.0
.
Simple Contract
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.1;
contract Fibonacci {
uint public currentNumber = 1;
uint public previousNumber = 0;
// Function to move to the next sequence
function next() public {
uint _currentNumber = currentNumber;
currentNumber += previousNumber;
previousNumber = _currentNumber;
}
// Function to go back to the previous sequence
function back() public {
require(previousNumber!=0, "Can't go back any further");
uint _previousNumber = previousNumber;
previousNumber = currentNumber - _previousNumber;
currentNumber = _previousNumber;
}
}
This contract stores the latest two numbers in the Fibonacci sequence as
storage variables, currentNumber
and previousNumber
. As public
variables, they come with a getter function to easily retrieve the values. There are two
public
functions in this contract, namely, next
which moves the sequence one step forward, storing the next numbers in the sequence, and back
which goes one step back.
Primitive Data Types
These are the primitive data types in Solidity and how to declare them.
- bool
- Declare a boolean data type by using the keyword
bool
- Either
true
orfalse
, default is false
- Declare a boolean data type by using the keyword
- uint
- Use keywords
uint
,uint8
,uint256
to declare an unsigned integer (integers that are zero or positive ranging from 8 bits to 256 bits) uint
is the same asuint256
- Use keywords
- int
- Use keywords
int
,int8
,int256
to declare a signed integer (negative, zero, or positive) and range from 8 bits to 256 bits int
is the same asint256
- Use keywords
- address
- Variables of type
address
hold a 20-byte value, the size of an Ethereum address. A special kind of address,address payable
, can receive ether from the contract.
- Variables of type
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.3;
contract Primitives {
bool public boo = true;
/*
uint stands for unsigned integer, meaning non negative integers
different sizes are available
uint8 ranges from 0 to 2 ** 8 - 1
uint16 ranges from 0 to 2 ** 16 - 1
...
uint256 ranges from 0 to 2 ** 256 - 1
*/
uint8 public u8 = 1;
uint public u256 = 456;
uint public u = 123; // uint is an alias for uint256
/*
Negative numbers are allowed for int types.
Like uint, different ranges are available from int8 to int256
*/
int8 public i8 = -1;
int public i256 = 456;
int public i = -123; // int is same as int256
address public addr = 0xCA35b7d915458EF540aDe6068dFe2F44E8fa733c;
// Default values
// Unassigned variables have a default value
bool public defaultBoo; // false
uint public defaultUint; // 0
int public defaultInt; // 0
address public defaultAddr; // 0x0000000000000000000000000000000000000000
address public newAddr = 0xB21eDCfbb2edBdFB3c8304C71235196483E6b8B3;
int public neg = -1;
uint8 public newU = type(uint8).min;
}