├── 10 - Operators - Arithmatic, Logical & Bitwise └── Operators.sol ├── 11 - Control Structures ├── .DS_Store └── ControlStructure.sol ├── 12 - Scoping & Declaration ├── .DS_Store └── Scoping.sol ├── 13 - Input & Output Parameters └── InputAndOutputParameters.sol ├── 14 - Function Calls └── FunctionCall.sol ├── 15 - Function Modifiers ├── .DS_Store └── FunctionModifiers.sol ├── 16 - Fallback Function └── fallback.sol ├── 17 - Abstract Contracts └── AbstractContracts.sol ├── 18 - Creating Contracts via new Operator └── new.sol ├── 19 - Inheriting Smart Contratcs └── Inheritance.sol ├── 20 - Importing Smart Contracts ├── Import.sol └── owned.sol ├── 21 - Events & Logging ├── .DS_Store └── Coin.sol ├── 22 - Exceptions ├── .DS_Store └── Exceptions.sol ├── 23 - Complete Example - CrowdFunder Smart Contract ├── .DS_Store ├── .gitignore ├── CrowdFunder.sol ├── README.md ├── ethpm.json ├── package.json └── truffle.js ├── 24 - Complete Example - Voting Ballot Smart Contract ├── .DS_Store ├── .gitignore ├── Ballot.sol ├── README.md ├── ethpm.json ├── package.json └── truffle.js ├── 5 - Basics of Solidity By Example ├── .DS_Store └── Coin.sol ├── 6 - Layout of Solidity Source File └── layout.sol ├── 7 - General Value Types ├── ValueTypes.sol └── value.sol ├── 8 - Ether Units, Time Units └── Units.sol ├── 9 - Global Variables & Functions └── GlobalVariablesAndFunctions.sol ├── GeneralExample.sol └── README.md /10 - Operators - Arithmatic, Logical & Bitwise/Operators.sol: -------------------------------------------------------------------------------- 1 | pragma solidity ^0.4.16; 2 | 3 | /* 4 | * @title A Units & Global Variables Example 5 | * @author Toshendra Sharma 6 | * @notice Example for the Solidity Course 7 | */ 8 | 9 | contract Operators { 10 | 11 | // Arithmatic Operators 12 | uint a = 10; 13 | // +, -, unary -, unary +, *, /, % (remainder), ** (exponentiation) 14 | uint b = 2**3; // b = 8 15 | 16 | // a++ and a-- are equivalent to a += 1 / a -= 1 17 | // but the expression itself still has the previous value of a. 18 | // In contrast, --a and ++a have the same effect on a but return the value after the change. 19 | a = a++; // a = 10 20 | a = ++a; // a = 11 21 | 22 | // a += e is equivalent to a = a + e. 23 | // The operators -=, *=, /=, %=, a |=, &= and ^= are defined accordingly. 24 | a += 5; // a = 11+5 = 16 25 | 26 | 27 | 28 | 29 | // Logical Operators 30 | // ! (logical negation) 31 | // && (logical conjunction, “and”) 32 | // || (logical disjunction, “or”) 33 | // == (equality) 34 | // != (inequality) 35 | bool isOwner; 36 | isOwner = isMortal && hasWriteAccess; 37 | isOwner = true && false; // false 38 | isOwner = true || false; // true 39 | isOwner = !false; // true 40 | var check = (1 ether == 1000 finney); // true 41 | var check = (1 ether != 2000 finney); // true 42 | 43 | // Bitwise Operators 44 | // & Bitwise AND 45 | // | Bitwise OR 46 | // ^ Bitwise exclusive OR 47 | // ~ Bbitwise negation 48 | // >> Bitwise right shift 49 | // << Bitwise left shift 50 | var orValue = 0x02 | 0x01; // orValue = 0x03 51 | uint shiftValue = 0x01 << 2; // shiftValue = 4 52 | 53 | 54 | // Define consutruct here 55 | function Operators(uint initialCoins) { 56 | // Initialize state variables here 57 | } 58 | 59 | } 60 | -------------------------------------------------------------------------------- /11 - Control Structures/.DS_Store: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/blockchain-council/learn-solidity/bea129846b27e66ae30006fc340d27a75cfbe926/11 - Control Structures/.DS_Store -------------------------------------------------------------------------------- /11 - Control Structures/ControlStructure.sol: -------------------------------------------------------------------------------- 1 | pragma solidity ^0.4.16; 2 | 3 | contract ControlStructure { 4 | /* 5 | * @title A Simple Subcurrency Example 6 | * @author Toshendra Sharma 7 | * @notice Example for the Solidity Course 8 | * @dev This is only for demo the simple Coin example 9 | * 10 | */ 11 | address public a; 12 | 13 | function ControlStructure(uint input1) { 14 | 15 | // if-else 16 | if(input1 == 2) 17 | a = 1; 18 | else 19 | a = 0; 20 | 21 | while(input1 >= 0){ 22 | if(input1 == 5) 23 | continue; 24 | input1 = input1 - 1; 25 | a++; 26 | 27 | } 28 | 29 | for(uint i=0; i<=50; i++) 30 | { 31 | a++; 32 | if(a == 4) break; 33 | } 34 | 35 | do{ 36 | a--; 37 | } (while a>0); 38 | 39 | bool isTrue = (a == 1)? true: false; 40 | 41 | 42 | // will show an error because 43 | //there is no type conversion from non-boolean to boolean 44 | if (1) { 45 | //some work 46 | } 47 | 48 | 49 | 50 | } 51 | 52 | } 53 | -------------------------------------------------------------------------------- /12 - Scoping & Declaration/.DS_Store: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/blockchain-council/learn-solidity/bea129846b27e66ae30006fc340d27a75cfbe926/12 - Scoping & Declaration/.DS_Store -------------------------------------------------------------------------------- /12 - Scoping & Declaration/Scoping.sol: -------------------------------------------------------------------------------- 1 | pragma solidity ^0.4.16; 2 | 3 | contract ScopingErrors { 4 | function scoping() public { 5 | uint i = 0; 6 | 7 | while (i++ < 1) { 8 | uint same1 = 0; 9 | } 10 | 11 | while (i++ < 2) { 12 | uint same1 = 0;// Illegal, second declaration of same1 13 | } 14 | } 15 | 16 | function minimalScoping() public { 17 | { 18 | uint same2 = 0; 19 | } 20 | 21 | { 22 | uint same2 = 0;// Illegal, second declaration of same2 23 | } 24 | } 25 | 26 | function forLoopScoping() public { 27 | for (uint same3 = 0; same3 < 1; same3++) { 28 | } 29 | 30 | for (uint same3 = 0; same3 < 1; same3++) {// Illegal, second declaration of same3 31 | } 32 | } 33 | 34 | function foo() returns (uint) public { 35 | // baz is implicitly initialized as 0 36 | uint bar = 5; 37 | if (true) { 38 | bar += baz; 39 | } else { 40 | uint baz = 10;// never executes 41 | } 42 | return bar;// returns 5 43 | } 44 | } 45 | -------------------------------------------------------------------------------- /13 - Input & Output Parameters/InputAndOutputParameters.sol: -------------------------------------------------------------------------------- 1 | pragma solidity ^0.4.16; 2 | 3 | /* 4 | * @title A Units & Global Variables Example 5 | * @author Toshendra Sharma 6 | * @notice Example for the Solidity Course 7 | */ 8 | 9 | contract InputAndOutputParameters { 10 | 11 | // Define consutruct here 12 | uint input1; 13 | address input2; 14 | function InputAndOutputParameters(uint _inputParam1, address _inputParam2) public { 15 | // Initialize state variables here 16 | input1 = _inputParam1; 17 | input2 = _inputParam2; 18 | } 19 | 20 | function newFunction1(uint _inputParam1, address _inputParam2) public returns (uint _outputParam1){ 21 | _outputParam1 = _inputParam1 * 3; 22 | } 23 | 24 | function newFunction2(uint _inputParam1, uint _inputParam2) public returns (uint sum, uint product){ 25 | sum = _inputParam1 + _inputParam2; 26 | product = _inputParam1 * _inputParam2; 27 | } 28 | 29 | function newFunction3(uint _inputParam1, uint _inputParam2) public returns (uint, uint){ 30 | sum = _inputParam1 + _inputParam2; 31 | product = _inputParam1 * _inputParam2; 32 | return (sum, product); 33 | } 34 | 35 | } 36 | -------------------------------------------------------------------------------- /14 - Function Calls/FunctionCall.sol: -------------------------------------------------------------------------------- 1 | pragma solidity ^0.4.16; 2 | 3 | /* 4 | * @title A Units & Global Variables Example 5 | * @author Toshendra Sharma 6 | * @notice Example for the Solidity Course 7 | */ 8 | 9 | contract Miner{ 10 | //The modifier payable has to be used for info, 11 | // because otherwise, we would not be able to 12 | // send Ether to it in the call m.info.value(10).gas(800)(). 13 | function info() payable returns (uint ret) { return 42; } 14 | } 15 | 16 | contract FunctionCall { 17 | 18 | // Define consutruct here 19 | function FunctionCall(uint param1) { 20 | // Initialize state variables here 21 | } 22 | 23 | Miner m; 24 | function setMiner(address addr) { m = Miner(addr); } 25 | //function setMiner(Miner _m) { m = _m; } is also correct 26 | 27 | function callMinerInfo() { m.info.value(10).gas(800)(); } 28 | 29 | 30 | //function can also be called as json object as parameters 31 | function someFunction(uint key, uint value) { 32 | // Do something 33 | } 34 | 35 | function demoFunction() { 36 | // named arguments 37 | someFunction({value: 2, key: 3}); 38 | } 39 | 40 | 41 | //variable names are optional in parameters & in returns 42 | function someFunction2(uint key, uint) returns (uint){ 43 | // Do something 44 | reutrn key; 45 | } 46 | 47 | 48 | } 49 | -------------------------------------------------------------------------------- /15 - Function Modifiers/.DS_Store: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/blockchain-council/learn-solidity/bea129846b27e66ae30006fc340d27a75cfbe926/15 - Function Modifiers/.DS_Store -------------------------------------------------------------------------------- /15 - Function Modifiers/FunctionModifiers.sol: -------------------------------------------------------------------------------- 1 | pragma solidity ^0.4.16; 2 | 3 | /* 4 | * @title An Example for Solidity Course 5 | * @author Toshendra Sharma 6 | * @notice Example for the Solidity Course 7 | */ 8 | 9 | contract FunctionModifiers { 10 | 11 | address public creator; 12 | // Define consutruct here 13 | function FunctionModifiers() public { 14 | // Initialize state variables here 15 | creator = msg.sender; 16 | } 17 | 18 | modifier onlyCreator() { 19 | if (msg.sender != creator) throw; 20 | _; 21 | } 22 | 23 | function kill() onlyCreator public { 24 | selfdestruct(creator); 25 | } 26 | 27 | } 28 | -------------------------------------------------------------------------------- /16 - Fallback Function/fallback.sol: -------------------------------------------------------------------------------- 1 | pragma solidity ^0.4.8; 2 | 3 | /* 4 | * @title Example for Learn Solidity - Build Decentralized Applications on Blockchain 5 | * @author Ethereum Community 6 | * @notice Example for the Solidity Course 7 | */ 8 | 9 | contract Test { 10 | // This function is called for all messages sent to 11 | // this contract (there is no other function). 12 | // Sending Ether to this contract will cause an exception, 13 | // because the fallback function does not have the "payable" 14 | // modifier. 15 | function() { x = 1; } 16 | uint x; 17 | } 18 | 19 | 20 | // This contract keeps all Ether sent to it with no way 21 | // to get it back. 22 | contract Sink { 23 | function() payable public { } 24 | } 25 | 26 | 27 | contract Caller { 28 | function callTest(Test test) public { 29 | test.call(0xabcdef01); // hash does not exist 30 | // results in test.x becoming == 1. 31 | 32 | // The following call will fail, reject the 33 | // Ether and return false: 34 | test.send(2 ether); 35 | } 36 | } 37 | -------------------------------------------------------------------------------- /17 - Abstract Contracts/AbstractContracts.sol: -------------------------------------------------------------------------------- 1 | pragma solidity ^0.4.8; 2 | 3 | contract Feline { 4 | function utterance() public returns (bytes32); 5 | } 6 | 7 | contract Cat is Feline { 8 | function utterance() public returns (bytes32) { return "miaow"; } 9 | } 10 | 11 | 12 | -------------------------------------------------------------------------------- /18 - Creating Contracts via new Operator/new.sol: -------------------------------------------------------------------------------- 1 | pragma solidity ^0.4.8; 2 | 3 | /* 4 | * @title Example for Learn Solidity - Build Decentralized Applications on Blockchain 5 | * @author Ethereum Community 6 | * @notice Example for the Solidity Course 7 | */ 8 | contract D { 9 | uint x; 10 | function D(uint a) payable public { 11 | x = a; 12 | } 13 | } 14 | 15 | 16 | contract C { 17 | D d = new D(4); // will be executed as part of C's constructor 18 | 19 | function createD(uint arg) public { 20 | D newD = new D(arg); 21 | } 22 | 23 | function createAndEndowD(uint arg, uint amount) public { 24 | // Send ether along with the creation 25 | D newD = (new D).value(amount)(arg); 26 | } 27 | } 28 | -------------------------------------------------------------------------------- /19 - Inheriting Smart Contratcs/Inheritance.sol: -------------------------------------------------------------------------------- 1 | pragma solidity ^0.4.8; 2 | 3 | contract owned { 4 | function owned() public { owner = msg.sender; } 5 | address owner; 6 | } 7 | 8 | contract mortal is owned{ 9 | function kill() public { 10 | selfdestruct(owner); 11 | } 12 | } 13 | 14 | // Multiple inheritance is possible. Note that "owned" is 15 | // also a base class of "mortal", yet there is only a single 16 | // instance of "owned" (as for virtual inheritance in C++). 17 | contract User is owned, mortal{ 18 | string public UserName; 19 | 20 | function User(string _name) public { 21 | UserName = _name; 22 | } 23 | } 24 | 25 | 26 | 27 | -------------------------------------------------------------------------------- /20 - Importing Smart Contracts/Import.sol: -------------------------------------------------------------------------------- 1 | pragma solidity ^0.4.9; 2 | import "./owned.sol"; 3 | 4 | // import * as symbolName from "filename"; or import "filename" as symbolName; both are same 5 | // import {symbol1 as alias, symbol2} from "filename"; 6 | 7 | 8 | contract mortal is owned{ 9 | function kill() public { 10 | selfdestruct(owner); 11 | } 12 | } 13 | 14 | // Multiple inheritance is possible. Note that "owned" is 15 | // also a base class of "mortal", yet there is only a single 16 | // instance of "owned" (as for virtual inheritance in C++). 17 | contract User is owned, mortal{ 18 | string public UserName; 19 | 20 | function User(string _name) public { 21 | UserName = _name; 22 | } 23 | } 24 | 25 | 26 | 27 | -------------------------------------------------------------------------------- /20 - Importing Smart Contracts/owned.sol: -------------------------------------------------------------------------------- 1 | pragma solidity ^0.4.9; 2 | contract owned { 3 | function owned() public { owner = msg.sender; } 4 | address owner; 5 | } 6 | -------------------------------------------------------------------------------- /21 - Events & Logging/.DS_Store: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/blockchain-council/learn-solidity/bea129846b27e66ae30006fc340d27a75cfbe926/21 - Events & Logging/.DS_Store -------------------------------------------------------------------------------- /21 - Events & Logging/Coin.sol: -------------------------------------------------------------------------------- 1 | pragma solidity ^0.4.8; 2 | 3 | contract Coin { 4 | /* 5 | * @title A Simple Subcurrency Example 6 | * @author Toshendra Sharma 7 | * @notice Example for the Solidity Course 8 | * @dev This is only for demo the simple Coin example 9 | * 10 | */ 11 | address public minter; 12 | uint public totalCoins; 13 | 14 | event LogCoinsMinted(address indexed deliveredTo, uint indexed amount); 15 | event LogCoinsSent(address sentTo, uint amount); 16 | 17 | mapping (address => uint) balances; 18 | function Coin(uint initialCoins) public { 19 | minter = msg.sender; 20 | totalCoins = initialCoins; 21 | balances[minter] = initialCoins; 22 | } 23 | 24 | /// @notice Mint the coins 25 | /// @dev This does not return any value 26 | /// @param owner address of the coin owner, amount amount of coins to be delivered to owner 27 | /// @return Nothing 28 | function mint(address owner, uint amount) public { 29 | if (msg.sender != minter) return; 30 | balances[owner] += amount; 31 | totalCoins += amount; 32 | LogCoinsMinted(owner, amount); 33 | } 34 | 35 | function send(address receiver, uint amount) public { 36 | if (balances[msg.sender] < amount) return; 37 | balances[msg.sender] -= amount; 38 | balances[receiver] += amount; 39 | LogCoinsSent(receiver, amount); 40 | } 41 | 42 | function queryBalance(address addr) constant public returns (uint balance) { 43 | return balances[addr]; 44 | } 45 | 46 | function killCoin() public returns (bool) { 47 | if (msg.sender != minter) return; 48 | selfdestruct(minter); 49 | } 50 | } 51 | -------------------------------------------------------------------------------- /22 - Exceptions/.DS_Store: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/blockchain-council/learn-solidity/bea129846b27e66ae30006fc340d27a75cfbe926/22 - Exceptions/.DS_Store -------------------------------------------------------------------------------- /22 - Exceptions/Exceptions.sol: -------------------------------------------------------------------------------- 1 | pragma solidity ^0.4.8; 2 | 3 | /* 4 | * @title A Simple Subcurrency Example 5 | * @author Toshendra Sharma 6 | * @notice Example for the Solidity Course 7 | * @dev This is only for demo the simple Coin example 8 | * 9 | */ 10 | 11 | contract Sharer { 12 | function sendHalf(address addr) payable public returns (uint balance) { 13 | if (!addr.send(msg.value / 2)) 14 | revert(); // User Generated Exception also reverts the transfer to Sharer 15 | // assert(condition) will also generate a User Exception 16 | 17 | // catching an exception is yet not possible 18 | return this.balance; 19 | } 20 | } 21 | 22 | // Currently, Solidity automatically generates a runtime exception in the following situations: 23 | 24 | // If you access an array at a too large or negative index (i.e. x[i] where i >= x.length or i < 0). 25 | // If you access a fixed-length bytesN at a too large or negative index. 26 | // If you call a function via a message call but it does not finish properly (i.e. it runs out of gas, has no matching function, or throws an exception itself), except when a low level operation call, send, delegatecall or callcode is used. The low level operations never throw exceptions but indicate failures by returning false. 27 | // If you create a contract using the new keyword but the contract creation does not finish properly (see above for the definition of “not finish properly”). 28 | // If you divide or modulo by zero (e.g. 5 / 0 or 23 % 0). 29 | // If you shift by a negative amount. 30 | // If you convert a value too big or negative into an enum type. 31 | // If you perform an external function call targeting a contract that contains no code. 32 | // If your contract receives Ether via a public function without payable modifier (including the constructor and the fallback function). 33 | // If your contract receives Ether via a public getter function. 34 | // If you call a zero-initialized variable of internal function type. 35 | // If a .transfer() fails. 36 | 37 | 38 | 39 | } 40 | -------------------------------------------------------------------------------- /23 - Complete Example - CrowdFunder Smart Contract/.DS_Store: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/blockchain-council/learn-solidity/bea129846b27e66ae30006fc340d27a75cfbe926/23 - Complete Example - CrowdFunder Smart Contract/.DS_Store -------------------------------------------------------------------------------- /23 - Complete Example - CrowdFunder Smart Contract/.gitignore: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | #build files 5 | build/ 6 | 7 | #node modules 8 | node_modules/ 9 | 10 | 11 | -------------------------------------------------------------------------------- /23 - Complete Example - CrowdFunder Smart Contract/CrowdFunder.sol: -------------------------------------------------------------------------------- 1 | pragma solidity ^0.4.8; 2 | 3 | contract CrowdFunder { 4 | 5 | //tosh-dev: 0x4EF89736C984A3f34aa5b7a9D57457faA837F258 6 | //main: 0xC8A908F248e930a705A70400146fcF24d3Cc7275 7 | //toshendra: 0xc8d21EB34E504Ef70AA075eA2a36F21cD3c1B685 8 | 9 | // Variables set 10 | address public creator; 11 | address public fundRecipient; // creator may be different than recipient 12 | uint public minimumToRaise; // required to tip, else everyone gets refund 13 | string campaignUrl; 14 | byte constant version = "1"; 15 | 16 | // Data structures 17 | enum State { 18 | Fundraising, 19 | ExpiredRefund, 20 | Successful, 21 | Closed 22 | } 23 | struct Contribution { 24 | uint amount; 25 | address contributor; 26 | } 27 | 28 | // State variables 29 | State public state = State.Fundraising; // initialize on create 30 | uint public totalRaised; 31 | uint public currentBalance; 32 | uint public raiseBy; 33 | uint public completeAt; 34 | Contribution[] contributions; 35 | 36 | event LogFundingReceived(address addr, uint amount, uint currentTotal); 37 | event LogWinnerPaid(address winnerAddress); 38 | event LogFundingSuccessful(uint totalRaised); 39 | event LogFunderInitialized( 40 | address creator, 41 | address fundRecipient, 42 | string url, 43 | uint _minimumToRaise, 44 | uint256 raiseby); 45 | 46 | modifier inState(State _state) { 47 | if (state != _state) return; 48 | _; 49 | } 50 | 51 | modifier isCreator() { 52 | if (msg.sender != creator) return; 53 | _; 54 | } 55 | 56 | // Wait 1 hour after final contract state before allowing contract destruction 57 | modifier atEndOfLifecycle() { 58 | if(!((state == State.ExpiredRefund || state == State.Successful) && completeAt + 1 hours < now)) { 59 | revert(); 60 | } 61 | _; 62 | } 63 | 64 | function CrowdFunder( 65 | uint timeInHoursForFundraising, 66 | string _campaignUrl, 67 | address _fundRecipient, 68 | uint _minimumToRaise) 69 | public 70 | { 71 | creator = msg.sender; 72 | fundRecipient = _fundRecipient; 73 | campaignUrl = _campaignUrl; 74 | minimumToRaise = _minimumToRaise * 1000000000000000000; //convert to wei 75 | raiseBy = now + (timeInHoursForFundraising * 1 hours); 76 | currentBalance = 0; 77 | LogFunderInitialized( 78 | creator, 79 | fundRecipient, 80 | campaignUrl, 81 | minimumToRaise, 82 | raiseBy); 83 | } 84 | 85 | function contribute() 86 | public 87 | inState(State.Fundraising) payable returns (uint256) 88 | { 89 | contributions.push( 90 | Contribution({ 91 | amount: msg.value, 92 | contributor: msg.sender 93 | }) // use array, so can iterate 94 | ); 95 | totalRaised += msg.value; 96 | currentBalance = totalRaised; 97 | LogFundingReceived(msg.sender, msg.value, totalRaised); 98 | 99 | checkIfFundingCompleteOrExpired(); 100 | return contributions.length - 1; // return id 101 | } 102 | 103 | function checkIfFundingCompleteOrExpired() public { 104 | if (totalRaised > minimumToRaise) { 105 | state = State.Successful; 106 | LogFundingSuccessful(totalRaised); 107 | payOut(); 108 | 109 | // could incentivize sender who initiated state change here 110 | } else if ( now > raiseBy ) { 111 | state = State.ExpiredRefund; // backers can now collect refunds by calling getRefund(id) 112 | } 113 | completeAt = now; 114 | } 115 | 116 | function payOut() 117 | public 118 | inState(State.Successful) 119 | { 120 | if(!fundRecipient.send(this.balance)) { 121 | revert(); 122 | } 123 | state = State.Closed; 124 | currentBalance = 0; 125 | LogWinnerPaid(fundRecipient); 126 | } 127 | 128 | function getRefund(uint256 id) 129 | public 130 | inState(State.ExpiredRefund) 131 | returns (bool) 132 | { 133 | if (contributions.length <= id || id < 0 || contributions[id].amount == 0 ) { 134 | revert(); 135 | } 136 | 137 | uint amountToRefund = contributions[id].amount; 138 | contributions[id].amount = 0; 139 | 140 | if(!contributions[id].contributor.send(amountToRefund)) { 141 | contributions[id].amount = amountToRefund; 142 | return false; 143 | } 144 | else{ 145 | currentBalance -= amountToRefund; 146 | } 147 | 148 | return true; 149 | } 150 | 151 | function removeContract() 152 | public 153 | isCreator() 154 | atEndOfLifecycle() 155 | { 156 | selfdestruct(msg.sender); 157 | // creator gets all money that hasn't be claimed 158 | } 159 | 160 | function () public { revert(); } 161 | } 162 | -------------------------------------------------------------------------------- /23 - Complete Example - CrowdFunder Smart Contract/README.md: -------------------------------------------------------------------------------- 1 | # crowdfunder 2 | 3 | Learn how to make a smart contract "crowdfunder" using Solidity. Find the entire course [here](https://www.toshacademy.com/courses/take/best-solidity-programing-language-tutorial-for-ethereum-smart-contracts/lessons/1105963-full-example-crowdfunder-smart-contracts) by Toshendra Sharma 4 | 5 | ## Getting Started 6 | 7 | These instructions will get you a copy of the project up and running on your local machine for development and testing purposes. See deployment for notes on how to deploy the project on a live system. 8 | 9 | ### Prerequisites 10 | 11 | * [node](https://nodejs.org/en/download/) 12 | 13 | Node.js is a JavaScript runtime built on Chrome's V8 JavaScript engine. Node.js uses an event-driven, non-blocking I/O model that makes it lightweight and efficient. The Node.js package ecosystem, npm, is the largest ecosystem of open source libraries in the world. 14 | 15 | 16 | * [npm](https://www.npmjs.com/package/npm) 17 | 18 | npm makes it easy for JavaScript developers to share and reuse code, and it makes it easy to update the code that you're sharing.npm is a way to reuse code from other developers, and also a way to share your code with them, and it makes it easy to manage the different versions of code. 19 | 20 | 21 | * [truffle](https://www.npmjs.com/package/truffle/tutorial) 22 | 23 | Truffle is a development environment, testing framework and asset pipeline for Ethereum, aiming to make life as an Ethereum developer easier. With Truffle, you get: 24 | >Built-in smart contract compilation, linking, deployment and binary management. 25 | >Automated contract testing with Mocha and Chai. 26 | >Configurable build pipeline with support for custom build processes. 27 | >Scriptable deployment & migrations framework. 28 | >Network management for deploying to many public & private networks. 29 | >Interactive console for direct contract communication. 30 | >Instant rebuilding of assets during development. 31 | >External script runner that executes scripts within a Truffle environment. 32 | 33 | 34 | * Rpc client preferrably [testrpc](https://www.npmjs.com/package/ethereumjs-testrpc/tutorial) 35 | 36 | ### Setting up the environment 37 | 38 | * Installing Node.js [node](https://nodejs.org/en/download/) 39 | 40 | If you're using OS X or Windows, the best way to install Node.js is to use one of the installers from the Node.js download page.If you're using Linux, you can use the installer, or you can check NodeSource's binary distributions to see whether or not there's a more recent version that works with your system. 41 | 42 | ``` 43 | $ sudo apt-get install nodejs 44 | ``` 45 | Test: Run node -v. To check the version the of installed nodejs. 46 | 47 | * Install [npm](https://www.npmjs.com/package/npm) 48 | 49 | Node comes with npm installed so you should have a version of npm. However, npm gets updated more frequently than Node does, so you'll want to make sure it's the latest version. 50 | 51 | ``` 52 | $ npm install npm@latest -g 53 | ``` 54 | Test: Run npm -v. To check the version the of installed npm. 55 | 56 | 57 | ## Getting started(Quick Usage) 58 | 59 | * Clone the repository on your local machine. 60 | ``` 61 | $ git clone https://github.com/blockchain-council/coin-development.git 62 | ``` 63 | * "cd" into the cloned repository from command line. 64 | ``` 65 | $ cd clone-development 66 | ``` 67 | * Then run the below command which lets the dependencies from package.json gets installed locally for this particular project. The major requirements are truffle, testrpc, babel-register and babel-polyfill. 68 | ``` 69 | $ npm install 70 | ``` 71 | 72 | Then run the below command to start compling the contract: 73 | 74 | ``` 75 | $ truffle compile 76 | ``` 77 | 78 | You can also run truffle migrate and truffle test to compile your contracts, deploy those contracts to the network, and run their associated unit tests. 79 | 80 | Be sure you're connected to an ethereum client before running these commands. If you're new, install testrpc to run a local blockchain RPC server. After that, simply run testrpc in a new tab. 81 | 82 | ## Authors 83 | 84 | * [**Toshendra Sharma**](https://www.udemy.com/user/toshendrasharma2/) - Founder & CEO @ RecordsKeeper, Toshacademy, Toshblocks 85 | -------------------------------------------------------------------------------- /23 - Complete Example - CrowdFunder Smart Contract/ethpm.json: -------------------------------------------------------------------------------- 1 | { 2 | "package_name": "tosh", 3 | "version": "0.0.1", 4 | "description": "ICO & Token", 5 | "authors": [ 6 | "Toshendra Sharma " 7 | ], 8 | "keywords": [ 9 | "ethereum", 10 | "ICO" 11 | ], 12 | "devDependencies": { 13 | "zeppelin": "^1.0.6" 14 | }, 15 | "license": "MIT" 16 | } 17 | -------------------------------------------------------------------------------- /23 - Complete Example - CrowdFunder Smart Contract/package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "Blockhain-Council-Development-Codes", 3 | "version": "0.0.1", 4 | "description": "Blockhain-Council-Development-Codes", 5 | "main": "truffle.js", 6 | "repository": { 7 | "type": "git" 8 | }, 9 | "keywords": [ 10 | "solidity", 11 | "ethereum", 12 | "smart", 13 | "contracts", 14 | "security", 15 | "zeppelin" 16 | ], 17 | "author": "Toshendra sharma ", 18 | "license": "MIT", 19 | "bugs": { 20 | "url": "https://github.com/toshendra/ethereum-erc20-token/issues" 21 | }, 22 | "homepage": "https://github.com/toshendra/ethereum-erc20-token", 23 | "dependencies": { 24 | "truffle-hdwallet-provider": "0.0.3" 25 | }, 26 | "devDependencies": { 27 | "babel-polyfill": "^6.26.0", 28 | "babel-preset-es2015": "^6.18.0", 29 | "babel-preset-stage-2": "^6.18.0", 30 | "babel-preset-stage-3": "^6.17.0", 31 | "babel-register": "^6.26.0", 32 | "big-number": "^0.4.0", 33 | "coveralls": "^2.13.1", 34 | "ethereumjs-testrpc": "^3.0.2", 35 | "mocha-lcov-reporter": "^1.3.0", 36 | "solidity-coverage": "^0.1.0", 37 | "truffle": "^3.2.2" 38 | } 39 | } 40 | -------------------------------------------------------------------------------- /23 - Complete Example - CrowdFunder Smart Contract/truffle.js: -------------------------------------------------------------------------------- 1 | require('babel-register'); 2 | require('babel-polyfill'); 3 | 4 | 5 | module.exports = { 6 | networks: { 7 | testrpc: { 8 | host: "localhost", 9 | port: 8545, 10 | network_id: "*", 11 | gas: 1512388 12 | }, 13 | kovan: { 14 | host: "localhost", 15 | port: 8545, 16 | network_id: "3", 17 | from: "0x00FcEf22b8e9c3741B0082a8E16DD92c2FE63A32", 18 | gas: 1512388 19 | }, 20 | ropsten: { 21 | host: "localhost", 22 | port: 8545, 23 | network_id: "2", 24 | from: "0x00587216c3A97Cd31F12F78b88317DD03F2Ffa5D" 25 | }, 26 | mainnet: { 27 | host: "localhost", 28 | port: 8545, 29 | network_id: "1" 30 | } 31 | } 32 | }; -------------------------------------------------------------------------------- /24 - Complete Example - Voting Ballot Smart Contract/.DS_Store: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/blockchain-council/learn-solidity/bea129846b27e66ae30006fc340d27a75cfbe926/24 - Complete Example - Voting Ballot Smart Contract/.DS_Store -------------------------------------------------------------------------------- /24 - Complete Example - Voting Ballot Smart Contract/.gitignore: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | #build files 5 | build/ 6 | 7 | #node modules 8 | node_modules/ 9 | 10 | 11 | -------------------------------------------------------------------------------- /24 - Complete Example - Voting Ballot Smart Contract/Ballot.sol: -------------------------------------------------------------------------------- 1 | pragma solidity ^0.4.16; 2 | 3 | contract Ballot { 4 | struct Voter { 5 | uint weight; 6 | bool voted; 7 | address delegate; 8 | uint vote; 9 | } 10 | 11 | struct Proposal { 12 | bytes32 name; 13 | uint voteCount; 14 | } 15 | 16 | address public chairperson; 17 | 18 | // declare a state variable that stores a voter struct for each 19 | // possible address 20 | mapping(address => Voter) public voters; 21 | 22 | Proposal[] public proposals; 23 | 24 | function Ballot(bytes32[] proposalNames) public { 25 | chairperson = msg.sender; 26 | voters[chairperson].weight = 1; 27 | for (uint i = 0; i < proposalNames.length; i++) { 28 | createProposal(proposalNames[i]); 29 | } 30 | } 31 | 32 | function createProposal (bytes32 proposalName) public { 33 | require(msg.sender == chairperson); 34 | proposals.push(Proposal({ 35 | name: proposalName, 36 | voteCount: 0 37 | })); 38 | 39 | } 40 | 41 | function getProposalsCount() public constant returns (uint) { 42 | return proposals.length; 43 | } 44 | 45 | function getVoterWeight (address voter) constant public returns (uint) { 46 | return voters[voter].weight; 47 | } 48 | 49 | function giveRightToVote(address voter) public { 50 | require(msg.sender == chairperson); 51 | require(!voters[voter].voted); 52 | require(voters[voter].weight == 0); 53 | 54 | voters[voter].weight = 1; 55 | } 56 | 57 | function delegate(address to) public { 58 | Voter storage sender = voters[msg.sender]; 59 | require(!sender.voted); 60 | 61 | require(to != msg.sender); 62 | 63 | // iterate until we find an entry with an empty address entry 64 | while (voters[to].delegate != address(0)) { 65 | to = voters[to].delegate; 66 | require(to != msg.sender); 67 | } 68 | 69 | sender.voted = true; 70 | sender.delegate = to; 71 | 72 | Voter storage delegate_to = voters[to]; 73 | if (delegate_to.voted) { 74 | proposals[delegate_to.vote].voteCount += sender.weight; 75 | } else { 76 | delegate_to.weight += sender.weight; 77 | } 78 | } 79 | 80 | function vote (uint proposal) public { 81 | Voter storage sender = voters[msg.sender]; 82 | require(!sender.voted); 83 | sender.voted = true; 84 | sender.vote = proposal; 85 | 86 | proposals[proposal].voteCount += sender.weight; 87 | } 88 | 89 | function winningProposal() constant public returns (uint winner) { 90 | uint winningVoteCount = 0; 91 | for (uint p = 0; p < proposals.length; p ++) { 92 | if (proposals[p].voteCount > winningVoteCount) { 93 | winningVoteCount = proposals[p].voteCount; 94 | winner = p; 95 | } 96 | } 97 | } 98 | 99 | function winnerName() constant public returns (bytes32 name) { 100 | name = proposals[winningProposal()].name; 101 | } 102 | 103 | function getProposalName(uint index) public constant returns (bytes32) { 104 | require(index < proposals.length); 105 | require(index >= 0); 106 | return proposals[index].name; 107 | } 108 | 109 | function getProposalVoteCount(uint index) public constant returns (uint) { 110 | require(index < proposals.length); 111 | require(index >= 0); 112 | return proposals[index].voteCount; 113 | } 114 | } 115 | -------------------------------------------------------------------------------- /24 - Complete Example - Voting Ballot Smart Contract/README.md: -------------------------------------------------------------------------------- 1 | # voting-ballot 2 | Voting Ballot 3 | 4 | Learn how to make a smart contract "voting ballot" using Solidity. Find the entire course [here](https://www.toshacademy.com/courses/take/best-solidity-programing-language-tutorial-for-ethereum-smart-contracts/lessons/1105966-full-example-voting-ballot-smart-contract) by Toshendra Sharma 5 | 6 | ## Getting Started 7 | 8 | These instructions will get you a copy of the project up and running on your local machine for development and testing purposes. See deployment for notes on how to deploy the project on a live system. 9 | 10 | ### Prerequisites 11 | 12 | * [node](https://nodejs.org/en/download/) 13 | 14 | Node.js is a JavaScript runtime built on Chrome's V8 JavaScript engine. Node.js uses an event-driven, non-blocking I/O model that makes it lightweight and efficient. The Node.js package ecosystem, npm, is the largest ecosystem of open source libraries in the world. 15 | 16 | 17 | * [npm](https://www.npmjs.com/package/npm) 18 | 19 | npm makes it easy for JavaScript developers to share and reuse code, and it makes it easy to update the code that you're sharing.npm is a way to reuse code from other developers, and also a way to share your code with them, and it makes it easy to manage the different versions of code. 20 | 21 | 22 | * [truffle](https://www.npmjs.com/package/truffle/tutorial) 23 | 24 | Truffle is a development environment, testing framework and asset pipeline for Ethereum, aiming to make life as an Ethereum developer easier. With Truffle, you get: 25 | >Built-in smart contract compilation, linking, deployment and binary management. 26 | >Automated contract testing with Mocha and Chai. 27 | >Configurable build pipeline with support for custom build processes. 28 | >Scriptable deployment & migrations framework. 29 | >Network management for deploying to many public & private networks. 30 | >Interactive console for direct contract communication. 31 | >Instant rebuilding of assets during development. 32 | >External script runner that executes scripts within a Truffle environment. 33 | 34 | 35 | * Rpc client preferrably [testrpc](https://www.npmjs.com/package/ethereumjs-testrpc/tutorial) 36 | 37 | ### Setting up the environment 38 | 39 | * Installing Node.js [node](https://nodejs.org/en/download/) 40 | 41 | If you're using OS X or Windows, the best way to install Node.js is to use one of the installers from the Node.js download page.If you're using Linux, you can use the installer, or you can check NodeSource's binary distributions to see whether or not there's a more recent version that works with your system. 42 | 43 | ``` 44 | $ sudo apt-get install nodejs 45 | ``` 46 | Test: Run node -v. To check the version the of installed nodejs. 47 | 48 | * Install [npm](https://www.npmjs.com/package/npm) 49 | 50 | Node comes with npm installed so you should have a version of npm. However, npm gets updated more frequently than Node does, so you'll want to make sure it's the latest version. 51 | 52 | ``` 53 | $ npm install npm@latest -g 54 | ``` 55 | Test: Run npm -v. To check the version the of installed npm. 56 | 57 | 58 | ## Getting started(Quick Usage) 59 | 60 | * Clone the repository on your local machine. 61 | ``` 62 | $ git clone https://github.com/blockchain-council/coin-development.git 63 | ``` 64 | * "cd" into the cloned repository from command line. 65 | ``` 66 | $ cd clone-development 67 | ``` 68 | * Then run the below command which lets the dependencies from package.json gets installed locally for this particular project. The major requirements are truffle, testrpc, babel-register and babel-polyfill. 69 | ``` 70 | $ npm install 71 | ``` 72 | 73 | Then run the below command to start compling the contract: 74 | 75 | ``` 76 | $ truffle compile 77 | ``` 78 | 79 | You can also run truffle migrate and truffle test to compile your contracts, deploy those contracts to the network, and run their associated unit tests. 80 | 81 | Be sure you're connected to an ethereum client before running these commands. If you're new, install testrpc to run a local blockchain RPC server. After that, simply run testrpc in a new tab. 82 | 83 | ## Authors 84 | 85 | * [**Toshendra Sharma**](https://www.udemy.com/user/toshendrasharma2/) - Founder & CEO @ RecordsKeeper, Toshacademy, Toshblocks 86 | -------------------------------------------------------------------------------- /24 - Complete Example - Voting Ballot Smart Contract/ethpm.json: -------------------------------------------------------------------------------- 1 | { 2 | "package_name": "tosh", 3 | "version": "0.0.1", 4 | "description": "ICO & Token", 5 | "authors": [ 6 | "Toshendra Sharma " 7 | ], 8 | "keywords": [ 9 | "ethereum", 10 | "ICO" 11 | ], 12 | "devDependencies": { 13 | "zeppelin": "^1.0.6" 14 | }, 15 | "license": "MIT" 16 | } 17 | -------------------------------------------------------------------------------- /24 - Complete Example - Voting Ballot Smart Contract/package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "Blockhain-Council-Development-Codes", 3 | "version": "0.0.1", 4 | "description": "Blockhain-Council-Development-Codes", 5 | "main": "truffle.js", 6 | "repository": { 7 | "type": "git" 8 | }, 9 | "keywords": [ 10 | "solidity", 11 | "ethereum", 12 | "smart", 13 | "contracts", 14 | "security", 15 | "zeppelin" 16 | ], 17 | "author": "Toshendra sharma ", 18 | "license": "MIT", 19 | "bugs": { 20 | "url": "https://github.com/toshendra/ethereum-erc20-token/issues" 21 | }, 22 | "homepage": "https://github.com/toshendra/ethereum-erc20-token", 23 | "dependencies": { 24 | "truffle-hdwallet-provider": "0.0.3" 25 | }, 26 | "devDependencies": { 27 | "babel-polyfill": "^6.26.0", 28 | "babel-preset-es2015": "^6.18.0", 29 | "babel-preset-stage-2": "^6.18.0", 30 | "babel-preset-stage-3": "^6.17.0", 31 | "babel-register": "^6.26.0", 32 | "big-number": "^0.4.0", 33 | "coveralls": "^2.13.1", 34 | "ethereumjs-testrpc": "^3.0.2", 35 | "mocha-lcov-reporter": "^1.3.0", 36 | "solidity-coverage": "^0.1.0", 37 | "truffle": "^3.2.2" 38 | } 39 | } 40 | -------------------------------------------------------------------------------- /24 - Complete Example - Voting Ballot Smart Contract/truffle.js: -------------------------------------------------------------------------------- 1 | require('babel-register'); 2 | require('babel-polyfill'); 3 | 4 | 5 | module.exports = { 6 | networks: { 7 | testrpc: { 8 | host: "localhost", 9 | port: 8545, 10 | network_id: "*", 11 | gas: 1512388 12 | }, 13 | kovan: { 14 | host: "localhost", 15 | port: 8545, 16 | network_id: "3", 17 | from: "0x00FcEf22b8e9c3741B0082a8E16DD92c2FE63A32", 18 | gas: 1512388 19 | }, 20 | ropsten: { 21 | host: "localhost", 22 | port: 8545, 23 | network_id: "2", 24 | from: "0x00587216c3A97Cd31F12F78b88317DD03F2Ffa5D" 25 | }, 26 | mainnet: { 27 | host: "localhost", 28 | port: 8545, 29 | network_id: "1" 30 | } 31 | } 32 | }; -------------------------------------------------------------------------------- /5 - Basics of Solidity By Example/.DS_Store: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/blockchain-council/learn-solidity/bea129846b27e66ae30006fc340d27a75cfbe926/5 - Basics of Solidity By Example/.DS_Store -------------------------------------------------------------------------------- /5 - Basics of Solidity By Example/Coin.sol: -------------------------------------------------------------------------------- 1 | pragma solidity ^0.4.16; 2 | 3 | contract Coin { 4 | /* 5 | * @title A Simple Subcurrency Example 6 | * @author Toshendra Sharma 7 | * @notice Example for the Solidity Course 8 | * @dev This is only for demo the simple Coin example 9 | * 10 | */ 11 | 12 | address public minter; 13 | uint public totalCoins; 14 | 15 | event LogCoinsMinted(address deliveredTo, uint amount); 16 | event LogCoinsSent(address sentTo, uint amount); 17 | 18 | 19 | mapping (address => uint) balances; 20 | function Coin(uint initialCoins) public { 21 | minter = msg.sender; 22 | totalCoins = initialCoins; 23 | balances[minter] = initialCoins; 24 | } 25 | 26 | /// @notice Mint the coins 27 | /// @dev This does not return any value 28 | /// @param owner address of the coin owner, amount amount of coins to be delivered to owner 29 | /// @return Nothing 30 | function mint(address owner, uint amount) public { 31 | if (msg.sender != minter) return; 32 | balances[owner] += amount; 33 | totalCoins += amount; 34 | LogCoinsMinted(owner, amount); 35 | } 36 | 37 | function send(address receiver, uint amount) public { 38 | if (balances[msg.sender] < amount) return; 39 | balances[msg.sender] -= amount; 40 | balances[receiver] += amount; 41 | LogCoinsSent(receiver, amount); 42 | } 43 | 44 | function queryBalance(address addr) constant public returns (uint balance) { 45 | return balances[addr]; 46 | } 47 | 48 | function killCoin() public returns (bool) { 49 | require (msg.sender == minter) ; 50 | selfdestruct(minter); 51 | } 52 | } 53 | -------------------------------------------------------------------------------- /6 - Layout of Solidity Source File/layout.sol: -------------------------------------------------------------------------------- 1 | //versiona pragma 2 | pragma solidity ^0.4.16; 3 | 4 | //import section 5 | 6 | // filename.sol 7 | import "filename"; 8 | 9 | // begin the contract 10 | /// @title This is the layout of the solidity code 11 | contract ContractName { 12 | /* 13 | * @title A Simple Layout Example 14 | * @author Toshendra Sharma 15 | * @notice Example for the Solidity Course 16 | * @dev This line is for developers only 17 | * 18 | */ 19 | 20 | // This is a single-line comment. 21 | 22 | /* 23 | This is a 24 | multi-line comment. 25 | */ 26 | 27 | // State Variables 28 | address public stateVariable1; 29 | uint public stateVariable2; 30 | uint private stateVariable3; 31 | string public constant HOST_ID = 0x1234; 32 | 33 | // Events 34 | event LogEvent1(address param1, uint param2); 35 | event LogEvent2(address param1); 36 | event LogEvent3(); 37 | 38 | // Function Modifiers 39 | modifier onlyIfOwnerModifier() { 40 | if (msg.sender != owner) return; 41 | _; 42 | } 43 | 44 | modifier onlyIfMortalModifier() { 45 | if (msg.sender != mortal) return; 46 | _; 47 | } 48 | 49 | // Struct, arrays or Enum if any here 50 | enum enum1 { val1, val2, val3 } 51 | struct struct1 { 52 | uint weight; 53 | uint height; 54 | address location; 55 | } 56 | mapping (address => uint) balances; 57 | 58 | 59 | // Define consutruct here 60 | function ContractName(uint initialCoins) public { 61 | // Initialize state variables here 62 | } 63 | 64 | /// @dev Comment for developers 65 | /// @param parameters details 66 | /// @return return variable details 67 | function function1(address param1, uint param2) public { 68 | //body of function here 69 | // 70 | // 71 | } 72 | 73 | /// @dev Comment for developers 74 | /// @param parameters details 75 | /// @return return variable details 76 | function function2(address param1, uint param2) public { 77 | //body of function here 78 | // 79 | // 80 | } 81 | 82 | //default function 83 | function(){ 84 | revert(); 85 | } 86 | 87 | } 88 | -------------------------------------------------------------------------------- /7 - General Value Types/ValueTypes.sol: -------------------------------------------------------------------------------- 1 | pragma solidity ^0.4.16; 2 | 3 | /* 4 | * @title A Simple Value Type Example 5 | * @author Toshendra Sharma 6 | * @notice Example for the Solidity Course 7 | * @dev This line is for developers only 8 | * 9 | */ 10 | 11 | contract generalValueTypesContract { 12 | 13 | // uint is mostly used for currency value or amount as there is not float or double here 14 | //it can also be used for storing Unix timestamp 15 | uint x; 16 | 17 | // int of 256 bits, it cannot be changed after instantiation 18 | int constant variable1 = 8; 19 | 20 | // same effect as line above, here the 256 is explicit 21 | int256 constant variable2 = 8; 22 | 23 | // A hexadecimal value stored in a constant with 'constant', compiler replaces each occurrence with actual value 24 | uint constant VERSION_ID = 0x123A1; 25 | 26 | 27 | // For int and uint, size can be explicitly set in steps of 8 up to 256 e.g., int8, int16, int24 28 | uint8 b; 29 | int64 c; 30 | uint248 e; 31 | 32 | // Type casting 33 | int x = int(b); 34 | 35 | // boolean variable 36 | // or can also be declared by 'var b = true;' for inferred typing 37 | bool b = true; 38 | 39 | // Addresses - holds 20 byte/160 bit Ethereum addresses 40 | address public owner; 41 | 42 | // Bytes available from 1 to 32 43 | // byte is same as bytes1 44 | byte a; 45 | bytes2 b; 46 | bytes32 c; 47 | 48 | // Dynamically sized bytes 49 | // A special array, same as byte[] array (but packed tightly) 50 | bytes m; 51 | 52 | // same as bytes, but does not allow length or index access (for now) 53 | // below string is stored in UTF8, note double quotes, not single 54 | string n = "hello"; 55 | 56 | // Type inferrence 57 | // var does inferred typing based on first assignment, 58 | // can't be used in functions parameters 59 | var a = true; 60 | 61 | // Arrays 62 | // Below is a static array whose length is 5 fixed 63 | bytes32[5] nicknames; 64 | 65 | // dynamic array whose length can be changed at any time 66 | bytes32[] names; 67 | 68 | // adding a new element using push function returns new length of the array 69 | uint newLength = names.push("John"); 70 | 71 | // Dictionaries (any type to any other type) 72 | // mapping(_KeyType => _ValueType) 73 | mapping (string => uint) public balances; 74 | 75 | // Enums 76 | // often used for state machine 77 | enum State { Created, Locked, Inactive }; 78 | 79 | // Declare variable from enum 80 | State public state; 81 | 82 | // Initializing the state 83 | state = State.Created; 84 | 85 | // enums can be explicitly converted to ints 86 | // below Locked will return 1 87 | uint createdState = uint(State.Locked); 88 | 89 | 90 | // Define consutruct here 91 | function generalValueTypesContract(uint initialCoins) public { 92 | // Initialize state variables here 93 | } 94 | 95 | } 96 | -------------------------------------------------------------------------------- /7 - General Value Types/value.sol: -------------------------------------------------------------------------------- 1 | pragma solidity ^0.4.16; 2 | 3 | contract value{ 4 | 5 | } 6 | -------------------------------------------------------------------------------- /8 - Ether Units, Time Units/Units.sol: -------------------------------------------------------------------------------- 1 | pragma solidity ^0.4.16; 2 | 3 | /* 4 | * @title A Units & Global Variables Example 5 | * @author Toshendra Sharma 6 | * @notice Example for the Solidity Course 7 | */ 8 | 9 | contract Units { 10 | 11 | // msg represent the current message received by the contracts 12 | msg.sender; // address of sender 13 | msg.value; // amount of ether provided to this contract in wei 14 | msg.data; // bytes, complete call data 15 | msg.gas; // remaining gas 16 | msg.sig; // return the first four bytes of the call data 17 | 18 | 19 | 20 | // similarly tx represent the current transaction 21 | tx.origin; // address of sender of the transaction 22 | tx.gasprice; // gas price of the transaction 23 | 24 | // block represent the information about the current Block 25 | now; // current time (approximately), alias for block.timestamp (uses Unix time) 26 | block.number; // current block number 27 | block.difficulty; // current block difficulty 28 | block.blockhash(1); // returns bytes32, only works for most recent 256 blocks 29 | block.gasLimit(); //return Gas limit 30 | block.coinbase (); // return current block miner’s address 31 | 32 | // Ether Units are available as a global variables 33 | // wei, finney, szabo & ether are variable themself 34 | // variable can not be named as the wei, finney, szabo or ether 35 | bool isEqual = (2 ether == 2000 finney); 36 | 37 | 38 | // Time Units 39 | // seconds, minutes, hours, days, weeks, years are all available at time units to be used 40 | // can be used anywher in the program like mentioned below 41 | bool isEqual = (1 == 1 seconds); 42 | bool isEqual = (1 minutes == 60 seconds); 43 | bool isEqual = (1 hours == 60 minutes); 44 | bool isEqual = (1 days == 24 hours); 45 | bool isEqual = (1 weeks = 7 days); 46 | bool isEqual = (1 years = 365 days); 47 | 48 | 49 | Define consutruct here 50 | function Units(uint initialCoins) public { 51 | // Initialize state variables here 52 | } 53 | 54 | } 55 | -------------------------------------------------------------------------------- /9 - Global Variables & Functions/GlobalVariablesAndFunctions.sol: -------------------------------------------------------------------------------- 1 | pragma solidity ^0.4.16; 2 | 3 | /* 4 | * @title A Units & Global Variables Example 5 | * @author Toshendra Sharma 6 | * @notice Example for the Solidity Course 7 | */ 8 | 9 | contract GlobalVariablesAndFunctions { 10 | 11 | 12 | // msg represent the current message received by the contracts 13 | msg.sender; // address of sender 14 | msg.value; // amount of ether provided to this contract in wei 15 | msg.data; // bytes, complete call data 16 | msg.gas; // remaining gas 17 | msg.sig; // return the first four bytes of the call data 18 | 19 | 20 | 21 | // similarly tx represent the current transaction 22 | tx.origin; // address of sender of the transaction 23 | tx.gasprice; // gas price of the transaction 24 | 25 | // block represent the information about the current Block 26 | now; // current time (approximately), alias for block.timestamp (uses Unix time) 27 | block.number; // current block number 28 | block.difficulty; // current block difficulty 29 | block.blockhash(1); // returns bytes32, only works for most recent 256 blocks 30 | block.gasLimit(); //return Gas limit 31 | block.coinbase (); // return current block miner’s address 32 | 33 | 34 | // Mathematical and Cryptographic Functions 35 | assert(bool condition); // throws if the condition is not met. 36 | addmod(uint x, uint y, uint k) returns (uint); // compute (x + y) % k where the addition is performed with arbitrary precision and does not wrap around at 2**256. 37 | mulmod(uint x, uint y, uint k) returns (uint); // compute (x * y) % k where the multiplication is performed with arbitrary precision and does not wrap around at 2**256. 38 | keccak256(...) returns (bytes32); // compute the Ethereum-SHA-3 (Keccak-256) hash of the (tightly packed) arguments 39 | sha3(...) returns (bytes32); // alias to keccak256() 40 | sha256(...) returns (bytes32) // compute the SHA-256 hash of the (tightly packed) arguments 41 | ripemd160(...) returns (bytes20) // compute RIPEMD-160 hash of the (tightly packed) arguments 42 | ecrecover(bytes32 hash, uint8 v, bytes32 r, bytes32 s) returns (address) // recover the address associated with the public key from elliptic curve signature or return zero on error 43 | revert(); // abort execution and revert state changes 44 | // sha3("ab", "c") == sha3("abc") == sha3(0x616263) == sha3(6382179) = sha3(97, 98, 99) 45 | 46 | // Address Related 47 |
.balance (uint256); // balance of the Address in Wei 48 |
.send(uint256 amount) returns (bool); //send given amount of Wei to Address, returns false on failure 49 |
.transfer(uint256 amount); // send given amount of Wei to Address, throws on failure 50 | 51 | // Contracts Related 52 | this; // (current contract’s type) the current contract, explicitly convertible to Address 53 | selfdestruct(address recipient); // destroy the current contract, sending its funds to the given Address 54 | 55 | 56 | 57 | // Define consutruct here 58 | function GlobalVariablesAndFunctions(uint initialCoins) { 59 | // Initialize state variables here 60 | } 61 | 62 | } 63 | -------------------------------------------------------------------------------- /GeneralExample.sol: -------------------------------------------------------------------------------- 1 | pragma solidity ^0.4.8; 2 | 3 | contract owned { 4 | function owned() public { owner = msg.sender; } 5 | address owner; 6 | } 7 | 8 | contract mortal is owned{ 9 | 10 | function kill() public { 11 | selfdestruct(owner); 12 | } 13 | 14 | } 15 | 16 | contract User is owned, mortal{ 17 | string public UserName; 18 | function User(string _name) public { 19 | UserName = _name; 20 | } 21 | } 22 | 23 | 24 | 25 | 26 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Learn-solidity- Course Resources 2 | 3 | This code base is a part of the code templates of different modules in the e-learning course [Learn Solidity: Programing Language for Ethereum Smart Contracts](https://www.toshacademy.com/courses/best-solidity-programing-language-tutorial-for-ethereum-smart-contracts?utm_source=github&utm_medium=voting-ballot-repo) hosted at [Tosh Academy](https://www.toshacademy.com/collections/blockchain?utm_source=github&utm_medium=voting-ballot-repo). 4 | 5 | Learn to build smart contracts using Solidity. 6 | 7 | ## Getting Started 8 | 9 | These instructions will get you a copy of the project up and running on your local machine for development and testing purposes. See deployment for notes on how to deploy the project on a live system. 10 | 11 | ### Prerequisites 12 | 13 | * [node](https://nodejs.org/en/download/) 14 | 15 | Node.js is a JavaScript runtime built on Chrome's V8 JavaScript engine. Node.js uses an event-driven, non-blocking I/O model that makes it lightweight and efficient. The Node.js package ecosystem, npm, is the largest ecosystem of open source libraries in the world. 16 | 17 | 18 | * [npm](https://www.npmjs.com/package/npm) 19 | 20 | npm makes it easy for JavaScript developers to share and reuse code, and it makes it easy to update the code that you're sharing.npm is a way to reuse code from other developers, and also a way to share your code with them, and it makes it easy to manage the different versions of code. 21 | 22 | 23 | * [truffle](https://www.npmjs.com/package/truffle/tutorial) 24 | 25 | Truffle is a development environment, testing framework and asset pipeline for Ethereum, aiming to make life as an Ethereum developer easier. With Truffle, you get: 26 | >Built-in smart contract compilation, linking, deployment and binary management. 27 | >Automated contract testing with Mocha and Chai. 28 | >Configurable build pipeline with support for custom build processes. 29 | >Scriptable deployment & migrations framework. 30 | >Network management for deploying to many public & private networks. 31 | >Interactive console for direct contract communication. 32 | >Instant rebuilding of assets during development. 33 | >External script runner that executes scripts within a Truffle environment. 34 | 35 | 36 | * Rpc client preferrably [testrpc](https://www.npmjs.com/package/ethereumjs-testrpc/tutorial) 37 | 38 | ### Setting up the environment 39 | 40 | * Installing Node.js [node](https://nodejs.org/en/download/) 41 | 42 | If you're using OS X or Windows, the best way to install Node.js is to use one of the installers from the Node.js download page.If you're using Linux, you can use the installer, or you can check NodeSource's binary distributions to see whether or not there's a more recent version that works with your system. 43 | 44 | ``` 45 | $ sudo apt-get install nodejs 46 | ``` 47 | Test: Run node -v. To check the version the of installed nodejs. 48 | 49 | * Install [npm](https://www.npmjs.com/package/npm) 50 | 51 | Node comes with npm installed so you should have a version of npm. However, npm gets updated more frequently than Node does, so you'll want to make sure it's the latest version. 52 | 53 | ``` 54 | $ npm install npm@latest -g 55 | ``` 56 | Test: Run npm -v. To check the version the of installed npm. 57 | 58 | 59 | ## Getting started(Quick Usage) 60 | 61 | * Clone the repository on your local machine. 62 | ``` 63 | $ git clone https://github.com/blockchain-council/build-decentralized-democracy-dao-in-ethereum-solidity.git 64 | ``` 65 | * "cd" into the cloned repository from command line. 66 | ``` 67 | $ cd learn-solidity 68 | ``` 69 | * Then run the below command which lets the dependencies from package.json gets installed locally for this particular project. The major requirements are truffle, testrpc, babel-register and babel-polyfill. 70 | ``` 71 | $ npm install 72 | ``` 73 | 74 | * Then run the below command to start compling the contract: 75 | 76 | ``` 77 | $ truffle compile 78 | ``` 79 | 80 | You can also run truffle migrate and truffle test to compile your contracts, deploy those contracts to the network, and run their associated unit tests. 81 | 82 | Be sure you're connected to an ethereum client before running these commands. If you're new, install testrpc to run a local blockchain RPC server. After that, simply run testrpc in a new tab. 83 | 84 | ## Authors 85 | 86 | [**Toshendra Sharma**](https://www.udemy.com/user/toshendrasharma2/) 87 | 88 | Toshendra Sharma is the founder & CEO of [RecordsKeeper](https://recordskeeper.co), a Blockchain-based record keeping solution for businesses & also the founder of [Toshblocks](https://www.toshblocks.com), A Blockchain Consulting & Development. Toshendra also founded [Blockchain Council](https://www.blockchain-council.org) to bring elite Blockchain experts together on a single platform. 89 | 90 | Earlier, he founded Appvigil (Wegilant) in Nov 2011 while pursuing my Masters in Application Security from IIT Bombay, India. Appvigil is the Mobile App Vulnerability Scanner on Cloud. He was heading the team as CEO. The company has won many awards & accolades under his leadership. 91 | 92 | Toshendra has worked on many Ethereum based projects for many companies & individuals for their coins & Initial Coin Offering (ICO). 93 | 94 | He is a well-known instructor & speaker in Blockchain space and taught more than 12,000 students worldwide spread in 148+ countries. He is also the part of Forbes India 30Under30 List of 2016 in Technology space. 95 | 96 | You can find him on: 97 | * [Facebook](https://www.facebook.com/toshendra11) 98 | * [LinkedIn](https://linkedin.com/in/toshendra) 99 | * [Twitter](https://www.twitter.com/toshendrasharma) 100 | * [Tosh Academy](https://www.toshacademy.com) 101 | * [Toshblocks](https://www.toshblocks.com/) 102 | * [Udemy](https://www.udemy.com/user/toshendrasharma2/) 103 | * [Podacasts](http://www.buzzsprout.com/134645) 104 | 105 | 106 | License 107 | ------------- 108 | [GPL v3](https://www.gnu.org/licenses/gpl-3.0.txt) 109 | --------------------------------------------------------------------------------