Solidity:
// SPDX-License-Identifier: MIT
// compiler version must be greater than or equal to 0.8.17 and less than 0.9.0
pragma solidity ^0.8.17;
contract HelloWorld {
string public greet = "Hello World!";
}
Where:
pragma solidity ^0.8.17
specifies the version of the Solidity compiler.
contract HelloWorld
defines the beginning of the contract and its name, while the {}
indicate the block where the functions of our contract will be.
string public greet
is the way a state variable is defined that will contain a text string ("Hello World!"), where the variable's visibility is public, meaning it can be read from any function within the contract or from other contracts.
Cairo 1:
#[contract]
mod HelloStarknet {
struct Storage {
greet: felt,
}
}
Where:
The #[contract]
macro indicates that we are writing a contract for Starknet.
mod HelloStarknet
defines the beginning of the contract and its name. Similarly to Solidity, the {}
indicate the block where the functions of our contract will be.
In Cairo 1, all state variables are declared inside a struct named Storage
.
Unlike in Solidity, Cairo does not have a string
data type, and instead uses the felt
data type. In Cairo, we do not need to define the visibility of the greet
variable. By default, as it is of type Storage
, it is accessible to all contract functions and other contracts.
Solidity | Cairo | |
---|---|---|
Boolean | bool | bool |
Uint | uint8, uint16, uint32, uint64, uint128, uint256 | u8, u16, u32, u64, u128, u256 |
Int | int8, int16, int32, int64, int128, int256 | *felt |
address | address | *felt |
Where: