├── .gitattributes ├── .gitignore ├── Chapter01 └── HelloWorld.sol ├── Chapter03 ├── Allaboutints.txt ├── Contracts.sol ├── DemoInnerMapping.sol ├── DemoMemorytoMemoryReferenceTypeAssignment.txt ├── DemoMemorytoMemoryValueTypeAssignment.sol ├── DemoMemorytoStorageReferenceTypeAssignment.txt ├── DemoMemorytoStorageValueTypeAssignment.sol ├── DemoStoragetoMemoryReferenceTypeAssignment.sol ├── DemoStoragetoMemoryValueTypeAssignment.sol ├── DemoStoragetoStorageReferenceTypeAssignment.sol ├── DemoStoragetoStorageValueTypeAssignment.sol ├── GeneralMapping.sol ├── GeneralStructure.sol ├── MappingLooping.sol ├── MappinginMemory.sol ├── PragmaAndComments.sol ├── Struct-generalStructure.sol ├── boolContract.sol ├── bytesContract.sol └── enums.sol ├── Chapter04 ├── ConversionDemo.txt ├── CryptoFunctions.txt ├── ErrorDataType.txt ├── TransactionAndMessageVariables.txt ├── VarType.txt ├── VariableHoisting.txt └── VariableScoping.txt ├── Chapter05 ├── DowhileLoop.sol ├── ForLoopExample.sol ├── ForLoopExampleBreak.sol ├── ForLoopExampleContinue.sol ├── IfElseExample.sol ├── ReturnValues.sol └── whileLoop.sol ├── Chapter06 ├── Address.sol ├── Constructor.sol ├── HelloWorld.sol ├── IHelloWorld.sol ├── Inheritance.sol ├── ParentContract.sol ├── SumContract.sol ├── abstractHelloWorld.sol └── helloFunctionPloymorphism.sol ├── Chapter07 ├── ContractWithModifier.sol ├── ContractWithoutModifier.sol ├── Fallback.sol ├── FallbackFunction.sol ├── PureFunction.sol ├── SendAndTransferSample.sol ├── SimpleTransferToAccount.sol ├── UsingCall.sol ├── ViewFunction.sol └── parameters.sol ├── Chapter08 ├── AssertContract.sol ├── EventContract.sol ├── RequireContract.sol ├── RevertContract.sol └── eventsPromise.js ├── Chapter09 ├── 2_Custom.js ├── DebuggerSampleContract.sol ├── TestFirst.sol ├── first.sol └── second.sol ├── Chapter10 └── DebuggerSampleContract.sol ├── LICENSE └── README.md /.gitattributes: -------------------------------------------------------------------------------- 1 | # Auto detect text files and perform LF normalization 2 | * text=auto 3 | 4 | # Custom for Visual Studio 5 | *.cs diff=csharp 6 | 7 | # Standard to msysgit 8 | *.doc diff=astextplain 9 | *.DOC diff=astextplain 10 | *.docx diff=astextplain 11 | *.DOCX diff=astextplain 12 | *.dot diff=astextplain 13 | *.DOT diff=astextplain 14 | *.pdf diff=astextplain 15 | *.PDF diff=astextplain 16 | *.rtf diff=astextplain 17 | *.RTF diff=astextplain 18 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | # Windows image file caches 2 | Thumbs.db 3 | ehthumbs.db 4 | 5 | # Folder config file 6 | Desktop.ini 7 | 8 | # Recycle Bin used on file shares 9 | $RECYCLE.BIN/ 10 | 11 | # Windows Installer files 12 | *.cab 13 | *.msi 14 | *.msm 15 | *.msp 16 | 17 | # Windows shortcuts 18 | *.lnk 19 | 20 | # ========================= 21 | # Operating System Files 22 | # ========================= 23 | 24 | # OSX 25 | # ========================= 26 | 27 | .DS_Store 28 | .AppleDouble 29 | .LSOverride 30 | 31 | # Thumbnails 32 | ._* 33 | 34 | # Files that might appear in the root of a volume 35 | .DocumentRevisions-V100 36 | .fseventsd 37 | .Spotlight-V100 38 | .TemporaryItems 39 | .Trashes 40 | .VolumeIcon.icns 41 | 42 | # Directories potentially created on remote AFP share 43 | .AppleDB 44 | .AppleDesktop 45 | Network Trash Folder 46 | Temporary Items 47 | .apdisk 48 | -------------------------------------------------------------------------------- /Chapter01/HelloWorld.sol: -------------------------------------------------------------------------------- 1 | pragma Solidity ^0.4.18; 2 | contract HelloWorld 3 | { 4 | string private stateVariable = "Hello World"; 5 | function GetHelloWorld() public view returns (string) 6 | { 7 | return stateVariable; 8 | } } -------------------------------------------------------------------------------- /Chapter03/Allaboutints.txt: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | pragma solidity 0.4.19; 5 | 6 | contract AllAboutInts { 7 | 8 | 9 | uint stateUInt = 20; //state variable 10 | uint stateInt = 20; //state variable 11 | 12 | function getUInt(uint incomingValue) 13 | { 14 | uint memoryuint = 256; 15 | uint256 memoryuint256 = 256; 16 | uint8 memoruint8 = 8; //can store value from 0 upto 255 17 | 18 | //addition of two uint8 19 | uint256 result = memoruint8 + memoryuint; 20 | 21 | } 22 | 23 | function getInt(int incomingValue) 24 | { 25 | int memoryInt = 256; 26 | int256 memoryInt256 = 256; 27 | int8 memoryInt8 = 8; //can store value from -128 to 127 28 | } 29 | 30 | } 31 | 32 | 33 | 34 | 35 | 36 | -------------------------------------------------------------------------------- /Chapter03/Contracts.sol: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | pragma solidity 0.4.19; 5 | 6 | // This is a single line comment in Solidity 7 | 8 | /* This is a multi-line comment 9 | In solidity. Use this when multiple consecutive lines 10 | Should be commented as a whole */ 11 | 12 | contract firstCOntract { 13 | 14 | } 15 | 16 | contract secondContract { 17 | 18 | } 19 | 20 | library stringLibrary { 21 | 22 | } 23 | 24 | library mathLibrary { 25 | 26 | } 27 | 28 | interface IBank{ 29 | 30 | } 31 | 32 | interface IAccount { 33 | 34 | } 35 | 36 | 37 | 38 | 39 | 40 | 41 | 42 | -------------------------------------------------------------------------------- /Chapter03/DemoInnerMapping.sol: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | pragma solidity 0.4.19; 5 | 6 | contract DemoInnerMapping { 7 | 8 | mapping (uint => mapping(address => string)) accountDetails; 9 | uint counter; 10 | 11 | function addtoMapping(address addressDetails, bytes name) returns (uint) 12 | { 13 | string memory names = string(name); 14 | counter = counter + 1; 15 | accountDetails[counter][addressDetails] = names; 16 | 17 | return counter; 18 | } 19 | 20 | function getMappingMember(address addressDetails) returns (bytes) 21 | { 22 | // 0xca35b7d915458ef540ade6068dfe2f44e8fa733c 23 | return bytes( accountDetails[counter][addressDetails]); 24 | } 25 | } 26 | 27 | 28 | 29 | 30 | 31 | 32 | 33 | 34 | -------------------------------------------------------------------------------- /Chapter03/DemoMemorytoMemoryReferenceTypeAssignment.txt: -------------------------------------------------------------------------------- 1 | 2 | 3 | pragma solidity 0.4.19; 4 | 5 | contract DemoMemorytoMemoryReferenceTypeAssignment { 6 | 7 | uint stateVar = 20; 8 | function getUInt() returns (uint) 9 | { 10 | uint[] memory someVar = new uint[](1); 11 | 12 | someVar[0] = 23; 13 | 14 | uint[] memory otherVar = someVar; 15 | 16 | someVar[0] = 45; 17 | 18 | return (otherVar[0]); //returns 45 19 | 20 | } 21 | 22 | } 23 | -------------------------------------------------------------------------------- /Chapter03/DemoMemorytoMemoryValueTypeAssignment.sol: -------------------------------------------------------------------------------- 1 | 2 | pragma solidity 0.4.19; 3 | 4 | contract DemoMemorytoMemoryValueTypeAssignment { 5 | 6 | 7 | function getUInt() returns (uint) 8 | { 9 | uint localVar1 = 40; 10 | 11 | uint localVar2 = 80; 12 | 13 | localVar1 = localVar2; 14 | 15 | localVar2 = 100; 16 | 17 | return localVar1; // returns 80 18 | 19 | } 20 | 21 | } 22 | 23 | 24 | 25 | 26 | 27 | 28 | -------------------------------------------------------------------------------- /Chapter03/DemoMemorytoStorageReferenceTypeAssignment.txt: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | pragma solidity 0.4.19; 5 | 6 | contract DemoMemorytoStorageReferenceTypeAssignment { 7 | 8 | uint[2] stateArray ; 9 | function getUInt() returns (uint) 10 | { 11 | uint[2] memory localArray = [uint(1), 2]; 12 | 13 | stateArray = localArray; 14 | 15 | localArray[1] = 10; 16 | 17 | return stateArray[1]; // returns 2 18 | 19 | } 20 | 21 | } 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | 31 | 32 | 33 | 34 | 35 | 36 | 37 | 38 | 39 | 40 | 41 | 42 | -------------------------------------------------------------------------------- /Chapter03/DemoMemorytoStorageValueTypeAssignment.sol: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | pragma solidity 0.4.19; 5 | 6 | contract DemoMemorytoStorageValueTypeAssignment { 7 | 8 | uint stateVar = 20; 9 | function getUInt() returns (uint) 10 | { 11 | uint localVar = 40; 12 | 13 | stateVar = localVar; 14 | 15 | localVar = 50; 16 | 17 | return stateVar; // returns 40 18 | 19 | } 20 | 21 | } 22 | 23 | 24 | 25 | 26 | 27 | -------------------------------------------------------------------------------- /Chapter03/DemoStoragetoMemoryReferenceTypeAssignment.sol: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | pragma solidity 0.4.19; 5 | 6 | contract DemoStoragetoMemoryReferenceTypeAssignment { 7 | 8 | uint[2] stateArray = [uint(1), 2]; 9 | function getUInt() returns (uint) 10 | { 11 | uint[2] memory localArray = stateArray; 12 | 13 | stateArray[1] = 5; 14 | 15 | return localArray[1]; // returns 2 16 | 17 | } 18 | 19 | } 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | 31 | 32 | 33 | 34 | -------------------------------------------------------------------------------- /Chapter03/DemoStoragetoMemoryValueTypeAssignment.sol: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | pragma solidity 0.4.19; 5 | 6 | contract DemoStoragetoMemoryValueTypeAssignment { 7 | 8 | uint stateVar = 20; 9 | function getUInt() returns (uint) 10 | { 11 | uint localVar = 40; 12 | 13 | localVar = stateVar; 14 | 15 | stateVar = 50; 16 | 17 | return localVar; // returns 20 18 | 19 | } 20 | 21 | } 22 | 23 | 24 | 25 | 26 | 27 | -------------------------------------------------------------------------------- /Chapter03/DemoStoragetoStorageReferenceTypeAssignment.sol: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | pragma solidity 0.4.19; 9 | 10 | contract DemoStoragetoStorageReferenceTypeAssignment { 11 | 12 | uint[2] stateArray1 = [uint(1), 2]; 13 | 14 | uint[2] stateArray2 = [uint(3), 4]; 15 | 16 | function getUInt() returns (uint) 17 | { 18 | stateArray1 = stateArray2; 19 | 20 | stateArray2[1] = 5; 21 | 22 | return stateArray1[1]; // returns 4 23 | 24 | } 25 | 26 | } 27 | 28 | 29 | 30 | 31 | 32 | 33 | 34 | 35 | 36 | 37 | 38 | 39 | 40 | 41 | -------------------------------------------------------------------------------- /Chapter03/DemoStoragetoStorageValueTypeAssignment.sol: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | pragma solidity 0.4.19; 5 | 6 | contract DemoStoragetoStorageValueTypeAssignment { 7 | 8 | uint stateVar1 = 20; 9 | 10 | uint stateVar2 = 40; 11 | 12 | function getUInt() returns (uint) 13 | { 14 | stateVar1 = stateVar2; 15 | 16 | stateVar2 = 50; 17 | 18 | return stateVar1; // returns 40 19 | 20 | } 21 | 22 | } 23 | 24 | 25 | 26 | 27 | 28 | -------------------------------------------------------------------------------- /Chapter03/GeneralMapping.sol: -------------------------------------------------------------------------------- 1 | pragma solidity 0.4.19; 2 | 3 | contract GeneralMapping { 4 | 5 | mapping (uint => address) Names; 6 | 7 | uint counter; 8 | 9 | function addtoMapping(address addressDetails) returns (uint) 10 | { 11 | counter = counter + 1; 12 | Names[counter] = addressDetails; 13 | 14 | return counter; //returns false 15 | } 16 | 17 | function getMappingMember(uint id) returns (address) 18 | { 19 | return Names[id]; 20 | } 21 | } 22 | -------------------------------------------------------------------------------- /Chapter03/GeneralStructure.sol: -------------------------------------------------------------------------------- 1 | 2 | pragma solidity 0.4.19; 3 | 4 | //contract definition 5 | contract generalStructure { 6 | //state variables 7 | int public stateIntVariable; // vriable of integer type 8 | string stateStringVariable; //variable of string type 9 | address personIdentifier; // variable of address type 10 | myStruct human; // variable of structure type 11 | bool constant hasIncome = true; //variable of constant nature 12 | 13 | //structure definition 14 | struct myStruct { 15 | string name; //variable fo type string 16 | uint myAge; // variable of unsigned integer type 17 | bool isMarried; // variable of boolean type 18 | uint[] bankAccountsNumbers; // variable - dynamic array of unsigned integer 19 | } 20 | 21 | //modifier declaration 22 | modifier onlyBy(){ 23 | if (msg.sender == personIdentifier) { 24 | _; 25 | } 26 | } 27 | 28 | // event declaration 29 | event ageRead(address, int ); 30 | 31 | //enumeration declaration 32 | enum gender {male, female} 33 | 34 | //function definition 35 | function getAge (address _personIdentifier) onlyBy() payable external returns (uint) { 36 | 37 | human = myStruct("Ritesh",10,true,new uint[](3)); //using struct myStruct 38 | 39 | gender _gender = gender.male; //using enum 40 | 41 | ageRead(personIdentifier, stateIntVariable); 42 | } 43 | 44 | } 45 | 46 | 47 | 48 | 49 | 50 | 51 | 52 | 53 | 54 | 55 | -------------------------------------------------------------------------------- /Chapter03/MappingLooping.sol: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | pragma solidity 0.4.19; 5 | 6 | contract MappingLooping { 7 | 8 | mapping (uint => address) Names; 9 | 10 | uint counter; 11 | 12 | function addtoMapping(address addressDetails) returns (uint) 13 | { 14 | counter = counter + 1; 15 | 16 | Names[counter] = addressDetails; 17 | 18 | return counter; 19 | } 20 | 21 | function getMappingMember(uint id) returns (address[]) 22 | { 23 | address[] memory localBytes = new address[](counter); 24 | for(uint i=1; i<= counter; i++){ 25 | localBytes[i - 1] = Names[i]; 26 | } 27 | 28 | return localBytes; 29 | } 30 | } 31 | 32 | 33 | 34 | 35 | 36 | 37 | 38 | 39 | -------------------------------------------------------------------------------- /Chapter03/MappinginMemory.sol: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | pragma solidity 0.4.19; 5 | 6 | contract MappinginMemory { 7 | 8 | mapping (uint => address) Names; 9 | 10 | uint counter; 11 | 12 | function addtoMapping(address addressDetails) returns (uint) 13 | { 14 | counter = counter + 1; 15 | mapping (uint => address) localNames = Names; 16 | 17 | localNames[counter] = addressDetails; 18 | 19 | return counter; 20 | } 21 | 22 | function getMappingMember(uint id) returns (address) 23 | { 24 | return Names[id]; 25 | } 26 | } 27 | 28 | 29 | 30 | 31 | 32 | 33 | 34 | 35 | -------------------------------------------------------------------------------- /Chapter03/PragmaAndComments.sol: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | pragma solidity 0.4.19; 5 | 6 | // This is a single line comment in Solidity 7 | 8 | /* This is a multi-line comment 9 | In solidity. Use this when multiple consecutive lines 10 | Should be commented as a whole */ 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | -------------------------------------------------------------------------------- /Chapter03/Struct-generalStructure.sol: -------------------------------------------------------------------------------- 1 | 2 | pragma solidity 0.4.19; 3 | 4 | //contract definition 5 | contract generalStructure { 6 | //state variables 7 | 8 | //structure definition 9 | struct myStruct { 10 | string name; //variable fo type string 11 | uint myAge; // variable of unsigned integer type 12 | bool isMarried; // variable of boolean type 13 | uint[] bankAccountsNumbers; // variable - dynamic array of unsigned integer 14 | } 15 | 16 | // state structure 17 | myStruct stateStructure = myStruct("Ritesh", 10, true, new uint[](2)); 18 | 19 | myStruct stateStructure1; 20 | 21 | 22 | //function definition 23 | function getAge () returns (uint) { 24 | 25 | // local structure 26 | myStruct memory localStructure = myStruct("Modi", 20 ,false, new uint[](2)); 27 | 28 | //local pointer to State structure 29 | myStruct pointerStructure = stateStructure; 30 | 31 | // pointerlocalStructure is reference to localStructure 32 | myStruct memory pointerlocalStructure = localStructure; 33 | 34 | //changing value in localStructure 35 | localStructure.myAge = 30; 36 | 37 | //assigning values to state variable 38 | stateStructure1 = myStruct("Ritesh", 10, true, new uint[](2)); 39 | 40 | //returning pointerlocalStructure.Age -- returns 30 41 | return pointerlocalStructure.myAge; 42 | 43 | } 44 | 45 | } 46 | 47 | 48 | 49 | 50 | 51 | 52 | 53 | 54 | 55 | 56 | -------------------------------------------------------------------------------- /Chapter03/boolContract.sol: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | pragma solidity 0.4.19; 5 | 6 | contract boolContract { 7 | 8 | bool isPaid = true; 9 | 10 | function manageBool() returns (bool) 11 | { 12 | isPaid = false; 13 | 14 | return isPaid; //returns false 15 | } 16 | 17 | function convertToUint() returns (uint8) 18 | { 19 | isPaid = false; 20 | 21 | return uint8(isPaid); //error 22 | } 23 | 24 | 25 | 26 | } 27 | 28 | 29 | 30 | 31 | 32 | 33 | 34 | 35 | -------------------------------------------------------------------------------- /Chapter03/bytesContract.sol: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | pragma solidity 0.4.19; 5 | 6 | contract bytesContract { 7 | 8 | bytes1 aa = 0x65; 9 | bytes1 bb = 10; 10 | bytes2 cc = 256; 11 | bytes1 dd = 'a'; 12 | bytes1 ee = -100; 13 | bytes1 ff ; 14 | 15 | function getintff() returns (bytes1) 16 | { 17 | return ff; //returns 101 18 | } 19 | 20 | function getintaa() returns (uint) 21 | { 22 | return uint(aa); //returns 101 23 | } 24 | 25 | function getbyteaa() returns (bytes1) 26 | { 27 | return aa; //returns 0x65 28 | } 29 | 30 | function getbytebb() returns (bytes1) 31 | { 32 | return bb; //returns 0x0a 33 | } 34 | 35 | function getintbb() returns (uint) 36 | { 37 | return uint(bb); //returns 10 38 | } 39 | 40 | function getbytecc() returns (bytes2) 41 | { 42 | return cc; //returns 0x0100 43 | } 44 | 45 | function getintcc() returns (uint) 46 | { 47 | return uint(cc); //returns 256 48 | } 49 | 50 | function getbytedd() returns (bytes2) 51 | { 52 | return dd; //returns 0x6100 or 0x61 for bytes1 53 | } 54 | 55 | function getintdd() returns (uint) 56 | { 57 | return uint(dd); //returns 97 58 | } 59 | } 60 | 61 | 62 | 63 | 64 | 65 | -------------------------------------------------------------------------------- /Chapter03/enums.sol: -------------------------------------------------------------------------------- 1 | 2 | pragma solidity 0.4.19; 3 | 4 | //contract definition 5 | contract generalStructure { 6 | //state variables 7 | 8 | //structure definition 9 | struct myStruct { 10 | string name; //variable fo type string 11 | uint myAge; // variable of unsigned integer type 12 | bool isMarried; // variable of boolean type 13 | uint[] bankAccountsNumbers; // variable - dynamic array of unsigned integer 14 | } 15 | 16 | // state structure 17 | myStruct stateStructure = myStruct("Ritesh", 10, true, new uint[](2)); 18 | 19 | myStruct stateStructure1; 20 | 21 | 22 | //function definition 23 | function getAge () returns (uint) { 24 | 25 | // local structure 26 | myStruct memory localStructure = myStruct("Modi", 20 ,false, new uint[](2)); 27 | 28 | //local pointer to State structure 29 | myStruct pointerStructure = stateStructure; 30 | 31 | // pointerlocalStructure is reference to localStructure 32 | myStruct memory pointerlocalStructure = localStructure; 33 | 34 | //changing value in localStructure 35 | localStructure.myAge = 30; 36 | 37 | //assigning values to state variable 38 | stateStructure1 = myStruct("Ritesh", 10, true, new uint[](2)); 39 | 40 | //returning pointerlocalStructure.Age -- returns 30 41 | return pointerlocalStructure.myAge; 42 | 43 | } 44 | 45 | } 46 | 47 | 48 | 49 | 50 | 51 | 52 | 53 | 54 | 55 | 56 | -------------------------------------------------------------------------------- /Chapter04/ConversionDemo.txt: -------------------------------------------------------------------------------- 1 | 2 | 3 | pragma solidity ^0.4.19; 4 | 5 | contract ConversionDemo { 6 | 7 | function ConvertionExplicitUINT8toUINT256() returns (uint){ 8 | uint8 myVariable = 10; 9 | uint256 someVariable = myVariable; 10 | return someVariable; 11 | } 12 | 13 | function ConvertionExplicitUINT256toUINT8() returns (uint8){ 14 | uint256 myVariable = 10; 15 | uint8 someVariable = uint8(myVariable); 16 | return someVariable; 17 | } 18 | 19 | function ConvertionExplicitUINT256toUINT81() returns (uint8){ 20 | uint256 myVariable = 10000134; 21 | uint8 someVariable = uint8(myVariable); 22 | return someVariable; //returns 6 as return value 23 | } 24 | 25 | function Convertions() { 26 | 27 | uint256 myVariable = 10000134; 28 | uint8 someVariable = 100; 29 | bytes4 byte4 = 0x65666768; 30 | 31 | // bytes1 byte1 = 0x656667668; //error 32 | 33 | bytes1 byte1 = 0x65; 34 | 35 | // byte1 = byte4; //error, explicit conversion needed here 36 | 37 | byte1 = bytes1(byte4) ; //explicit conversion 38 | 39 | byte4 = byte1; //Implicit conversion 40 | 41 | // uint8 someVariable = myVariable; // error, explicit conversion needed here 42 | 43 | myVariable = someVariable; //Implicit conversion 44 | 45 | string memory name = "Ritesh"; 46 | bytes memory nameInBytes = bytes(name); //explicit string to bytes conversion 47 | 48 | name = string(nameInBytes); //explicit bytes to string conversion 49 | 50 | } 51 | } 52 | 53 | 54 | 55 | 56 | 57 | 58 | 59 | 60 | 61 | 62 | 63 | -------------------------------------------------------------------------------- /Chapter04/CryptoFunctions.txt: -------------------------------------------------------------------------------- 1 | 2 | 3 | pragma solidity ^0.4.19; 4 | 5 | contract CryptoFunctions { 6 | 7 | function cryptoDemo() returns (bytes32, bytes32, bytes32){ 8 | 9 | return (sha256("r"), keccak256("r"), sha3("r")); 10 | 11 | 12 | } 13 | 14 | } 15 | 16 | -------------------------------------------------------------------------------- /Chapter04/ErrorDataType.txt: -------------------------------------------------------------------------------- 1 | 2 | 3 | pragma solidity ^0.4.19; 4 | 5 | contract ErrorDataType { 6 | 7 | function hoistingDemo() returns (uint){ 8 | 9 | uint8 someVar = 100; 10 | someVar = 300; //error 11 | 12 | 13 | } 14 | 15 | } 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | -------------------------------------------------------------------------------- /Chapter04/TransactionAndMessageVariables.txt: -------------------------------------------------------------------------------- 1 | 2 | 3 | pragma solidity ^0.4.19; 4 | 5 | contract TransactionAndMessageVariables { 6 | 7 | event logstring(string); 8 | event loguint(uint); 9 | event logbytes(bytes); 10 | event logaddress(address); 11 | event logbyte4(bytes4); 12 | event logblock(bytes32); 13 | 14 | function globalVariable() payable { 15 | 16 | logaddress( block.coinbase ); // 0x94d76e24f818426ae84aa404140e8d5f60e10e7e 17 | 18 | loguint( block.difficulty ); //71762765929000 19 | 20 | loguint( block.gaslimit ); // 6000000 21 | 22 | loguint( msg.gas ); //2975428 23 | 24 | loguint( tx.gasprice ); // 1 25 | 26 | loguint( block.number ); //123 27 | 28 | loguint( block.timestamp ); //1513061946 29 | 30 | loguint( now ); //1513061946 31 | 32 | logbytes( msg.data ); // 0x4048d797 33 | 34 | logbyte4( msg.sig ); // // 0x4048d797 35 | 36 | loguint( msg.value ); // 0 or in Wei if ether are send 37 | 38 | logaddress( msg.sender ); //0xca35b7d915458ef540ade6068dfe2f44e8fa733c" 39 | 40 | logaddress( tx.origin ); // 0xca35b7d915458ef540ade6068dfe2f44e8fa733c" 41 | 42 | logblock ( block.blockhash( block.number) ); //0x00000000000000000000000 43 | 44 | } 45 | } 46 | 47 | 48 | 49 | 50 | 51 | 52 | 53 | 54 | 55 | 56 | 57 | -------------------------------------------------------------------------------- /Chapter04/VarType.txt: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | pragma solidity 0.4.19; 5 | 6 | contract VarExamples { 7 | 8 | function VarType() 9 | { 10 | var uintVar8 = 10; //uint8 11 | uintVar8 = 255; //256 is error 12 | 13 | var uintVar16 = 256; //uint16 14 | uintVar16 = 65535; //aaa = 65536; is error 15 | 16 | var intVar8 = -1; //int8 values -128 to 127 17 | 18 | var intVar16 = -129; //int16 values -32768 to 32767 19 | 20 | var boolVar = true; 21 | boolVar = false; // 10 is error, 0 is error, 1 is error, -1 is error 22 | 23 | 24 | var stringVar = "0x10"; // this is string memory 25 | stringVar = "10"; // cc =1231231231231231231212222222 is error 26 | 27 | var bytesVar = 0x100; // this is byte memory 28 | 29 | 30 | var Var = hex"001122FF"; 31 | 32 | var arrayInteger = [uint8(1),2]; 33 | arrayInteger[1] = 255; 34 | 35 | var arrayByte = bytes10(0x2222); 36 | arrayByte = 0x11111111111111111111; //0x111111111111111111111 is error 37 | 38 | } 39 | 40 | } 41 | 42 | 43 | 44 | 45 | 46 | -------------------------------------------------------------------------------- /Chapter04/VariableHoisting.txt: -------------------------------------------------------------------------------- 1 | 2 | 3 | pragma solidity ^0.4.19; 4 | 5 | contract variableHoisting { 6 | 7 | function hoistingDemo() returns (uint){ 8 | 9 | firstVar = 10; 10 | secondVar = 20; 11 | 12 | result = firstVar + secondVar; 13 | 14 | uint firstVar; 15 | uint secondVar; 16 | uint result; 17 | return result; 18 | 19 | 20 | } 21 | 22 | } 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | 31 | 32 | 33 | 34 | -------------------------------------------------------------------------------- /Chapter04/VariableScoping.txt: -------------------------------------------------------------------------------- 1 | 2 | 3 | pragma solidity ^0.4.19; 4 | 5 | contract ScopingDtateVariables { 6 | 7 | // uint64 public myVar = 0; 8 | 9 | // uint64 private myVar = 0; 10 | 11 | // uint64 internal myVar = 0; 12 | 13 | 14 | 15 | 16 | } 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | -------------------------------------------------------------------------------- /Chapter05/DowhileLoop.sol: -------------------------------------------------------------------------------- 1 | 2 | 3 | pragma solidity ^0.4.19; 4 | 5 | contract DowhileLoop { 6 | 7 | mapping (uint => uint) blockNumber; 8 | uint counter; 9 | 10 | event uintNumber(uint); 11 | bytes aa; 12 | 13 | function SetNumber() { 14 | 15 | blockNumber[counter++] = block.number; 16 | 17 | 18 | } 19 | 20 | function getNumbers() { 21 | 22 | uint i = 0; 23 | do { 24 | uintNumber( blockNumber[i] ); 25 | i = i + 1; 26 | } while (i < counter); 27 | 28 | } 29 | } -------------------------------------------------------------------------------- /Chapter05/ForLoopExample.sol: -------------------------------------------------------------------------------- 1 | 2 | pragma solidity ^0.4.19; 3 | 4 | 5 | contract ForLoopExample { 6 | 7 | mapping (uint => uint) blockNumber; 8 | uint counter; 9 | 10 | event uintNumber(uint); 11 | 12 | function SetNumber() { 13 | 14 | blockNumber[counter++] = block.number; 15 | 16 | 17 | } 18 | 19 | function getNumbers() { 20 | 21 | for (uint i=0; i < counter; i++){ 22 | uintNumber( blockNumber[i] ); 23 | } 24 | 25 | } 26 | } 27 | -------------------------------------------------------------------------------- /Chapter05/ForLoopExampleBreak.sol: -------------------------------------------------------------------------------- 1 | 2 | pragma solidity ^0.4.19; 3 | 4 | 5 | contract ForLoopExampleBreak { 6 | 7 | mapping (uint => uint) blockNumber; 8 | uint counter; 9 | 10 | event uintNumber(uint); 11 | 12 | function SetNumber() { 13 | 14 | blockNumber[counter++] = block.number; 15 | 16 | 17 | } 18 | 19 | function getNumbers() { 20 | 21 | for (uint i=0; i < counter; i++){ 22 | if (i == 1) 23 | break; 24 | uintNumber( blockNumber[i] ); 25 | 26 | } 27 | 28 | } 29 | } -------------------------------------------------------------------------------- /Chapter05/ForLoopExampleContinue.sol: -------------------------------------------------------------------------------- 1 | 2 | 3 | pragma solidity ^0.4.19; 4 | 5 | 6 | contract ForLoopExampleContinue { 7 | 8 | mapping (uint => uint) blockNumber; 9 | uint counter; 10 | 11 | event uintNumber(uint); 12 | 13 | function SetNumber() { 14 | 15 | blockNumber[counter++] = block.number; 16 | 17 | 18 | } 19 | 20 | function getNumbers() { 21 | 22 | for (uint i=0; i < counter; i++){ 23 | if ((i > 5) ) 24 | { continue;} 25 | uintNumber( blockNumber[i] ); 26 | 27 | } 28 | 29 | } 30 | } 31 | -------------------------------------------------------------------------------- /Chapter05/IfElseExample.sol: -------------------------------------------------------------------------------- 1 | 2 | 3 | pragma solidity ^0.4.19; 4 | 5 | contract IfElseExample { 6 | 7 | enum requestState {created, approved, provisioned, rejected, deleted, none} 8 | 9 | function StateManagement(uint8 _state) returns (int result) { 10 | 11 | requestState currentState = requestState(_state); 12 | 13 | if(currentState == requestState(1)){ 14 | result = 1; 15 | } else if ((currentState == requestState.approved) || (currentState == requestState.provisioned)) { 16 | result = 2; 17 | } else { 18 | currentState == requestState.none; 19 | result = 3; 20 | } 21 | 22 | } 23 | } 24 | -------------------------------------------------------------------------------- /Chapter05/ReturnValues.sol: -------------------------------------------------------------------------------- 1 | pragma solidity ^0.4.19; 2 | 3 | 4 | 5 | contract ReturnValues { 6 | 7 | uint counter; 8 | 9 | function SetNumber() { 10 | 11 | counter = block.number; 12 | 13 | 14 | } 15 | 16 | function getBlockNumber() returns (uint) { 17 | 18 | return counter; 19 | 20 | } 21 | 22 | function getBlockNumber1() returns (uint result) { 23 | 24 | result = counter; 25 | 26 | } 27 | } 28 | 29 | -------------------------------------------------------------------------------- /Chapter05/whileLoop.sol: -------------------------------------------------------------------------------- 1 | 2 | 3 | pragma solidity ^0.4.19; 4 | 5 | contract whileLoop { 6 | 7 | mapping (uint => uint) blockNumber; 8 | uint counter; 9 | 10 | event uintNumber(uint); 11 | bytes aa; 12 | 13 | function SetNumber() { 14 | 15 | blockNumber[counter++] = block.number; 16 | } 17 | 18 | function getNumbers() { 19 | 20 | uint i = 0; 21 | while (i < counter) { 22 | uintNumber( blockNumber[i] ); 23 | i = i + 1; 24 | } 25 | 26 | } 27 | } 28 | -------------------------------------------------------------------------------- /Chapter06/Address.sol: -------------------------------------------------------------------------------- 1 | pragma solidity 0.4.19; 2 | 3 | contract HelloWorld { 4 | uint private simpleInt; 5 | 6 | function GetValue() public view returns (uint) { 7 | return simpleInt; 8 | } 9 | 10 | function SetValue(uint _value) public { 11 | simpleInt = _value; 12 | } 13 | } 14 | 15 | contract client { 16 | 17 | address obj ; 18 | 19 | function setObject(address _obj) external { 20 | obj = _obj; 21 | } 22 | 23 | function UseExistingAddress() public returns { 24 | HelloWorld myObj = new HelloWorld(obj); 25 | myObj.SetValue(10); 26 | return myObj.GetValue(); 27 | } 28 | } -------------------------------------------------------------------------------- /Chapter06/Constructor.sol: -------------------------------------------------------------------------------- 1 | pragma solidity 0.4.19; 2 | 3 | contraCT HelloWorld { 4 | 5 | uint private simpleInt; 6 | 7 | function HelloWorld() public { 8 | simpleInt = 5; 9 | } 10 | 11 | function GetValue() public returns (uint) { 12 | return = simpleInt; 13 | } 14 | 15 | function SetValue(uint _value) public { 16 | simpleInt = _value; 17 | } 18 | } -------------------------------------------------------------------------------- /Chapter06/HelloWorld.sol: -------------------------------------------------------------------------------- 1 | pragma solidity 0.4.19; 2 | 3 | contract HelloWorld { 4 | uint private simpleInt; 5 | 6 | function getValue() public view returns (uint) { 7 | return simpleInt; 8 | } 9 | 10 | function setValue(uint _value) public { 11 | simpleInt = _value; 12 | } 13 | } 14 | 15 | contract client { 16 | 17 | function useNewKeyword() public returns (uint) { 18 | 19 | HelloWorld myObj = new HelloWorld(); 20 | 21 | myObj.setValue(10); 22 | 23 | return myObj.getValue(); 24 | } 25 | } -------------------------------------------------------------------------------- /Chapter06/IHelloWorld.sol: -------------------------------------------------------------------------------- 1 | pragma solidity 0.4.19; 2 | 3 | interface IHelloWorld { 4 | function GetValue() public view returns (uint); 5 | function SetValue(uint _value) public; 6 | } 7 | 8 | contract HelloWorld is IHelloWorld{ 9 | uint private simpleInteger; 10 | 11 | function GetValue() public view returns (uint) { 12 | return simpleInteger; 13 | } 14 | 15 | function SetValue(uint _value) public { 16 | simpleInteger = _value; 17 | } 18 | } 19 | 20 | contract client { 21 | IHelloWorld myObj; 22 | 23 | function client(){ 24 | myObj = new HelloWorld(); 25 | } 26 | 27 | function GetSetIntegerValue() public returns (uint) { 28 | myObj.SetValue(100); 29 | return myObj.GetValue(); 30 | } 31 | } -------------------------------------------------------------------------------- /Chapter06/Inheritance.sol: -------------------------------------------------------------------------------- 1 | pragma solidity 0.4.19; 2 | 3 | contract ParentContract { 4 | uint internal simpleInteger; 5 | 6 | function SetInteger(uint _value) external { 7 | simpleInteger = _value; 8 | } 9 | } 10 | 11 | contract ChildContract is ParentContract { 12 | bool private simpleBool; 13 | 14 | function GetInteger() public view returns (uint) { 15 | return simpleInteger; 16 | } 17 | } 18 | 19 | contract Client { 20 | ChildContract pc = new ChildContract(); 21 | 22 | function workWithInheritance() public returns (uint) { 23 | pc.SetInteger(100); 24 | return pc.GetInteger(); 25 | } 26 | } -------------------------------------------------------------------------------- /Chapter06/ParentContract.sol: -------------------------------------------------------------------------------- 1 | pragma solidity ^0.4.19; 2 | 3 | contract ParentContract { 4 | uint internal simpleInteger; 5 | 6 | function SetInteger(uint _value) public { 7 | simpleInteger = _value; 8 | } 9 | 10 | function GetInteger() public view returns (uint) { 11 | return 10; 12 | } 13 | } 14 | 15 | contract ChildContract is ParentContract { 16 | 17 | 18 | function GetInteger() public view returns (uint) { 19 | return simpleInteger; 20 | } 21 | } 22 | 23 | contract Client { 24 | ParentContract pc = new ChildContract(); 25 | 26 | function workWithInheritance() public returns (uint) { 27 | pc.SetInteger(100); 28 | return pc.GetInteger(); 29 | } 30 | } -------------------------------------------------------------------------------- /Chapter06/SumContract.sol: -------------------------------------------------------------------------------- 1 | pragma solidity 0.4.19; 2 | 3 | conract SumContract { 4 | 5 | function Sum(uint a, uint b) public returns (uint) { 6 | return a + b; 7 | } 8 | } 9 | 10 | 11 | contract MultiContract is SumContract { 12 | 13 | function Multiply(uint a, uint b) public returns (uint) { 14 | return a * b; 15 | } 16 | } 17 | 18 | 19 | contract DivideContract is SumContract { 20 | 21 | function Multiply(uint a, uint b) public returns (uint) { 22 | return a / b; 23 | } 24 | } 25 | 26 | contract SubContract is SumContract, MultiContract,DivideContract { 27 | 28 | function sub(uint a, uint b) public returns (uint) { 29 | return a - b; 30 | } 31 | } 32 | 33 | contratc client { 34 | 35 | function workWithInheritance() public returna (uint) { 36 | uint a = 20; 37 | uint b = 10; 38 | SubContract subt = new SubContract(); 39 | return subt.Sum(a,b); 40 | } 41 | } 42 | -------------------------------------------------------------------------------- /Chapter06/abstractHelloWorld.sol: -------------------------------------------------------------------------------- 1 | pragma solidity 0.4.19; 2 | 3 | contract abstractHelloWorld { 4 | function GetValue() public view returns (uint); 5 | function SetValue(uint _value) public; 6 | 7 | function AddNumber(uint _value) public returna (uint) { 8 | return 10; 9 | } 10 | } 11 | 12 | contract HelloWorld is AbstractHelloWorld{ 13 | uint private simpleInteger; 14 | 15 | function GetValue() public view returns (uint) { 16 | return simpleInteger; 17 | } 18 | 19 | function SetValue(uint _value) public { 20 | simpleInteger = _value; 21 | } 22 | 23 | function AddNumber(uint _value) public returns (uint){ 24 | returns simpleInteger = _value; 25 | } 26 | } 27 | 28 | contract client { 29 | abstractHelloWorld myObj; 30 | 31 | function client() { 32 | myObj = new HelloWorld(); 33 | } 34 | 35 | function GetIntegerValue() public returns (uint) { 36 | myObj.SetValue(100); 37 | return myObj.AddNumber(200); 38 | } 39 | } -------------------------------------------------------------------------------- /Chapter06/helloFunctionPloymorphism.sol: -------------------------------------------------------------------------------- 1 | pragma solidity ^0.4.19; 2 | 3 | contratc helloFunctionPloymorphism 4 | { 5 | function getVariableData(int8 data) public constant returns(int8 output) 6 | { 7 | return data; 8 | } 9 | 10 | function overloadedFunction(int16 data) public constant retruns(int16 output) 11 | { 12 | return data; 13 | } 14 | } -------------------------------------------------------------------------------- /Chapter07/ContractWithModifier.sol: -------------------------------------------------------------------------------- 1 | 2 | 3 | pragma solidity ^0.4.17; 4 | 5 | 6 | contract ContractWithModifier { 7 | 8 | address owner; 9 | int public mydata; 10 | 11 | function ContractWithoutModifier(){ 12 | owner = msg.sender; 13 | } 14 | 15 | modifier isOwner { 16 | // require(msg.sender == owner); 17 | if(msg.sender == owner) { 18 | _; 19 | } 20 | } 21 | 22 | function AssignDoubleValue(int _data) public isOwner { 23 | mydata = _data * 2; 24 | } 25 | 26 | function AssignTenerValue(int _data) public { 27 | mydata = _data * 10; 28 | } 29 | 30 | } -------------------------------------------------------------------------------- /Chapter07/ContractWithoutModifier.sol: -------------------------------------------------------------------------------- 1 | 2 | 3 | pragma solidity ^0.4.17; 4 | 5 | 6 | contract ContractWithoutModifier { 7 | 8 | address owner; 9 | int public mydata; 10 | 11 | function ContractWithoutModifier(){ 12 | owner = msg.sender; 13 | } 14 | 15 | function AssignDoubleValue(int _data) public { 16 | if(msg.sender == owner) { 17 | mydata = _data * 2; 18 | } 19 | } 20 | 21 | function AssignTenerValue(int _data) public { 22 | if(msg.sender == owner) { 23 | mydata = _data * 10; 24 | } 25 | } 26 | 27 | } 28 | 29 | 30 | 31 | 32 | 33 | 34 | -------------------------------------------------------------------------------- /Chapter07/Fallback.sol: -------------------------------------------------------------------------------- 1 | 2 | pragma solidity ^0.4.17; 3 | 4 | contract EtherBox { 5 | uint balance; 6 | event logme(string); 7 | 8 | function SetBalance() public { 9 | balance = balance + 10; 10 | } 11 | 12 | function GetBalance() public payable returns(uint) { 13 | return balance; 14 | } 15 | 16 | function() payable { 17 | logme("fallback called"); 18 | } 19 | } 20 | 21 | contract UsingCall { 22 | function UsingCall() public payable { 23 | } 24 | 25 | function SimpleCall() public returns (uint) { 26 | bool status = true; 27 | EtherBox eb = new EtherBox(); 28 | address myaddr = address(eb); 29 | status = myaddr.call(bytes4(sha3("SetBalance()"))); 30 | return eb.GetBalance(); 31 | } 32 | 33 | function SimpleCallwithGas() public returns (bool) { 34 | bool status = true; 35 | EtherBox eb = new EtherBox(); 36 | address myaddr = address(eb); 37 | return status = myaddr.call.gas(200000)(bytes4(sha3("GetBalance()"))); 38 | } 39 | 40 | function SimpleCallwithGasAndValue() public returns (bool) { 41 | bool status = true; 42 | EtherBox eb = new EtherBox(); 43 | address myaddr = address(eb); 44 | return status = myaddr.call.gas(200000).value(1)(bytes4(sha3("GetBalance()"))); 45 | } 46 | 47 | function SimpleCallwithGasAndValueWithWrongName() public returns (bool) { 48 | bool status = true; 49 | EtherBox eb = new EtherBox(); 50 | address myaddr = address(eb); 51 | return myaddr.call.gas(200000).value(1)(bytes4(sha3("GetBalance1()"))); 52 | } 53 | } 54 | 55 | 56 | 57 | 58 | 59 | 60 | 61 | 62 | 63 | -------------------------------------------------------------------------------- /Chapter07/FallbackFunction.sol: -------------------------------------------------------------------------------- 1 | 2 | 3 | pragma solidity ^0.4.17; 4 | 5 | 6 | contract FallbackFunction { 7 | 8 | function() { 9 | var a = 0; 10 | } 11 | } -------------------------------------------------------------------------------- /Chapter07/PureFunction.sol: -------------------------------------------------------------------------------- 1 | 2 | 3 | pragma solidity ^0.4.17; 4 | 5 | 6 | contract PureFunction { 7 | 8 | function GetTenerValue(int _data) public pure returns (int) { 9 | return _data * 10; 10 | } 11 | 12 | } 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | -------------------------------------------------------------------------------- /Chapter07/SendAndTransferSample.sol: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | mapping (address => uint) balance; 7 | 8 | function SimpleSendToAccount(uint amount) public returns (bool) { 9 | if(balance[msg.sender] >= amount ) { 10 | balance[msg.sender] -= amount; 11 | if (msg.sender.send(amount) == true) { 12 | return true; 13 | } 14 | else { 15 | balance[msg.sender] += amount; 16 | return false; 17 | } 18 | } 19 | } 20 | 21 | 22 | function SimpleTransferToAccount() public { 23 | msg.sender.transfer(1); 24 | } 25 | 26 | 27 | 28 | -------------------------------------------------------------------------------- /Chapter07/SimpleTransferToAccount.sol: -------------------------------------------------------------------------------- 1 | function SimpleTransferToAccount() public { 2 | 3 | msg.sender.transfer(1); 4 | 5 | } -------------------------------------------------------------------------------- /Chapter07/UsingCall.sol: -------------------------------------------------------------------------------- 1 | 2 | 3 | pragma solidity ^0.4.17; 4 | 5 | contract EtherBox { 6 | uint balance; 7 | 8 | function SetBalance() public { 9 | balance = balance + 10; 10 | } 11 | 12 | function GetBalance() public payable returns(uint) { 13 | return balance; 14 | } 15 | } 16 | 17 | contract UsingCall { 18 | function UsingCall() public payable { 19 | } 20 | 21 | function SimpleCall() public returns (uint) { 22 | bool status = true; 23 | EtherBox eb = new EtherBox(); 24 | address myaddr = address(eb); 25 | status = myaddr.call(bytes4(sha3("SetBalance()"))); 26 | return eb.GetBalance(); 27 | } 28 | 29 | function SimpleCallwithGas() public returns (bool) { 30 | bool status = true; 31 | EtherBox eb = new EtherBox(); 32 | address myaddr = address(eb); 33 | status = myaddr.call.gas(200000)(bytes4(sha3("GetBalance()"))); 34 | return status; 35 | } 36 | 37 | function SimpleCallwithGasAndValue() public returns (bool) { 38 | bool status = true; 39 | EtherBox eb = new EtherBox(); 40 | address myaddr = address(eb); 41 | status = myaddr.call.gas(200000).value(1)(bytes4(sha3("GetBalance()"))); 42 | return status; 43 | } 44 | 45 | } -------------------------------------------------------------------------------- /Chapter07/ViewFunction.sol: -------------------------------------------------------------------------------- 1 | 2 | 3 | pragma solidity ^0.4.17; 4 | 5 | 6 | contract ViewFunction { 7 | 8 | function GetTenerValue(int _data) public view returns (int) { 9 | return _data * 10; 10 | } 11 | 12 | } 13 | 14 | -------------------------------------------------------------------------------- /Chapter07/parameters.sol: -------------------------------------------------------------------------------- 1 | 2 | 3 | pragma solidity ^0.4.19; 4 | 5 | 6 | contract Parameters { 7 | 8 | 9 | function singleIncomingParameter(int _data) returns (int _output) { 10 | return _data * 2; 11 | } 12 | 13 | function multipleIncomingParameter(int _data, int _data2) returns (int _output) { 14 | return _data * _data2; 15 | } 16 | 17 | function multipleOutgoingParameter(int _data) returns (int square, int half) { 18 | square = _data * _data; 19 | half = _data /2 ; 20 | } 21 | 22 | function multipleOutgoingTuple(int _data) returns (int square, int half) { 23 | (square, half) = (_data * _data,_data /2 ); 24 | } 25 | 26 | 27 | } 28 | 29 | 30 | 31 | 32 | 33 | 34 | -------------------------------------------------------------------------------- /Chapter08/AssertContract.sol: -------------------------------------------------------------------------------- 1 | 2 | 3 | pragma solidity ^0.4.19; 4 | 5 | contract AssertContract { 6 | 7 | function ValidInt8(uint _data) public returns(uint8){ 8 | require(_data >= 0); 9 | require(_data <= 255); 10 | 11 | uint8 value = 20; 12 | 13 | //checking datatype overflow 14 | assert (value + _data <= 255); 15 | 16 | return uint8(_data); 17 | } 18 | } 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | 31 | 32 | -------------------------------------------------------------------------------- /Chapter08/EventContract.sol: -------------------------------------------------------------------------------- 1 | 2 | 3 | pragma solidity ^0.4.19; 4 | 5 | contract EventContract { 6 | 7 | event LogFunctionFlow(string); 8 | 9 | function ValidInt8(int _data) public returns(uint8){ 10 | LogFunctionFlow("Within function ValidInt8"); 11 | 12 | if(_data < 0 || _data > 255) { 13 | revert(); 14 | } 15 | 16 | LogFunctionFlow("Value is within expected range"); 17 | LogFunctionFlow("Returning value from function"); 18 | 19 | return uint8(_data); 20 | } 21 | } 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | 31 | 32 | 33 | 34 | 35 | 36 | 37 | 38 | 39 | 40 | 41 | 42 | 43 | 44 | 45 | 46 | 47 | -------------------------------------------------------------------------------- /Chapter08/RequireContract.sol: -------------------------------------------------------------------------------- 1 | 2 | 3 | pragma solidity ^0.4.19; 4 | 5 | contract RequireContract { 6 | 7 | function ValidInt8(uint _data) public returns(uint8){ 8 | require(_data >= 0); 9 | require(_data <= 255); 10 | 11 | return uint8(_data); 12 | } 13 | 14 | function ShouldbeEven(uint _data) public returns(bool){ 15 | require(_data % 2 == 0); 16 | return true; 17 | } 18 | 19 | } 20 | 21 | 22 | -------------------------------------------------------------------------------- /Chapter08/RevertContract.sol: -------------------------------------------------------------------------------- 1 | 2 | 3 | pragma solidity ^0.4.19; 4 | 5 | contract RevertContract { 6 | 7 | function ValidInt8(int _data) public returns(uint8){ 8 | 9 | if(_data < 0 || _data > 255) { 10 | revert(); 11 | } 12 | 13 | return uint8(_data); 14 | } 15 | } 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | 31 | 32 | 33 | 34 | 35 | 36 | 37 | 38 | 39 | -------------------------------------------------------------------------------- /Chapter08/eventsPromise.js: -------------------------------------------------------------------------------- 1 | var myEvent = instance.ageRead({fromBlock: 25000, toBlock: 'latest'}); 2 | myEvent.watch(function(error, result){ 3 | if(error) { 4 | console.log(error); 5 | } 6 | console.log(result.args) 7 | }); 8 | 9 | 10 | var myEvent = instance.allEvents({fromBlock: 24000, toBlock: 'latest'}); 11 | myEvent.watch(function(error, result){ 12 | if(error) { 13 | console.log(error); 14 | } 15 | console.log(result) 16 | }); 17 | 18 | 19 | -------------------------------------------------------------------------------- /Chapter09/2_Custom.js: -------------------------------------------------------------------------------- 1 | var hw = artifacts.require("First"); var hw1 = artifacts.require("Second"); 2 | 3 | module.exports = function(deployer) { 4 | deployer.deploy(hw).then 5 | (function() { return deployer.deploy(hw1,hw.address);})}; 6 | -------------------------------------------------------------------------------- /Chapter09/DebuggerSampleContract.sol: -------------------------------------------------------------------------------- 1 | 2 | 3 | pragma solidity ^0.4.0; 4 | 5 | contract DebuggerSampleContract { 6 | 7 | int counter = 10; 8 | 9 | function LoopCounter(int _input) public view returns (int) { 10 | int returnValue; 11 | 12 | for (; _input < counter; _input ++) 13 | { 14 | returnValue += _input; 15 | } 16 | return returnValue; 17 | } 18 | } 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | 31 | 32 | 33 | 34 | 35 | 36 | 37 | 38 | -------------------------------------------------------------------------------- /Chapter09/TestFirst.sol: -------------------------------------------------------------------------------- 1 | pragma solidity ^0.4.19; 2 | 3 | import "truffle/Assert.sol"; 4 | import "truffle/DeployedAddresses.sol"; 5 | import "../contracts/first.sol"; 6 | 7 | contract TestFirst { 8 | function testInitialBalanceUsingDeployedContract() { 9 | First meta = First(DeployedAddresses.First()); 10 | 11 | Assert.equal(meta.GetDouble(10), 20, "done"); 12 | } 13 | 14 | } -------------------------------------------------------------------------------- /Chapter09/first.sol: -------------------------------------------------------------------------------- 1 | pragma solidity ^0.4.17; 2 | 3 | 4 | contract First { 5 | 6 | int public mydata; 7 | 8 | function GetDouble(int _data) public returns (int _output) { 9 | mydata = _data * 2; 10 | return _data * 2; 11 | } 12 | 13 | } 14 | 15 | 16 | 17 | 18 | 19 | 20 | -------------------------------------------------------------------------------- /Chapter09/second.sol: -------------------------------------------------------------------------------- 1 | pragma solidity ^0.4.17; 2 | 3 | import "./First.sol"; 4 | 5 | contract Second { 6 | address firstAddress; 7 | int public _data; 8 | 9 | function Second(address _first) { 10 | firstAddress = _first; 11 | } 12 | 13 | function SetData() public { 14 | First h = First(firstAddress); 15 | _data = h.GetDouble(21); 16 | } 17 | } 18 | -------------------------------------------------------------------------------- /Chapter10/DebuggerSampleContract.sol: -------------------------------------------------------------------------------- 1 | 2 | 3 | pragma solidity ^0.4.0; 4 | 5 | contract DebuggerSampleContract { 6 | 7 | int counter = 10; 8 | 9 | function LoopCounter(int _input) public view returns (int) { 10 | int returnValue; 11 | 12 | for (; _input < counter; _input ++) 13 | { 14 | returnValue += _input; 15 | } 16 | return returnValue; 17 | } 18 | } 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | 31 | 32 | 33 | 34 | 35 | 36 | 37 | 38 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2018 Packt 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | # Solidity Programming Essentials 5 | This is the code repository for [Solidity Programming Essentials](https://www.packtpub.com/application-development/solidity-programming-essentials?utm_source=github&utm_medium=repository&utm_campaign=9781788831383), published by [Packt](https://www.packtpub.com/?utm_source=github). It contains all the supporting project files necessary to work through the book from start to finish. 6 | ## About the Book 7 | We begin with a brief run-through of blockchain, Ethereum, and their most important concepts or components. You will learn how to install all the necessary tools to write, test, and debug Solidity contracts on Ethereum. Then, you will explore the layout of a Solidity source file and work with the different data types. The next set of recipes will help you work with operators, control structures, and data structures while building your smart contracts. We take you through function calls, return types, function modifers, and recipes in object-oriented programming with Solidity. Learn all you can on event logging and exception handling, as well as testing and debugging smart contracts. 8 | 9 | 10 | ## Instructions and Navigation 11 | All of the code is organized into folders. Each folder starts with a number followed by the application name. For example, Chapter02. 12 | 13 | Chapter 2 does not have code files. 14 | 15 | The code will look like the following: 16 | ``` 17 | pragma solidity ^0.4.17; 18 | contract First { 19 | int public mydata; 20 | function GetDouble(int _data) public returns (int _output) { 21 | mydata = _data * 2; 22 | return _data * 2; 23 | } 24 | } 25 | ``` 26 | 27 | This book assumes a basic level knowledge of programming. It is ideal to have some background on any scripting language. All you need is an internet connectivity and a browser for using a majority of this book. There are sections that will need creating a machine to deploy blockchain specific tools and utilities. This machine can be physical or virtual, on cloud or on-premise. 28 | 29 | ## Related Products 30 | * [Blockchain and Cryptocurrency (Bitcoin, Ethereum) Essentials [Video]](https://www.packtpub.com/application-development/blockchain-and-cryptocurrency-bitcoin-ethereum-essentials-video?utm_source=github&utm_medium=repository&utm_campaign=9781788990837) 31 | 32 | * [Mastering Blockchain - Second Edition](https://www.packtpub.com/big-data-and-business-intelligence/mastering-blockchain-second-edition?utm_source=github&utm_medium=repository&utm_campaign=9781788839044) 33 | 34 | * [Ethereum Smart Contract Development](https://www.packtpub.com/big-data-and-business-intelligence/ethereum-smart-contract-development?utm_source=github&utm_medium=repository&utm_campaign=9781788473040) 35 | 36 | ### Suggestions and Feedback 37 | [Click here](https://docs.google.com/forms/d/e/1FAIpQLSe5qwunkGf6PUvzPirPDtuy1Du5Rlzew23UBp2S-P3wB-GcwQ/viewform) if you have any feedback or suggestions. 38 | ### Download a free PDF 39 | 40 | If you have already purchased a print or Kindle version of this book, you can get a DRM-free PDF version at no cost.
Simply click on the link to claim your free PDF.
41 |

https://packt.link/free-ebook/9781788831383

--------------------------------------------------------------------------------