-
Notifications
You must be signed in to change notification settings - Fork 0
/
Learning.sol
182 lines (130 loc) · 5.65 KB
/
Learning.sol
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
//
//solidity is very similar to python and js
//It is a contract oriented language, quite similar to object oriented. It is also static language means types of all variables must be declared first
//first thing is to define the version of solidity
//It can also be defined in range eg. >=0.6.1 <0.8.0
pragma solidity ^0.6.6; //this indicates use of a specific version of solidity
contract Learning {
string value;
//the code inside constructor will get executed first
constructor(address payable _wallet) public {
value = "Default value";
state = State.Waiting;
owner = msg.sender; //the deploying account will be owner, check mapping section down below
wallet = _wallet; //for dealing with ETH. check that section below
}
// the following is function in solidity. public is the visibility status, view is type of action, check docs for more info
function get() public view returns(string memory){
return value;
}
//following function will set the value of variabe value of datatype string, memory stands for storage type
function set(string memory _value) public {
value = _value;
}
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//dataypes
string public mystring = "this is my string";
bool public mybool = true;
int public myint = -1;
uint256 public myuint = 1;
//unit can't have sign means can't be negative
//enum is enumerated list that is used to keep tracks of things in contract
enum State { Waiting, Ready, Active} //all 3 are states of contract
State public state;
function activate() public {
state = State.Active;
}
//function to check if state is active or not
function isActive() public view returns(bool) {
return state == State.Active;
}
//structs are custom data structures
struct Person {
string _firstName;
string _lastName;
}
Person[] public people; //shows an array names people filled with data structure Person
function addPerson(string memory _firstName, string memory _lastName) public {
peopleCount +=1;
people.push(Person(_firstName, _lastName));
}
//Point to note here is that, unlike python solidity doesn't know the size of dynamic array hence entire array
//can not be accessed, only array members can be accessed by their id (index), quite same to c++
uint256 public peopleCount;
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//mapping and modifiers
//mapping is also a data structure but very important here. its like a key,value pair
uint256 peopleCount2;
struct Person2 {
uint id;
string _firstName;
string _lastName;
}
mapping(uint256 => Person2) public people_mapping;
//onlyOwner modifier allows only owner address to use function
address owner; //address is anothr datatype stands for account or wallet address
//Function Modifiers are used to modify the behaviour of a function. For example to add a prerequisite to a function
modifier onlyOwner() {
require(msg.sender == owner);
_; //consider this something like syntax
}
//msg is special global variable which displays metadata of transaction.
//There are many other special functions and variables in solidity and I would highly recommend to read about them at -
//https://docs.soliditylang.org/en/develop/units-and-global-variables.html#special-variables-and-functions
function addPerson2(string memory _firstName, string memory _lastName) public onlyOwner{
peopleCount2 += 1;
people_mapping[peopleCount2] = Person2(peopleCount2, _firstName, _lastName);
}
// To make a timeout in contract, solidity uses epoch time, google if you wanna know more
uint256 openingTime = 1673834726;
modifier onlyWhileOpen() {
require(block.timestamp >= openingTime); //every block has its time in solidity, read more on docs
_;
}
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//dealing in ETH
mapping(address => uint256) public balances;
address payable wallet;
function buyToken() public payable{
//buy a token
balances[msg.sender] += 1;
//send ether to wallet
wallet.transfer(msg.value);
emit Purchase(msg.sender, 1);
}
//fallback funciton, quite used in ICOs combined with events
fallback() external payable{
buyToken();
}
event Purchase(
address _buyer,
uint256 _amount
);
}
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// now a basic idea of how ERC20 tokens work
contract ERC20Token {
string name;
mapping(address => uint256) public balances;
function mint() public {
balances[tx.origin] += 1;
}
}
contract MyContract{
address payable wallet2;
address public token;
constructor(address payable _wallet2, address _token) public {
wallet2 = _wallet2;
token = _token;
}
fallback() external payable{
buyToken2();
}
function buyToken2() public payable{
ERC20Token _token= ERC20Token(address(token));
_token.mint();
wallet2.transfer(msg.value);
}
//solidity also supports inheritence, but I believe self practice will be best way to learn it
//so refer the docs and start writing your contracts.
}