├── Day6 ├── React Lottery App │ ├── test.txt │ ├── public │ │ ├── test.txt │ │ ├── robots.txt │ │ ├── favicon.ico │ │ ├── logo192.png │ │ ├── logo512.png │ │ ├── manifest.json │ │ └── index.html │ ├── src │ │ ├── test.txt │ │ ├── web3.js │ │ ├── setupTests.js │ │ ├── App.test.js │ │ ├── index.css │ │ ├── index.js │ │ ├── App.css │ │ ├── lottery.js │ │ ├── logo.svg │ │ ├── App.js │ │ └── serviceWorker.js │ ├── package.json │ └── README.md ├── README.md └── Lottery.sol ├── Day5 ├── erc20Token.sol ├── erc721Token.sol ├── Auction.sol └── README.md ├── Day2 ├── contracts │ └── vote.sol └── README.md ├── Day3 ├── contracts │ ├── ERC20.sol │ └── hw1.sol └── README.md ├── Day1 └── README.md └── Day4 └── README.md /Day6/React Lottery App/test.txt: -------------------------------------------------------------------------------- 1 | 2 | -------------------------------------------------------------------------------- /Day6/React Lottery App/public/test.txt: -------------------------------------------------------------------------------- 1 | 2 | -------------------------------------------------------------------------------- /Day6/React Lottery App/src/test.txt: -------------------------------------------------------------------------------- 1 | 2 | -------------------------------------------------------------------------------- /Day6/README.md: -------------------------------------------------------------------------------- 1 | Deployed App: https://clinquant-gingersnap-3349f7.netlify.app/ 2 | -------------------------------------------------------------------------------- /Day6/React Lottery App/public/robots.txt: -------------------------------------------------------------------------------- 1 | # https://www.robotstxt.org/robotstxt.html 2 | User-agent: * 3 | Disallow: 4 | -------------------------------------------------------------------------------- /Day6/React Lottery App/public/favicon.ico: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/srv-smn/Workshop/HEAD/Day6/React Lottery App/public/favicon.ico -------------------------------------------------------------------------------- /Day6/React Lottery App/public/logo192.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/srv-smn/Workshop/HEAD/Day6/React Lottery App/public/logo192.png -------------------------------------------------------------------------------- /Day6/React Lottery App/public/logo512.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/srv-smn/Workshop/HEAD/Day6/React Lottery App/public/logo512.png -------------------------------------------------------------------------------- /Day6/React Lottery App/src/web3.js: -------------------------------------------------------------------------------- 1 | import Web3 from 'web3'; 2 | 3 | const web3 = new Web3(window.web3.currentProvider); 4 | 5 | export default web3; 6 | -------------------------------------------------------------------------------- /Day6/React Lottery App/src/setupTests.js: -------------------------------------------------------------------------------- 1 | // jest-dom adds custom jest matchers for asserting on DOM nodes. 2 | // allows you to do things like: 3 | // expect(element).toHaveTextContent(/react/i) 4 | // learn more: https://github.com/testing-library/jest-dom 5 | import '@testing-library/jest-dom/extend-expect'; 6 | -------------------------------------------------------------------------------- /Day6/React Lottery App/src/App.test.js: -------------------------------------------------------------------------------- 1 | import React from 'react'; 2 | import { render } from '@testing-library/react'; 3 | import App from './App'; 4 | 5 | test('renders learn react link', () => { 6 | const { getByText } = render(); 7 | const linkElement = getByText(/learn react/i); 8 | expect(linkElement).toBeInTheDocument(); 9 | }); 10 | -------------------------------------------------------------------------------- /Day5/erc20Token.sol: -------------------------------------------------------------------------------- 1 | // SPDX-License-Identifier: MIT 2 | pragma solidity ^0.8.9; 3 | 4 | import "@openzeppelin/contracts/token/ERC20/ERC20.sol"; 5 | import "@openzeppelin/contracts/access/Ownable.sol"; 6 | 7 | contract MyToken is ERC20, Ownable { 8 | constructor() ERC20("MyToken", "MTK") {} 9 | 10 | function mint(address to, uint256 amount) public onlyOwner { 11 | _mint(to, amount); 12 | } 13 | } 14 | -------------------------------------------------------------------------------- /Day6/React Lottery App/src/index.css: -------------------------------------------------------------------------------- 1 | body { 2 | margin: 0; 3 | font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', 'Roboto', 'Oxygen', 4 | 'Ubuntu', 'Cantarell', 'Fira Sans', 'Droid Sans', 'Helvetica Neue', 5 | sans-serif; 6 | -webkit-font-smoothing: antialiased; 7 | -moz-osx-font-smoothing: grayscale; 8 | } 9 | 10 | code { 11 | font-family: source-code-pro, Menlo, Monaco, Consolas, 'Courier New', 12 | monospace; 13 | } 14 | -------------------------------------------------------------------------------- /Day6/React Lottery App/src/index.js: -------------------------------------------------------------------------------- 1 | import React from 'react'; 2 | import ReactDOM from 'react-dom'; 3 | import './index.css'; 4 | import App from './App'; 5 | import * as serviceWorker from './serviceWorker'; 6 | 7 | ReactDOM.render( 8 | 9 | 10 | , 11 | document.getElementById('root') 12 | ); 13 | 14 | // If you want your app to work offline and load faster, you can change 15 | // unregister() to register() below. Note this comes with some pitfalls. 16 | // Learn more about service workers: https://bit.ly/CRA-PWA 17 | serviceWorker.unregister(); 18 | -------------------------------------------------------------------------------- /Day2/contracts/vote.sol: -------------------------------------------------------------------------------- 1 | 2 | // SPDX-License-Identifier: MIT 3 | pragma solidity ^0.8.7; 4 | 5 | contract Voting { 6 | uint public liked; 7 | uint public disliked; 8 | mapping(address => bool) check; 9 | 10 | modifier onlyOnce(){ 11 | require(!check[msg.sender], "already voted"); 12 | _; 13 | } 14 | 15 | function iLiked() public onlyOnce{ 16 | liked = liked+1; 17 | check[msg.sender] = true; 18 | } 19 | 20 | function idisLiked() public onlyOnce{ 21 | disliked = disliked+1; 22 | check[msg.sender] = true; 23 | } 24 | } 25 | 26 | -------------------------------------------------------------------------------- /Day3/contracts/ERC20.sol: -------------------------------------------------------------------------------- 1 | // SPDX-License-Identifier: MIT 2 | pragma solidity ^0.8.9; 3 | 4 | import "@openzeppelin/contracts/token/ERC20/ERC20.sol"; 5 | import "@openzeppelin/contracts/token/ERC20/extensions/ERC20Burnable.sol"; 6 | import "@openzeppelin/contracts/access/Ownable.sol"; 7 | 8 | contract RUPEES is ERC20, ERC20Burnable, Ownable { 9 | constructor() ERC20("RUPEES", "RS") {} 10 | 11 | function mint(address to, uint256 amount) public onlyOwner { 12 | _mint(to, amount); 13 | } 14 | 15 | function decimals() public view virtual override returns (uint8) { 16 | return 2; 17 | } 18 | } 19 | -------------------------------------------------------------------------------- /Day6/React Lottery App/public/manifest.json: -------------------------------------------------------------------------------- 1 | { 2 | "short_name": "React App", 3 | "name": "Create React App Sample", 4 | "icons": [ 5 | { 6 | "src": "favicon.ico", 7 | "sizes": "64x64 32x32 24x24 16x16", 8 | "type": "image/x-icon" 9 | }, 10 | { 11 | "src": "logo192.png", 12 | "type": "image/png", 13 | "sizes": "192x192" 14 | }, 15 | { 16 | "src": "logo512.png", 17 | "type": "image/png", 18 | "sizes": "512x512" 19 | } 20 | ], 21 | "start_url": ".", 22 | "display": "standalone", 23 | "theme_color": "#000000", 24 | "background_color": "#ffffff" 25 | } 26 | -------------------------------------------------------------------------------- /Day6/React Lottery App/src/App.css: -------------------------------------------------------------------------------- 1 | .App { 2 | text-align: center; 3 | } 4 | 5 | .App-logo { 6 | height: 40vmin; 7 | pointer-events: none; 8 | } 9 | 10 | @media (prefers-reduced-motion: no-preference) { 11 | .App-logo { 12 | animation: App-logo-spin infinite 20s linear; 13 | } 14 | } 15 | 16 | .App-header { 17 | background-color: #282c34; 18 | min-height: 100vh; 19 | display: flex; 20 | flex-direction: column; 21 | align-items: center; 22 | justify-content: center; 23 | font-size: calc(10px + 2vmin); 24 | color: white; 25 | } 26 | 27 | .App-link { 28 | color: #61dafb; 29 | } 30 | 31 | @keyframes App-logo-spin { 32 | from { 33 | transform: rotate(0deg); 34 | } 35 | to { 36 | transform: rotate(360deg); 37 | } 38 | } 39 | -------------------------------------------------------------------------------- /Day3/contracts/hw1.sol: -------------------------------------------------------------------------------- 1 | /** 2 | *Submitted for verification at polygonscan.com on 2023-03-19 3 | */ 4 | 5 | // SPDX-License-Identifier: MIT 6 | pragma solidity ^0.8.7; 7 | import './Votingtoken.sol'; 8 | 9 | contract Voting { 10 | uint public liked; 11 | uint public disliked; 12 | mapping(address => bool) check; 13 | VotingToken vt; 14 | 15 | modifier onlyOnce(){ 16 | require(!check[msg.sender], "already voted"); 17 | _; 18 | } 19 | 20 | constructor(address _vt) { 21 | vt = VotingToken(_vt); 22 | } 23 | 24 | function iLiked() public onlyOnce{ 25 | liked = liked+1; 26 | check[msg.sender] = true; 27 | vt.mint(msg.sender,1 * 10 ** vt.decimals()); 28 | } 29 | 30 | function idisLiked() public onlyOnce{ 31 | disliked = disliked+1; 32 | check[msg.sender] = true; 33 | vt.mint(msg.sender,1 * 10 ** vt.decimals()); 34 | 35 | } 36 | } 37 | -------------------------------------------------------------------------------- /Day6/Lottery.sol: -------------------------------------------------------------------------------- 1 | // SPDX-License-Identifier: MIT 2 | pragma solidity 0.8.17 ; 3 | 4 | contract Lottery{ 5 | address public manager ; 6 | address[] public players ; 7 | 8 | constructor() { 9 | manager = msg.sender; 10 | } 11 | 12 | function enter() public payable { 13 | require(msg.value > 0.001 ether); 14 | players.push(msg.sender); 15 | } 16 | function random() private view returns(uint){ 17 | return uint(keccak256(abi.encodePacked(block.difficulty,block.timestamp,players))); 18 | } 19 | function pickWinner() public { 20 | require(msg.sender == manager); 21 | uint index = random() % players.length ; 22 | payable(players[index]).transfer(address(this).balance); 23 | players = new address[](0); 24 | } 25 | 26 | function getPlayers() public view returns(address[] memory){ 27 | return players; 28 | } 29 | 30 | } 31 | -------------------------------------------------------------------------------- /Day6/React Lottery App/package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "lottery-react", 3 | "version": "0.1.0", 4 | "private": true, 5 | "dependencies": { 6 | "@testing-library/jest-dom": "^4.2.4", 7 | "@testing-library/react": "^9.5.0", 8 | "@testing-library/user-event": "^7.2.1", 9 | "react": "^16.13.1", 10 | "react-dom": "^16.13.1", 11 | "react-scripts": "3.4.2", 12 | "semantic-ui-react": "^1.2.0", 13 | "web3": "^1.0.0-beta.26" 14 | }, 15 | "scripts": { 16 | "start": "react-scripts start", 17 | "build": "react-scripts build", 18 | "test": "react-scripts test", 19 | "eject": "react-scripts eject" 20 | }, 21 | "eslintConfig": { 22 | "extends": "react-app" 23 | }, 24 | "browserslist": { 25 | "production": [ 26 | ">0.2%", 27 | "not dead", 28 | "not op_mini all" 29 | ], 30 | "development": [ 31 | "last 1 chrome version", 32 | "last 1 firefox version", 33 | "last 1 safari version" 34 | ] 35 | } 36 | } 37 | -------------------------------------------------------------------------------- /Day5/erc721Token.sol: -------------------------------------------------------------------------------- 1 | // SPDX-License-Identifier: MIT 2 | pragma solidity ^0.8.9; 3 | 4 | import "@openzeppelin/contracts/token/ERC721/ERC721.sol"; 5 | import "@openzeppelin/contracts/token/ERC721/extensions/ERC721URIStorage.sol"; 6 | import "@openzeppelin/contracts/access/Ownable.sol"; 7 | 8 | contract Gokuldham is ERC721, ERC721URIStorage, Ownable { 9 | constructor() ERC721("FRIENDS", "GS") {} 10 | 11 | function _baseURI() internal pure override returns (string memory) { 12 | return "https://gateway.pinata.cloud/ipfs/"; 13 | } 14 | 15 | function safeMint(address to, uint256 tokenId, string memory uri) 16 | public 17 | onlyOwner 18 | { 19 | _safeMint(to, tokenId); 20 | _setTokenURI(tokenId, uri); 21 | } 22 | 23 | // The following functions are overrides required by Solidity. 24 | 25 | function _burn(uint256 tokenId) internal override(ERC721, ERC721URIStorage) { 26 | super._burn(tokenId); 27 | } 28 | 29 | function tokenURI(uint256 tokenId) 30 | public 31 | view 32 | override(ERC721, ERC721URIStorage) 33 | returns (string memory) 34 | { 35 | return super.tokenURI(tokenId); 36 | } 37 | } 38 | 39 | 40 | 41 | -------------------------------------------------------------------------------- /Day6/React Lottery App/src/lottery.js: -------------------------------------------------------------------------------- 1 | import web3 from './web3' 2 | 3 | const address = '0xE4630698Cf6d56349dFA422eD86451BF115692dc'; 4 | 5 | const abi = [ 6 | { 7 | "constant": false, 8 | "inputs": [], 9 | "name": "enter", 10 | "outputs": [], 11 | "payable": true, 12 | "stateMutability": "payable", 13 | "type": "function" 14 | }, 15 | { 16 | "constant": false, 17 | "inputs": [], 18 | "name": "pickWinner", 19 | "outputs": [], 20 | "payable": false, 21 | "stateMutability": "nonpayable", 22 | "type": "function" 23 | }, 24 | { 25 | "inputs": [], 26 | "payable": false, 27 | "stateMutability": "nonpayable", 28 | "type": "constructor" 29 | }, 30 | { 31 | "constant": true, 32 | "inputs": [], 33 | "name": "getPlayers", 34 | "outputs": [ 35 | { 36 | "name": "", 37 | "type": "address[]" 38 | } 39 | ], 40 | "payable": false, 41 | "stateMutability": "view", 42 | "type": "function" 43 | }, 44 | { 45 | "constant": true, 46 | "inputs": [], 47 | "name": "manager", 48 | "outputs": [ 49 | { 50 | "name": "", 51 | "type": "address" 52 | } 53 | ], 54 | "payable": false, 55 | "stateMutability": "view", 56 | "type": "function" 57 | }, 58 | { 59 | "constant": true, 60 | "inputs": [ 61 | { 62 | "name": "", 63 | "type": "uint256" 64 | } 65 | ], 66 | "name": "players", 67 | "outputs": [ 68 | { 69 | "name": "", 70 | "type": "address" 71 | } 72 | ], 73 | "payable": false, 74 | "stateMutability": "view", 75 | "type": "function" 76 | } 77 | ]; 78 | 79 | export default new web3.eth.Contract(abi, address); 80 | -------------------------------------------------------------------------------- /Day6/React Lottery App/public/index.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 12 | 13 | 17 | 26 | 27 | React App 28 | 29 | 30 | 31 |
32 | 42 | 43 | 44 | -------------------------------------------------------------------------------- /Day3/README.md: -------------------------------------------------------------------------------- 1 | # ERC20 2 | 3 | - Ethereum Request for Comment 20 4 | - Fungible 5 | 6 | ## ERC20 interface 7 | 8 | ` 9 | 10 | interface IERC20 { 11 | function totalSupply() external view returns (uint); 12 | 13 | function balanceOf(address account) external view returns (uint); 14 | 15 | function transfer(address recipient, uint amount) external returns (bool); 16 | 17 | function allowance(address owner, address spender) external view returns (uint); 18 | 19 | function approve(address spender, uint amount) external returns (bool); 20 | 21 | function transferFrom( 22 | address sender, 23 | address recipient, 24 | uint amount 25 | ) external returns (bool); 26 | 27 | event Transfer(address indexed from, address indexed to, uint value); 28 | event Approval(address indexed owner, address indexed spender, uint value); 29 | } 30 | 31 | ` 32 | 33 | ## implementing IERC20 34 | 35 | ` 36 | 37 | // SPDX-License-Identifier: MIT 38 | pragma solidity ^0.8.17; 39 | contract ERC20 is IERC20 { 40 | uint public totalSupply; 41 | mapping(address => uint) public balanceOf; 42 | mapping(address => mapping(address => uint)) public allowance; 43 | string public name = "Solidity by Example"; 44 | string public symbol = "SOLBYEX"; 45 | uint8 public decimals = 18; 46 | 47 | function transfer(address recipient, uint amount) external returns (bool) { 48 | balanceOf[msg.sender] -= amount; 49 | balanceOf[recipient] += amount; 50 | emit Transfer(msg.sender, recipient, amount); 51 | return true; 52 | } 53 | 54 | function approve(address spender, uint amount) external returns (bool) { 55 | allowance[msg.sender][spender] = amount; 56 | emit Approval(msg.sender, spender, amount); 57 | return true; 58 | } 59 | 60 | function transferFrom( 61 | address sender, 62 | address recipient, 63 | uint amount 64 | ) external returns (bool) { 65 | allowance[sender][msg.sender] -= amount; 66 | balanceOf[sender] -= amount; 67 | balanceOf[recipient] += amount; 68 | emit Transfer(sender, recipient, amount); 69 | return true; 70 | } 71 | 72 | function mint(uint amount) external { 73 | balanceOf[msg.sender] += amount; 74 | totalSupply += amount; 75 | emit Transfer(address(0), msg.sender, amount); 76 | } 77 | 78 | function burn(uint amount) external { 79 | balanceOf[msg.sender] -= amount; 80 | totalSupply -= amount; 81 | emit Transfer(msg.sender, address(0), amount); 82 | } 83 | } 84 | 85 | 86 | ` 87 | 88 | -------------------------------------------------------------------------------- /Day6/React Lottery App/src/logo.svg: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | -------------------------------------------------------------------------------- /Day5/Auction.sol: -------------------------------------------------------------------------------- 1 | // SPDX-License-Identifier: MIT 2 | pragma solidity ^0.8.17; 3 | 4 | import "@openzeppelin/contracts/token/ERC20/IERC20.sol"; 5 | 6 | interface IERC721 { 7 | function safeTransferFrom( 8 | address from, 9 | address to, 10 | uint256 tokenId 11 | ) external; 12 | 13 | function transferFrom( 14 | address, 15 | address, 16 | uint256 17 | ) external; 18 | } 19 | 20 | contract realstate { 21 | IERC20 erc20; 22 | IERC721 erc721; 23 | uint256 nftid; 24 | uint256 highestbid; 25 | address seller; 26 | bool started; 27 | address highestBidder; 28 | uint256 endtime; 29 | mapping(address => uint256) public bids; 30 | 31 | constructor( 32 | address _erc20, 33 | address _erc721, 34 | uint256 _nftid, 35 | uint256 _highestBid 36 | ) { 37 | // erc20 address 38 | 39 | erc20 = IERC20(_erc20); 40 | 41 | //erc721 address 42 | erc721 = IERC721(_erc721); 43 | //nft id 44 | nftid = _nftid; 45 | // highest bid 46 | highestbid = _highestBid; 47 | // seller 48 | seller = msg.sender; 49 | } 50 | 51 | function start() external { 52 | require(!started, "alreadstarted"); 53 | require(msg.sender == seller, "You are not seller."); 54 | erc721.transferFrom(msg.sender, address(this), nftid); 55 | started = true; 56 | endtime = block.timestamp + 900; 57 | 58 | // endtime 59 | // transfer NFT 60 | // start ? 61 | } 62 | 63 | function bid(uint256 _amount) external { 64 | //start or not 65 | require(started, "not started"); 66 | require(block.timestamp < endtime, "ended"); 67 | if (highestBidder != address(0)) { 68 | bids[highestBidder] += highestbid; 69 | } 70 | 71 | // highest bidder 72 | require(_amount > highestbid, "you cannot bid"); 73 | highestbid = _amount; 74 | 75 | highestBidder = msg.sender; 76 | // erc20 transfer 77 | erc20.transferFrom(msg.sender, address(this), _amount); 78 | } 79 | 80 | function withdraw() external { 81 | // token transer to user 82 | 83 | uint256 bal = bids[msg.sender]; 84 | bids[msg.sender] = 0; 85 | erc20.transfer(msg.sender, bal); 86 | } 87 | 88 | function end() external { 89 | // highest bidder - erc721 transfer 90 | require(block.timestamp > endtime, "not ended"); 91 | if (highestBidder != address(0)) { 92 | erc721.safeTransferFrom(address(this), highestBidder, nftid); 93 | erc20.transfer(seller, highestbid); 94 | } else { 95 | erc721.safeTransferFrom(address(this), seller, nftid); 96 | } 97 | } 98 | } 99 | -------------------------------------------------------------------------------- /Day6/React Lottery App/README.md: -------------------------------------------------------------------------------- 1 | This project was bootstrapped with [Create React App](https://github.com/facebook/create-react-app). 2 | 3 | ## Available Scripts 4 | 5 | In the project directory, you can run: 6 | 7 | ### `npm start` 8 | 9 | Runs the app in the development mode.
10 | Open [http://localhost:3000](http://localhost:3000) to view it in the browser. 11 | 12 | The page will reload if you make edits.
13 | You will also see any lint errors in the console. 14 | 15 | ### `npm test` 16 | 17 | Launches the test runner in the interactive watch mode.
18 | See the section about [running tests](https://facebook.github.io/create-react-app/docs/running-tests) for more information. 19 | 20 | ### `npm run build` 21 | 22 | Builds the app for production to the `build` folder.
23 | It correctly bundles React in production mode and optimizes the build for the best performance. 24 | 25 | The build is minified and the filenames include the hashes.
26 | Your app is ready to be deployed! 27 | 28 | See the section about [deployment](https://facebook.github.io/create-react-app/docs/deployment) for more information. 29 | 30 | ### `npm run eject` 31 | 32 | **Note: this is a one-way operation. Once you `eject`, you can’t go back!** 33 | 34 | If you aren’t satisfied with the build tool and configuration choices, you can `eject` at any time. This command will remove the single build dependency from your project. 35 | 36 | Instead, it will copy all the configuration files and the transitive dependencies (webpack, Babel, ESLint, etc) right into your project so you have full control over them. All of the commands except `eject` will still work, but they will point to the copied scripts so you can tweak them. At this point you’re on your own. 37 | 38 | You don’t have to ever use `eject`. The curated feature set is suitable for small and middle deployments, and you shouldn’t feel obligated to use this feature. However we understand that this tool wouldn’t be useful if you couldn’t customize it when you are ready for it. 39 | 40 | ## Learn More 41 | 42 | You can learn more in the [Create React App documentation](https://facebook.github.io/create-react-app/docs/getting-started). 43 | 44 | To learn React, check out the [React documentation](https://reactjs.org/). 45 | 46 | ### Code Splitting 47 | 48 | This section has moved here: https://facebook.github.io/create-react-app/docs/code-splitting 49 | 50 | ### Analyzing the Bundle Size 51 | 52 | This section has moved here: https://facebook.github.io/create-react-app/docs/analyzing-the-bundle-size 53 | 54 | ### Making a Progressive Web App 55 | 56 | This section has moved here: https://facebook.github.io/create-react-app/docs/making-a-progressive-web-app 57 | 58 | ### Advanced Configuration 59 | 60 | This section has moved here: https://facebook.github.io/create-react-app/docs/advanced-configuration 61 | 62 | ### Deployment 63 | 64 | This section has moved here: https://facebook.github.io/create-react-app/docs/deployment 65 | 66 | ### `npm run build` fails to minify 67 | 68 | This section has moved here: https://facebook.github.io/create-react-app/docs/troubleshooting#npm-run-build-fails-to-minify 69 | -------------------------------------------------------------------------------- /Day5/README.md: -------------------------------------------------------------------------------- 1 | ## Steps to deploy 2 | 3 | ## Contracts 4 | - ERC 20 token Contract 5 | - NFT Contract 6 | - Auction/Realstate contract 7 | 8 | For this current example we will take 3 Accounts.Let’s call them as Account 1, Account 2, Account 3. 9 | 10 | Steps to deploy: 11 | 12 | 1. Deploy `ERC20`(from Account 1) 13 | 2. Deploy `NFT`(from Account 1) 14 | 3. Mint a `NFT` (from Account 1) to Account 1. 15 | 16 | Steps: 17 | 18 | a. Call `safeMint` function of ERC721 19 | b. in `to` argument pass the address of Account 1 20 | c. in `tokenId` argument pass the tokenId 1 21 | d. in `uri` pass any string , since you are deploying it on local network. 22 | 23 | 4. Deploy the `Auction` contract by passing the required arguments: 24 | 25 | Steps: 26 | 27 | a. pass the address of `ERC20` contract in `_ERC20` parameter 28 | b. pass the address of `ERC721` contract in `_ERC721` parameter 29 | c. pass the NFT index (ie. 1) in `NFTID` parameter 30 | d. pass the `1000000000000000000` (1 eth denomination)in `_HIGHESTBID` paramenter. 31 | 32 | Now We want Account 2 and Account 3 to participate in the bidding process. But to get started with the biding process Account 2 and Account 3 should have ERC20 token into their wallet. 33 | 34 | so lets see the further process: 35 | 36 | 5. Mint ERC20 token from Account 1(as this account is the owner of ERC20) to Account 2. 37 | 38 | Steps to mint: 39 | 40 | a. call `mint` function of ERC20 and pass Account’s 2 address in `to` argument and `1000000000000000000000` (1000 token) in `amount` argument. 41 | b. call `mint` function of ERC20 and pass Account’s 3 address in `to` argument and `1000000000000000000000` (1000 token) in `amount` argument. 42 | 43 | Before we can bid we need to start the bidding process. As we know in `start()` function we will transfer NFT from Account 1 to Auction Contract. so before calling Start we need to give approval to Auction contract to access Account’s 1 NFT . 44 | 45 | 6. call `approve` function from Account 1 of NFT contract. pass `Auction` contract address in `to` argument and `1` in `tokenId` argument. 46 | To start the bidding process we need follow the below steps: 47 | 7. call `start()` function from Account 1 (as he is the owner of Auction) 48 | 49 | Now we are all set to start the Bidding process, Since Account 2 and Account 3 will participate in bidding. we need to give approval to `Auction` contract from Account 2 and Account 3 of ERC20 contract. Here is the steps to do so. 50 | 51 | 52 | 8. call `approve()` function from Account 2 of ERC20 contract, pass `Auction` contract address in `spender` parameter and `1000000000000000000000` in `amount` parameter. 53 | Note we had provided approval `1000000000000000000000` (ie 1000) token. We had passed large number because we don’t want to call `approve()` function before calling `bid()` function every time. 54 | 9. call `approve()` function from Account 3 of ERC20 contract, pass `Auction` contract address in `spender` parameter and `1000000000000000000000` in `amount` parameter. 55 | Note we had provided approval `1000000000000000000000` (ie 1000) token. We had passed large number because we don’t want to call `approve()` function before calling `bid()` function every time. 56 | 57 | Now we can `bid()` function from Account 2 and Account 3. and play around it. 58 | once time is over for bidding you can call `end()` function so that NFT will be transfered to Highest Bidder and Money will be transfered to seller. 59 | 60 | Note: to check the balance ofERC20 token you can call balanceOf function of ERC20. 61 | 62 | 63 | -------------------------------------------------------------------------------- /Day6/React Lottery App/src/App.js: -------------------------------------------------------------------------------- 1 | import React ,{Component} from 'react'; 2 | import logo from './logo.svg'; 3 | import './App.css'; 4 | import web3 from './web3'; 5 | import lottery from './lottery'; 6 | import { Segment,Header, Card, Form, Container, Button } from 'semantic-ui-react' ; 7 | 8 | 9 | class App extends Component { 10 | state ={ 11 | manager:'', 12 | players:[], 13 | balance: '', 14 | value:'', 15 | message:'' 16 | }; 17 | 18 | async componentDidMount(){ 19 | const manager = await lottery.methods.manager().call(); 20 | const players = await lottery.methods.getPlayers().call(); 21 | const balance = await web3.eth.getBalance(lottery.options.address) 22 | this.setState({manager, players, balance}); 23 | 24 | } 25 | // header 26 | HeaderExampleFloating(){ 27 | return ( 28 | 29 |
30 | LOTTERY CONTRACT 31 |
32 |
33 | ); 34 | } 35 | 36 | renderCards(){ 37 | const items = [ 38 | { 39 | header: this.state.manager, 40 | description: 41 | 'Owner/creator of the contract', 42 | meta: 'MANAGER', 43 | color: 'red', 44 | centered: true, 45 | style:{overflowWrap: 'break-word'}, 46 | }, 47 | { 48 | header: '0.001 ETH', 49 | description: 50 | 'Minimum contribution to participate in the lottery', 51 | meta: 'Minimum Contribution', 52 | color: 'blue', 53 | centered: true, 54 | style:{overflowWrap: 'break-word'}, 55 | }, 56 | { 57 | header: this.state.players.length, 58 | description: 59 | 'Tolal number of players in the Lottery', 60 | meta: 'total participants', 61 | color: 'yellow', 62 | centered: true, 63 | style:{overflowWrap: 'break-word'}, 64 | }, 65 | { 66 | header: web3.utils.fromWei(this.state.balance,'ether'), 67 | description: 68 | 'Total money in the lottery', 69 | meta: 'Winning amount', 70 | color: 'blue', 71 | centered: true, 72 | style:{overflowWrap: 'break-word'}, 73 | }, 74 | ] 75 | return ; 76 | 77 | } 78 | onSubmit = async event =>{ 79 | event.preventDefault(); 80 | const accounts = await web3.eth.getAccounts(); 81 | this.setState({message: 'Waiting for transaction success ...'}); 82 | await lottery.methods.enter().send({ 83 | from:accounts[0], 84 | value: web3.utils.toWei(this.state.value,'ether') 85 | }); 86 | this.setState({message: 'You have entered in the contract'}); 87 | window.location.reload(false); 88 | 89 | }; 90 | 91 | onClick = async() =>{ 92 | const accounts = await web3.eth.getAccounts(); 93 | this.setState({message: 'waiting for transaction success ...'}); 94 | await lottery.methods.pickWinner().send({ 95 | from: accounts[0] 96 | }); 97 | this.setState({message: 'Winner has been picked...'}); 98 | window.location.reload(false); 99 | } 100 | 101 | onConnect = async() =>{ 102 | try { 103 | await window.ethereum.request({ method: 'eth_requestAccounts' }); 104 | console.log('Connected to Metamask wallet'); 105 | // additional code to handle connected wallet here 106 | } catch (error) { 107 | console.log(error); 108 | } 109 | } 110 | 111 | 112 | 113 | 114 | 115 | 116 | render(){ 117 | return ( 118 |
119 |

{this.HeaderExampleFloating()}

120 |

{this.renderCards()}

121 |

122 | 123 | 124 | 125 |

Connect your wallet

126 | 128 |
{this.state.message}
129 | 130 | 131 | 132 | 133 | 134 |
135 |

Want to try your luck ?

136 | this.setState({value: event.target.value})} 140 | /> 141 | 142 | 143 |
144 |
145 |

146 | 147 | 148 |

Ready to pick a winner ?

149 | 151 |
{this.state.message}
152 |
153 |
154 | 155 |
156 | ); 157 | } 158 | } 159 | 160 | export default App; 161 | -------------------------------------------------------------------------------- /Day6/React Lottery App/src/serviceWorker.js: -------------------------------------------------------------------------------- 1 | // This optional code is used to register a service worker. 2 | // register() is not called by default. 3 | 4 | // This lets the app load faster on subsequent visits in production, and gives 5 | // it offline capabilities. However, it also means that developers (and users) 6 | // will only see deployed updates on subsequent visits to a page, after all the 7 | // existing tabs open on the page have been closed, since previously cached 8 | // resources are updated in the background. 9 | 10 | // To learn more about the benefits of this model and instructions on how to 11 | // opt-in, read https://bit.ly/CRA-PWA 12 | 13 | const isLocalhost = Boolean( 14 | window.location.hostname === 'localhost' || 15 | // [::1] is the IPv6 localhost address. 16 | window.location.hostname === '[::1]' || 17 | // 127.0.0.0/8 are considered localhost for IPv4. 18 | window.location.hostname.match( 19 | /^127(?:\.(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)){3}$/ 20 | ) 21 | ); 22 | 23 | export function register(config) { 24 | if (process.env.NODE_ENV === 'production' && 'serviceWorker' in navigator) { 25 | // The URL constructor is available in all browsers that support SW. 26 | const publicUrl = new URL(process.env.PUBLIC_URL, window.location.href); 27 | if (publicUrl.origin !== window.location.origin) { 28 | // Our service worker won't work if PUBLIC_URL is on a different origin 29 | // from what our page is served on. This might happen if a CDN is used to 30 | // serve assets; see https://github.com/facebook/create-react-app/issues/2374 31 | return; 32 | } 33 | 34 | window.addEventListener('load', () => { 35 | const swUrl = `${process.env.PUBLIC_URL}/service-worker.js`; 36 | 37 | if (isLocalhost) { 38 | // This is running on localhost. Let's check if a service worker still exists or not. 39 | checkValidServiceWorker(swUrl, config); 40 | 41 | // Add some additional logging to localhost, pointing developers to the 42 | // service worker/PWA documentation. 43 | navigator.serviceWorker.ready.then(() => { 44 | console.log( 45 | 'This web app is being served cache-first by a service ' + 46 | 'worker. To learn more, visit https://bit.ly/CRA-PWA' 47 | ); 48 | }); 49 | } else { 50 | // Is not localhost. Just register service worker 51 | registerValidSW(swUrl, config); 52 | } 53 | }); 54 | } 55 | } 56 | 57 | function registerValidSW(swUrl, config) { 58 | navigator.serviceWorker 59 | .register(swUrl) 60 | .then(registration => { 61 | registration.onupdatefound = () => { 62 | const installingWorker = registration.installing; 63 | if (installingWorker == null) { 64 | return; 65 | } 66 | installingWorker.onstatechange = () => { 67 | if (installingWorker.state === 'installed') { 68 | if (navigator.serviceWorker.controller) { 69 | // At this point, the updated precached content has been fetched, 70 | // but the previous service worker will still serve the older 71 | // content until all client tabs are closed. 72 | console.log( 73 | 'New content is available and will be used when all ' + 74 | 'tabs for this page are closed. See https://bit.ly/CRA-PWA.' 75 | ); 76 | 77 | // Execute callback 78 | if (config && config.onUpdate) { 79 | config.onUpdate(registration); 80 | } 81 | } else { 82 | // At this point, everything has been precached. 83 | // It's the perfect time to display a 84 | // "Content is cached for offline use." message. 85 | console.log('Content is cached for offline use.'); 86 | 87 | // Execute callback 88 | if (config && config.onSuccess) { 89 | config.onSuccess(registration); 90 | } 91 | } 92 | } 93 | }; 94 | }; 95 | }) 96 | .catch(error => { 97 | console.error('Error during service worker registration:', error); 98 | }); 99 | } 100 | 101 | function checkValidServiceWorker(swUrl, config) { 102 | // Check if the service worker can be found. If it can't reload the page. 103 | fetch(swUrl, { 104 | headers: { 'Service-Worker': 'script' }, 105 | }) 106 | .then(response => { 107 | // Ensure service worker exists, and that we really are getting a JS file. 108 | const contentType = response.headers.get('content-type'); 109 | if ( 110 | response.status === 404 || 111 | (contentType != null && contentType.indexOf('javascript') === -1) 112 | ) { 113 | // No service worker found. Probably a different app. Reload the page. 114 | navigator.serviceWorker.ready.then(registration => { 115 | registration.unregister().then(() => { 116 | window.location.reload(); 117 | }); 118 | }); 119 | } else { 120 | // Service worker found. Proceed as normal. 121 | registerValidSW(swUrl, config); 122 | } 123 | }) 124 | .catch(() => { 125 | console.log( 126 | 'No internet connection found. App is running in offline mode.' 127 | ); 128 | }); 129 | } 130 | 131 | export function unregister() { 132 | if ('serviceWorker' in navigator) { 133 | navigator.serviceWorker.ready 134 | .then(registration => { 135 | registration.unregister(); 136 | }) 137 | .catch(error => { 138 | console.error(error.message); 139 | }); 140 | } 141 | } 142 | -------------------------------------------------------------------------------- /Day1/README.md: -------------------------------------------------------------------------------- 1 | # Workshop DAY 1 - Blockchain Basics 2 | 3 | ## Content 4 | - What is Blockchain ? 5 | - Understanding SHA256 6 | - Immutable ledger 7 | - Distributed P2P network 8 | - Mining 9 | - Consensus 10 | 11 | ## History: 12 | - [“How to time stamp a digital document” 1991 by Stuart Haber.](http://www.staroceans.org/e-book/Haber_Stornetta.pdf) 13 | 14 | ## What do you understand by BLOCKCHAIN? 15 | - BLOCK + CHAIN 16 | image 17 | 18 | ## Looking inside a block from bird's view. 19 | image 20 | 21 | ## Basic overview of a chain 22 | image 23 | - First block is called Genesis Block 24 | 25 | ## Backbone of Blockchain 26 | - Mining 27 | - Consensus 28 | - Hash 29 | - Immutable Ledger 30 | - P2P Network 31 | 32 | ## Hashing 33 | - Hashing is like a unique identity for every unique individual. Likefingerprint of humans 34 | image 35 | 36 | - Similarity with real world 37 | image 38 | 39 | ### Example: SHA256 40 | - NSA 41 | - 64 CHARACTER 42 | - 256 BITS 43 | - HEXA DECIMAL 44 | - Eg. ba7816bf8f01cfea414140de5dae2223b00361a396177a9cb410ff61f20015ad 45 | - Demo : https://andersbrownworth.com/blockchain/hash 46 | 47 | ### Properties of good hashing algorithm 48 | - One way 49 | - Deterministic 50 | - Fast computation 51 | - The Avalanche effect 52 | - Withstand collision 53 | 54 | ## Immutable ledger 55 | image 56 | image 57 | 58 | ### Distributed Peer to Peer Network 59 | image 60 | 61 | ### DATA ON CHAIN 62 | image 63 | 64 | ### TAMPERING DATA 65 | image 66 | 67 | ### Recovering 68 | image 69 | 70 | image 71 | 72 | image 73 | 74 | ## Mining 75 | image 76 | 77 | ## CONSENSUS PROTOCOL 78 | Through consensus algorithms reliability is achieved in the Blockchain network and establish trust between unknown peers in a distributed computing environment. 79 | 80 | - PROOF OF WORK 81 | - PROOF OF STAKE 82 | 83 | ## Let's play around with BLOCKCHAIN 84 | [BLOCKCHAIN DEMO](https://andersbrownworth.com/blockchain/blockchain) 85 | 86 | ## Why Blockchain? 87 | - Decentralized System: Decentralized nature of blockchain make the resources shared to everyone, as it does not store its information at any central location but the blockchain is copied and spread across a network of computers. 88 | - Security: Even in today’s era digitalization we are facing a lot of password related threats, data tampering, unauthorized access. The numerous cases of data leaks that has shaken the trust of the people at a large extent. But the blockchain technology is creating trust in an untrusting ecosystem, making it a potentially strong cybersecurity technology. 89 | - Transparency: There is a lack of trust between companies and consumers in sharing their personal data. There is no involvement of participants so that they know no tampering has been done with the data. But in the case of blockchain everything is visible to all the participants from the beginning till date. 90 | - No intermediaries: Blockchain has efficiently eliminate the role of intermediaries by removing the need of central authority. 91 | 92 | ## Let me ask you guys a few question. 93 | 94 | - How much sure you are about the wine you drink is labeled as a fair trade product? 95 | - Are you sure about the identity of a person you meet online is legit? 96 | - Is the healthcare insurance company sure about that the person is not suffering from any diseases by just looking at the medical reports? What if it’s tampered? 97 | - Are you sure that even after providing multi factor authorization your data is secured? 98 | - Are you sure that the donations you are giving for a charity purpose are really reaching to the right person? 99 | - Are you sure if no one is reading your chats, accessing you data , listening to your calls except you and the other person? 100 | 101 | 102 | If you are sure about the every question I asked then definitely you are lying. Isn’t? And what if I tell you blockchain resolves all these problems even more that that? Let me assure you that it does. In order to be 100% sure you need to take a ride of this blockchain journey with me . 103 | -------------------------------------------------------------------------------- /Day4/README.md: -------------------------------------------------------------------------------- 1 | # ERC721 2 | - Ethereum Request for Comment 721 3 | - Non Fungible 4 | 5 | ## Interface 6 | ` 7 | interface IERC721 { 8 | function balanceOf(address owner) external view returns (uint balance); 9 | 10 | function ownerOf(uint tokenId) external view returns (address owner); 11 | 12 | function safeTransferFrom(address from, address to, uint tokenId) external; 13 | 14 | function safeTransferFrom( 15 | address from, 16 | address to, 17 | uint tokenId, 18 | bytes calldata data 19 | ) external; 20 | 21 | function transferFrom(address from, address to, uint tokenId) external; 22 | 23 | function approve(address to, uint tokenId) external; 24 | 25 | function getApproved(uint tokenId) external view returns (address operator); 26 | 27 | function setApprovalForAll(address operator, bool _approved) external; 28 | 29 | function isApprovedForAll( 30 | address owner, 31 | address operator 32 | ) external view returns (bool); 33 | } 34 | ` 35 | 36 | ## Implementing 37 | 38 | ` 39 | contract ERC721 is IERC721 { 40 | event Transfer(address indexed from, address indexed to, uint indexed id); 41 | event Approval(address indexed owner, address indexed spender, uint indexed id); 42 | event ApprovalForAll( 43 | address indexed owner, 44 | address indexed operator, 45 | bool approved 46 | ); 47 | 48 | // Mapping from token ID to owner address 49 | mapping(uint => address) internal _ownerOf; 50 | 51 | // Mapping owner address to token count 52 | mapping(address => uint) internal _balanceOf; 53 | 54 | // Mapping from token ID to approved address 55 | mapping(uint => address) internal _approvals; 56 | 57 | // Mapping from owner to operator approvals 58 | mapping(address => mapping(address => bool)) public isApprovedForAll; 59 | 60 | function ownerOf(uint id) external view returns (address owner) { 61 | owner = _ownerOf[id]; 62 | require(owner != address(0), "token doesn't exist"); 63 | } 64 | 65 | function balanceOf(address owner) external view returns (uint) { 66 | require(owner != address(0), "owner = zero address"); 67 | return _balanceOf[owner]; 68 | } 69 | 70 | function setApprovalForAll(address operator, bool approved) external { 71 | isApprovedForAll[msg.sender][operator] = approved; 72 | emit ApprovalForAll(msg.sender, operator, approved); 73 | } 74 | 75 | function approve(address spender, uint id) external { 76 | address owner = _ownerOf[id]; 77 | require( 78 | msg.sender == owner || isApprovedForAll[owner][msg.sender], 79 | "not authorized" 80 | ); 81 | 82 | _approvals[id] = spender; 83 | 84 | emit Approval(owner, spender, id); 85 | } 86 | 87 | function getApproved(uint id) external view returns (address) { 88 | require(_ownerOf[id] != address(0), "token doesn't exist"); 89 | return _approvals[id]; 90 | } 91 | 92 | function _isApprovedOrOwner( 93 | address owner, 94 | address spender, 95 | uint id 96 | ) internal view returns (bool) { 97 | return (spender == owner || 98 | isApprovedForAll[owner][spender] || 99 | spender == _approvals[id]); 100 | } 101 | 102 | function transferFrom(address from, address to, uint id) public { 103 | require(from == _ownerOf[id], "from != owner"); 104 | require(to != address(0), "transfer to zero address"); 105 | 106 | require(_isApprovedOrOwner(from, msg.sender, id), "not authorized"); 107 | 108 | _balanceOf[from]--; 109 | _balanceOf[to]++; 110 | _ownerOf[id] = to; 111 | 112 | delete _approvals[id]; 113 | 114 | emit Transfer(from, to, id); 115 | } 116 | 117 | function safeTransferFrom(address from, address to, uint id) external { 118 | transferFrom(from, to, id); 119 | 120 | require( 121 | to.code.length == 0 || 122 | IERC721Receiver(to).onERC721Received(msg.sender, from, id, "") == 123 | IERC721Receiver.onERC721Received.selector, 124 | "unsafe recipient" 125 | ); 126 | } 127 | 128 | function safeTransferFrom( 129 | address from, 130 | address to, 131 | uint id, 132 | bytes calldata data 133 | ) external { 134 | transferFrom(from, to, id); 135 | 136 | require( 137 | to.code.length == 0 || 138 | IERC721Receiver(to).onERC721Received(msg.sender, from, id, data) == 139 | IERC721Receiver.onERC721Received.selector, 140 | "unsafe recipient" 141 | ); 142 | } 143 | 144 | function _mint(address to, uint id) internal { 145 | require(to != address(0), "mint to zero address"); 146 | require(_ownerOf[id] == address(0), "already minted"); 147 | 148 | _balanceOf[to]++; 149 | _ownerOf[id] = to; 150 | 151 | emit Transfer(address(0), to, id); 152 | } 153 | 154 | function _burn(uint id) internal { 155 | address owner = _ownerOf[id]; 156 | require(owner != address(0), "not minted"); 157 | 158 | _balanceOf[owner] -= 1; 159 | 160 | delete _ownerOf[id]; 161 | delete _approvals[id]; 162 | 163 | emit Transfer(owner, address(0), id); 164 | } 165 | } 166 | ` 167 | 168 | ## Implementation using openzepellin 169 | 170 | ` 171 | // SPDX-License-Identifier: MIT 172 | pragma solidity ^0.8.9; 173 | 174 | import "@openzeppelin/contracts/token/ERC721/ERC721.sol"; 175 | import "@openzeppelin/contracts/token/ERC721/extensions/ERC721Burnable.sol"; 176 | import "@openzeppelin/contracts/access/Ownable.sol"; 177 | 178 | contract MyToken is ERC721, ERC721Burnable, Ownable { 179 | constructor() ERC721("MyToken", "MTK") {} 180 | 181 | function _baseURI() internal pure override returns (string memory) { 182 | return "https://dummyurl/"; 183 | } 184 | 185 | function safeMint(address to, uint256 tokenId) public onlyOwner { 186 | _safeMint(to, tokenId); 187 | } 188 | } 189 | ` 190 | 191 | ## URI 192 | URI stands for Universal Resource Identifier. Think of it as an URL, through which you can get all metadata associated with the NFT. The metadata can be image URL, description, attributes, etc. 193 | 194 | ## URI Standard 195 | - URI standard of OPEN SEA 196 | 197 | ` 198 | { 199 | "description": "Friendly OpenSea Creature that enjoys long swims in the ocean.", 200 | "external_url": "https://openseacreatures.io/3", 201 | "image": "https://storage.googleapis.com/opensea-prod.appspot.com/puffs/3.png", 202 | "name": "Dave Starbelly", 203 | "attributes": [ ... ] 204 | } 205 | ` 206 | [OPENSEA STANDARD](https://docs.opensea.io/docs/metadata-standards) 207 | 208 | 209 | -------------------------------------------------------------------------------- /Day2/README.md: -------------------------------------------------------------------------------- 1 | # Workshop DAY 2 - Blockchain Basics 2 | 3 | ## Content 4 | - Introduction to Ethereum 5 | - Introduction to Remix 6 | - Introduction to Solidity 7 | 8 | ## Introtuction to Remix: [Remix IDE](https://remix.ethereum.org/) 9 | 10 | ## Introductin to Solidity 11 | 12 | ### Doc 13 | [Official Doc](https://docs.soliditylang.org/en/v0.8.19/) 14 | 15 | 16 | ### Sample Contract 17 | 18 | ``` 19 | // SPDX-License-Identifier: GPL-3.0 20 | 21 | pragma solidity >=0.8.2 <0.9.0; 22 | 23 | /** 24 | * @title Storage 25 | * @dev Store & retrieve value in a variable 26 | * @custom:dev-run-script ./scripts/deploy_with_ethers.ts 27 | */ 28 | contract Storage { 29 | 30 | uint256 number; 31 | 32 | /** 33 | * @dev Store value in variable 34 | * @param num value to store 35 | */ 36 | function store(uint256 num) public { 37 | number = num; 38 | } 39 | 40 | /** 41 | * @dev Return value 42 | * @return value of 'number' 43 | */ 44 | function retrieve() public view returns (uint256){ 45 | return number; 46 | } 47 | } 48 | ``` 49 | 50 | ### define the Solidity version compiler 51 | 52 | ``` 53 | // Any compiler version from the 0.8 release (= 0.8.x) 54 | pragma solidity ^0.8.0; 55 | // Greater than version 0.7.6, less than version 0.8.4 56 | pragma solidity >0.7.6 <0.8.4; 57 | ``` 58 | 59 | ### define a contract 60 | ``` 61 | contract Score { 62 | // You will start writing your code here =) 63 | } 64 | ``` 65 | 66 | ### variable in Solidity 67 | 68 | ``` 69 | contract Score { 70 | uint score = 5; 71 | } 72 | ``` 73 | 74 | ## Data types 75 | 76 | `bool` : The possible values are constants true and false. 77 | 78 | `Integers` 79 | 80 | `uint` : defines an unsigned integer of 256 bits by default. 81 | You can also specify the number of bits, by range of 8 bits. Here are some examples below: 82 | image 83 | 84 | 85 | `int` : defines an signed integer of 256 bits by default. 86 | 87 | `Address` : Holds a 20 byte value (size of an Ethereum address). 88 | 89 | `address a = 0x777788889999AaAAbBbbCcccddDdeeeEfFFfCcCc;` 90 | 91 | ## Getter and Setter 92 | ` contract Score { 93 | 94 | uint score = 5; 95 | 96 | function getScore() returns (uint) { 97 | 98 | return score; 99 | 100 | } 101 | 102 | function setScore(uint new_score) { 103 | 104 | score = new_score; 105 | 106 | } 107 | } ` 108 | 109 | ## Function visibility 110 | image 111 | 112 | ## View vs Pure ? 113 | view functions can only read from the contract storage. They can’t modify the contract 114 | storage. Usually, you will use view for getters. 115 | pure functions can neither read nor modify the contract storage. They are only used 116 | for computation (like mathematical operations). 117 | Because our function getScore() only reads from the contract state, it is a view function. 118 | ` 119 | function getScore() public view returns (uint) { 120 | 121 | return score; 122 | 123 | } 124 | ` 125 | 126 | ## Extra Info 127 | Solidity provides a global variable msg , that refers to the address that interacts with the 128 | contract’s functions. The msg variables offers two associated fields: 129 | 130 | `msg.sender` : returns the address of the caller of the function. 131 | `msg.value` : returns the value in Wei of the amount of Ether sent to the function. 132 | 133 | ## modifiers 134 | 135 | A modifier is a special function that enables us to change the behaviour of 136 | functions in Solidity. It is mostly used to automatically check a condition before executing a 137 | function. 138 | 139 | ` 140 | 141 | address owner; 142 | 143 | modifier onlyOwner { 144 | 145 | if (msg.sender == owner) { 146 | 147 | _; 148 | 149 | } 150 | 151 | } 152 | 153 | function setScore(uint new_score) public onlyOwner { 154 | 155 | score = new_score; 156 | 157 | } 158 | 159 | ` 160 | 161 | The modifier works with the following flow: 162 | 1. Check that the address of the caller ( msg.sender ) is equal to owner address. 163 | 2. If 1) is true, it passes the check. The _; will be replaced by the function body where 164 | the modifier is attached. 165 | A modifier can receive arguments like functions. Here is an example of a modifier that 166 | requires the caller to send a specific amount of Ether. 167 | 168 | ` 169 | modifier Fee(uint fee) { 170 | 171 | if (msg.value == fee) { 172 | 173 | _; 174 | 175 | } 176 | 177 | } 178 | ` 179 | 180 | ## Constructor 181 | 182 | ` 183 | contract Score { 184 | 185 | address owner; 186 | 187 | constructor() { 188 | 189 | owner = msg.sender; 190 | 191 | } 192 | 193 | } ` 194 | 195 | ## Advance Data Types 196 | 197 | ### Mappings 198 | 199 | Mappings are another important complex data type used in Solidity. They are useful for 200 | association, such as associating an address with a balance or a score. You define a 201 | mapping in Solidity as follow: 202 | 203 | mapping(KeyType => ValueType) mapping_name; 204 | 205 | image 206 | 207 | You can access the value associated with a key in a mapping by specifing the key name 208 | inside square brackets [] as follows: mapping_name[key] . 209 | Our smart contract will store a mapping of all the user’s addresses and their associated 210 | score. The function getUserScore(address _user) enables to retrieve the score 211 | associated to a specific user’s address. 212 | 213 | ` 214 | mapping(address => uint) score_list; 215 | 216 | function getUserScore(address user) public view returns (uint) { 217 | 218 | return score_list[user]; 219 | 220 | } 221 | ` 222 | 223 | ### Arrays 224 | 225 | Arrays are also an important part of Solidity. You have two types of arrays ( T represents 226 | the data type and k the maximum number of elements): 227 | Fixed size array : T[k] 228 | Dynamic size array : T[] 229 | 230 | uint[] all_possible_number; 231 | uint[9] one_digit_number; 232 | 233 | In Solidity, arrays are ordered numerically. Array indices are zero based. So the index of the 234 | 1st element will be 0. You access an element in an array in the same way than you do for a 235 | mapping: 236 | 237 | ` uint my_score = score_list[owner]; ` 238 | 239 | You can also used the following two methods to work with arrays: 240 | array_name.length : returns the number of elements the array holds. 241 | array_name.push(new_element) : adds a new element at the end of the array. 242 | 243 | ### Structs 244 | 245 | We can build our own datatypes by combining simpler datatypes together into more complex 246 | types using structs. 247 | We use the keyword struct, followed by the structure name , then the fields that make up 248 | the structure. 249 | For example: 250 | 251 | ` 252 | 253 | struct Funder { 254 | 255 | address addr; 256 | 257 | uint amount; 258 | 259 | } ` 260 | 261 | Here we have created a datatype called Funder, that is composed of an address and a uint. 262 | We can now declare a variable of that type 263 | 264 | Funder giver; 265 | 266 | and reference the elements using dot notation 267 | 268 | giver.addr = address (0xBA7283457B0A138890F21DE2ebCF1AfB9186A2EF); 269 | giver.amount = 2500; 270 | 271 | The size of the structure has to be finite, this imposes restrictions on the elements that can 272 | be included in the struct. 273 | 274 | ## For Reference 275 | [Solidity by example](https://solidity-by-example.org/) 276 | 277 | 278 | 279 | 280 | 281 | 282 | --------------------------------------------------------------------------------