- 自我介绍
大家好我是Robin(cxk228922),一名正在台灣就讀高二的學生,對資訊安全頗有興趣
之前都是往計算機本身漏洞探究,想慢慢跨進到網路相關的內容。
- 你认为你会完成本次残酷学习吗?
我會盡我所能完成一切
這邊先和助教說聲抱歉,由於我最近課業繁忙,並且對網路、區塊鏈並沒有基礎,每天能學習的內容有限><。以及我想趁此機會練習英文,文法可能有許多不妥處,可能有點難以閱讀。
- Remix IDE
- File Extension of Solidity :
.sol
Ctrl
+ S
-> Compile
Deploy
-> Simulate Ethereum chain -> run smart contracts
- Basic
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.21;
contract HelloWeb3{
string public _string = "Hello Web3!";
}
// SPDX-License-Identifier: MIT
-> Denotes license identifier (MIT
for example)
pragma solidity ^0.8.21;
-> Only allow compiler version 0.8.21
~ 0.9.0
(0.8.21
for example)
contract HelloWeb3
-> Similar as class
in other programming language (HelloWeb3
for example). also,we call it "合約"
string public _string = "Hello Web3!";
-> Declared Variables
string
: Value type
Type |
Explain |
Example |
int |
integer |
3 , 300 , -123 |
uint |
unsigned integer(include 0) |
3 , 300 |
uint |
k-bit unsigned integer |
uint8 , uint64 |
bool |
boolean |
true , false |
address |
address(e.g. ETH account) |
0x1234556 |
enum |
enumeration |
{Mon=1,Tue,Wed} |
Type |
Outside Contract |
In Contract |
Inheritance Contract |
public |
✅ |
✅ |
✅ |
private |
❌ |
✅ |
❌ |
external (function) |
✅ |
✅use this.func() |
✅use this.func() |
internal |
❌ |
✅ |
✅ |
_string
: Value name,Define by your self
"Hello Web3!"
: Value
function <functionName>(<Type> <ParameterName>) <visibility> <modifier> returns (<Type>)
<Type> <parameter1>
: Input parameter,e.g. int a
Also,you can input more than 1 parameter
<modifier>
:
Value |
Explain |
pure |
cannot read nor write |
view |
can read but cannot write |
(NULL) |
can both read and write |
payable |
can receive ETH |
- Variables
data storage locations:
Type |
stored |
modified |
storage(deafult) |
on-chain |
✅ |
memory |
in memory |
✅ |
calldata |
in memory |
❌(read-only) |
scope:
contract Example {
uint public count;
function increment() public pure returns (uint) {
uint localCount = 0;
return localCount;
}
}
Type |
Location |
Complain/Example |
State(狀態變量) |
storage |
count (in contract storage) |
Local(局部變量) |
memory (reference types)
stack (basic types) |
localCount (in function) |
Global(全局變量) |
EVM dynamic |
msg.sender ,block.number |
- Array
//fixed-sized arrays
<type>[<length>] <name>;
<type>[] memory <name> = new <type>[](<length>);
uint[9] array;
uint[] memory array3 = new uint[](5);
//dynamic-sized array
uint[] array1;
bytes array2; //dynamic byte array
.length
-> return array's length
dynamic array :
.push
-> add 0
element at the end of array
.puxh(x)
-> add x
element at the end of array
.pop
-> remove the last element of array
- Struct
struct Student{
uint id;
uint score;
}
//4 ways to assign value
function method1() external{
Student storage _student = student; //copy Student
_student.id = 11;
_student.score = 100;
}
function method2() external{
student.id = 1;
student.score = 80;
}
function method3() external {
student = Student(3, 90);
}
function method4() external {
student = Student({id: 4, score: 60});
}
- Map
key -> Value
mapping(<KeyType> => <ValueType>) public <name>;
mapping(address => uint) public balances;
<name>[<key>] = <Value>;
balances[msg.sender] = 100;
- constant/immutable
Characteristic |
constant |
immutable |
Initialization time |
declaration |
constructor/declaration |
Modification time |
Compile-time constant |
Determined at deployment |
Storage location |
Inlined directly at compile-time |
storage |
Use cases |
completely fixed values (e.g. fixed rates) |
values determined by conditions at deployment time (e.g.deployer) |
- Control flow
as same as C
//if-else
function ifElseTest(uint256 _number) public pure returns(bool){
if(_number == 0){
return(true);
}else{
return(false);
}
}
//for loop
function forLoopTest() public pure returns(uint256){
uint sum = 0;
for(uint i = 0; i < 10; i++){
sum += i;
}
return(sum);
}
//while
function doWhileTest() public pure returns(uint256){
uint sum = 0;
uint i = 0;
do{
sum += i;
i++;
}while(i < 10);
return(sum);
}
//Insertion Sort
function insertionSort(uint[] memory a) public pure returns(uint[] memory) {
for (uint i = 1;i < a.length;i++){
uint temp = a[i];
//uint j=i-1;
uint j=i; //uint can't <0
while( (j >= 1) && (temp < a[j-1])){
//before:(temp < a[j])
a[j] = a[j-1];
j--;
}
a[j] = temp;
}
return(a);
}