├── Solidity ├── Structure.sol ├── bytes.sol ├── conditional.sol ├── enum.sol ├── identity.sol ├── loops.sol ├── mapping.sol ├── mapping2.sol ├── overflow.sol └── payment.sol └── Web3.js ├── .gitignore ├── Documents.md ├── demo.sol ├── index.html ├── package-lock.json ├── package.json └── web3-new.js /Solidity/Structure.sol: -------------------------------------------------------------------------------- 1 | // SPDX-License-Identifier: GPL-3.0 2 | 3 | pragma solidity >= 0.7.0; 4 | 5 | struct Student { 6 | uint roll; 7 | string name; 8 | } 9 | 10 | contract Demo { 11 | 12 | Student public s1; 13 | 14 | constructor (uint _roll, string memory _name){ 15 | s1.roll = _roll; 16 | s1.name = _name; 17 | } 18 | 19 | function change (uint _roll, string memory _name) public { 20 | 21 | Student memory newStudent = Student({ 22 | roll: _roll, 23 | name: _name 24 | }); 25 | s1 = newStudent; 26 | } 27 | } 28 | -------------------------------------------------------------------------------- /Solidity/bytes.sol: -------------------------------------------------------------------------------- 1 | // SPDX-License-Identifier: GPL-3.0 2 | 3 | pragma solidity >= 0.7.0; 4 | 5 | contract by{ 6 | 7 | bytes public b1; 8 | 9 | function pushelement() public { 10 | b1.push('d'); 11 | } 12 | 13 | function getelement (uint i) public view returns (bytes1) { 14 | return b1[i]; 15 | } 16 | 17 | function getLength () public view returns(uint){ 18 | return b1.length; 19 | } 20 | } -------------------------------------------------------------------------------- /Solidity/conditional.sol: -------------------------------------------------------------------------------- 1 | // SPDX-License-Identifier: GPL-3.0 2 | 3 | pragma solidity >= 0.7.0; 4 | 5 | contract condition { 6 | 7 | function conditions (int a) public pure returns(string memory){ 8 | 9 | string memory value; 10 | 11 | if(a == 0){ 12 | value = "It's a zero"; 13 | } 14 | else if(a > 0){ 15 | value = "Bhai positive hai"; 16 | } 17 | else{ 18 | value = "Bhai ye to negative nikla"; 19 | } 20 | return value; 21 | } 22 | } -------------------------------------------------------------------------------- /Solidity/enum.sol: -------------------------------------------------------------------------------- 1 | // SPDX-License-Identifier: GPL-3.0 2 | 3 | pragma solidity >= 0.7.0; 4 | 5 | contract Enum { 6 | 7 | enum user {allowed, not_allowed, wait} 8 | user u1 = user.allowed; 9 | uint public lottery = 1000; 10 | 11 | function Owner() public { 12 | 13 | if (u1 == user.allowed){ 14 | lottery = 0; 15 | } 16 | } 17 | } -------------------------------------------------------------------------------- /Solidity/identity.sol: -------------------------------------------------------------------------------- 1 | // SPDX-License-Identifier: GPL 3.0 2 | 3 | pragma solidity >= 0.5.0 < 0.9.0; 4 | 5 | contract Identity { 6 | 7 | string name; 8 | uint age; 9 | 10 | constructor() { 11 | 12 | name = "Vishal"; 13 | age = 21; 14 | } 15 | 16 | function getName () view public returns (string memory) { 17 | return name; 18 | } 19 | 20 | function getAge () view public returns (uint) { 21 | return age; 22 | } 23 | } -------------------------------------------------------------------------------- /Solidity/loops.sol: -------------------------------------------------------------------------------- 1 | // SPDX-License-Identifier: GPL-3.0 2 | 3 | pragma solidity >= 0.7.0; 4 | 5 | contract loops { 6 | 7 | uint[3] public arr; 8 | uint public count; 9 | 10 | function loop() public { 11 | while (count < arr.length){ 12 | arr[count] = count; 13 | count++; 14 | } 15 | } 16 | } -------------------------------------------------------------------------------- /Solidity/mapping.sol: -------------------------------------------------------------------------------- 1 | // SPDX-License-Identifier: GPL-3.0 2 | 3 | pragma solidity >= 0.7.0; 4 | 5 | contract Map { 6 | 7 | mapping (uint => string) public roll_no; 8 | 9 | function setter(uint keys, string memory value) public { 10 | 11 | roll_no[keys] = value; 12 | } 13 | } -------------------------------------------------------------------------------- /Solidity/mapping2.sol: -------------------------------------------------------------------------------- 1 | // SPDX-License-Identifier: GPL-3.0 2 | 3 | pragma solidity >= 0.7.0; 4 | 5 | contract Map2 { 6 | 7 | struct Student { 8 | string name; 9 | uint class; 10 | } 11 | 12 | mapping (uint => Student) public data; 13 | 14 | function setter (uint _roll, string memory _name, uint _class) public { 15 | 16 | data[_roll] = Student(_name,_class); 17 | } 18 | } -------------------------------------------------------------------------------- /Solidity/overflow.sol: -------------------------------------------------------------------------------- 1 | //Overflow condition in smart contract 2 | 3 | // SPDX-License-Identifier: GPL-3.0 4 | 5 | pragma solidity >= 0.5.0; 6 | 7 | contract overflow{ 8 | 9 | uint8 public money = 255; 10 | 11 | function setdata() public{ 12 | money = money + 1; 13 | } 14 | 15 | } 16 | -------------------------------------------------------------------------------- /Solidity/payment.sol: -------------------------------------------------------------------------------- 1 | // SPDX-License-Identifier: GPL-3.0 2 | 3 | pragma solidity >= 0.7.0; 4 | 5 | contract pay { 6 | 7 | address payable user = payable(0xAb8483F64d9C6d1EcF9b849Ae677dD3315835cb2); 8 | 9 | function payether() public payable { 10 | 11 | } 12 | 13 | function getbalance() public view returns(uint) { 14 | return address(this).balance; 15 | } 16 | 17 | function sendether() public { 18 | user.transfer(1 ether); 19 | } 20 | } -------------------------------------------------------------------------------- /Web3.js/.gitignore: -------------------------------------------------------------------------------- 1 | node_modules -------------------------------------------------------------------------------- /Web3.js/Documents.md: -------------------------------------------------------------------------------- 1 | # Commands related to Web3.js 2 | 3 | 1. To install web3 - npm install --save web3 4 | 2. To import web3 - let Web3 =require("web3"); 5 | 3. To connect with Ganache - let web3 = new Web3(new Web3.providers.HttpProvider("[HTTP://127.0.0.1:7545]")); [Remember that the ganache must be running] 6 | 7 | 4. To get the balance of an account - web3.eth.getBalance("paste the address of the account inside it").then(console.log); 8 | 9 | 5. To convert wei into ether - web3.eth.getBalance("paste the address of the account inside it").then(function(result) {console.log(web3.utils.fromWei(result,"ether"));}) 10 | 11 | 6. To transfer ether from one account to another - web3.eth.sendTransaction({from:"paste the address of the account inside it",to:"paste the address of the account inside it",value:web3.utils.toWei("1","ether")}); 12 | -------------------------------------------------------------------------------- /Web3.js/demo.sol: -------------------------------------------------------------------------------- 1 | // SPDX-License-Identifier: MIT 2 | 3 | pragma solidity >= 0.8.21; 4 | 5 | contract demo { 6 | 7 | uint public x = 10; 8 | 9 | function set (uint _x) public { 10 | x = _x; 11 | } 12 | } -------------------------------------------------------------------------------- /Web3.js/index.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | Document 7 | 8 | 9 | 10 | 11 | 12 | -------------------------------------------------------------------------------- /Web3.js/package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "web3", 3 | "version": "1.0.0", 4 | "main": "index.js", 5 | "scripts": { 6 | "test": "echo \"Error: no test specified\" && exit 1" 7 | }, 8 | "keywords": [], 9 | "author": "", 10 | "license": "ISC", 11 | "dependencies": { 12 | "dotenv": "^16.3.1", 13 | "fs": "^0.0.1-security", 14 | "ganache-cli": "^6.12.2", 15 | "solc": "^0.8.21", 16 | "web3": "^1.10.2", 17 | "web3.js-browser": "^0.2.0" 18 | }, 19 | "description": "" 20 | } 21 | -------------------------------------------------------------------------------- /Web3.js/web3-new.js: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | // The Deployment is not working in this code.... 6 | 7 | 8 | 9 | 10 | solc = require("solc"); 11 | 12 | // file system - read and write files to your computer 13 | fs = require("fs"); 14 | 15 | // ganache - local blockchain 16 | 17 | // web3 interface 18 | Web3 = require("web3"); 19 | 20 | // setup a http provider 21 | web3 = new Web3(new Web3.providers.HttpProvider("http://127.0.0.1:7545")); 22 | 23 | // reading the file contents of the smart contract 24 | 25 | fileContent = fs.readFileSync("demo.sol").toString(); 26 | console.log(fileContent); 27 | 28 | // create an input structure for my solidity complier 29 | var input = { 30 | language: "Solidity", 31 | sources: { 32 | "demo.sol": { 33 | content: fileContent, 34 | }, 35 | }, 36 | 37 | settings: { 38 | outputSelection: { 39 | "*": { 40 | "*": ["*"], 41 | }, 42 | }, 43 | }, 44 | }; 45 | 46 | var output = JSON.parse(solc.compile(JSON.stringify(input))); 47 | console.log("Output: ", output); 48 | 49 | ABI = output.contracts["demo.sol"]["demo"].abi; 50 | bytecode = output.contracts["demo.sol"]["demo"].evm.bytecode.object; 51 | console.log("Bytecode: ", bytecode); 52 | console.log("ABI: ", ABI); 53 | 54 | contract = new web3.eth.Contract(ABI); 55 | let defaultAccount; 56 | web3.eth.getAccounts().then((accounts) => { 57 | console.log("Accounts:", accounts); //it will show all the ganache accounts 58 | 59 | defaultAccount = accounts[0]; 60 | console.log("Default Account:", defaultAccount); //to deploy the contract from default Account 61 | contract 62 | .deploy({ data: bytecode }) 63 | .send({ from: defaultAccount, gas: 470000 }) 64 | .on("receipt", (receipt) => { //event,transactions,contract address will be returned by blockchain 65 | console.log("Contract Address:", receipt.contractAddress); 66 | }) 67 | .then((demoContract) => { 68 | demoContract.methods.x().call((err, data) => { 69 | console.log("Initial Value:", data); 70 | }); 71 | }); 72 | 73 | }); 74 | --------------------------------------------------------------------------------