30 days of Solidity programming challenge is a step-by-step guide to learn Solidity programming language in 30 days.
-
local declared inside a function not stored on the blockchain
-
state declared outside a function stored on the blockchain
-
global (provides information about the blockchain)
-
- Declaration : bool isBool;
-
- Declaration : bytes32 name;
-
- Declaration : uint address;
-
- Declaration : address myAddress = 0x123;
-
- Declaration : string myString = 'Solidity';
-
- Declaration : enum my_enum { sol , idi , ty }
-
- Declaration :
pragma solidity ^0.4.0; contract Ballot { struct Voter { // Struct uint weight; bool voted; address delegate; uint vote; } }
- Contracts in Solidity are similar to classes in object-oriented languages.
- Example :
pragma solidity ^0.4.0; contract HelloWorld { }
- Example :
- Functions can be defined inside and outside of contracts.
- Example :
pragma solidity ^0.4.0; contract Calculation{ function add(uint A, uint B) public{ uint sum = A+B; } }
- Example :
- Functions outside of a contract, also called “free functions”.
- Example :
pragma solidity ^0.4.0; //return uint of subtraction function sub(uint A, uint B) public returns(uint){ return B>A ? (B-A) : (A-B); } contract Calculation{ function add(uint A, uint B) public{ uint sum = A+B; uint dif = sub(10,3); } }
- Example :
- Solidity knows two kinds of function calls: external ones that do create an actual EVM(Ethereum Virtual Machine) message call and internal ones that do not. Furthermore, internal functions can be made inaccessible to derived contracts. This gives rise to four types of visibility for functions.
- external : External functions are part of the contract interface, which means they can be called from other contracts and via transactions.
- public : Public functions are part of the contract interface and can be either called internally or via message calls.
- internal : Internal functions can only be accessed from within the current contract or contracts deriving from it.
- private : Private functions are like internal ones but they are not visible in derived contracts.
- In Solidity, there are two locations you can store variables — in storage and in memory.
- Storage refers to variables stored permanently on the blockchain. Memory variables are temporary.
- Array with a fixed length of 2 elements:
- uint[2] fixedArray;
- A dynamic Array - has no fixed size, can keep growing:
- uint[] dynamicArray;
keccak256(abi.encodePacked("aaaab"));
o/p : 6e91ec6b618bb462a4a6ee5aa2cb0e9cf30f7a052bb467b0ba58b8748c00d2e5
mapping(address => uint) public myMap;
//Example
mapping(uint => uint) public uintMap;
function set() public{
uintMap[0] = 123;
}
function get() view public returns(uint){
uintMap[0];
}
o/p : 123
struct user {
string name;
uint dna;
}
- "internal" is the same as private,"external" is similar to public.