// SPDX-License-Identifier: MIT
pragma solidity ^0.8.13;
contract Counter{
// public means we check the parameter
// declare the parameter first
unit public count;
//unit means the int >= 0
//view can use the parameters global but cant't change the state
//it just return data cost no gas
function get() public view returns (uint){
return (count);
}
function increment() public {
count += 1; //It will reduce gas
}
function decrement() public {
count -=1; //The min est is 0
}
}
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.13;
contract Primitives{
//There is the corresponding button only when it's public
uint [] public nums = [1,2,3];
uint [3] public numsFixed = [4,5,6];
function esamples() external {
nums.push(4);
uint x = nums[1];
nums[2]=777;
//delete means modify the value into 0
//it won't change the length of the array
delete nums[1];
nums.pop();//length will less
uint len = nums.length;
//create array in memory you should use new
//Here you cna't create a dynamic array
uint[] memory a = new uint[](5);
//You can't use pop() or pushs()
//because they can modify their length
a[1] = 123;
}
//return the whole array
function returnArray() external view returns (uint[] memory){
return nums;
}
}
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.13;
contract Constants {
//The fixed variable use constant can reduce gas consumption
//The name you'd better use upper
address public constant MY_ADDRESS = 0x0000000000000000000000000000000000000000;
}
//contract Var{
// address public MY_ADDRESS = 0x0000000000000000000000000000000000000000;
//}
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.13;
contract Stru {
struct Car {
string model;
uint year;
address owner;
}
Car public car;//create a variable no other effect
Car[] public cars;// an dynamatic array
mapping(address => Car) public carByOwner;
function examples()external {
//They are in the meomory
Car memory toyata = Car("Toyota",1990,msg.sender);
Car memory lambo = Car({year:1980,model:"Lamborghini",owner:msg.sender});
Car memory tesla;
tesla.model = "Tesla";
tesla.year = 2010;
tesla.owner = msg.sender;
cars.push(toyata);
cars.push(lambo);
cars.push(tesla);
//integrate the create and push together
cars.push(Car("Ferrari",2020,msg.sender));
//There is no public in a function
Car storage _car = cars[0];//you can use both stroage or memory
//but you can't use the variable outside the function
_car.year = 1999;
delete _car.owner;//reset _car.owner into its initive
delete cars[1];
}
}
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.13;
contract DataLocatioons {
struct MyStruct {
uint foo;
string text;
}
mapping(address => MyStruct) public myStructors;
//calldata just used in parameter
function examples(uint[] memory y, string memory s) external returns (uint[] memory){
//The disction
//memory is just in the function
//calldata can't be changed
myStructors[msg.sender] = MyStruct({foo:123,text:"bar"});
// equals to MyStruct(123,"bar")
MyStruct storage myStructor = myStructors[msg.sender];
myStructor.text = "foo";
MyStruct memory readOnly =myStructors[msg.sender];
readOnly.foo = 456;
//memory must have a fixed length can't push or pop
uint[] memory memArr =new uint[](3);//remember use new
memArr[0] = 234;
return memArr;
}
}
memory and storage only used in the function parameters or inside's arrays or strings
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.13;
contract Ifelse{
function ifelse(uint _x) external pure returns (uint){
if(_x<10){
return 1;
}else if (_x<20){
return 2;
}else {
return 3;
}
}
function _Th(uint _x) external pure returns (uint){
return _x<10 ? 1:2;
}
function loop() external pure{
for(uint i=0;i<10;i++){
if(i==3){
continue;
}
if(i==5){
break;
}
}
uint j=0;
while(j<10){
j++;
}
}
function _sum(uint _n) external pure returns (uint){
uint s;
for(uint i = 1;i <= _n;i++){
s += i;
}
return s;
}
}
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.13;
contract ToDoList {
struct Todo {
string text;
bool completed;
}
Todo[] public todos;
function create(string calldata _text) external {
todos.push(Todo({
text:_text,
completed:false
}));
}
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.13;
contract Mapping {
mapping (address => uint )public balances;
mapping (address => mapping (address => bool)) public isFriend;
function examples() external {
balances[msg.sender] = 123;
uint bal = balances[msg.sender];
//if the index not exist we will get the initive value of uint
uint bal2 = balances[address(1)];
balances[msg.sender] = 456;
delete balances[msg.sender];//reset to 0
// address(this) refer to the address of current contract
isFriend[msg.sender][address(this)]=true;
}
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.13;
contract Variables{
uint public a = type(uint).max;
//Remember if you want to change the return you should change the returns above
function dosth() public view returns(uint, address,uint){
//Local
uint numb = 99;
//current time of the chain it's a global
// the seconds from 1970 to now
uint time = block.timestamp;
//The number of the block currently
uint blockNum = block.number;
//The address of the function
//The last place use the function
address sender = msg.sender;
// Return the tuple (time, sender)
return (time, sender,blockNum);
}
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.13;
contract FunctonOutputs {
function returnMany() public pure returns (uint,bool){
return (1,true);
}
function named() public pure returns (uint x,bool b){
return (1,true);
}
function assigned() public pure returns (uint x,bool b){
x = 1;
b = true;
}
function destructingAssigments() public pure {
//keep the name of the parameters identical
(uint x,bool b) = returnMany();
(, bool _b) = returnMany();
}
}
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.13;
contract IterableMapping {
mapping(address => uint )public balances;
mapping(address => bool )public inserted;
address[] public keys;//array
function set(address _key,uint _val) external {
balances[_key] = _val;
if(!inserted[_key]){//the value of _key is false
inserted[_key] = true;
keys.push(_key);
}
}
function getSize() external view returns (uint){
return keys.length;
}
function first() external view returns (uint){
return balances[keys[0]];
}
function last() external view returns (uint){
return balances[keys[keys.length - 1]];
}
function get(uint _i)external view returns (uint){//input an index
return balances[keys[_i]];
}
}
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.13;
contract FuncionIntro{
//external means the function inside the contract can't use it
//pure means can't modify the state of the constract
//It just depend on the parameter you give to ouput
//view = prue +msg.value
//If you use the varible in thr chain in your function you should use view
//If you only use the parameter you can use pure
//If you want to revise the data in the chain you can use neither view or pure
function add(uint x,uint y) external pure returns (uint){
return x+y;
}
function sub(uint x,uint y) external pure returns (uint){
return x-y;
}
}
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.13;
//revert 应用于可以通过改变输入条件来修复的错误,比如无效的参数或条件不满足。
//require 用于验证输入参数的有效性或者合约的当前状态是否满足执行函数所需的条件。
//assert 断言
contract Error{
function test_require(uint _i) public pure{
require(_i<=10,"i>10");
//code
}
function test_revert(uint _i) public pure{
if(_i>10){
revert("i>10");
}
}
uint public num = 123;
function test_assert() public view{
assert(num == 123);
}
function foo(uint _i)public{
num += 1;
require(_i<10);
}
error MyError(address caller ,uint i);
function test_Custom_Error(uint _i) public view{
if(_i >10){
revert MyError(msg.sender,_i);
}
}
}
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.13;
contract FunctionModifier {
bool public paused;
uint public count;
function setPause(bool _paused)external {
paused = paused;
}
modifier whenNotPaused(){
//When you reach the _; it will stop
require (!paused ,"paused");
_;
}
function inc() external whenNotPaused{
count += 1;
}
function dec()external whenNotPaused{
count -= 1;
}
modifier cap(uint _x){
require(_x<100 ,"x>=100");
_;
}
function incBy(uint _x)external whenNotPaused cap(_x){
count += _x;
}
modifier sandwich(){
count += 10;
_;
count *= 2;
}
function foo() external sandwich(){
count +=1;
}
}
//modifier has _;
//first do modifier then continue function at _; (continue modifier)
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.13;
contract Enum{
enum Status {//
None,//initive
Pending,
Shipped,
Completed,
Rejected,
Canceled
}
Status public status;//return an index
struct Order {
address buyer;
Status sta;//a trait
}
Order[] public orders;//an array dtype is Order
function get() view external returns (Status) {
return status; //return an index
}
function set(Status _status)external {
status = _status;
}
function ship() external {
status = Status.Shipped;//
}
function reset()external{
delete status;
}
}