├── 01_helloworld ├── README.md ├── contracts │ ├── HelloWorld.sol │ └── Migrations.sol ├── migrations │ ├── 1_initial_migration.js │ └── 2_custom.js ├── test │ └── TestHelloWorld.sol ├── truffle-config.js └── truffle.js ├── 02_state_variables ├── README.md ├── contracts │ ├── Migrations.sol │ └── StateVariables.sol ├── migrations │ ├── 1_initial_migration.js │ └── 2_custom.js ├── test │ └── TestStateVariables.sol ├── truffle-config.js └── truffle.js ├── 03_struct ├── README.md ├── contracts │ ├── Contract1.sol │ └── Migrations.sol ├── migrations │ ├── 1_initial_migration.js │ └── 2_custom.js ├── test │ └── TestContract1.sol ├── truffle-config.js └── truffle.js ├── 04_enum ├── README.md ├── contracts │ ├── Contract1.sol │ └── Migrations.sol ├── migrations │ ├── 1_initial_migration.js │ └── 2_custom.js ├── test │ └── TestContract1.sol ├── truffle-config.js └── truffle.js ├── 05_functions ├── README.md ├── contracts │ ├── Contract1.sol │ └── Migrations.sol ├── migrations │ ├── 1_initial_migration.js │ └── 2_custom.js ├── test │ └── TestContract1.sol ├── truffle-config.js └── truffle.js ├── 06_functions_2 ├── README.md ├── contracts │ ├── Contract1.sol │ └── Migrations.sol ├── migrations │ ├── 1_initial_migration.js │ └── 2_custom.js ├── test │ └── TestContract1.sol ├── truffle-config.js └── truffle.js ├── 07_functions_3 ├── README.md ├── contracts │ ├── Contract1.sol │ └── Migrations.sol ├── migrations │ ├── 1_initial_migration.js │ └── 2_custom.js ├── test │ └── TestContract1.sol ├── truffle-config.js └── truffle.js ├── 08_functions_4 ├── README.md ├── contracts │ ├── Contract1.sol │ └── Migrations.sol ├── migrations │ ├── 1_initial_migration.js │ └── 2_custom.js ├── test │ └── TestContract1.sol ├── truffle-config.js └── truffle.js ├── 09_value_datatypes ├── README.md ├── contracts │ ├── Contract1.sol │ └── Migrations.sol ├── migrations │ ├── 1_initial_migration.js │ └── 2_custom.js ├── test │ └── TestContract1.sol ├── truffle-config.js └── truffle.js ├── 10_ref_datatypes ├── README.md ├── contracts │ ├── Contract1.sol │ └── Migrations.sol ├── migrations │ ├── 1_initial_migration.js │ └── 2_custom.js ├── test │ └── TestContract1.sol ├── truffle-config.js └── truffle.js ├── 11_rules_of_datatypes ├── README.md ├── contracts │ ├── Contract1.sol │ └── Migrations.sol ├── migrations │ ├── 1_initial_migration.js │ └── 2_custom.js ├── test │ └── TestContract1.sol ├── truffle-config.js └── truffle.js ├── 12_integer_values ├── README.md ├── contracts │ ├── Contract1.sol │ └── Migrations.sol ├── migrations │ ├── 1_initial_migration.js │ └── 2_custom.js ├── test │ └── TestContract1.sol ├── truffle-config.js └── truffle.js ├── 13_bytes_values ├── README.md ├── contracts │ ├── Contract1.sol │ └── Migrations.sol ├── migrations │ ├── 1_initial_migration.js │ └── 2_custom.js ├── test │ └── TestContract1.sol ├── truffle-config.js └── truffle.js ├── 14_fixed_size_arrays ├── README.md ├── contracts │ ├── Contract1.sol │ └── Migrations.sol ├── migrations │ ├── 1_initial_migration.js │ └── 2_custom.js ├── test │ └── TestContract1.sol ├── truffle-config.js └── truffle.js ├── 15_dynamic_arrays ├── README.md ├── contracts │ ├── Contract1.sol │ └── Migrations.sol ├── migrations │ ├── 1_initial_migration.js │ └── 2_custom.js ├── test │ └── TestContract1.sol ├── truffle-config.js └── truffle.js ├── 16_bytes_strings_arrays ├── README.md ├── contracts │ ├── Contract1.sol │ └── Migrations.sol ├── migrations │ ├── 1_initial_migration.js │ └── 2_custom.js ├── test │ └── TestContract1.sol ├── truffle-config.js └── truffle.js ├── 17_address ├── README.md ├── contracts │ ├── Contract1.sol │ └── Migrations.sol ├── migrations │ ├── 1_initial_migration.js │ └── 2_custom.js ├── test │ └── TestContract1.sol ├── truffle-config.js └── truffle.js └── README.md /01_helloworld/README.md: -------------------------------------------------------------------------------- 1 | # Hello World -------------------------------------------------------------------------------- /01_helloworld/contracts/HelloWorld.sol: -------------------------------------------------------------------------------- 1 | pragma solidity ^0.4.23; 2 | contract HelloWorld { 3 | 4 | function welcome() public returns (string) { 5 | return "Hello World"; 6 | } 7 | } -------------------------------------------------------------------------------- /01_helloworld/contracts/Migrations.sol: -------------------------------------------------------------------------------- 1 | pragma solidity ^0.4.23; 2 | 3 | contract Migrations { 4 | address public owner; 5 | uint public last_completed_migration; 6 | 7 | 8 | constructor() public { 9 | owner = msg.sender; 10 | } 11 | 12 | modifier restricted() { 13 | if (msg.sender == owner) _; 14 | } 15 | 16 | function setCompleted(uint completed) public restricted { 17 | last_completed_migration = completed; 18 | } 19 | 20 | function upgrade(address new_address) public restricted { 21 | Migrations upgraded = Migrations(new_address); 22 | upgraded.setCompleted(last_completed_migration); 23 | } 24 | } 25 | -------------------------------------------------------------------------------- /01_helloworld/migrations/1_initial_migration.js: -------------------------------------------------------------------------------- 1 | var Migrations = artifacts.require("./Migrations.sol"); 2 | 3 | module.exports = function(deployer) { 4 | deployer.deploy(Migrations); 5 | }; 6 | -------------------------------------------------------------------------------- /01_helloworld/migrations/2_custom.js: -------------------------------------------------------------------------------- 1 | var HelloWorld = artifacts.require("./HelloWorld.sol"); 2 | 3 | module.exports = function(deployer) { 4 | deployer.deploy(HelloWorld); 5 | }; -------------------------------------------------------------------------------- /01_helloworld/test/TestHelloWorld.sol: -------------------------------------------------------------------------------- 1 | pragma solidity ^0.4.19; 2 | import "truffle/Assert.sol"; 3 | import "truffle/DeployedAddresses.sol"; 4 | import "../contracts/HelloWorld.sol"; 5 | 6 | contract TestHelloWorld { 7 | function testHelloWorld() public { 8 | HelloWorld meta = HelloWorld(DeployedAddresses.HelloWorld()); 9 | Assert.equal(meta.welcome(),"Hello World","Hello World String value test"); 10 | } 11 | } -------------------------------------------------------------------------------- /01_helloworld/truffle-config.js: -------------------------------------------------------------------------------- 1 | module.exports = { 2 | // See 3 | // to customize your Truffle configuration! 4 | }; 5 | -------------------------------------------------------------------------------- /01_helloworld/truffle.js: -------------------------------------------------------------------------------- 1 | module.exports = { 2 | // See 3 | // to customize your Truffle configuration! 4 | networks: { 5 | development: { 6 | host: "127.0.0.1", 7 | port: 8545, 8 | network_id: "*" // Match any network id 9 | } 10 | } 11 | }; 12 | -------------------------------------------------------------------------------- /02_state_variables/README.md: -------------------------------------------------------------------------------- 1 | # State Variables 2 | -------------------------------------------------------------------------------- /02_state_variables/contracts/Migrations.sol: -------------------------------------------------------------------------------- 1 | pragma solidity ^0.4.23; 2 | 3 | contract Migrations { 4 | address public owner; 5 | uint public last_completed_migration; 6 | 7 | 8 | constructor() public { 9 | owner = msg.sender; 10 | } 11 | 12 | modifier restricted() { 13 | if (msg.sender == owner) _; 14 | } 15 | 16 | function setCompleted(uint completed) public restricted { 17 | last_completed_migration = completed; 18 | } 19 | 20 | function upgrade(address new_address) public restricted { 21 | Migrations upgraded = Migrations(new_address); 22 | upgraded.setCompleted(last_completed_migration); 23 | } 24 | } 25 | -------------------------------------------------------------------------------- /02_state_variables/contracts/StateVariables.sol: -------------------------------------------------------------------------------- 1 | pragma solidity ^0.4.23; 2 | contract StateVariables { 3 | 4 | //1) Variables declared in a contract that are not within any function are called state variables 5 | //2) State variables store the current values of the contract. 6 | //3) The allocated memory for a state variable is statically assigned and it cannot change during the lifetime of the contract. 7 | //4) Each state variable has a type that must be defined statically. 8 | //5) The Solidity compiler must ascertain the memory allocation details for each state variables and so the state variable data type must be declared. 9 | 10 | // State Variables 11 | int age = 40; // by default its qualifiers is 'internal' 12 | int private privateAge = 40; 13 | int public publicAge = 40; 14 | int constant CONSTANT_AGE = 40; // by default its qualifiers is 'internal' 15 | int public constant CONSTANT_AGE_2 = 40; 16 | int constant public CONSTANT_AGE_3 = 40; 17 | 18 | function getInternalAge() public returns (int) { 19 | return age; 20 | } 21 | function getPrivateAge() public returns (int) { 22 | return privateAge; 23 | } 24 | function getPublicAge() public returns (int) { 25 | return publicAge; 26 | } 27 | function getConstantAge() public returns (int) { 28 | return CONSTANT_AGE; 29 | } 30 | function getConstantAge2() public returns (int) { 31 | return CONSTANT_AGE_2; 32 | } 33 | function getConstantAge3() public returns (int) { 34 | return CONSTANT_AGE_3; 35 | } 36 | } -------------------------------------------------------------------------------- /02_state_variables/migrations/1_initial_migration.js: -------------------------------------------------------------------------------- 1 | var Migrations = artifacts.require("./Migrations.sol"); 2 | 3 | module.exports = function(deployer) { 4 | deployer.deploy(Migrations); 5 | }; 6 | -------------------------------------------------------------------------------- /02_state_variables/migrations/2_custom.js: -------------------------------------------------------------------------------- 1 | var contract1 = artifacts.require("./StateVariables.sol"); 2 | 3 | module.exports = function(deployer) { 4 | deployer.deploy(contract1); 5 | }; -------------------------------------------------------------------------------- /02_state_variables/test/TestStateVariables.sol: -------------------------------------------------------------------------------- 1 | pragma solidity ^0.4.19; 2 | import "truffle/Assert.sol"; 3 | import "truffle/DeployedAddresses.sol"; 4 | import "../contracts/StateVariables.sol"; 5 | 6 | contract TestStateVariables { 7 | 8 | function testGetInternalAge() public { 9 | StateVariables meta = StateVariables(DeployedAddresses.StateVariables()); 10 | Assert.equal(meta.getInternalAge(),40,"Testing State Variables should be 40"); 11 | 12 | // meta.age is not visible outside of contract 13 | // Uncomment below line and it will give error 14 | //Assert.equal(meta.age,40,"Testing State Variables should be 40"); 15 | //Assert.equal(meta.age(),40,"Testing State Variables should be 40"); 16 | } 17 | 18 | function testGetPrivateAge() public { 19 | StateVariables meta = StateVariables(DeployedAddresses.StateVariables()); 20 | Assert.equal(meta.getPrivateAge(),40,"Testing Private Age State Variables should be 40"); 21 | 22 | // Not visible 23 | //Assert.equal(meta.privateAge(),40,"Testing State Variables direct access"); 24 | } 25 | 26 | function testGetPublicAge() public { 27 | StateVariables meta = StateVariables(DeployedAddresses.StateVariables()); 28 | Assert.equal(meta.getPublicAge(),40,"Testing Public age State Variables should be 40"); 29 | 30 | // meta.publicAge IS visible outside of contract 31 | // Solidity Provides function with the name of public variable to access its value 32 | Assert.equal(meta.publicAge(),40,"Testing State Variables direct access should be 40"); 33 | 34 | // This will not work 35 | //Assert.equal(meta.publicAge,40,"Testing State Variables direct access"); 36 | } 37 | 38 | function testGetConstantAge() public { 39 | StateVariables meta = StateVariables(DeployedAddresses.StateVariables()); 40 | Assert.equal(meta.getConstantAge(),40,"Testing Constant Age State Variables should be 40"); 41 | 42 | // Will not work as Contant age is internal 43 | //Assert.equal(meta.CONSTANT_AGE(),40,"Testing Constant Age State Variables"); 44 | } 45 | 46 | function testGetConstantAge2() public { 47 | StateVariables meta = StateVariables(DeployedAddresses.StateVariables()); 48 | Assert.equal(meta.getConstantAge2(),40,"Testing public Constant Age State Variables should be 40"); 49 | Assert.equal(meta.CONSTANT_AGE_2(),40,"Testing public Constant Age State Variables should be 40"); 50 | } 51 | 52 | function testGetConstantAge3() public { 53 | StateVariables meta = StateVariables(DeployedAddresses.StateVariables()); 54 | Assert.equal(meta.getConstantAge3(),40,"Testing Constant public Age State Variables should be 40"); 55 | Assert.equal(meta.CONSTANT_AGE_3(),40,"Testing Constant public Age State Variables should be 40"); 56 | 57 | } 58 | } -------------------------------------------------------------------------------- /02_state_variables/truffle-config.js: -------------------------------------------------------------------------------- 1 | module.exports = { 2 | // See 3 | // to customize your Truffle configuration! 4 | }; 5 | -------------------------------------------------------------------------------- /02_state_variables/truffle.js: -------------------------------------------------------------------------------- 1 | module.exports = { 2 | // See 3 | // to customize your Truffle configuration! 4 | networks: { 5 | development: { 6 | host: "127.0.0.1", 7 | port: 8545, 8 | network_id: "*" // Match any network id 9 | } 10 | } 11 | }; 12 | -------------------------------------------------------------------------------- /03_struct/README.md: -------------------------------------------------------------------------------- 1 | # Struct in Soldity -------------------------------------------------------------------------------- /03_struct/contracts/Contract1.sol: -------------------------------------------------------------------------------- 1 | pragma solidity ^0.4.23; 2 | contract Contract1 { 3 | 4 | struct MyStruct { 5 | string name; 6 | uint age; 7 | bool isMarried; 8 | } 9 | 10 | // Empty Struct allowed 11 | // It is deprecated 12 | struct MyStructNew { 13 | } 14 | 15 | MyStruct public myStruct = MyStruct("Test",34,false); 16 | MyStruct myStruct2 = MyStruct("Test2",44,true); 17 | MyStruct myStruct3; 18 | 19 | MyStructNew myStructNew = MyStructNew(); 20 | 21 | 22 | // Can not return Struct from function its in experiment and 23 | // need to use below line, but also with that not working properly 24 | // pragma experimental ABIEncoderV2; 25 | /* 26 | function getMyStruct() public returns (MyStruct) { 27 | return myStruct; 28 | } 29 | */ 30 | 31 | function getNameFromStruct() public returns (string) { 32 | return myStruct.name; 33 | } 34 | 35 | function getNameFromStruct2() public returns (string) { 36 | return myStruct2.name; 37 | } 38 | 39 | function demoStruct3() public returns (string) { 40 | MyStruct memory mystr1 = MyStruct("Test3",23,false); 41 | myStruct3 = MyStruct("Test4",43,true); 42 | 43 | return mystr1.name; 44 | //return myStruct2.name; 45 | } 46 | 47 | 48 | } -------------------------------------------------------------------------------- /03_struct/contracts/Migrations.sol: -------------------------------------------------------------------------------- 1 | pragma solidity ^0.4.23; 2 | 3 | contract Migrations { 4 | address public owner; 5 | uint public last_completed_migration; 6 | 7 | 8 | constructor() public { 9 | owner = msg.sender; 10 | } 11 | 12 | modifier restricted() { 13 | if (msg.sender == owner) _; 14 | } 15 | 16 | function setCompleted(uint completed) public restricted { 17 | last_completed_migration = completed; 18 | } 19 | 20 | function upgrade(address new_address) public restricted { 21 | Migrations upgraded = Migrations(new_address); 22 | upgraded.setCompleted(last_completed_migration); 23 | } 24 | } 25 | -------------------------------------------------------------------------------- /03_struct/migrations/1_initial_migration.js: -------------------------------------------------------------------------------- 1 | var Migrations = artifacts.require("./Migrations.sol"); 2 | 3 | module.exports = function(deployer) { 4 | deployer.deploy(Migrations); 5 | }; 6 | -------------------------------------------------------------------------------- /03_struct/migrations/2_custom.js: -------------------------------------------------------------------------------- 1 | var contract1 = artifacts.require("./Contract1.sol"); 2 | 3 | module.exports = function(deployer) { 4 | deployer.deploy(contract1); 5 | }; -------------------------------------------------------------------------------- /03_struct/test/TestContract1.sol: -------------------------------------------------------------------------------- 1 | pragma solidity ^0.4.19; 2 | import "truffle/Assert.sol"; 3 | import "truffle/DeployedAddresses.sol"; 4 | import "../contracts/Contract1.sol"; 5 | 6 | contract TestContract1 { 7 | 8 | function testStruct1() public { 9 | Contract1 meta = Contract1(DeployedAddresses.Contract1()); 10 | 11 | Assert.equal(meta.getNameFromStruct(),"Test","meta.getNameFromStruct() should return 'Test'"); 12 | 13 | // Can not access Struct, if access need to store in variable 14 | // but varaible to MyStruct type is not allowed here 15 | // May be it worked with library, not sure 16 | //MyStruct a = meta.myStruct(); 17 | 18 | // This is also not allowed 19 | //Conract1.MyStruct a = meta.myStruct(); 20 | 21 | // myStruct varaible is public in Contract 22 | // Can not access name property like this 23 | //Assert.equal(meta.myStruct().name,"Test","Testing Struct values"); 24 | 25 | // Can not access name property like this too 26 | //Assert.equal(meta.myStruct().name(),"Test","Testing Struct values"); 27 | 28 | } 29 | 30 | function testStruct2() public { 31 | Contract1 meta = Contract1(DeployedAddresses.Contract1()); 32 | 33 | Assert.equal(meta.demoStruct3(),"Test3","meta.demoStruct3() should return 'Test3'"); 34 | 35 | } 36 | 37 | } -------------------------------------------------------------------------------- /03_struct/truffle-config.js: -------------------------------------------------------------------------------- 1 | module.exports = { 2 | // See 3 | // to customize your Truffle configuration! 4 | }; 5 | -------------------------------------------------------------------------------- /03_struct/truffle.js: -------------------------------------------------------------------------------- 1 | module.exports = { 2 | // See 3 | // to customize your Truffle configuration! 4 | networks: { 5 | development: { 6 | host: "127.0.0.1", 7 | port: 8545, 8 | network_id: "*" // Match any network id 9 | } 10 | } 11 | }; 12 | -------------------------------------------------------------------------------- /04_enum/README.md: -------------------------------------------------------------------------------- 1 | # Enum -------------------------------------------------------------------------------- /04_enum/contracts/Contract1.sol: -------------------------------------------------------------------------------- 1 | pragma solidity ^0.4.23; 2 | contract Contract1 { 3 | 4 | enum Languages { 5 | java, 6 | javascript, 7 | solidity, 8 | python 9 | } 10 | 11 | Languages languages; 12 | 13 | function getLanguage() public returns (int) { 14 | return int(Languages.solidity); 15 | } 16 | 17 | function getLanguage2() public returns (int) { 18 | Languages language = Languages.python; 19 | return int(language); 20 | } 21 | 22 | function setLanguage(int lang) public { 23 | languages = Languages(lang); 24 | } 25 | 26 | function getLanguageStateVariable() public returns (Languages) { 27 | return languages; 28 | } 29 | 30 | 31 | 32 | 33 | 34 | } -------------------------------------------------------------------------------- /04_enum/contracts/Migrations.sol: -------------------------------------------------------------------------------- 1 | pragma solidity ^0.4.23; 2 | 3 | contract Migrations { 4 | address public owner; 5 | uint public last_completed_migration; 6 | 7 | 8 | constructor() public { 9 | owner = msg.sender; 10 | } 11 | 12 | modifier restricted() { 13 | if (msg.sender == owner) _; 14 | } 15 | 16 | function setCompleted(uint completed) public restricted { 17 | last_completed_migration = completed; 18 | } 19 | 20 | function upgrade(address new_address) public restricted { 21 | Migrations upgraded = Migrations(new_address); 22 | upgraded.setCompleted(last_completed_migration); 23 | } 24 | } 25 | -------------------------------------------------------------------------------- /04_enum/migrations/1_initial_migration.js: -------------------------------------------------------------------------------- 1 | var Migrations = artifacts.require("./Migrations.sol"); 2 | 3 | module.exports = function(deployer) { 4 | deployer.deploy(Migrations); 5 | }; 6 | -------------------------------------------------------------------------------- /04_enum/migrations/2_custom.js: -------------------------------------------------------------------------------- 1 | var contract1 = artifacts.require("./Contract1.sol"); 2 | 3 | module.exports = function(deployer) { 4 | deployer.deploy(contract1); 5 | }; -------------------------------------------------------------------------------- /04_enum/test/TestContract1.sol: -------------------------------------------------------------------------------- 1 | pragma solidity ^0.4.19; 2 | import "truffle/Assert.sol"; 3 | import "truffle/DeployedAddresses.sol"; 4 | import "../contracts/Contract1.sol"; 5 | 6 | contract TestContract1 { 7 | 8 | function testUsingDeployedContract() public { 9 | Contract1 meta = Contract1(DeployedAddresses.Contract1()); 10 | Assert.equal(meta.getLanguage(),2,"Testing Enum, value should be 2"); 11 | } 12 | 13 | function testUsingDeployedContract2() public { 14 | Contract1 meta = Contract1(DeployedAddresses.Contract1()); 15 | Assert.equal(meta.getLanguage2(),3,"Testing Enum, value should be 3"); 16 | } 17 | 18 | 19 | function testLanguageSetAndGet() public { 20 | Contract1 meta = Contract1(DeployedAddresses.Contract1()); 21 | meta.setLanguage(1); 22 | 23 | // Not allowed 24 | //Languages lang = meta.getLanguageStateVariable(); 25 | 26 | Assert.equal(int(meta.getLanguageStateVariable()),1,"Testing Enum, value should be 1"); 27 | } 28 | 29 | } -------------------------------------------------------------------------------- /04_enum/truffle-config.js: -------------------------------------------------------------------------------- 1 | module.exports = { 2 | // See 3 | // to customize your Truffle configuration! 4 | }; 5 | -------------------------------------------------------------------------------- /04_enum/truffle.js: -------------------------------------------------------------------------------- 1 | module.exports = { 2 | // See 3 | // to customize your Truffle configuration! 4 | networks: { 5 | development: { 6 | host: "127.0.0.1", 7 | port: 8545, 8 | network_id: "*" // Match any network id 9 | } 10 | } 11 | }; 12 | -------------------------------------------------------------------------------- /05_functions/README.md: -------------------------------------------------------------------------------- 1 | # Basic functions with qualifiers -------------------------------------------------------------------------------- /05_functions/contracts/Contract1.sol: -------------------------------------------------------------------------------- 1 | pragma solidity ^0.4.23; 2 | contract Contract1 { 3 | 4 | int public age; 5 | event Log(string message); 6 | 7 | // Default visibility is public 8 | function setAge(int a) { 9 | age = a; 10 | } 11 | 12 | function setAge2(int a) public { 13 | age = a; 14 | } 15 | 16 | function doSomeWork() internal returns (string) { 17 | return "Do Some Work Internal"; 18 | } 19 | 20 | function doSomeWork2() private returns (string) { 21 | return "Do Some Work Private"; 22 | } 23 | 24 | function doSomeWork3() external returns (string) { 25 | return "Do Some Work External"; 26 | } 27 | 28 | function doSomeWork4() public returns (string) { 29 | 30 | // This line will give compilation error because 31 | // external functions can only be called from outside 32 | // Uncomment this and it will give compile time error 33 | //doSomeWork3(); 34 | 35 | return "Do Some Work public"; 36 | } 37 | } -------------------------------------------------------------------------------- /05_functions/contracts/Migrations.sol: -------------------------------------------------------------------------------- 1 | pragma solidity ^0.4.23; 2 | 3 | contract Migrations { 4 | address public owner; 5 | uint public last_completed_migration; 6 | 7 | 8 | constructor() public { 9 | owner = msg.sender; 10 | } 11 | 12 | modifier restricted() { 13 | if (msg.sender == owner) _; 14 | } 15 | 16 | function setCompleted(uint completed) public restricted { 17 | last_completed_migration = completed; 18 | } 19 | 20 | function upgrade(address new_address) public restricted { 21 | Migrations upgraded = Migrations(new_address); 22 | upgraded.setCompleted(last_completed_migration); 23 | } 24 | } 25 | -------------------------------------------------------------------------------- /05_functions/migrations/1_initial_migration.js: -------------------------------------------------------------------------------- 1 | var Migrations = artifacts.require("./Migrations.sol"); 2 | 3 | module.exports = function(deployer) { 4 | deployer.deploy(Migrations); 5 | }; 6 | -------------------------------------------------------------------------------- /05_functions/migrations/2_custom.js: -------------------------------------------------------------------------------- 1 | var contract1 = artifacts.require("./Contract1.sol"); 2 | 3 | module.exports = function(deployer) { 4 | deployer.deploy(contract1); 5 | }; -------------------------------------------------------------------------------- /05_functions/test/TestContract1.sol: -------------------------------------------------------------------------------- 1 | pragma solidity ^0.4.19; 2 | import "truffle/Assert.sol"; 3 | import "truffle/DeployedAddresses.sol"; 4 | import "../contracts/Contract1.sol"; 5 | 6 | contract TestContract1 { 7 | 8 | 9 | 10 | function testPublicFunction() public { 11 | Contract1 meta = Contract1(DeployedAddresses.Contract1()); 12 | meta.setAge(5); 13 | meta.setAge2(44); 14 | } 15 | 16 | // internal and private can not be called from outside 17 | function testInternalAndPrivateFunction() public { 18 | Contract1 meta = Contract1(DeployedAddresses.Contract1()); 19 | 20 | // Will not work because qualifier is internal 21 | //meta.doSomeWork(); 22 | 23 | // Will not work because qualifier is private 24 | //meta.doSomeWork2(); 25 | } 26 | 27 | function testExternalFunction() public { 28 | Contract1 meta = Contract1(DeployedAddresses.Contract1()); 29 | Assert.equal(meta.doSomeWork3(),"Do Some Work External","Testing External function, value should be 'Do Some Work External'"); 30 | } 31 | 32 | function testExternalInsidePublicFunction() public { 33 | Contract1 meta = Contract1(DeployedAddresses.Contract1()); 34 | Assert.equal(meta.doSomeWork4(),"Do Some Work public","Testing External inside public function, value should be 'Do Some Work public'"); 35 | } 36 | 37 | 38 | } -------------------------------------------------------------------------------- /05_functions/truffle-config.js: -------------------------------------------------------------------------------- 1 | module.exports = { 2 | // See 3 | // to customize your Truffle configuration! 4 | }; 5 | -------------------------------------------------------------------------------- /05_functions/truffle.js: -------------------------------------------------------------------------------- 1 | module.exports = { 2 | // See 3 | // to customize your Truffle configuration! 4 | networks: { 5 | development: { 6 | host: "127.0.0.1", 7 | port: 8545, 8 | network_id: "*" // Match any network id 9 | } 10 | } 11 | }; 12 | -------------------------------------------------------------------------------- /06_functions_2/README.md: -------------------------------------------------------------------------------- 1 | # Functions -------------------------------------------------------------------------------- /06_functions_2/contracts/Contract1.sol: -------------------------------------------------------------------------------- 1 | pragma solidity ^0.4.23; 2 | contract Contract1 { 3 | 4 | int public age; 5 | 6 | function setAge(int a) public constant { 7 | // Changing state variable in constant function is not allowed 8 | // This will raise warning but no error, this is not 9 | // strictly enforced yet 10 | age = a; 11 | } 12 | 13 | 14 | function doSomeWork() public constant returns (int) { 15 | // No warning or error works fine 16 | // Accessing state variable in local and returning from 17 | // function 18 | int b = age; 19 | return b; 20 | } 21 | 22 | 23 | function doSomeWork2() public constant returns (int) { 24 | // No warning or error works fine 25 | // Returning state variable from function 26 | return age; 27 | } 28 | 29 | function doSomeWork3() public view returns (int) { 30 | // Not allowed to change state variables, but shows only warning 31 | age = 10; 32 | return age; 33 | } 34 | 35 | // pure function 36 | function doSomeWork4(int b) public pure returns (int) { 37 | // Compilation error, on both changing state variable or access 38 | // state variable 39 | 40 | // access or assignment not allowed 41 | //age = b; // not allowed, uncoment and try 42 | //b = age; // not allowed, uncoment and try 43 | return b; 44 | } 45 | 46 | // Returning multiple values from function 47 | function doSomeWork6(int c) public returns (int,string) { 48 | return (5,"hello"); 49 | } 50 | 51 | } -------------------------------------------------------------------------------- /06_functions_2/contracts/Migrations.sol: -------------------------------------------------------------------------------- 1 | pragma solidity ^0.4.23; 2 | 3 | contract Migrations { 4 | address public owner; 5 | uint public last_completed_migration; 6 | 7 | 8 | constructor() public { 9 | owner = msg.sender; 10 | } 11 | 12 | modifier restricted() { 13 | if (msg.sender == owner) _; 14 | } 15 | 16 | function setCompleted(uint completed) public restricted { 17 | last_completed_migration = completed; 18 | } 19 | 20 | function upgrade(address new_address) public restricted { 21 | Migrations upgraded = Migrations(new_address); 22 | upgraded.setCompleted(last_completed_migration); 23 | } 24 | } 25 | -------------------------------------------------------------------------------- /06_functions_2/migrations/1_initial_migration.js: -------------------------------------------------------------------------------- 1 | var Migrations = artifacts.require("./Migrations.sol"); 2 | 3 | module.exports = function(deployer) { 4 | deployer.deploy(Migrations); 5 | }; 6 | -------------------------------------------------------------------------------- /06_functions_2/migrations/2_custom.js: -------------------------------------------------------------------------------- 1 | var contract1 = artifacts.require("./Contract1.sol"); 2 | 3 | module.exports = function(deployer) { 4 | deployer.deploy(contract1); 5 | }; -------------------------------------------------------------------------------- /06_functions_2/test/TestContract1.sol: -------------------------------------------------------------------------------- 1 | pragma solidity ^0.4.19; 2 | import "truffle/Assert.sol"; 3 | import "truffle/DeployedAddresses.sol"; 4 | import "../contracts/Contract1.sol"; 5 | 6 | contract TestContract1 { 7 | 8 | function testConstantFunction() public { 9 | Contract1 meta = Contract1(DeployedAddresses.Contract1()); 10 | meta.setAge(5); 11 | } 12 | 13 | function testConstantFunction2() public { 14 | Contract1 meta = Contract1(DeployedAddresses.Contract1()); 15 | 16 | Assert.equal(meta.doSomeWork(),5,"Testing constant function, value should be 5"); 17 | Assert.equal(meta.doSomeWork2(),5,"Testing constant function, value should be 5"); 18 | } 19 | 20 | function testViewFunction() public { 21 | Contract1 meta = Contract1(DeployedAddresses.Contract1()); 22 | 23 | Assert.equal(meta.doSomeWork3(),10,"Testing view function, value should be 5"); 24 | } 25 | 26 | function testPureFunction() public { 27 | Contract1 meta = Contract1(DeployedAddresses.Contract1()); 28 | 29 | Assert.equal(meta.doSomeWork4(3),3,"Testing pure function, value should be 5"); 30 | } 31 | 32 | function testMultipleReturnFunction() public { 33 | Contract1 meta = Contract1(DeployedAddresses.Contract1()); 34 | // both of the below lines will work 35 | //var (a, b) = meta.doSomeWork6(3); 36 | //(int a, int b) = (5,6); 37 | (int a, string memory b)= meta.doSomeWork6(3); 38 | Assert.equal(a,5,"Testing multiple return funciton, A should be 5"); 39 | Assert.equal(b,"hello","Testing multiple return funciton, B should be hello"); 40 | } 41 | } -------------------------------------------------------------------------------- /06_functions_2/truffle-config.js: -------------------------------------------------------------------------------- 1 | module.exports = { 2 | // See 3 | // to customize your Truffle configuration! 4 | }; 5 | -------------------------------------------------------------------------------- /06_functions_2/truffle.js: -------------------------------------------------------------------------------- 1 | module.exports = { 2 | // See 3 | // to customize your Truffle configuration! 4 | networks: { 5 | development: { 6 | host: "127.0.0.1", 7 | port: 8545, 8 | network_id: "*" // Match any network id 9 | } 10 | } 11 | }; 12 | -------------------------------------------------------------------------------- /07_functions_3/README.md: -------------------------------------------------------------------------------- 1 | # Paybale Functions 2 | 3 | https://medium.com/coinmonks/3-things-i-learned-this-week-using-solidity-truffle-and-web3-a911c3adc730 4 | 5 | https://michalzalecki.com/ethereum-test-driven-introduction-to-solidity/ -------------------------------------------------------------------------------- /07_functions_3/contracts/Contract1.sol: -------------------------------------------------------------------------------- 1 | pragma solidity ^0.4.23; 2 | contract Contract1 { 3 | 4 | uint public myBalance; 5 | 6 | function doAcceptEther() public payable { 7 | myBalance += msg.value; 8 | } 9 | 10 | } -------------------------------------------------------------------------------- /07_functions_3/contracts/Migrations.sol: -------------------------------------------------------------------------------- 1 | pragma solidity ^0.4.23; 2 | 3 | contract Migrations { 4 | address public owner; 5 | uint public last_completed_migration; 6 | 7 | 8 | constructor() public { 9 | owner = msg.sender; 10 | } 11 | 12 | modifier restricted() { 13 | if (msg.sender == owner) _; 14 | } 15 | 16 | function setCompleted(uint completed) public restricted { 17 | last_completed_migration = completed; 18 | } 19 | 20 | function upgrade(address new_address) public restricted { 21 | Migrations upgraded = Migrations(new_address); 22 | upgraded.setCompleted(last_completed_migration); 23 | } 24 | } 25 | -------------------------------------------------------------------------------- /07_functions_3/migrations/1_initial_migration.js: -------------------------------------------------------------------------------- 1 | var Migrations = artifacts.require("./Migrations.sol"); 2 | 3 | module.exports = function(deployer) { 4 | deployer.deploy(Migrations); 5 | }; 6 | -------------------------------------------------------------------------------- /07_functions_3/migrations/2_custom.js: -------------------------------------------------------------------------------- 1 | var contract1 = artifacts.require("./Contract1.sol"); 2 | 3 | module.exports = function(deployer) { 4 | deployer.deploy(contract1); 5 | }; -------------------------------------------------------------------------------- /07_functions_3/test/TestContract1.sol: -------------------------------------------------------------------------------- 1 | pragma solidity ^0.4.24; 2 | import "truffle/Assert.sol"; 3 | import "truffle/DeployedAddresses.sol"; 4 | import "../contracts/Contract1.sol"; 5 | 6 | contract TestContract1 { 7 | 8 | //This will set balance of this Test Contract to 50 ether 9 | uint public initialBalance = 50 ether; 10 | 11 | // Test payable function 12 | function testBalance() public { 13 | Contract1 meta = Contract1(DeployedAddresses.Contract1()); 14 | 15 | // This way of getting balance is depricated new way is 16 | // address(this).balance 17 | uint val = this.balance; 18 | 19 | // If we use direct literal value in both first and second argument 20 | // then it will not compile 21 | //Assert.equal(5,5,"Testing view function"); 22 | 23 | Assert.equal(val,50 ether,"Test balance of Test Contract, value should be 50 ether"); 24 | } 25 | 26 | 27 | function testBalance2() public { 28 | Contract1 meta = Contract1(DeployedAddresses.Contract1()); 29 | uint val = address(this).balance; 30 | 31 | Assert.equal(val,50 ether,"Test balance of Test Contract, value should be 50 ether"); 32 | } 33 | 34 | // Test payable function 35 | function testPaybleFunction() public { 36 | Contract1 meta = Contract1(DeployedAddresses.Contract1()); 37 | meta.doAcceptEther.value(20 wei)(); 38 | 39 | Assert.equal(address(meta).balance, 20 wei, "Test paybale function, contract balance should be 20 wei"); 40 | Assert.equal(meta.myBalance(), 20 wei, "Test paybale function, contract balance should be 20 wei"); 41 | } 42 | } -------------------------------------------------------------------------------- /07_functions_3/truffle-config.js: -------------------------------------------------------------------------------- 1 | module.exports = { 2 | // See 3 | // to customize your Truffle configuration! 4 | }; 5 | -------------------------------------------------------------------------------- /07_functions_3/truffle.js: -------------------------------------------------------------------------------- 1 | module.exports = { 2 | // See 3 | // to customize your Truffle configuration! 4 | networks: { 5 | development: { 6 | host: "127.0.0.1", 7 | port: 8545, 8 | network_id: "*" // Match any network id 9 | } 10 | } 11 | }; 12 | -------------------------------------------------------------------------------- /08_functions_4/README.md: -------------------------------------------------------------------------------- 1 | #IN COMPLETE 2 | 3 | # Default Payable Function 4 | 5 | https://medium.com/coinmonks/3-things-i-learned-this-week-using-solidity-truffle-and-web3-a911c3adc730 6 | 7 | https://michalzalecki.com/ethereum-test-driven-introduction-to-solidity/ -------------------------------------------------------------------------------- /08_functions_4/contracts/Contract1.sol: -------------------------------------------------------------------------------- 1 | pragma solidity ^0.4.23; 2 | contract Contract1 { 3 | 4 | uint public mybalance; 5 | 6 | function doAcceptEther() public payable { 7 | mybalance += msg.value; 8 | } 9 | 10 | function () public payable { 11 | mybalance += msg.value; 12 | } 13 | 14 | } -------------------------------------------------------------------------------- /08_functions_4/contracts/Migrations.sol: -------------------------------------------------------------------------------- 1 | pragma solidity ^0.4.23; 2 | 3 | contract Migrations { 4 | address public owner; 5 | uint public last_completed_migration; 6 | 7 | 8 | constructor() public { 9 | owner = msg.sender; 10 | } 11 | 12 | modifier restricted() { 13 | if (msg.sender == owner) _; 14 | } 15 | 16 | function setCompleted(uint completed) public restricted { 17 | last_completed_migration = completed; 18 | } 19 | 20 | function upgrade(address new_address) public restricted { 21 | Migrations upgraded = Migrations(new_address); 22 | upgraded.setCompleted(last_completed_migration); 23 | } 24 | } 25 | -------------------------------------------------------------------------------- /08_functions_4/migrations/1_initial_migration.js: -------------------------------------------------------------------------------- 1 | var Migrations = artifacts.require("./Migrations.sol"); 2 | 3 | module.exports = function(deployer) { 4 | deployer.deploy(Migrations); 5 | }; 6 | -------------------------------------------------------------------------------- /08_functions_4/migrations/2_custom.js: -------------------------------------------------------------------------------- 1 | var contract1 = artifacts.require("./Contract1.sol"); 2 | 3 | module.exports = function(deployer) { 4 | deployer.deploy(contract1); 5 | }; -------------------------------------------------------------------------------- /08_functions_4/test/TestContract1.sol: -------------------------------------------------------------------------------- 1 | pragma solidity ^0.4.24; 2 | import "truffle/Assert.sol"; 3 | import "truffle/DeployedAddresses.sol"; 4 | import "../contracts/Contract1.sol"; 5 | 6 | contract TestContract1 { 7 | 8 | //This will set balance of this Test Contract to 10 ether 9 | uint public initialBalance = 50 ether; 10 | 11 | // Test payable function 12 | function testTransferFunction() public { 13 | Contract1 meta = Contract1(DeployedAddresses.Contract1()); 14 | address(meta).transfer(50 wei); 15 | 16 | Assert.equal(address(meta).balance,50 wei,"Test paybale function"); 17 | Assert.equal(meta.mybalance(),50 wei,"Test paybale function"); 18 | } 19 | 20 | } -------------------------------------------------------------------------------- /08_functions_4/truffle-config.js: -------------------------------------------------------------------------------- 1 | module.exports = { 2 | // See 3 | // to customize your Truffle configuration! 4 | }; 5 | -------------------------------------------------------------------------------- /08_functions_4/truffle.js: -------------------------------------------------------------------------------- 1 | module.exports = { 2 | // See 3 | // to customize your Truffle configuration! 4 | networks: { 5 | development: { 6 | host: "127.0.0.1", 7 | port: 8545, 8 | network_id: "*" // Match any network id 9 | } 10 | } 11 | }; 12 | -------------------------------------------------------------------------------- /09_value_datatypes/README.md: -------------------------------------------------------------------------------- 1 | # Value Data types 2 | 3 | Value types are types that do not take more than 32 bytes of memory in size. Solidity provides the following value types: 4 | 1) bool: The boolean value that can hold true or false as its value 5 | 2) uint: These are unsigned integers that can hold 0 and positive values only 6 | 3) int: These are signed integers that can hold both negative and positive values 7 | 4) address: This represents an address of an account on Ethereum environment 8 | 5) byte: This represents fixed sized byte array (byte1 to bytes32) 9 | 6) enum: Enumerations that can hold predefined constant values 10 | -------------------------------------------------------------------------------- /09_value_datatypes/contracts/Contract1.sol: -------------------------------------------------------------------------------- 1 | pragma solidity ^0.4.23; 2 | contract Contract1 { 3 | 4 | uint public age = 45; 5 | uint public count; 6 | 7 | // Updating Value data type state variable 8 | function updateAge() public { 9 | age = 55; 10 | } 11 | 12 | function updateAge2() public returns (uint) { 13 | return receiveAgeAndUpdate(age); 14 | } 15 | 16 | // Receive state variable in paramerter but create a separate copy of 17 | // value type data will not effect state variable 18 | function receiveAgeAndUpdate(uint a) public returns (uint) { 19 | a = 60; 20 | return a; 21 | } 22 | 23 | // assigning value of on state variable to another will create copy 24 | // so chaning the one state variable will not effect other 25 | function updateCount() public { 26 | count = age; 27 | age = 70; 28 | } 29 | 30 | } -------------------------------------------------------------------------------- /09_value_datatypes/contracts/Migrations.sol: -------------------------------------------------------------------------------- 1 | pragma solidity ^0.4.23; 2 | 3 | contract Migrations { 4 | address public owner; 5 | uint public last_completed_migration; 6 | 7 | 8 | constructor() public { 9 | owner = msg.sender; 10 | } 11 | 12 | modifier restricted() { 13 | if (msg.sender == owner) _; 14 | } 15 | 16 | function setCompleted(uint completed) public restricted { 17 | last_completed_migration = completed; 18 | } 19 | 20 | function upgrade(address new_address) public restricted { 21 | Migrations upgraded = Migrations(new_address); 22 | upgraded.setCompleted(last_completed_migration); 23 | } 24 | } 25 | -------------------------------------------------------------------------------- /09_value_datatypes/migrations/1_initial_migration.js: -------------------------------------------------------------------------------- 1 | var Migrations = artifacts.require("./Migrations.sol"); 2 | 3 | module.exports = function(deployer) { 4 | deployer.deploy(Migrations); 5 | }; 6 | -------------------------------------------------------------------------------- /09_value_datatypes/migrations/2_custom.js: -------------------------------------------------------------------------------- 1 | var contract1 = artifacts.require("./Contract1.sol"); 2 | 3 | module.exports = function(deployer) { 4 | deployer.deploy(contract1); 5 | }; -------------------------------------------------------------------------------- /09_value_datatypes/test/TestContract1.sol: -------------------------------------------------------------------------------- 1 | pragma solidity ^0.4.24; 2 | import "truffle/Assert.sol"; 3 | import "truffle/DeployedAddresses.sol"; 4 | import "../contracts/Contract1.sol"; 5 | 6 | contract TestContract1 { 7 | 8 | function testUpdateAge() public { 9 | Contract1 meta = Contract1(DeployedAddresses.Contract1()); 10 | // Value should be 45 before update and 55 after updated 11 | Assert.equal(meta.age(),45,"Age should be 45"); 12 | meta.updateAge(); 13 | Assert.equal(meta.age(),55,"Age should be 55"); 14 | } 15 | 16 | 17 | function testUpdateAge2() public { 18 | Contract1 meta = Contract1(DeployedAddresses.Contract1()); 19 | uint updatedAge = meta.updateAge2(); 20 | // After update state variable remains same 21 | Assert.equal(meta.age(),55,"Age should be 55"); 22 | Assert.equal(updatedAge,60,"Updated Age should be 60"); 23 | } 24 | 25 | function testUpdateCount() public { 26 | Contract1 meta = Contract1(DeployedAddresses.Contract1()); 27 | meta.updateCount(); 28 | Assert.equal(meta.age(),70,"Age should be 70"); 29 | Assert.equal(meta.count(),55,"Count should be 55"); 30 | } 31 | 32 | function testAccessAgeAndUpdate() public { 33 | Contract1 meta = Contract1(DeployedAddresses.Contract1()); 34 | // Access State variable of contract and try to update it 35 | // it will change local variable but will not effect state variable 36 | uint age = meta.age(); 37 | age = 12; 38 | Assert.equal(age,12,"Local Age should be 12"); 39 | Assert.equal(meta.age(),70,"Age should be 70"); 40 | } 41 | 42 | } -------------------------------------------------------------------------------- /09_value_datatypes/truffle-config.js: -------------------------------------------------------------------------------- 1 | module.exports = { 2 | // See 3 | // to customize your Truffle configuration! 4 | }; 5 | -------------------------------------------------------------------------------- /09_value_datatypes/truffle.js: -------------------------------------------------------------------------------- 1 | module.exports = { 2 | // See 3 | // to customize your Truffle configuration! 4 | networks: { 5 | development: { 6 | host: "127.0.0.1", 7 | port: 8545, 8 | network_id: "*" // Match any network id 9 | } 10 | } 11 | }; 12 | -------------------------------------------------------------------------------- /10_ref_datatypes/README.md: -------------------------------------------------------------------------------- 1 | # Reference Datatype 2 | 3 | Solidity provides the following reference types: 4 | 1) Arrays: These are fixed as well as dynamic arrays. Details are given later in this chapter. 5 | 2) Structs: These are custom, user-defined structures. 6 | 3) String: This is sequence of characters. In Solidity, strings are eventually stored as bytes. Details are give later in this chapter. 7 | 4) Mappings: This is similar to a hash table or dictionary in other languages storing key-value pairs. 8 | -------------------------------------------------------------------------------- /10_ref_datatypes/contracts/Contract1.sol: -------------------------------------------------------------------------------- 1 | pragma solidity ^0.4.23; 2 | contract Contract1 { 3 | 4 | int[3] public list = [31,32,33]; 5 | string name = "Hello"; 6 | struct User { 7 | int age; 8 | string name; 9 | } 10 | 11 | // internal 12 | User myUser = User(32,"Asad"); 13 | 14 | // This will update array's particular location 15 | function updateList() public { 16 | list[1] = 45; 17 | } 18 | 19 | function updateList2() public { 20 | receiveListAndUpdate(list); 21 | } 22 | 23 | // function parameters are always memory data locations 24 | // Will not effect original variable that is passed in parameter 25 | function receiveListAndUpdate(int[3] a) public { 26 | a[2] = 55; 27 | } 28 | 29 | function updateList3() public returns (int) { 30 | // Reference types are always at storage location we need to 31 | // provide memory keywork to create local variable 32 | // And assigning state ref variable to memory ref variable will create a separate copy 33 | // changing one will not effect other 34 | int[3] memory newList = list; 35 | newList[2] = 67; 36 | return list[2]; //33 37 | } 38 | 39 | function updateList4() public returns (int) { 40 | // By default ref type's location is storage so it works as passby ref 41 | 42 | //int[3] storage newList = list; //this is same below line 43 | 44 | // Assigning Ref state (storage) variable to ref storage local variable 45 | // will not create a separate copy so changing one will effect other 46 | int[3] newList = list; 47 | newList[1] = 100; 48 | return list[1]; //100 49 | } 50 | 51 | 52 | 53 | function updateStruct() public returns (int) { 54 | // Assigning Ref state (storage) variable to ref storage local variable 55 | // will not create a separate copy so changing one will effect other 56 | User myUser2 = myUser; 57 | myUser2.age = 40; 58 | return myUser.age; // 40 59 | } 60 | 61 | function updateStruct2() public returns (int) { 62 | // And assigning state ref variable to memory ref variable will create a separate copy 63 | // changing one will not effect other 64 | myUser.age = 98; 65 | User memory myUser2 = myUser; 66 | myUser2.age = 50; 67 | return myUser.age; // 98 68 | } 69 | 70 | } -------------------------------------------------------------------------------- /10_ref_datatypes/contracts/Migrations.sol: -------------------------------------------------------------------------------- 1 | pragma solidity ^0.4.23; 2 | 3 | contract Migrations { 4 | address public owner; 5 | uint public last_completed_migration; 6 | 7 | 8 | constructor() public { 9 | owner = msg.sender; 10 | } 11 | 12 | modifier restricted() { 13 | if (msg.sender == owner) _; 14 | } 15 | 16 | function setCompleted(uint completed) public restricted { 17 | last_completed_migration = completed; 18 | } 19 | 20 | function upgrade(address new_address) public restricted { 21 | Migrations upgraded = Migrations(new_address); 22 | upgraded.setCompleted(last_completed_migration); 23 | } 24 | } 25 | -------------------------------------------------------------------------------- /10_ref_datatypes/migrations/1_initial_migration.js: -------------------------------------------------------------------------------- 1 | var Migrations = artifacts.require("./Migrations.sol"); 2 | 3 | module.exports = function(deployer) { 4 | deployer.deploy(Migrations); 5 | }; 6 | -------------------------------------------------------------------------------- /10_ref_datatypes/migrations/2_custom.js: -------------------------------------------------------------------------------- 1 | var contract1 = artifacts.require("./Contract1.sol"); 2 | 3 | module.exports = function(deployer) { 4 | deployer.deploy(contract1); 5 | }; -------------------------------------------------------------------------------- /10_ref_datatypes/test/TestContract1.sol: -------------------------------------------------------------------------------- 1 | pragma solidity ^0.4.24; 2 | import "truffle/Assert.sol"; 3 | import "truffle/DeployedAddresses.sol"; 4 | import "../contracts/Contract1.sol"; 5 | 6 | contract TestContract1 { 7 | 8 | function testUpdateList() public { 9 | Contract1 meta = Contract1(DeployedAddresses.Contract1()); 10 | meta.updateList(); 11 | Assert.equal(meta.list(0),31,"List 0 should be 31"); 12 | Assert.equal(meta.list(1),45,"List 1 should be 45"); 13 | Assert.equal(meta.list(2),33,"List 2 should be 33"); 14 | } 15 | 16 | function testUpdateList2() public { 17 | Contract1 meta = Contract1(DeployedAddresses.Contract1()); 18 | // updateList2 function pass state variable which is array to another function 19 | // but because function's parameter are always memory location so it 20 | // will create separate copy and changing the one will not effect the other 21 | meta.updateList2(); 22 | Assert.equal(meta.list(0),31,"List 0 should be 31"); 23 | Assert.equal(meta.list(1),45,"List 1 should be 45"); 24 | Assert.equal(meta.list(2),33,"List 2 should be 33"); 25 | } 26 | 27 | function testUpdateList3() public { 28 | Contract1 meta = Contract1(DeployedAddresses.Contract1()); 29 | // And assigning state ref variable to memory ref variable will create a separate copy 30 | // changing one will not effect other 31 | int val = meta.updateList3(); 32 | Assert.equal(val,33,"Val should be 33"); 33 | 34 | } 35 | 36 | function testUpdateList4() public { 37 | Contract1 meta = Contract1(DeployedAddresses.Contract1()); 38 | // Assigning Ref state (storage) variable to ref storage local variable 39 | // will not create a separate copy so changing one will effect other 40 | int val = meta.updateList4(); 41 | Assert.equal(val,100,"Val should be 100"); 42 | 43 | } 44 | 45 | function testUpdateStruct() public { 46 | Contract1 meta = Contract1(DeployedAddresses.Contract1()); 47 | // Assigning Ref state (storage) variable to ref storage local variable 48 | // will not create a separate copy so changing one will effect other 49 | int val = meta.updateStruct(); 50 | Assert.equal(val,40,"Val should be 40"); 51 | } 52 | 53 | function testUpdateStruct2() public { 54 | Contract1 meta = Contract1(DeployedAddresses.Contract1()); 55 | // And assigning state ref variable to memory ref variable will create a separate copy 56 | // changing one will not effect other 57 | int val = meta.updateStruct2(); 58 | Assert.equal(val,98,"Val should be 98"); 59 | 60 | } 61 | } -------------------------------------------------------------------------------- /10_ref_datatypes/truffle-config.js: -------------------------------------------------------------------------------- 1 | module.exports = { 2 | // See 3 | // to customize your Truffle configuration! 4 | }; 5 | -------------------------------------------------------------------------------- /10_ref_datatypes/truffle.js: -------------------------------------------------------------------------------- 1 | module.exports = { 2 | // See 3 | // to customize your Truffle configuration! 4 | networks: { 5 | development: { 6 | host: "127.0.0.1", 7 | port: 8545, 8 | network_id: "*" // Match any network id 9 | } 10 | } 11 | }; 12 | -------------------------------------------------------------------------------- /11_rules_of_datatypes/README.md: -------------------------------------------------------------------------------- 1 | # Rules of Assignment and Data Location 2 | -------------------------------------------------------------------------------- /11_rules_of_datatypes/contracts/Contract1.sol: -------------------------------------------------------------------------------- 1 | pragma solidity ^0.4.23; 2 | contract Contract1 { 3 | 4 | //Rule 1 5 | // State Variables are storage variables 6 | int public stateVar1 = 5; 7 | 8 | // Rule 2 Starts 9 | int public stateVar2 = 5; 10 | int[3] public stateRefVar3 = [int(3),4,5]; 11 | // function parameter are memory data variables 12 | function changeMemoryValueType(int a) public returns (int) { 13 | a = stateVar2; 14 | // changing a will not change stateVar2 15 | a = 6; 16 | return stateVar2; 17 | } 18 | 19 | // Rule 2 20 | // function ref parameter are also memory data variables 21 | function changeMemoryRefType(int[3] a) public returns (int) { 22 | a = stateRefVar3; 23 | a[1] = 45; 24 | return stateRefVar3[1]; 25 | } 26 | 27 | // Rule 3 Ends 28 | 29 | // Rule 3 starts 30 | int[2] public rule3StateRefVar = [int(1),2]; 31 | function demoRule3RefAndVal1 () public { 32 | int a = 5; // Default to memoery 33 | 34 | // Default to Storage but this will not work because it needs values 35 | // from storage data type which can come from state variable 36 | //int[2] arr = [int(1),2]; 37 | 38 | // b is default to storage in this case and need state variable 39 | // changs in value of b will change rule3StateRefVar 40 | // changs in value of rule3StateRefVar will change b 41 | int[2] b = rule3StateRefVar; 42 | 43 | // changs in value of d will NOT change rule3StateRefVar 44 | // changs in value of rule3StateRefVar will NOT change d 45 | int[2] memory d = rule3StateRefVar; 46 | 47 | // overriding default form memory to storage 48 | // if you want to use ref type then you have you use memory keyword 49 | int[3] memory c = [int(11),12,13]; 50 | 51 | b[1] = 5; // rule3StateRefVar[1] also changed to 5 52 | d[0] = 18; // rule3StateRefVar[0] will NOT change and stay 1 53 | } 54 | 55 | function demoRule3RefAndVal2 () public returns (string) { 56 | 57 | // Default to Storage but this will not work because it needs values 58 | // from storage data type which can come from state variable 59 | //string a = "Hello World"; 60 | 61 | // To make it work you need to provide memory keyword 62 | string memory a = "Hello World"; 63 | 64 | // Value type can not be overridden to stroage they can only be memory 65 | //int storage b = 45; 66 | 67 | return a; 68 | 69 | } 70 | 71 | mapping(int => string) keyValueList; 72 | function demoRule3RefAndVal3 () public { 73 | 74 | // local mapping variable must use state variable 75 | // it can not be declared locally 76 | mapping(int => string) keyValueListLocal = keyValueList; 77 | keyValueListLocal[23] = "Hello"; 78 | } 79 | 80 | // Rule 3 ends 81 | 82 | // Rule 4 does not have example 83 | 84 | // Rule 5 starts 85 | int public rule5StateVar1 = 10; 86 | int public rule5StateVar2 = 20; 87 | function demoStorageToStorageValueAssignment1() public returns (int) { 88 | 89 | // Assignment of value type from storage variable to another storage 90 | // variable creates copy so both maintain separate copy of data 91 | rule5StateVar1 = rule5StateVar2; 92 | rule5StateVar2 = 60; 93 | 94 | return rule5StateVar1; 95 | } 96 | 97 | int[2] rule5StateVar3 = [int(1),2]; 98 | int[2] rule5StateVar4 = [int(3),4]; 99 | function demoStorageToStorageRefAssignment2() public returns (int) { 100 | 101 | 102 | // This is diffrent from almost all programming languages 103 | // as in other programming languages assignment of referance 104 | // variable to another referance variable create referance and 105 | // both variable points to same object 106 | // BUT here in solidity it creates copy of ref variable and both 107 | // points to different object 108 | rule5StateVar3 = rule5StateVar4; 109 | rule5StateVar4[1] = 10; 110 | 111 | return rule5StateVar3[1]; // returns 4 112 | } 113 | 114 | // Rule 5 ends 115 | 116 | 117 | 118 | // Rule 6 starts 119 | int public rule6StateVar1 = 10; 120 | function demoMemoryToStorageValueAssignment1() public returns (int) { 121 | // Assignment from memory variable to storage variable creates copy 122 | // so both maintain separate copy of data 123 | int rule6LocalVar = 20; 124 | rule6StateVar1 = rule6LocalVar; 125 | rule6LocalVar = 60; 126 | 127 | return rule6StateVar1; // return 20 128 | } 129 | 130 | int[2] rule6StateVar2 = [int(1),2]; 131 | function demoMemoryToStorageRefAssignment2() public returns (int) { 132 | // Assignment from memory variable to storage variable creates copy 133 | // so both maintain separate copy of data 134 | 135 | // This is also diffrent from other programming languages 136 | // In other language it creates referance and both variable points 137 | // to same data 138 | // BUT here both points to separate copy of data 139 | int[2] memory rule6LocalVar1 = [int(3),4]; 140 | 141 | rule6StateVar2 = rule6LocalVar1; 142 | rule6LocalVar1[1] = 10; 143 | 144 | return rule6StateVar2[1]; // returns 4 145 | } 146 | 147 | // Rule 6 ends 148 | 149 | 150 | // Rule 7 starts -- Nothing special same as rule 6 and 5 151 | int public rule7StateVar1 = 10; 152 | function demoStorageToMemoryValueAssignment1() public returns (int) { 153 | // Assignment from storage variable to memory variable creates copy 154 | // so both maintain separate copy of data 155 | int rule7LocalVar = 20; 156 | rule7LocalVar = rule7StateVar1; 157 | rule7StateVar1 = 60; 158 | 159 | return rule7LocalVar; // return 10 160 | } 161 | 162 | int[2] rule7StateVar2 = [int(1),2]; 163 | function demoStorageToMemoryRefAssignment2() public returns (int) { 164 | // Assignment from storage variable to memory variable creates copy 165 | // so both maintain separate copy of data 166 | 167 | // This is also diffrent from other programming languages 168 | // In other language it creates referance and both variable points 169 | // to same data 170 | // BUT here both points to separate copy of data 171 | int[2] memory rule7LocalVar1 = [int(3),4]; 172 | 173 | rule7LocalVar1 = rule7StateVar2; 174 | rule7StateVar2[1] = 10; 175 | 176 | return rule7LocalVar1[1]; // returns 2 177 | } 178 | 179 | // Rule 7 ends 180 | 181 | 182 | 183 | // Rule 8 starts -- This also same as others but the difference is 184 | // assignment of memory to memory of referance type DO NOT create copy 185 | 186 | function demoMemoryToMemoryValueAssignment1() public returns (int) { 187 | // Assignment from memory variable to memory variable creates copy 188 | // so both maintain separate copy of data 189 | int rule8LocalVar1 = 20; 190 | int rule8LocalVar2 = 30; 191 | rule8LocalVar1 = rule8LocalVar2; 192 | rule8LocalVar2 = 60; 193 | 194 | return rule8LocalVar1; // return 30 195 | } 196 | 197 | function demoMemoryToMemoryRefAssignment2() public returns (int) { 198 | // Assignment from memory variable to memory for referance variable 199 | // DO NOT creates copy so both have same data 200 | 201 | int[2] memory rule8LocalVar1 = [int(1),2]; 202 | int[2] memory rule8LocalVar2 = [int(3),4]; 203 | 204 | // This works are ref type, changes in one will change other 205 | rule8LocalVar1 = rule8LocalVar2; 206 | rule8LocalVar2[1] = 10; 207 | 208 | return rule8LocalVar1[1]; // returns 10 209 | } 210 | 211 | // Rule 8 ends 212 | 213 | 214 | 215 | 216 | 217 | 218 | 219 | 220 | 221 | 222 | } -------------------------------------------------------------------------------- /11_rules_of_datatypes/contracts/Migrations.sol: -------------------------------------------------------------------------------- 1 | pragma solidity ^0.4.23; 2 | 3 | contract Migrations { 4 | address public owner; 5 | uint public last_completed_migration; 6 | 7 | 8 | constructor() public { 9 | owner = msg.sender; 10 | } 11 | 12 | modifier restricted() { 13 | if (msg.sender == owner) _; 14 | } 15 | 16 | function setCompleted(uint completed) public restricted { 17 | last_completed_migration = completed; 18 | } 19 | 20 | function upgrade(address new_address) public restricted { 21 | Migrations upgraded = Migrations(new_address); 22 | upgraded.setCompleted(last_completed_migration); 23 | } 24 | } 25 | -------------------------------------------------------------------------------- /11_rules_of_datatypes/migrations/1_initial_migration.js: -------------------------------------------------------------------------------- 1 | var Migrations = artifacts.require("./Migrations.sol"); 2 | 3 | module.exports = function(deployer) { 4 | deployer.deploy(Migrations); 5 | }; 6 | -------------------------------------------------------------------------------- /11_rules_of_datatypes/migrations/2_custom.js: -------------------------------------------------------------------------------- 1 | var contract1 = artifacts.require("./Contract1.sol"); 2 | 3 | module.exports = function(deployer) { 4 | deployer.deploy(contract1); 5 | }; -------------------------------------------------------------------------------- /11_rules_of_datatypes/test/TestContract1.sol: -------------------------------------------------------------------------------- 1 | pragma solidity ^0.4.24; 2 | import "truffle/Assert.sol"; 3 | import "truffle/DeployedAddresses.sol"; 4 | import "../contracts/Contract1.sol"; 5 | 6 | contract TestContract1 { 7 | 8 | //Rule 2 9 | int[3] list = [int(21),22,23]; 10 | function testRule2() public { 11 | Contract1 meta = Contract1(DeployedAddresses.Contract1()); 12 | 13 | int localVal = 45; 14 | int a = meta.changeMemoryValueType(localVal); 15 | int b = meta.changeMemoryRefType(list); 16 | 17 | Assert.equal(localVal,45,"localVal should be 45"); 18 | Assert.equal(list[1],22,"List 1 should be 22"); 19 | Assert.equal(a,5,"A should be 5"); 20 | Assert.equal(b,4,"B should be 4"); 21 | } 22 | 23 | //Rule 5 24 | function testRule5() public { 25 | Contract1 meta = Contract1(DeployedAddresses.Contract1()); 26 | int a = meta.demoStorageToStorageValueAssignment1(); 27 | int b = meta.demoStorageToStorageRefAssignment2(); 28 | Assert.equal(a,20,"a should be 20"); 29 | Assert.equal(b,4,"a should be 4"); 30 | 31 | } 32 | 33 | //Rule 6 34 | function testRule6() public { 35 | Contract1 meta = Contract1(DeployedAddresses.Contract1()); 36 | int a = meta.demoMemoryToStorageValueAssignment1(); 37 | int b = meta.demoMemoryToStorageRefAssignment2(); 38 | Assert.equal(a,20,"a should be 20"); 39 | Assert.equal(b,4,"a should be 4"); 40 | 41 | } 42 | 43 | //Rule 7 44 | function testRule7() public { 45 | Contract1 meta = Contract1(DeployedAddresses.Contract1()); 46 | int a = meta.demoStorageToMemoryValueAssignment1(); 47 | int b = meta.demoStorageToMemoryRefAssignment2(); 48 | Assert.equal(a,10,"a should be 10"); 49 | Assert.equal(b,2,"a should be 2"); 50 | 51 | } 52 | 53 | //Rule 8 54 | function testRule8() public { 55 | Contract1 meta = Contract1(DeployedAddresses.Contract1()); 56 | int a = meta.demoMemoryToMemoryValueAssignment1(); 57 | int b = meta.demoMemoryToMemoryRefAssignment2(); 58 | Assert.equal(a,30,"a should be 30"); 59 | Assert.equal(b,10,"a should be 10"); 60 | 61 | } 62 | 63 | } -------------------------------------------------------------------------------- /11_rules_of_datatypes/truffle-config.js: -------------------------------------------------------------------------------- 1 | module.exports = { 2 | // See 3 | // to customize your Truffle configuration! 4 | }; 5 | -------------------------------------------------------------------------------- /11_rules_of_datatypes/truffle.js: -------------------------------------------------------------------------------- 1 | module.exports = { 2 | // See 3 | // to customize your Truffle configuration! 4 | networks: { 5 | development: { 6 | host: "127.0.0.1", 7 | port: 8545, 8 | network_id: "*" // Match any network id 9 | } 10 | } 11 | }; 12 | -------------------------------------------------------------------------------- /12_integer_values/README.md: -------------------------------------------------------------------------------- 1 | # Integer Values -------------------------------------------------------------------------------- /12_integer_values/contracts/Contract1.sol: -------------------------------------------------------------------------------- 1 | pragma solidity ^0.4.23; 2 | contract Contract1 { 3 | 4 | function demoIntAndUint() public returns (int) { 5 | int a = 5; 6 | int b = 6; 7 | 8 | int8 c = 76; 9 | int16 d = 120; 10 | 11 | a = b; 12 | d = c; 13 | 14 | a = d; 15 | return d; 16 | } 17 | 18 | function demoIntAndUint2() public returns (int) { 19 | // uint and int can not be assigned to each other 20 | // int is alias to int256 21 | int a = 5; 22 | 23 | int8 b = 76; 24 | 25 | //b = a; // smaller datatype can not hold value of larger datatype 26 | a = b; 27 | return a; 28 | } 29 | 30 | function demoIntAndUint3() public returns (int) { 31 | uint8 a = 5; 32 | int8 b = 76; 33 | 34 | // casting 35 | a = uint8(b); 36 | return a; 37 | } 38 | 39 | function demoIntAndUint4() public returns (int) { 40 | uint8 a = 5; 41 | int8 b = -76; 42 | 43 | // casting, this will change value because unit8 can only store positive number 44 | a = uint8(b); 45 | return a; 46 | } 47 | 48 | 49 | } -------------------------------------------------------------------------------- /12_integer_values/contracts/Migrations.sol: -------------------------------------------------------------------------------- 1 | pragma solidity ^0.4.23; 2 | 3 | contract Migrations { 4 | address public owner; 5 | uint public last_completed_migration; 6 | 7 | 8 | constructor() public { 9 | owner = msg.sender; 10 | } 11 | 12 | modifier restricted() { 13 | if (msg.sender == owner) _; 14 | } 15 | 16 | function setCompleted(uint completed) public restricted { 17 | last_completed_migration = completed; 18 | } 19 | 20 | function upgrade(address new_address) public restricted { 21 | Migrations upgraded = Migrations(new_address); 22 | upgraded.setCompleted(last_completed_migration); 23 | } 24 | } 25 | -------------------------------------------------------------------------------- /12_integer_values/migrations/1_initial_migration.js: -------------------------------------------------------------------------------- 1 | var Migrations = artifacts.require("./Migrations.sol"); 2 | 3 | module.exports = function(deployer) { 4 | deployer.deploy(Migrations); 5 | }; 6 | -------------------------------------------------------------------------------- /12_integer_values/migrations/2_custom.js: -------------------------------------------------------------------------------- 1 | var contract1 = artifacts.require("./Contract1.sol"); 2 | 3 | module.exports = function(deployer) { 4 | deployer.deploy(contract1); 5 | }; -------------------------------------------------------------------------------- /12_integer_values/test/TestContract1.sol: -------------------------------------------------------------------------------- 1 | pragma solidity ^0.4.24; 2 | import "truffle/Assert.sol"; 3 | import "truffle/DeployedAddresses.sol"; 4 | import "../contracts/Contract1.sol"; 5 | 6 | contract TestContract1 { 7 | 8 | function testIntegerValues() public { 9 | Contract1 meta = Contract1(DeployedAddresses.Contract1()); 10 | int a = meta.demoIntAndUint(); 11 | int b = meta.demoIntAndUint2(); 12 | 13 | Assert.equal(a,76,"A should be 76"); 14 | Assert.equal(b,76,"A should be 76"); 15 | } 16 | 17 | function testIntegerValues2() public { 18 | Contract1 meta = Contract1(DeployedAddresses.Contract1()); 19 | int a = meta.demoIntAndUint3(); 20 | Assert.equal(a,76,"A should be 76"); 21 | 22 | } 23 | 24 | function testIntegerValues3() public { 25 | Contract1 meta = Contract1(DeployedAddresses.Contract1()); 26 | int a = meta.demoIntAndUint4(); 27 | Assert.equal(a,180,"A should be 180"); 28 | 29 | } 30 | 31 | } -------------------------------------------------------------------------------- /12_integer_values/truffle-config.js: -------------------------------------------------------------------------------- 1 | module.exports = { 2 | // See 3 | // to customize your Truffle configuration! 4 | }; 5 | -------------------------------------------------------------------------------- /12_integer_values/truffle.js: -------------------------------------------------------------------------------- 1 | module.exports = { 2 | // See 3 | // to customize your Truffle configuration! 4 | networks: { 5 | development: { 6 | host: "127.0.0.1", 7 | port: 8545, 8 | network_id: "*" // Match any network id 9 | } 10 | } 11 | }; 12 | -------------------------------------------------------------------------------- /13_bytes_values/README.md: -------------------------------------------------------------------------------- 1 | # Byte Values -------------------------------------------------------------------------------- /13_bytes_values/contracts/Contract1.sol: -------------------------------------------------------------------------------- 1 | pragma solidity ^0.4.23; 2 | contract Contract1 { 3 | 4 | // bytes store binary data 5 | function getbyteaa() public returns (bytes1) { 6 | int a = 0x65; 7 | bytes1 aa = 0x65; 8 | return aa; //returns 0x65 9 | } 10 | 11 | function getbytebb() public returns (bytes1) { 12 | bytes1 bb = 10; 13 | return bb; //returns 10 14 | } 15 | 16 | // For 1 letter use bytes3 17 | function getbytecc() public returns (bytes1) { 18 | bytes1 cc = "a"; 19 | return cc; //returns "a" 20 | } 21 | 22 | // For 2 letters use bytes2 23 | function getbytedd() public returns (bytes2) { 24 | bytes2 dd = "he"; 25 | return dd; //returns "he" 26 | } 27 | 28 | // For 3 letters use bytes3 29 | function getbyteee() public returns (bytes3) { 30 | bytes3 ee = "hel"; 31 | return ee; //returns "hel" 32 | } 33 | 34 | 35 | } -------------------------------------------------------------------------------- /13_bytes_values/contracts/Migrations.sol: -------------------------------------------------------------------------------- 1 | pragma solidity ^0.4.23; 2 | 3 | contract Migrations { 4 | address public owner; 5 | uint public last_completed_migration; 6 | 7 | 8 | constructor() public { 9 | owner = msg.sender; 10 | } 11 | 12 | modifier restricted() { 13 | if (msg.sender == owner) _; 14 | } 15 | 16 | function setCompleted(uint completed) public restricted { 17 | last_completed_migration = completed; 18 | } 19 | 20 | function upgrade(address new_address) public restricted { 21 | Migrations upgraded = Migrations(new_address); 22 | upgraded.setCompleted(last_completed_migration); 23 | } 24 | } 25 | -------------------------------------------------------------------------------- /13_bytes_values/migrations/1_initial_migration.js: -------------------------------------------------------------------------------- 1 | var Migrations = artifacts.require("./Migrations.sol"); 2 | 3 | module.exports = function(deployer) { 4 | deployer.deploy(Migrations); 5 | }; 6 | -------------------------------------------------------------------------------- /13_bytes_values/migrations/2_custom.js: -------------------------------------------------------------------------------- 1 | var contract1 = artifacts.require("./Contract1.sol"); 2 | 3 | module.exports = function(deployer) { 4 | deployer.deploy(contract1); 5 | }; -------------------------------------------------------------------------------- /13_bytes_values/test/TestContract1.sol: -------------------------------------------------------------------------------- 1 | pragma solidity ^0.4.24; 2 | import "truffle/Assert.sol"; 3 | import "truffle/DeployedAddresses.sol"; 4 | import "../contracts/Contract1.sol"; 5 | 6 | contract TestContract1 { 7 | 8 | function testBytesValues1() public { 9 | Contract1 meta = Contract1(DeployedAddresses.Contract1()); 10 | bytes1 a = meta.getbyteaa(); 11 | 12 | Assert.equal(int(a),101,"A should be 101"); 13 | Assert.equal(a,bytes1(101),"A should be 101"); 14 | 15 | } 16 | 17 | function testBytesValues2() public { 18 | Contract1 meta = Contract1(DeployedAddresses.Contract1()); 19 | bytes1 a = meta.getbytebb(); 20 | 21 | Assert.equal(int(a),10,"A should be 10"); 22 | 23 | } 24 | 25 | function testBytesValues3() public { 26 | Contract1 meta = Contract1(DeployedAddresses.Contract1()); 27 | bytes1 a = meta.getbytecc(); 28 | 29 | Assert.equal(a,"a","A should be a"); 30 | 31 | } 32 | 33 | function testBytesValues4() public { 34 | Contract1 meta = Contract1(DeployedAddresses.Contract1()); 35 | bytes2 a = meta.getbytedd(); 36 | 37 | Assert.equal(a,"he","A should be a"); 38 | 39 | } 40 | 41 | function testBytesValues5() public { 42 | Contract1 meta = Contract1(DeployedAddresses.Contract1()); 43 | bytes3 a = meta.getbyteee(); 44 | 45 | Assert.equal(a,"hel","A should be a"); 46 | 47 | } 48 | 49 | } -------------------------------------------------------------------------------- /13_bytes_values/truffle-config.js: -------------------------------------------------------------------------------- 1 | module.exports = { 2 | // See 3 | // to customize your Truffle configuration! 4 | }; 5 | -------------------------------------------------------------------------------- /13_bytes_values/truffle.js: -------------------------------------------------------------------------------- 1 | module.exports = { 2 | // See 3 | // to customize your Truffle configuration! 4 | networks: { 5 | development: { 6 | host: "127.0.0.1", 7 | port: 8545, 8 | network_id: "*" // Match any network id 9 | } 10 | } 11 | }; 12 | -------------------------------------------------------------------------------- /14_fixed_size_arrays/README.md: -------------------------------------------------------------------------------- 1 | # Fixed Size Array -------------------------------------------------------------------------------- /14_fixed_size_arrays/contracts/Contract1.sol: -------------------------------------------------------------------------------- 1 | pragma solidity ^0.4.23; 2 | contract Contract1 { 3 | 4 | int[3] a = [3,4,5]; 5 | int[3] b; 6 | function demoFixedSizeStateArray() public returns (int) { 7 | a[1] = 5; 8 | b = [21,22,23]; 9 | uint[3] memory bb = [uint(23),34,4]; 10 | int[3] memory c = [int(31),32,33]; // explicitly mentioned as int which is int256 11 | c[2] = 56; 12 | return a[1]; 13 | } 14 | 15 | 16 | // Return type and function parameter must have to specify size 17 | // of array to accept in parameter and return from function 18 | function demoFixedSizeArrayReturns() public returns (int[3]) { 19 | int[3] memory c = [int(31),32,33]; 20 | return c; 21 | } 22 | 23 | 24 | int[3] c; 25 | function demoFixedSizeStateArray2() public returns (int) { 26 | // completely different from other languages, 27 | // here it initialized all elments of array with default 0 28 | // we have not initialized the array 'c' 29 | return c[1]; // returns 0 30 | } 31 | 32 | function demoFixedSizeStateArray3() public returns (int) { 33 | c[2] = 45; 34 | return c[2]; 35 | } 36 | 37 | function demoFixedSizeMemoryArray4() public returns (int) { 38 | // completely different from other languages, 39 | // here it initialized all elments of array with default 0 40 | int[3] memory d; // = [int(2),3,4]; 41 | return d[1]; 42 | } 43 | } -------------------------------------------------------------------------------- /14_fixed_size_arrays/contracts/Migrations.sol: -------------------------------------------------------------------------------- 1 | pragma solidity ^0.4.23; 2 | 3 | contract Migrations { 4 | address public owner; 5 | uint public last_completed_migration; 6 | 7 | 8 | constructor() public { 9 | owner = msg.sender; 10 | } 11 | 12 | modifier restricted() { 13 | if (msg.sender == owner) _; 14 | } 15 | 16 | function setCompleted(uint completed) public restricted { 17 | last_completed_migration = completed; 18 | } 19 | 20 | function upgrade(address new_address) public restricted { 21 | Migrations upgraded = Migrations(new_address); 22 | upgraded.setCompleted(last_completed_migration); 23 | } 24 | } 25 | -------------------------------------------------------------------------------- /14_fixed_size_arrays/migrations/1_initial_migration.js: -------------------------------------------------------------------------------- 1 | var Migrations = artifacts.require("./Migrations.sol"); 2 | 3 | module.exports = function(deployer) { 4 | deployer.deploy(Migrations); 5 | }; 6 | -------------------------------------------------------------------------------- /14_fixed_size_arrays/migrations/2_custom.js: -------------------------------------------------------------------------------- 1 | var contract1 = artifacts.require("./Contract1.sol"); 2 | 3 | module.exports = function(deployer) { 4 | deployer.deploy(contract1); 5 | }; -------------------------------------------------------------------------------- /14_fixed_size_arrays/test/TestContract1.sol: -------------------------------------------------------------------------------- 1 | pragma solidity ^0.4.24; 2 | import "truffle/Assert.sol"; 3 | import "truffle/DeployedAddresses.sol"; 4 | import "../contracts/Contract1.sol"; 5 | 6 | contract TestContract1 { 7 | 8 | function testFixedSizeArray() public { 9 | Contract1 meta = Contract1(DeployedAddresses.Contract1()); 10 | int a = meta.demoFixedSizeStateArray(); 11 | 12 | Assert.equal(a,5,"A should be 5"); 13 | 14 | } 15 | 16 | function testFixedSizeArray2() public { 17 | Contract1 meta = Contract1(DeployedAddresses.Contract1()); 18 | // here we need to specify memory keyword otherwise it will not work 19 | int[3] memory a = meta.demoFixedSizeArrayReturns(); 20 | Assert.equal(a[1],32,"A should be 32"); 21 | } 22 | 23 | function testFixedSizeArray3() public { 24 | Contract1 meta = Contract1(DeployedAddresses.Contract1()); 25 | int a = meta.demoFixedSizeStateArray2(); 26 | Assert.equal(a,0,"A should be 0"); 27 | 28 | } 29 | 30 | function testFixedSizeArray4() public { 31 | Contract1 meta = Contract1(DeployedAddresses.Contract1()); 32 | int a = meta.demoFixedSizeStateArray3(); 33 | Assert.equal(a,45,"A should be 45"); 34 | 35 | } 36 | 37 | function testFixedSizeArray5() public { 38 | Contract1 meta = Contract1(DeployedAddresses.Contract1()); 39 | int a = meta.demoFixedSizeMemoryArray4(); 40 | Assert.equal(a,0,"A should be 0"); 41 | } 42 | } -------------------------------------------------------------------------------- /14_fixed_size_arrays/truffle-config.js: -------------------------------------------------------------------------------- 1 | module.exports = { 2 | // See 3 | // to customize your Truffle configuration! 4 | }; 5 | -------------------------------------------------------------------------------- /14_fixed_size_arrays/truffle.js: -------------------------------------------------------------------------------- 1 | module.exports = { 2 | // See 3 | // to customize your Truffle configuration! 4 | networks: { 5 | development: { 6 | host: "127.0.0.1", 7 | port: 8545, 8 | network_id: "*" // Match any network id 9 | } 10 | } 11 | }; 12 | -------------------------------------------------------------------------------- /15_dynamic_arrays/README.md: -------------------------------------------------------------------------------- 1 | # Dynamic Arrays -------------------------------------------------------------------------------- /15_dynamic_arrays/contracts/Contract1.sol: -------------------------------------------------------------------------------- 1 | pragma solidity ^0.4.23; 2 | contract Contract1 { 3 | 4 | int[] a = [3,4,5]; 5 | int[] b; 6 | function demoDynamicArray() public returns (int) { 7 | a[1] = 5; 8 | b = [21,22,23]; 9 | 10 | // Not allowed to create dynamic memory array in this way 11 | //int[] memory c = [int(31),32,33]; 12 | //c[2] = 56; 13 | return a[1]; 14 | } 15 | 16 | function demoDynamicArray2() public returns (int) { 17 | int[] memory c = new int[](3); 18 | return c[1]; 19 | } 20 | 21 | function demoDynamicArray3() public returns (int[]) { 22 | int[] memory c = new int[](3); 23 | c[1] = 31; 24 | return c; 25 | } 26 | 27 | int[] d = [int(2),3,4]; 28 | function demoDynamicArray4() public returns (int) { 29 | //int[] memory c = new int[](3); 30 | //int[] memory c = [int(1),2,3]; 31 | // push function only works on storage array and that array must be 32 | // initialized already 33 | d.push(34); 34 | return d[3]; 35 | } 36 | 37 | int[] e = new int[](3); 38 | function demoDynamicArray5() public returns (int) { 39 | 40 | // push function only works on storage array and that array must be 41 | // initialized already 42 | // push function will add element at the end of array 43 | // in this case it will be 4th element 44 | d.push(34); 45 | return d[3]; 46 | } 47 | 48 | 49 | 50 | int[] f; 51 | function demoDynamicArray6() public returns (int) { 52 | 53 | // This will never works because if we have dynamic array 54 | // then it must be initialized first 55 | //f[1] = 45; 56 | 57 | 58 | return 5; // f[1]; // access will also require initilization first 59 | } 60 | } -------------------------------------------------------------------------------- /15_dynamic_arrays/contracts/Migrations.sol: -------------------------------------------------------------------------------- 1 | pragma solidity ^0.4.23; 2 | 3 | contract Migrations { 4 | address public owner; 5 | uint public last_completed_migration; 6 | 7 | 8 | constructor() public { 9 | owner = msg.sender; 10 | } 11 | 12 | modifier restricted() { 13 | if (msg.sender == owner) _; 14 | } 15 | 16 | function setCompleted(uint completed) public restricted { 17 | last_completed_migration = completed; 18 | } 19 | 20 | function upgrade(address new_address) public restricted { 21 | Migrations upgraded = Migrations(new_address); 22 | upgraded.setCompleted(last_completed_migration); 23 | } 24 | } 25 | -------------------------------------------------------------------------------- /15_dynamic_arrays/migrations/1_initial_migration.js: -------------------------------------------------------------------------------- 1 | var Migrations = artifacts.require("./Migrations.sol"); 2 | 3 | module.exports = function(deployer) { 4 | deployer.deploy(Migrations); 5 | }; 6 | -------------------------------------------------------------------------------- /15_dynamic_arrays/migrations/2_custom.js: -------------------------------------------------------------------------------- 1 | var contract1 = artifacts.require("./Contract1.sol"); 2 | 3 | module.exports = function(deployer) { 4 | deployer.deploy(contract1); 5 | }; -------------------------------------------------------------------------------- /15_dynamic_arrays/test/TestContract1.sol: -------------------------------------------------------------------------------- 1 | pragma solidity ^0.4.24; 2 | import "truffle/Assert.sol"; 3 | import "truffle/DeployedAddresses.sol"; 4 | import "../contracts/Contract1.sol"; 5 | 6 | contract TestContract1 { 7 | 8 | function testDynamicArray() public { 9 | Contract1 meta = Contract1(DeployedAddresses.Contract1()); 10 | int a = meta.demoDynamicArray(); 11 | 12 | Assert.equal(a,5,"A should be 5"); 13 | 14 | } 15 | 16 | function testDynamicArray2() public { 17 | Contract1 meta = Contract1(DeployedAddresses.Contract1()); 18 | int a = meta.demoDynamicArray2(); 19 | Assert.equal(a,0,"A should be 0"); 20 | } 21 | 22 | function testDynamicArray3() public { 23 | Contract1 meta = Contract1(DeployedAddresses.Contract1()); 24 | int[] memory a = meta.demoDynamicArray3(); 25 | Assert.equal(a[1],31,"A should be 31"); 26 | } 27 | 28 | function testDynamicArray4() public { 29 | Contract1 meta = Contract1(DeployedAddresses.Contract1()); 30 | int a = meta.demoDynamicArray4(); 31 | Assert.equal(a,34,"A should be 34"); 32 | } 33 | 34 | function testDynamicArray5() public { 35 | Contract1 meta = Contract1(DeployedAddresses.Contract1()); 36 | int a = meta.demoDynamicArray5(); 37 | Assert.equal(a,34,"A should be 34"); 38 | } 39 | 40 | function testDynamicArray6() public { 41 | Contract1 meta = Contract1(DeployedAddresses.Contract1()); 42 | int a = meta.demoDynamicArray6(); 43 | Assert.equal(a,5,"A should be 45"); 44 | } 45 | 46 | } -------------------------------------------------------------------------------- /15_dynamic_arrays/truffle-config.js: -------------------------------------------------------------------------------- 1 | module.exports = { 2 | // See 3 | // to customize your Truffle configuration! 4 | }; 5 | -------------------------------------------------------------------------------- /15_dynamic_arrays/truffle.js: -------------------------------------------------------------------------------- 1 | module.exports = { 2 | // See 3 | // to customize your Truffle configuration! 4 | networks: { 5 | development: { 6 | host: "127.0.0.1", 7 | port: 8545, 8 | network_id: "*" // Match any network id 9 | } 10 | } 11 | }; 12 | -------------------------------------------------------------------------------- /16_bytes_strings_arrays/README.md: -------------------------------------------------------------------------------- 1 | # Bytes and String Arrays 2 | 3 | String concatination is not allowed for now 4 | Current version of solidity is 0.4.24 5 | https://solidity.readthedocs.io/en/v0.4.24/frequently-asked-questions.html#what-are-some-examples-of-basic-string-manipulation-substring-indexof-charat-etc 6 | -------------------------------------------------------------------------------- /16_bytes_strings_arrays/contracts/Contract1.sol: -------------------------------------------------------------------------------- 1 | pragma solidity ^0.4.23; 2 | 3 | contract Contract1 { 4 | 5 | function demoBytesArray() public returns (uint) { 6 | 7 | // bytes is referance type and by default it is storage level type 8 | // you have to specify memory keywork for local 9 | 10 | // Important: bytes can not be assigned directly with values so 11 | // follwoing are INVALID: 12 | // bytes a = 45; 13 | // bytes b = "Hello World"; 14 | // bytes c = byte(32); 15 | 16 | // This bytes type is different from bytes1, bytes2 ... bytes32 17 | bytes memory b1 = new bytes(2); 18 | return b1.length; 19 | } 20 | 21 | bytes b1 = new bytes(2); 22 | function demoBytesArray2() public returns (uint) { 23 | 24 | // Length modificaiton not allowed on memory location data 25 | //bytes memory b1 = new bytes(2); 26 | //b1.length = 5; 27 | 28 | b1.length = 5; 29 | return b1.length; 30 | } 31 | 32 | function demoBytesArray3() public returns (bytes) { 33 | bytes memory b = new bytes(3); 34 | b = "Hello World"; 35 | return b; 36 | } 37 | 38 | bytes b4 = new bytes(2); 39 | function demoBytesArray4() public returns (bytes) { 40 | // Push will not work with memory variable 41 | //bytes memory b = new bytes(3); 42 | //b.push("Hello World"); 43 | b4 = "He"; 44 | b4.push(byte(65)); 45 | return b4; 46 | } 47 | 48 | //String assignment in state variable is allowed 49 | bytes b5 = "Hello World Testing"; 50 | function demoBytesArray5() public returns (bytes) { 51 | return b5; 52 | } 53 | 54 | // Strings 55 | function demoStrings1() public returns (string) { 56 | string memory st = "Hello World"; 57 | return st; 58 | } 59 | 60 | // String concatination is not allowed for now 61 | // Current version of solidity is 0.4.24 62 | //https://solidity.readthedocs.io/en/v0.4.24/frequently-asked-questions.html#what-are-some-examples-of-basic-string-manipulation-substring-indexof-charat-etc 63 | // Manual String concatenation 64 | int[] a = [int(1),2,3]; 65 | function demoStrings2() public returns (string) { 66 | string memory st1 = "Hello"; 67 | string memory st2 = "World"; 68 | 69 | // Not allowed 70 | //string memory st3 = st1 + st2; 71 | //string memory st3 = st1.concat(st2); 72 | 73 | /* 74 | // This is also not allowed as you can not create bytes(st1) which 75 | // locate in memory and bytes b1 which is storage type. 76 | // if you try to create bytes in memory like this 77 | // bytes memory b1 = new bytes(3) then push method will not work in it 78 | 79 | bytes b1 = new bytes(3); 80 | b1 = bytes(st1); 81 | bytes b2 = st2; 82 | b1.push(b2[0]); 83 | b1.push(b2[1]); 84 | b1.push(b2[2]); 85 | b1.push(b2[3]); 86 | */ 87 | 88 | // Implementing it without loops for now 89 | bytes memory bb1 = bytes(st1); 90 | bytes memory bb2 = bytes(st2); 91 | bytes memory bb0 = new bytes(bb1.length + bb2.length); 92 | 93 | uint index = 0; 94 | bb0[index++] = bb1[0]; 95 | bb0[index++] = bb1[1]; 96 | bb0[index++] = bb1[2]; 97 | bb0[index++] = bb1[3]; 98 | bb0[index++] = bb1[4]; 99 | 100 | bb0[index++] = bb2[0]; 101 | bb0[index++] = bb2[1]; 102 | bb0[index++] = bb2[2]; 103 | bb0[index++] = bb2[3]; 104 | bb0[index++] = bb2[4]; 105 | string memory st3 = string(bb0); 106 | 107 | 108 | a.length = 5; 109 | return st3; 110 | } 111 | 112 | function demoStringArray() public returns (string) { 113 | // Array for string type will have same rules as for any other array 114 | // of int 115 | string[2] memory st = ["Hello","World"]; 116 | return st[1]; 117 | } 118 | 119 | } -------------------------------------------------------------------------------- /16_bytes_strings_arrays/contracts/Migrations.sol: -------------------------------------------------------------------------------- 1 | pragma solidity ^0.4.23; 2 | 3 | contract Migrations { 4 | address public owner; 5 | uint public last_completed_migration; 6 | 7 | 8 | constructor() public { 9 | owner = msg.sender; 10 | } 11 | 12 | modifier restricted() { 13 | if (msg.sender == owner) _; 14 | } 15 | 16 | function setCompleted(uint completed) public restricted { 17 | last_completed_migration = completed; 18 | } 19 | 20 | function upgrade(address new_address) public restricted { 21 | Migrations upgraded = Migrations(new_address); 22 | upgraded.setCompleted(last_completed_migration); 23 | } 24 | } 25 | -------------------------------------------------------------------------------- /16_bytes_strings_arrays/migrations/1_initial_migration.js: -------------------------------------------------------------------------------- 1 | var Migrations = artifacts.require("./Migrations.sol"); 2 | 3 | module.exports = function(deployer) { 4 | deployer.deploy(Migrations); 5 | }; 6 | -------------------------------------------------------------------------------- /16_bytes_strings_arrays/migrations/2_custom.js: -------------------------------------------------------------------------------- 1 | var contract1 = artifacts.require("./Contract1.sol"); 2 | 3 | module.exports = function(deployer) { 4 | deployer.deploy(contract1); 5 | }; -------------------------------------------------------------------------------- /16_bytes_strings_arrays/test/TestContract1.sol: -------------------------------------------------------------------------------- 1 | pragma solidity ^0.4.24; 2 | import "truffle/Assert.sol"; 3 | import "truffle/DeployedAddresses.sol"; 4 | import "../contracts/Contract1.sol"; 5 | 6 | contract TestContract1 { 7 | 8 | function testBytesArray() public { 9 | Contract1 meta = Contract1(DeployedAddresses.Contract1()); 10 | uint a = meta.demoBytesArray(); 11 | 12 | Assert.equal(a,2,"A should be 2"); 13 | 14 | } 15 | 16 | function testBytesArray2() public { 17 | Contract1 meta = Contract1(DeployedAddresses.Contract1()); 18 | uint a = meta.demoBytesArray2(); 19 | 20 | Assert.equal(a,5,"A should be 2"); 21 | 22 | } 23 | 24 | function testBytesArray3() public { 25 | Contract1 meta = Contract1(DeployedAddresses.Contract1()); 26 | // Must specify memory keyword, otherwise will not compile 27 | // Because ref types returned from functions are at memory location 28 | bytes memory a = meta.demoBytesArray3(); 29 | bytes memory b = new bytes(3); 30 | b = "Hello World"; 31 | // Not allowed to compare directly variables 32 | //Assert.equal(a,b,"A should be 'Hello World'"); 33 | //Assert.equal(a,"Hello World","A should be 'Hello World'"); 34 | 35 | Assert.equal(a[0],"H","A should be 'H'"); 36 | Assert.equal(a[0],b[0],"A should be 'H'"); 37 | Assert.equal(a[1],"e","A should be 'e'"); 38 | Assert.equal(a[0],byte(72),"A should be 'e'"); 39 | 40 | } 41 | 42 | function testBytesArray4() public { 43 | Contract1 meta = Contract1(DeployedAddresses.Contract1()); 44 | bytes memory a = meta.demoBytesArray4(); 45 | 46 | Assert.equal(a.length,3,"A.length should be 3"); 47 | Assert.equal(a[0],"H","A[0] should be 'H'"); 48 | Assert.equal(a[1],"e","A[1] should be 'e'"); 49 | Assert.equal(a[2],byte(65),"A[2] should be 65"); 50 | Assert.equal(a[2],"A","A[2] should be 'A'"); 51 | 52 | } 53 | 54 | function testBytesArray5() public { 55 | Contract1 meta = Contract1(DeployedAddresses.Contract1()); 56 | bytes memory a = meta.demoBytesArray5(); 57 | 58 | Assert.equal(a.length,19,"A.length should be 19"); 59 | 60 | } 61 | 62 | function testStrings1() public { 63 | Contract1 meta = Contract1(DeployedAddresses.Contract1()); 64 | string memory a = meta.demoStrings1(); 65 | 66 | Assert.equal(a,"Hello World","A should be 'Hello World'"); 67 | 68 | } 69 | 70 | function testConcatStrings2() public { 71 | Contract1 meta = Contract1(DeployedAddresses.Contract1()); 72 | string memory a = meta.demoStrings2(); 73 | Assert.equal(a,"HelloWorld","A should be 'HelloWorld'"); 74 | 75 | } 76 | 77 | function testStringArray() public { 78 | Contract1 meta = Contract1(DeployedAddresses.Contract1()); 79 | string memory a = meta.demoStringArray(); 80 | Assert.equal(a,"World","A should be 'World'"); 81 | 82 | } 83 | } -------------------------------------------------------------------------------- /16_bytes_strings_arrays/truffle-config.js: -------------------------------------------------------------------------------- 1 | module.exports = { 2 | // See 3 | // to customize your Truffle configuration! 4 | }; 5 | -------------------------------------------------------------------------------- /16_bytes_strings_arrays/truffle.js: -------------------------------------------------------------------------------- 1 | module.exports = { 2 | // See 3 | // to customize your Truffle configuration! 4 | networks: { 5 | development: { 6 | host: "127.0.0.1", 7 | port: 8545, 8 | network_id: "*" // Match any network id 9 | } 10 | } 11 | }; 12 | -------------------------------------------------------------------------------- /17_address/README.md: -------------------------------------------------------------------------------- 1 | # Address datatype 2 | 3 | # IN COMPLETE -------------------------------------------------------------------------------- /17_address/contracts/Contract1.sol: -------------------------------------------------------------------------------- 1 | pragma solidity ^0.4.24; 2 | contract Contract1 { 3 | 4 | // All addresses used here are from 'ganache-cli' 5 | // so while running this example use your addresses 6 | 7 | uint initialBalance = 100 ether; 8 | 9 | function demoAddressDataType() public returns (uint) { 10 | 11 | // Some random address in the list of 'ganache-cli' addresses but not 12 | // the first one 13 | address add = 0x787d793cc5353f86b4025f40a5583c9ac63a0c52; 14 | //0x787d793cc5353f86b4025f40a5583c9ac63a0c52 15 | return add.balance; 16 | } 17 | 18 | function demoAddressDataType2() public returns (uint) { 19 | // First address in the list of 'ganache-cli' addresses 20 | // all transactions are done with first address so balance can very 21 | // for everyone 22 | address add = 0x38f671c36acb116dc837d5cc4a6be2be72da2c4a; 23 | //0x38f671c36acb116dc837d5cc4a6be2be72da2c4a 24 | return add.balance; 25 | } 26 | 27 | // Incomplete 28 | function demoAddressDataTypeTransfer() public returns (uint) { 29 | /* 30 | address add1 = 0x53c9f8756a0458aca4c703d2be7e03a5e720b678; 31 | address add2 = 0x25c15a2304e43cff2a8b22a7aff34a8235d6b8df; 32 | //0x1c9d306c82592eDbC6ac6f90F8f10ab5974D0F5E 33 | add1.transfer(4000 wei); 34 | return add1.balance; 35 | */ 36 | return 0; 37 | } 38 | 39 | 40 | 41 | } -------------------------------------------------------------------------------- /17_address/contracts/Migrations.sol: -------------------------------------------------------------------------------- 1 | pragma solidity ^0.4.23; 2 | 3 | contract Migrations { 4 | address public owner; 5 | uint public last_completed_migration; 6 | 7 | 8 | constructor() public { 9 | owner = msg.sender; 10 | } 11 | 12 | modifier restricted() { 13 | if (msg.sender == owner) _; 14 | } 15 | 16 | function setCompleted(uint completed) public restricted { 17 | last_completed_migration = completed; 18 | } 19 | 20 | function upgrade(address new_address) public restricted { 21 | Migrations upgraded = Migrations(new_address); 22 | upgraded.setCompleted(last_completed_migration); 23 | } 24 | } 25 | -------------------------------------------------------------------------------- /17_address/migrations/1_initial_migration.js: -------------------------------------------------------------------------------- 1 | var Migrations = artifacts.require("./Migrations.sol"); 2 | 3 | module.exports = function(deployer) { 4 | deployer.deploy(Migrations); 5 | }; 6 | -------------------------------------------------------------------------------- /17_address/migrations/2_custom.js: -------------------------------------------------------------------------------- 1 | var contract1 = artifacts.require("./Contract1.sol"); 2 | 3 | module.exports = function(deployer) { 4 | deployer.deploy(contract1); 5 | }; -------------------------------------------------------------------------------- /17_address/test/TestContract1.sol: -------------------------------------------------------------------------------- 1 | pragma solidity ^0.4.24; 2 | import "truffle/Assert.sol"; 3 | import "truffle/DeployedAddresses.sol"; 4 | import "../contracts/Contract1.sol"; 5 | 6 | contract TestContract1 { 7 | 8 | function testAddressDataType() public { 9 | Contract1 meta = Contract1(DeployedAddresses.Contract1()); 10 | uint a = meta.demoAddressDataType(); 11 | 12 | //Assert.equal(a,100000000000000000000,"Balance should be 100000000000000000000"); 13 | Assert.equal(a,100000000000000000000,"Balance should be 100000000000000000000"); 14 | Assert.equal(a,100 ether,"Balance should be 100 ether"); 15 | 16 | } 17 | 18 | function testAddressDataType2() public { 19 | Contract1 meta = Contract1(DeployedAddresses.Contract1()); 20 | uint a = meta.demoAddressDataType2(); 21 | 22 | // This can be diffrent every time as I'm testing first address in the list 23 | // of address provided by 'ganache-cli' 24 | 25 | // Uncomment for Testing 26 | //Assert.equal(a,95090858000000000000,"Balance should be 95090858000000000000"); 27 | 28 | } 29 | 30 | // Incomplete 31 | function testAddressDataType3() public { 32 | Contract1 meta = Contract1(DeployedAddresses.Contract1()); 33 | uint a = meta.demoAddressDataTypeTransfer(); 34 | 35 | Assert.equal(a,0,"Balance should be 5"); 36 | } 37 | 38 | } -------------------------------------------------------------------------------- /17_address/truffle-config.js: -------------------------------------------------------------------------------- 1 | module.exports = { 2 | // See 3 | // to customize your Truffle configuration! 4 | }; 5 | -------------------------------------------------------------------------------- /17_address/truffle.js: -------------------------------------------------------------------------------- 1 | module.exports = { 2 | // See 3 | // to customize your Truffle configuration! 4 | networks: { 5 | development: { 6 | host: "127.0.0.1", 7 | port: 8545, 8 | network_id: "*" // Match any network id 9 | } 10 | } 11 | }; 12 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Learn Solidity 2 | Learn Solidity Step by Step 3 | 4 | 5 | # Prerequisite 6 | To Run these examples please follow following steps 7 | 8 | ## Installation 9 | 1. Install **Geth** (https://ethereum.github.io/go-ethereum/downloads/) 10 | 2. Install **Truffle** ```npm install -g truffle``` 11 | 3. Start Test Network - There are three options 12 | - Use Test network using following comming 13 | - ```Geth --testnet``` **OR** 14 | - ```Geth --rinkeby``` 15 | - Create Private network 16 | - Use ganache-cli 17 | - npm install -g ganache-cli 18 | - Run ganache-cli using following command 19 | - ```ganache-cli``` 20 | - This will create Test network 21 | 22 | # Compile, Deploy and Test Project 23 | 1. Go to project folder e.g 24 | - ```cd 01_HelloWorld``` 25 | 26 | 2. Inside that folder Compile with following command 27 | - ```truffle.cmd compile``` on Windows 28 | - ```truffle compile``` 29 | 30 | 3. Deploy Contract with following command 31 | - ```truffle.cmd migrate``` on Windows 32 | - ```truffle migrate``` 33 | 34 | 35 | 4. Test Contract with following command 36 | - ```truffle.cmd test``` on Windows 37 | - ```truffle test``` --------------------------------------------------------------------------------