├── LearnSolidity.sol └── README.md /LearnSolidity.sol: -------------------------------------------------------------------------------- 1 | // SPDX-License-Identifier: GPL-3.0 2 | 3 | pragma solidity >=0.7.0 <0.9.0; 4 | 5 | contract LearnSolidity { 6 | 7 | // Define different data types and variables 8 | 9 | // String - a sequence of characters 10 | string public name = "Param"; 11 | 12 | // Integer - a whole number 13 | // "int" can be used to define both negative and positive values 14 | int public number = -5; 15 | 16 | // Unsigned integer - a whole number that can only be positive 17 | uint public myNumber = 25; 18 | 19 | // Boolean - a true/false value 20 | bool public class = false; 21 | bool public king = true; 22 | 23 | // Address - a 20-byte value that represents an Ethereum address 24 | address public myWallet = 0x5B38Da6a701c568545dCfcB03FcB875f56beddC4; 25 | 26 | // Bytes - a fixed-size sequence of bytes 27 | bytes30 public hashh = "hello web3"; 28 | 29 | // Arrays - a collection of elements of the same data type 30 | 31 | // Dynamic array - can have a variable size 32 | string[] public names = ["param", "rahul", "vipin"]; 33 | 34 | // Fixed-size array - has a defined size that cannot be changed 35 | string[2] public twoNames = ["param", "rahul"]; 36 | 37 | // Enum - a way to create a custom data type with a set of named values 38 | enum State { PENDING, ACTIVE, INACTIVE } 39 | State public currentState; 40 | 41 | // Function to set the current state 42 | function setState(State _state) public { 43 | currentState = _state; 44 | } 45 | 46 | // Function to check if the current state is active 47 | function isActive() public view returns (bool) { 48 | return currentState == State.ACTIVE; 49 | } 50 | 51 | // Struct - a way to create a custom data type that groups together variables of different data types 52 | struct Person { 53 | string name; 54 | uint age; 55 | bool isTrue; 56 | } 57 | Person[] public people; 58 | 59 | function addPerson(string memory _name, uint _age,bool val) public { 60 | 61 | Person memory newPerson = Person(_name, _age,val); 62 | 63 | people.push(newPerson); 64 | 65 | } 66 | // Events 67 | 68 | event Transfer(address indexed _to, uint _value); 69 | 70 | function sendETH(address add, uint amt) public { 71 | payable(add).transfer(amt); 72 | emit Transfer( add , amt); 73 | 74 | } 75 | 76 | // modifier 77 | address owner = 0x5B38Da6a701c568545dCfcB03FcB875f56beddC4; 78 | modifier onlyOwner() { 79 | require(msg.sender == owner, "Only contract owner can call this function"); 80 | _; 81 | } 82 | // change Owner function 83 | function changeOwner(address newOwner) public onlyOwner { 84 | owner = newOwner; 85 | } 86 | // Constructor : a constructor is a special function that is executed only once when a contract is deployed to the blockchain. It is used to initialize the contract's state variables and perform any other setup tasks that are required. 87 | uint public myValue; 88 | 89 | constructor(uint initiaValue){ 90 | myValue = initiaValue; 91 | } 92 | function setMyVariable(uint newValue) public { 93 | myValue = newValue; 94 | } 95 | 96 | // View vs Pure 97 | uint public a = 1; 98 | 99 | // View functions may only read the state of the contract 100 | function addView(uint b) public view returns (uint) { 101 | return a+b; 102 | } 103 | 104 | //Pure function does not read or write the state variables 105 | function addPure(uint x, uint y) public pure returns (uint) { 106 | return x + y; 107 | } 108 | //mapping helps to keep track of keys and it values(a good example of key and value is wallet and amount) 109 | mapping(address => uint) public balanceof; 110 | 111 | 112 | event Log(string message); 113 | 114 | // Global Variables 115 | function paySent() public payable { 116 | require(msg.sender == tx.origin); // msg.sender = caller of function, tx.origin = origin of transaction 117 | amount = msg.value; // Amount of ether sent alongside the transaction 118 | } 119 | 120 | // function visiblity 121 | 122 | // Can be called outside SC and called by another function 123 | function checkVisibility() public { 124 | } 125 | 126 | // Cannot be called outside SC but can be called inside of another function in thesame SC 127 | function checkVisibility1() private { 128 | } 129 | 130 | // Can only be called outside the SC 131 | function checkVisibility2() external { 132 | } 133 | 134 | // Can only be called inside the SM from another function 135 | function checkVisibility3() internal { 136 | } 137 | 138 | // There are two types of functions in solidity write functions and read functions 139 | 140 | // This is a write function so you have to pay a gas fee to call function 141 | function setValue(string memory _age) public { 142 | age = _age; 143 | } 144 | 145 | // While with the read function is completely free 146 | function getValue() public view returns (string memory) { 147 | return age; 148 | } 149 | 150 | // Operators in solidity 151 | 152 | // Equal to 153 | function eq(uint q, uint w) external pure returns(bool) { 154 | return q == w; 155 | } 156 | 157 | // Not Equal to 158 | function notEq(uint q, uint w) external pure returns(bool) { 159 | return q != w; 160 | } 161 | 162 | // Greater than 163 | function gT(uint q, uint w) external pure returns(bool) { 164 | return q > w; 165 | } 166 | 167 | // Less than 168 | function lT(uint q, uint w) external pure returns(bool) { 169 | return q < w; 170 | } 171 | 172 | // Greater than Equal to 173 | function gTEq2(uint q, uint w) external pure returns(bool) { 174 | return q >= w; 175 | } 176 | 177 | // Less than Equal to 178 | function lTEq2(uint q, uint w) external pure returns(bool) { 179 | return q <= w; 180 | } 181 | 182 | // Addition 183 | function add(uint q, uint w) external pure returns(uint) { 184 | return q + w; 185 | } 186 | 187 | // Subtraction 188 | function sub(uint q, uint w) external pure returns(uint) { 189 | return q - w; 190 | } 191 | 192 | // Division 193 | function div(uint q, uint w) external pure returns(uint) { 194 | return q / w; 195 | } 196 | 197 | // Multiplication 198 | function mul(uint q, uint w) external pure returns(uint) { 199 | return q * w; 200 | } 201 | 202 | // Exponents 203 | function exp(uint q, uint w) external pure returns(uint) { 204 | return q ** w; 205 | } 206 | 207 | // Modulus 208 | function mod(uint q, uint w) external pure returns(uint) { 209 | return q % w; 210 | } 211 | 212 | // Bitwise Xor 213 | function xor(uint q, uint w) external pure returns(uint) { 214 | return q ^ w; 215 | } 216 | 217 | // Increment 218 | function inc(uint q) external pure returns(uint) { 219 | q++; 220 | return q; 221 | } 222 | 223 | // Decrement 224 | function dec(uint q) external pure returns(uint) { 225 | q--; 226 | return q; 227 | } 228 | 229 | // And making sure both things are true 230 | function and(bool q, bool w) external pure returns(bool) { 231 | return q && w; 232 | } 233 | 234 | // Or either one of the variables is true 235 | function or(bool q, bool w) external pure returns(bool) { 236 | return q || w; 237 | } 238 | 239 | // Check if something is not true 240 | function not(bool q) external pure returns(bool) { 241 | return !q; 242 | } 243 | 244 | function addExample() external pure returns(bool) { 245 | return (1+1 == 2) && (2+2 == 4); 246 | } 247 | 248 | // Conditionals in Solidity 249 | 250 | // if q % 2 == 0 return 'even', else return 'odd' 251 | function evenOrOdd(uint q) public pure returns (string memory) { 252 | if(q % 2 == 0) { 253 | return "even"; 254 | } else { 255 | return "odd"; 256 | } 257 | } 258 | 259 | function evenOrOdd1(uint q) public pure returns (string memory) { 260 | if(q % 2 == 0) { 261 | return "even"; 262 | } 263 | return "odd"; 264 | } 265 | 266 | function evenOrOdd2(uint q) public pure returns (string memory) { 267 | return q % 2 == 0 ? "even" : "odd"; 268 | } 269 | 270 | // Mappings - Key value pairs used to store information inside SC 271 | 272 | mapping(uint => address) public accountBalance; 273 | mapping(uint => string) public name; 274 | mapping(address => uint) public addresses; 275 | mapping(address => bool) public hasVoted; 276 | 277 | // Nested mapping - mapping inside a mapping 278 | mapping(address => mapping(uint => bool)) public myMapping; 279 | 280 | // Error Handling 281 | function example1(uint _amount) public { 282 | require(_amount > 10, "must be greater than 10"); 283 | emit Log("success"); 284 | } 285 | 286 | function example2(uint _amount) public { 287 | if(_amount <= 10) { 288 | revert("must be greater than 10"); 289 | } 290 | emit Log("success"); 291 | } 292 | 293 | ======= 294 | //nested mapping used to keep track of events involving multi users 295 | //mapping(address => mapping(address => uint) public allowance; 296 | //>>>>>>> main 297 | 298 | //Try Catch 299 | function divide(uint256 a, uint256 b) public returns (uint256) { 300 | uint256 result; 301 | try this.doDivide(a, b) returns (uint256 _result) { 302 | result = _result; 303 | } catch { 304 | revert("Division by zero"); 305 | } 306 | return result; 307 | } 308 | 309 | function doDivide(uint256 a, uint256 b) public pure returns (uint256) { 310 | require(b != 0, "Division by zero"); 311 | return a / b; 312 | } 313 | 314 | 315 | //For loop 316 | function sum(uint[] memory numbers) public pure returns (uint) { 317 | uint total = 0; 318 | for (uint i = 0; i < numbers.length; i++) { 319 | total += numbers[i]; 320 | } 321 | return total; 322 | } 323 | // While loops 324 | 325 | function factorial(uint n) public pure returns (uint) { 326 | uint result = 1; 327 | while (n > 0) { 328 | result *= n; 329 | n--; 330 | } 331 | return result; 332 | } 333 | 334 | 335 | 336 | } 337 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # learn-solidity-language 2 | 3 | This REPO is open for contributions. 4 | 5 | # Resources to learn Solidity 6 | - https://cryptozombies.io/ 7 | - https://www.learnweb3.io/ 8 | - https://www.smartcontract.engineer/ 9 | - https://solidity-by-example.org/ 10 | - https://www.web3.university/ 11 | - https://www.useweb3.xyz/ 12 | - https://university.alchemy.com/ 13 | --------------------------------------------------------------------------------