├── Chapter01 ├── app │ ├── index.html │ ├── javascripts │ │ └── app.js │ └── stylesheets │ │ └── app.css ├── box-img-lg.png ├── box-img-sm.png ├── build │ └── contracts │ │ ├── ConvertLib.json │ │ ├── MetaCoin.json │ │ └── Migrations.json ├── contracts │ ├── ConvertLib.sol │ ├── MetaCoin.sol │ └── Migrations.sol ├── migrations │ ├── 1_initial_migration.js │ └── 2_deploy_contracts.js ├── package-lock.json ├── package.json ├── test │ ├── TestMetacoin.sol │ └── metacoin.js ├── truffle.js └── webpack.config.js ├── Chapter02 ├── app │ ├── index.html │ ├── javascripts │ │ └── app.js │ └── stylesheets │ │ └── app.css ├── box-img-lg.png ├── box-img-sm.png ├── build │ └── contracts │ │ ├── ConvertLib.json │ │ ├── MetaCoin.json │ │ └── Migrations.json ├── contracts │ ├── ConvertLib.sol │ ├── MetaCoin.sol │ └── Migrations.sol ├── migrations │ ├── 1_initial_migration.js │ └── 2_deploy_contracts.js ├── package-lock.json ├── package.json ├── test │ ├── TestMetacoin.sol │ └── metacoin.js ├── truffle.js └── webpack.config.js ├── Chapter03 ├── app │ ├── index.html │ ├── javascripts │ │ └── app.js │ └── stylesheets │ │ └── app.css ├── box-img-lg.png ├── box-img-sm.png ├── build │ └── contracts │ │ ├── BasicToken.json │ │ ├── ERC20.json │ │ ├── ERC20Basic.json │ │ ├── MetaCoin.json │ │ ├── Migrations.json │ │ ├── SafeMath.json │ │ └── StandardToken.json ├── contracts │ ├── MetaCoin.sol │ └── Migrations.sol ├── migrations │ ├── 1_initial_migration.js │ └── 2_deploy_contracts.js ├── package-lock.json ├── package.json ├── test │ ├── TestMetacoin.sol │ └── metacoin.js ├── truffle.js └── webpack.config.js ├── Chapter04 └── Section4 │ ├── DocumentExample.sol │ ├── SensitiveData.sol │ └── UserExample.sol ├── LICENSE └── README.md /Chapter01/app/index.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | MetaCoin - Truffle Webpack Demo w/ Frontend 5 | 6 | 7 | 8 | 9 |

MetaCoin

10 |

Example Truffle Dapp

11 |

You have META

12 | 13 |
14 |

Send MetaCoin

15 |
16 |
17 |

18 |

19 | 20 |
21 | Hint: open the browser developer console to view any errors and warnings. 22 | 23 | 24 | -------------------------------------------------------------------------------- /Chapter01/app/javascripts/app.js: -------------------------------------------------------------------------------- 1 | // Import the page's CSS. Webpack will know what to do with it. 2 | import "../stylesheets/app.css"; 3 | 4 | // Import libraries we need. 5 | import { default as Web3} from 'web3'; 6 | import { default as contract } from 'truffle-contract' 7 | 8 | // Import our contract artifacts and turn them into usable abstractions. 9 | import metacoin_artifacts from '../../build/contracts/MetaCoin.json' 10 | 11 | // MetaCoin is our usable abstraction, which we'll use through the code below. 12 | var MetaCoin = contract(metacoin_artifacts); 13 | 14 | // The following code is simple to show off interacting with your contracts. 15 | // As your needs grow you will likely need to change its form and structure. 16 | // For application bootstrapping, check out window.addEventListener below. 17 | var accounts; 18 | var account; 19 | 20 | window.App = { 21 | start: function() { 22 | var self = this; 23 | 24 | // Bootstrap the MetaCoin abstraction for Use. 25 | MetaCoin.setProvider(web3.currentProvider); 26 | 27 | // Get the initial account balance so it can be displayed. 28 | web3.eth.getAccounts(function(err, accs) { 29 | if (err != null) { 30 | alert("There was an error fetching your accounts."); 31 | return; 32 | } 33 | 34 | if (accs.length == 0) { 35 | alert("Couldn't get any accounts! Make sure your Ethereum client is configured correctly."); 36 | return; 37 | } 38 | 39 | accounts = accs; 40 | account = accounts[0]; 41 | 42 | self.refreshBalance(); 43 | }); 44 | }, 45 | 46 | setStatus: function(message) { 47 | var status = document.getElementById("status"); 48 | status.innerHTML = message; 49 | }, 50 | 51 | refreshBalance: function() { 52 | var self = this; 53 | 54 | var meta; 55 | MetaCoin.deployed().then(function(instance) { 56 | meta = instance; 57 | return meta.getBalance.call(account, {from: account}); 58 | }).then(function(value) { 59 | var balance_element = document.getElementById("balance"); 60 | balance_element.innerHTML = value.valueOf(); 61 | }).catch(function(e) { 62 | console.log(e); 63 | self.setStatus("Error getting balance; see log."); 64 | }); 65 | }, 66 | 67 | sendCoin: function() { 68 | var self = this; 69 | 70 | var amount = parseInt(document.getElementById("amount").value); 71 | var receiver = document.getElementById("receiver").value; 72 | 73 | this.setStatus("Initiating transaction... (please wait)"); 74 | 75 | var meta; 76 | MetaCoin.deployed().then(function(instance) { 77 | meta = instance; 78 | return meta.sendCoin(receiver, amount, {from: account}); 79 | }).then(function() { 80 | self.setStatus("Transaction complete!"); 81 | self.refreshBalance(); 82 | }).catch(function(e) { 83 | console.log(e); 84 | self.setStatus("Error sending coin; see log."); 85 | }); 86 | } 87 | }; 88 | 89 | window.addEventListener('load', function() { 90 | // Checking if Web3 has been injected by the browser (Mist/MetaMask) 91 | if (typeof web3 !== 'undefined') { 92 | console.warn("Using web3 detected from external source. If you find that your accounts don't appear or you have 0 MetaCoin, ensure you've configured that source properly. If using MetaMask, see the following link. Feel free to delete this warning. :) http://truffleframework.com/tutorials/truffle-and-metamask") 93 | // Use Mist/MetaMask's provider 94 | window.web3 = new Web3(web3.currentProvider); 95 | } else { 96 | console.warn("No web3 detected. Falling back to http://127.0.0.1:9545. You should remove this fallback when you deploy live, as it's inherently insecure. Consider switching to Metamask for development. More info here: http://truffleframework.com/tutorials/truffle-and-metamask"); 97 | // fallback - use your fallback strategy (local node / hosted node + in-dapp id mgmt / fail) 98 | window.web3 = new Web3(new Web3.providers.HttpProvider("http://127.0.0.1:9545")); 99 | } 100 | 101 | App.start(); 102 | }); 103 | -------------------------------------------------------------------------------- /Chapter01/app/stylesheets/app.css: -------------------------------------------------------------------------------- 1 | body { 2 | margin-left: 25%; 3 | margin-right: 25%; 4 | margin-top: 10%; 5 | font-family: "Open Sans", sans-serif; 6 | } 7 | 8 | label { 9 | display: inline-block; 10 | width: 100px; 11 | } 12 | 13 | input { 14 | width: 500px; 15 | padding: 5px; 16 | font-size: 16px; 17 | } 18 | 19 | button { 20 | font-size: 16px; 21 | padding: 5px; 22 | } 23 | 24 | h1, h2 { 25 | display: inline-block; 26 | vertical-align: middle; 27 | margin-top: 0px; 28 | margin-bottom: 10px; 29 | } 30 | 31 | h2 { 32 | color: #AAA; 33 | font-size: 32px; 34 | } 35 | 36 | h3 { 37 | font-weight: normal; 38 | color: #AAA; 39 | font-size: 24px; 40 | } 41 | 42 | .black { 43 | color: black; 44 | } 45 | 46 | #balance { 47 | color: black; 48 | } 49 | 50 | .hint { 51 | color: #666; 52 | } 53 | -------------------------------------------------------------------------------- /Chapter01/box-img-lg.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/PacktPublishing/Ethereum-Projects-for-Beginners/96e2448b3061b45b6a77e54d527524cd0c23d4be/Chapter01/box-img-lg.png -------------------------------------------------------------------------------- /Chapter01/box-img-sm.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/PacktPublishing/Ethereum-Projects-for-Beginners/96e2448b3061b45b6a77e54d527524cd0c23d4be/Chapter01/box-img-sm.png -------------------------------------------------------------------------------- /Chapter01/build/contracts/ConvertLib.json: -------------------------------------------------------------------------------- 1 | { 2 | "contractName": "ConvertLib", 3 | "abi": [ 4 | { 5 | "constant": true, 6 | "inputs": [ 7 | { 8 | "name": "amount", 9 | "type": "uint256" 10 | }, 11 | { 12 | "name": "conversionRate", 13 | "type": "uint256" 14 | } 15 | ], 16 | "name": "convert", 17 | "outputs": [ 18 | { 19 | "name": "convertedAmount", 20 | "type": "uint256" 21 | } 22 | ], 23 | "payable": false, 24 | "stateMutability": "pure", 25 | "type": "function" 26 | } 27 | ], 28 | "bytecode": "0x6060604052341561000f57600080fd5b60b08061001d6000396000f300606060405260043610603f576000357c0100000000000000000000000000000000000000000000000000000000900463ffffffff16806396e4ee3d146044575b600080fd5b606160048080359060200190919080359060200190919050506077565b6040518082815260200191505060405180910390f35b60008183029050929150505600a165627a7a72305820c203807f080cb0220a38ed19317d8308a1da97f006865dcd0531aa87a1df060a0029", 29 | "deployedBytecode": "0x606060405260043610603f576000357c0100000000000000000000000000000000000000000000000000000000900463ffffffff16806396e4ee3d146044575b600080fd5b606160048080359060200190919080359060200190919050506077565b6040518082815260200191505060405180910390f35b60008183029050929150505600a165627a7a72305820c203807f080cb0220a38ed19317d8308a1da97f006865dcd0531aa87a1df060a0029", 30 | "sourceMap": "26:170:0:-;;;;;;;;;;;;;;;;;", 31 | "deployedSourceMap": "26:170:0:-;;;;;;;;;;;;;;;;;;;;;;;;50:144;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;121:20;173:14;164:6;:23;157:30;;50:144;;;;:::o", 32 | "source": "pragma solidity ^0.4.17;\n\nlibrary ConvertLib{\n function convert(uint amount,uint conversionRate) public pure returns (uint convertedAmount)\n {\n return amount * conversionRate;\n }\n}\n", 33 | "sourcePath": "F:\\Dev\\ethereum\\my_first_project\\contracts\\ConvertLib.sol", 34 | "ast": { 35 | "attributes": { 36 | "absolutePath": "/F/Dev/ethereum/my_first_project/contracts/ConvertLib.sol", 37 | "exportedSymbols": { 38 | "ConvertLib": [ 39 | 16 40 | ] 41 | } 42 | }, 43 | "children": [ 44 | { 45 | "attributes": { 46 | "literals": [ 47 | "solidity", 48 | "^", 49 | "0.4", 50 | ".17" 51 | ] 52 | }, 53 | "id": 1, 54 | "name": "PragmaDirective", 55 | "src": "0:24:0" 56 | }, 57 | { 58 | "attributes": { 59 | "baseContracts": [ 60 | null 61 | ], 62 | "contractDependencies": [ 63 | null 64 | ], 65 | "contractKind": "library", 66 | "documentation": null, 67 | "fullyImplemented": true, 68 | "linearizedBaseContracts": [ 69 | 16 70 | ], 71 | "name": "ConvertLib", 72 | "scope": 17 73 | }, 74 | "children": [ 75 | { 76 | "attributes": { 77 | "constant": true, 78 | "implemented": true, 79 | "isConstructor": false, 80 | "modifiers": [ 81 | null 82 | ], 83 | "name": "convert", 84 | "payable": false, 85 | "scope": 16, 86 | "stateMutability": "pure", 87 | "superFunction": null, 88 | "visibility": "public" 89 | }, 90 | "children": [ 91 | { 92 | "children": [ 93 | { 94 | "attributes": { 95 | "constant": false, 96 | "name": "amount", 97 | "scope": 15, 98 | "stateVariable": false, 99 | "storageLocation": "default", 100 | "type": "uint256", 101 | "value": null, 102 | "visibility": "internal" 103 | }, 104 | "children": [ 105 | { 106 | "attributes": { 107 | "name": "uint", 108 | "type": "uint256" 109 | }, 110 | "id": 2, 111 | "name": "ElementaryTypeName", 112 | "src": "67:4:0" 113 | } 114 | ], 115 | "id": 3, 116 | "name": "VariableDeclaration", 117 | "src": "67:11:0" 118 | }, 119 | { 120 | "attributes": { 121 | "constant": false, 122 | "name": "conversionRate", 123 | "scope": 15, 124 | "stateVariable": false, 125 | "storageLocation": "default", 126 | "type": "uint256", 127 | "value": null, 128 | "visibility": "internal" 129 | }, 130 | "children": [ 131 | { 132 | "attributes": { 133 | "name": "uint", 134 | "type": "uint256" 135 | }, 136 | "id": 4, 137 | "name": "ElementaryTypeName", 138 | "src": "79:4:0" 139 | } 140 | ], 141 | "id": 5, 142 | "name": "VariableDeclaration", 143 | "src": "79:19:0" 144 | } 145 | ], 146 | "id": 6, 147 | "name": "ParameterList", 148 | "src": "66:33:0" 149 | }, 150 | { 151 | "children": [ 152 | { 153 | "attributes": { 154 | "constant": false, 155 | "name": "convertedAmount", 156 | "scope": 15, 157 | "stateVariable": false, 158 | "storageLocation": "default", 159 | "type": "uint256", 160 | "value": null, 161 | "visibility": "internal" 162 | }, 163 | "children": [ 164 | { 165 | "attributes": { 166 | "name": "uint", 167 | "type": "uint256" 168 | }, 169 | "id": 7, 170 | "name": "ElementaryTypeName", 171 | "src": "121:4:0" 172 | } 173 | ], 174 | "id": 8, 175 | "name": "VariableDeclaration", 176 | "src": "121:20:0" 177 | } 178 | ], 179 | "id": 9, 180 | "name": "ParameterList", 181 | "src": "120:22:0" 182 | }, 183 | { 184 | "children": [ 185 | { 186 | "attributes": { 187 | "functionReturnParameters": 9 188 | }, 189 | "children": [ 190 | { 191 | "attributes": { 192 | "argumentTypes": null, 193 | "commonType": { 194 | "typeIdentifier": "t_uint256", 195 | "typeString": "uint256" 196 | }, 197 | "isConstant": false, 198 | "isLValue": false, 199 | "isPure": false, 200 | "lValueRequested": false, 201 | "operator": "*", 202 | "type": "uint256" 203 | }, 204 | "children": [ 205 | { 206 | "attributes": { 207 | "argumentTypes": null, 208 | "overloadedDeclarations": [ 209 | null 210 | ], 211 | "referencedDeclaration": 3, 212 | "type": "uint256", 213 | "value": "amount" 214 | }, 215 | "id": 10, 216 | "name": "Identifier", 217 | "src": "164:6:0" 218 | }, 219 | { 220 | "attributes": { 221 | "argumentTypes": null, 222 | "overloadedDeclarations": [ 223 | null 224 | ], 225 | "referencedDeclaration": 5, 226 | "type": "uint256", 227 | "value": "conversionRate" 228 | }, 229 | "id": 11, 230 | "name": "Identifier", 231 | "src": "173:14:0" 232 | } 233 | ], 234 | "id": 12, 235 | "name": "BinaryOperation", 236 | "src": "164:23:0" 237 | } 238 | ], 239 | "id": 13, 240 | "name": "Return", 241 | "src": "157:30:0" 242 | } 243 | ], 244 | "id": 14, 245 | "name": "Block", 246 | "src": "147:47:0" 247 | } 248 | ], 249 | "id": 15, 250 | "name": "FunctionDefinition", 251 | "src": "50:144:0" 252 | } 253 | ], 254 | "id": 16, 255 | "name": "ContractDefinition", 256 | "src": "26:170:0" 257 | } 258 | ], 259 | "id": 17, 260 | "name": "SourceUnit", 261 | "src": "0:197:0" 262 | }, 263 | "compiler": { 264 | "name": "solc", 265 | "version": "0.4.19+commit.c4cbbb05.Emscripten.clang" 266 | }, 267 | "networks": { 268 | "1520343466351": { 269 | "events": {}, 270 | "links": {}, 271 | "address": "0x65bf225df375be248775020ab921c0159c2a4152" 272 | } 273 | }, 274 | "schemaVersion": "1.0.1", 275 | "updatedAt": "2018-03-06T13:42:17.603Z" 276 | } -------------------------------------------------------------------------------- /Chapter01/build/contracts/Migrations.json: -------------------------------------------------------------------------------- 1 | { 2 | "contractName": "Migrations", 3 | "abi": [ 4 | { 5 | "constant": true, 6 | "inputs": [], 7 | "name": "last_completed_migration", 8 | "outputs": [ 9 | { 10 | "name": "", 11 | "type": "uint256" 12 | } 13 | ], 14 | "payable": false, 15 | "stateMutability": "view", 16 | "type": "function" 17 | }, 18 | { 19 | "constant": true, 20 | "inputs": [], 21 | "name": "owner", 22 | "outputs": [ 23 | { 24 | "name": "", 25 | "type": "address" 26 | } 27 | ], 28 | "payable": false, 29 | "stateMutability": "view", 30 | "type": "function" 31 | }, 32 | { 33 | "inputs": [], 34 | "payable": false, 35 | "stateMutability": "nonpayable", 36 | "type": "constructor" 37 | }, 38 | { 39 | "constant": false, 40 | "inputs": [ 41 | { 42 | "name": "completed", 43 | "type": "uint256" 44 | } 45 | ], 46 | "name": "setCompleted", 47 | "outputs": [], 48 | "payable": false, 49 | "stateMutability": "nonpayable", 50 | "type": "function" 51 | }, 52 | { 53 | "constant": false, 54 | "inputs": [ 55 | { 56 | "name": "new_address", 57 | "type": "address" 58 | } 59 | ], 60 | "name": "upgrade", 61 | "outputs": [], 62 | "payable": false, 63 | "stateMutability": "nonpayable", 64 | "type": "function" 65 | } 66 | ], 67 | "bytecode": "0x6060604052341561000f57600080fd5b336000806101000a81548173ffffffffffffffffffffffffffffffffffffffff021916908373ffffffffffffffffffffffffffffffffffffffff1602179055506102db8061005e6000396000f300606060405260043610610062576000357c0100000000000000000000000000000000000000000000000000000000900463ffffffff1680630900f01014610067578063445df0ac146100a05780638da5cb5b146100c9578063fdacd5761461011e575b600080fd5b341561007257600080fd5b61009e600480803573ffffffffffffffffffffffffffffffffffffffff16906020019091905050610141565b005b34156100ab57600080fd5b6100b3610224565b6040518082815260200191505060405180910390f35b34156100d457600080fd5b6100dc61022a565b604051808273ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff16815260200191505060405180910390f35b341561012957600080fd5b61013f600480803590602001909190505061024f565b005b60008060009054906101000a900473ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff163373ffffffffffffffffffffffffffffffffffffffff161415610220578190508073ffffffffffffffffffffffffffffffffffffffff1663fdacd5766001546040518263ffffffff167c010000000000000000000000000000000000000000000000000000000002815260040180828152602001915050600060405180830381600087803b151561020b57600080fd5b6102c65a03f1151561021c57600080fd5b5050505b5050565b60015481565b6000809054906101000a900473ffffffffffffffffffffffffffffffffffffffff1681565b6000809054906101000a900473ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff163373ffffffffffffffffffffffffffffffffffffffff1614156102ac57806001819055505b505600a165627a7a7230582033a36094f4743c7313f2088c3792f043703667e5275960cb7f17e71630f261e80029", 68 | "deployedBytecode": "0x606060405260043610610062576000357c0100000000000000000000000000000000000000000000000000000000900463ffffffff1680630900f01014610067578063445df0ac146100a05780638da5cb5b146100c9578063fdacd5761461011e575b600080fd5b341561007257600080fd5b61009e600480803573ffffffffffffffffffffffffffffffffffffffff16906020019091905050610141565b005b34156100ab57600080fd5b6100b3610224565b6040518082815260200191505060405180910390f35b34156100d457600080fd5b6100dc61022a565b604051808273ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff16815260200191505060405180910390f35b341561012957600080fd5b61013f600480803590602001909190505061024f565b005b60008060009054906101000a900473ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff163373ffffffffffffffffffffffffffffffffffffffff161415610220578190508073ffffffffffffffffffffffffffffffffffffffff1663fdacd5766001546040518263ffffffff167c010000000000000000000000000000000000000000000000000000000002815260040180828152602001915050600060405180830381600087803b151561020b57600080fd5b6102c65a03f1151561021c57600080fd5b5050505b5050565b60015481565b6000809054906101000a900473ffffffffffffffffffffffffffffffffffffffff1681565b6000809054906101000a900473ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff163373ffffffffffffffffffffffffffffffffffffffff1614156102ac57806001819055505b505600a165627a7a7230582033a36094f4743c7313f2088c3792f043703667e5275960cb7f17e71630f261e80029", 69 | "sourceMap": "26:488:2:-;;;178:58;;;;;;;;221:10;213:5;;:18;;;;;;;;;;;;;;;;;;26:488;;;;;;", 70 | "deployedSourceMap": "26:488:2:-;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;347:165;;;;;;;;;;;;;;;;;;;;;;;;;;;;74:36;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;50:20;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;240:103;;;;;;;;;;;;;;;;;;;;;;;;;;347:165;409:19;161:5;;;;;;;;;;;147:19;;:10;:19;;;143:26;;;442:11;409:45;;460:8;:21;;;482:24;;460:47;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;143:26;347:165;;:::o;74:36::-;;;;:::o;50:20::-;;;;;;;;;;;;;:::o;240:103::-;161:5;;;;;;;;;;;147:19;;:10;:19;;;143:26;;;329:9;302:24;:36;;;;143:26;240:103;:::o", 71 | "source": "pragma solidity ^0.4.17;\n\ncontract Migrations {\n address public owner;\n uint public last_completed_migration;\n\n modifier restricted() {\n if (msg.sender == owner) _;\n }\n\n function Migrations() public {\n owner = msg.sender;\n }\n\n function setCompleted(uint completed) public restricted {\n last_completed_migration = completed;\n }\n\n function upgrade(address new_address) public restricted {\n Migrations upgraded = Migrations(new_address);\n upgraded.setCompleted(last_completed_migration);\n }\n}\n", 72 | "sourcePath": "F:\\Dev\\ethereum\\my_first_project\\contracts\\Migrations.sol", 73 | "ast": { 74 | "attributes": { 75 | "absolutePath": "/F/Dev/ethereum/my_first_project/contracts/Migrations.sol", 76 | "exportedSymbols": { 77 | "Migrations": [ 78 | 169 79 | ] 80 | } 81 | }, 82 | "children": [ 83 | { 84 | "attributes": { 85 | "literals": [ 86 | "solidity", 87 | "^", 88 | "0.4", 89 | ".17" 90 | ] 91 | }, 92 | "id": 114, 93 | "name": "PragmaDirective", 94 | "src": "0:24:2" 95 | }, 96 | { 97 | "attributes": { 98 | "baseContracts": [ 99 | null 100 | ], 101 | "contractDependencies": [ 102 | null 103 | ], 104 | "contractKind": "contract", 105 | "documentation": null, 106 | "fullyImplemented": true, 107 | "linearizedBaseContracts": [ 108 | 169 109 | ], 110 | "name": "Migrations", 111 | "scope": 170 112 | }, 113 | "children": [ 114 | { 115 | "attributes": { 116 | "constant": false, 117 | "name": "owner", 118 | "scope": 169, 119 | "stateVariable": true, 120 | "storageLocation": "default", 121 | "type": "address", 122 | "value": null, 123 | "visibility": "public" 124 | }, 125 | "children": [ 126 | { 127 | "attributes": { 128 | "name": "address", 129 | "type": "address" 130 | }, 131 | "id": 115, 132 | "name": "ElementaryTypeName", 133 | "src": "50:7:2" 134 | } 135 | ], 136 | "id": 116, 137 | "name": "VariableDeclaration", 138 | "src": "50:20:2" 139 | }, 140 | { 141 | "attributes": { 142 | "constant": false, 143 | "name": "last_completed_migration", 144 | "scope": 169, 145 | "stateVariable": true, 146 | "storageLocation": "default", 147 | "type": "uint256", 148 | "value": null, 149 | "visibility": "public" 150 | }, 151 | "children": [ 152 | { 153 | "attributes": { 154 | "name": "uint", 155 | "type": "uint256" 156 | }, 157 | "id": 117, 158 | "name": "ElementaryTypeName", 159 | "src": "74:4:2" 160 | } 161 | ], 162 | "id": 118, 163 | "name": "VariableDeclaration", 164 | "src": "74:36:2" 165 | }, 166 | { 167 | "attributes": { 168 | "name": "restricted", 169 | "visibility": "internal" 170 | }, 171 | "children": [ 172 | { 173 | "attributes": { 174 | "parameters": [ 175 | null 176 | ] 177 | }, 178 | "children": [], 179 | "id": 119, 180 | "name": "ParameterList", 181 | "src": "134:2:2" 182 | }, 183 | { 184 | "children": [ 185 | { 186 | "attributes": { 187 | "falseBody": null 188 | }, 189 | "children": [ 190 | { 191 | "attributes": { 192 | "argumentTypes": null, 193 | "commonType": { 194 | "typeIdentifier": "t_address", 195 | "typeString": "address" 196 | }, 197 | "isConstant": false, 198 | "isLValue": false, 199 | "isPure": false, 200 | "lValueRequested": false, 201 | "operator": "==", 202 | "type": "bool" 203 | }, 204 | "children": [ 205 | { 206 | "attributes": { 207 | "argumentTypes": null, 208 | "isConstant": false, 209 | "isLValue": false, 210 | "isPure": false, 211 | "lValueRequested": false, 212 | "member_name": "sender", 213 | "referencedDeclaration": null, 214 | "type": "address" 215 | }, 216 | "children": [ 217 | { 218 | "attributes": { 219 | "argumentTypes": null, 220 | "overloadedDeclarations": [ 221 | null 222 | ], 223 | "referencedDeclaration": 181, 224 | "type": "msg", 225 | "value": "msg" 226 | }, 227 | "id": 120, 228 | "name": "Identifier", 229 | "src": "147:3:2" 230 | } 231 | ], 232 | "id": 121, 233 | "name": "MemberAccess", 234 | "src": "147:10:2" 235 | }, 236 | { 237 | "attributes": { 238 | "argumentTypes": null, 239 | "overloadedDeclarations": [ 240 | null 241 | ], 242 | "referencedDeclaration": 116, 243 | "type": "address", 244 | "value": "owner" 245 | }, 246 | "id": 122, 247 | "name": "Identifier", 248 | "src": "161:5:2" 249 | } 250 | ], 251 | "id": 123, 252 | "name": "BinaryOperation", 253 | "src": "147:19:2" 254 | }, 255 | { 256 | "id": 124, 257 | "name": "PlaceholderStatement", 258 | "src": "168:1:2" 259 | } 260 | ], 261 | "id": 125, 262 | "name": "IfStatement", 263 | "src": "143:26:2" 264 | } 265 | ], 266 | "id": 126, 267 | "name": "Block", 268 | "src": "137:37:2" 269 | } 270 | ], 271 | "id": 127, 272 | "name": "ModifierDefinition", 273 | "src": "115:59:2" 274 | }, 275 | { 276 | "attributes": { 277 | "constant": false, 278 | "implemented": true, 279 | "isConstructor": true, 280 | "modifiers": [ 281 | null 282 | ], 283 | "name": "Migrations", 284 | "payable": false, 285 | "scope": 169, 286 | "stateMutability": "nonpayable", 287 | "superFunction": null, 288 | "visibility": "public" 289 | }, 290 | "children": [ 291 | { 292 | "attributes": { 293 | "parameters": [ 294 | null 295 | ] 296 | }, 297 | "children": [], 298 | "id": 128, 299 | "name": "ParameterList", 300 | "src": "197:2:2" 301 | }, 302 | { 303 | "attributes": { 304 | "parameters": [ 305 | null 306 | ] 307 | }, 308 | "children": [], 309 | "id": 129, 310 | "name": "ParameterList", 311 | "src": "207:0:2" 312 | }, 313 | { 314 | "children": [ 315 | { 316 | "children": [ 317 | { 318 | "attributes": { 319 | "argumentTypes": null, 320 | "isConstant": false, 321 | "isLValue": false, 322 | "isPure": false, 323 | "lValueRequested": false, 324 | "operator": "=", 325 | "type": "address" 326 | }, 327 | "children": [ 328 | { 329 | "attributes": { 330 | "argumentTypes": null, 331 | "overloadedDeclarations": [ 332 | null 333 | ], 334 | "referencedDeclaration": 116, 335 | "type": "address", 336 | "value": "owner" 337 | }, 338 | "id": 130, 339 | "name": "Identifier", 340 | "src": "213:5:2" 341 | }, 342 | { 343 | "attributes": { 344 | "argumentTypes": null, 345 | "isConstant": false, 346 | "isLValue": false, 347 | "isPure": false, 348 | "lValueRequested": false, 349 | "member_name": "sender", 350 | "referencedDeclaration": null, 351 | "type": "address" 352 | }, 353 | "children": [ 354 | { 355 | "attributes": { 356 | "argumentTypes": null, 357 | "overloadedDeclarations": [ 358 | null 359 | ], 360 | "referencedDeclaration": 181, 361 | "type": "msg", 362 | "value": "msg" 363 | }, 364 | "id": 131, 365 | "name": "Identifier", 366 | "src": "221:3:2" 367 | } 368 | ], 369 | "id": 132, 370 | "name": "MemberAccess", 371 | "src": "221:10:2" 372 | } 373 | ], 374 | "id": 133, 375 | "name": "Assignment", 376 | "src": "213:18:2" 377 | } 378 | ], 379 | "id": 134, 380 | "name": "ExpressionStatement", 381 | "src": "213:18:2" 382 | } 383 | ], 384 | "id": 135, 385 | "name": "Block", 386 | "src": "207:29:2" 387 | } 388 | ], 389 | "id": 136, 390 | "name": "FunctionDefinition", 391 | "src": "178:58:2" 392 | }, 393 | { 394 | "attributes": { 395 | "constant": false, 396 | "implemented": true, 397 | "isConstructor": false, 398 | "name": "setCompleted", 399 | "payable": false, 400 | "scope": 169, 401 | "stateMutability": "nonpayable", 402 | "superFunction": null, 403 | "visibility": "public" 404 | }, 405 | "children": [ 406 | { 407 | "children": [ 408 | { 409 | "attributes": { 410 | "constant": false, 411 | "name": "completed", 412 | "scope": 148, 413 | "stateVariable": false, 414 | "storageLocation": "default", 415 | "type": "uint256", 416 | "value": null, 417 | "visibility": "internal" 418 | }, 419 | "children": [ 420 | { 421 | "attributes": { 422 | "name": "uint", 423 | "type": "uint256" 424 | }, 425 | "id": 137, 426 | "name": "ElementaryTypeName", 427 | "src": "262:4:2" 428 | } 429 | ], 430 | "id": 138, 431 | "name": "VariableDeclaration", 432 | "src": "262:14:2" 433 | } 434 | ], 435 | "id": 139, 436 | "name": "ParameterList", 437 | "src": "261:16:2" 438 | }, 439 | { 440 | "attributes": { 441 | "parameters": [ 442 | null 443 | ] 444 | }, 445 | "children": [], 446 | "id": 142, 447 | "name": "ParameterList", 448 | "src": "296:0:2" 449 | }, 450 | { 451 | "attributes": { 452 | "arguments": [ 453 | null 454 | ] 455 | }, 456 | "children": [ 457 | { 458 | "attributes": { 459 | "argumentTypes": null, 460 | "overloadedDeclarations": [ 461 | null 462 | ], 463 | "referencedDeclaration": 127, 464 | "type": "modifier ()", 465 | "value": "restricted" 466 | }, 467 | "id": 140, 468 | "name": "Identifier", 469 | "src": "285:10:2" 470 | } 471 | ], 472 | "id": 141, 473 | "name": "ModifierInvocation", 474 | "src": "285:10:2" 475 | }, 476 | { 477 | "children": [ 478 | { 479 | "children": [ 480 | { 481 | "attributes": { 482 | "argumentTypes": null, 483 | "isConstant": false, 484 | "isLValue": false, 485 | "isPure": false, 486 | "lValueRequested": false, 487 | "operator": "=", 488 | "type": "uint256" 489 | }, 490 | "children": [ 491 | { 492 | "attributes": { 493 | "argumentTypes": null, 494 | "overloadedDeclarations": [ 495 | null 496 | ], 497 | "referencedDeclaration": 118, 498 | "type": "uint256", 499 | "value": "last_completed_migration" 500 | }, 501 | "id": 143, 502 | "name": "Identifier", 503 | "src": "302:24:2" 504 | }, 505 | { 506 | "attributes": { 507 | "argumentTypes": null, 508 | "overloadedDeclarations": [ 509 | null 510 | ], 511 | "referencedDeclaration": 138, 512 | "type": "uint256", 513 | "value": "completed" 514 | }, 515 | "id": 144, 516 | "name": "Identifier", 517 | "src": "329:9:2" 518 | } 519 | ], 520 | "id": 145, 521 | "name": "Assignment", 522 | "src": "302:36:2" 523 | } 524 | ], 525 | "id": 146, 526 | "name": "ExpressionStatement", 527 | "src": "302:36:2" 528 | } 529 | ], 530 | "id": 147, 531 | "name": "Block", 532 | "src": "296:47:2" 533 | } 534 | ], 535 | "id": 148, 536 | "name": "FunctionDefinition", 537 | "src": "240:103:2" 538 | }, 539 | { 540 | "attributes": { 541 | "constant": false, 542 | "implemented": true, 543 | "isConstructor": false, 544 | "name": "upgrade", 545 | "payable": false, 546 | "scope": 169, 547 | "stateMutability": "nonpayable", 548 | "superFunction": null, 549 | "visibility": "public" 550 | }, 551 | "children": [ 552 | { 553 | "children": [ 554 | { 555 | "attributes": { 556 | "constant": false, 557 | "name": "new_address", 558 | "scope": 168, 559 | "stateVariable": false, 560 | "storageLocation": "default", 561 | "type": "address", 562 | "value": null, 563 | "visibility": "internal" 564 | }, 565 | "children": [ 566 | { 567 | "attributes": { 568 | "name": "address", 569 | "type": "address" 570 | }, 571 | "id": 149, 572 | "name": "ElementaryTypeName", 573 | "src": "364:7:2" 574 | } 575 | ], 576 | "id": 150, 577 | "name": "VariableDeclaration", 578 | "src": "364:19:2" 579 | } 580 | ], 581 | "id": 151, 582 | "name": "ParameterList", 583 | "src": "363:21:2" 584 | }, 585 | { 586 | "attributes": { 587 | "parameters": [ 588 | null 589 | ] 590 | }, 591 | "children": [], 592 | "id": 154, 593 | "name": "ParameterList", 594 | "src": "403:0:2" 595 | }, 596 | { 597 | "attributes": { 598 | "arguments": [ 599 | null 600 | ] 601 | }, 602 | "children": [ 603 | { 604 | "attributes": { 605 | "argumentTypes": null, 606 | "overloadedDeclarations": [ 607 | null 608 | ], 609 | "referencedDeclaration": 127, 610 | "type": "modifier ()", 611 | "value": "restricted" 612 | }, 613 | "id": 152, 614 | "name": "Identifier", 615 | "src": "392:10:2" 616 | } 617 | ], 618 | "id": 153, 619 | "name": "ModifierInvocation", 620 | "src": "392:10:2" 621 | }, 622 | { 623 | "children": [ 624 | { 625 | "attributes": { 626 | "assignments": [ 627 | 156 628 | ] 629 | }, 630 | "children": [ 631 | { 632 | "attributes": { 633 | "constant": false, 634 | "name": "upgraded", 635 | "scope": 168, 636 | "stateVariable": false, 637 | "storageLocation": "default", 638 | "type": "contract Migrations", 639 | "value": null, 640 | "visibility": "internal" 641 | }, 642 | "children": [ 643 | { 644 | "attributes": { 645 | "contractScope": null, 646 | "name": "Migrations", 647 | "referencedDeclaration": 169, 648 | "type": "contract Migrations" 649 | }, 650 | "id": 155, 651 | "name": "UserDefinedTypeName", 652 | "src": "409:10:2" 653 | } 654 | ], 655 | "id": 156, 656 | "name": "VariableDeclaration", 657 | "src": "409:19:2" 658 | }, 659 | { 660 | "attributes": { 661 | "argumentTypes": null, 662 | "isConstant": false, 663 | "isLValue": false, 664 | "isPure": false, 665 | "isStructConstructorCall": false, 666 | "lValueRequested": false, 667 | "names": [ 668 | null 669 | ], 670 | "type": "contract Migrations", 671 | "type_conversion": true 672 | }, 673 | "children": [ 674 | { 675 | "attributes": { 676 | "argumentTypes": [ 677 | { 678 | "typeIdentifier": "t_address", 679 | "typeString": "address" 680 | } 681 | ], 682 | "overloadedDeclarations": [ 683 | null 684 | ], 685 | "referencedDeclaration": 169, 686 | "type": "type(contract Migrations)", 687 | "value": "Migrations" 688 | }, 689 | "id": 157, 690 | "name": "Identifier", 691 | "src": "431:10:2" 692 | }, 693 | { 694 | "attributes": { 695 | "argumentTypes": null, 696 | "overloadedDeclarations": [ 697 | null 698 | ], 699 | "referencedDeclaration": 150, 700 | "type": "address", 701 | "value": "new_address" 702 | }, 703 | "id": 158, 704 | "name": "Identifier", 705 | "src": "442:11:2" 706 | } 707 | ], 708 | "id": 159, 709 | "name": "FunctionCall", 710 | "src": "431:23:2" 711 | } 712 | ], 713 | "id": 160, 714 | "name": "VariableDeclarationStatement", 715 | "src": "409:45:2" 716 | }, 717 | { 718 | "children": [ 719 | { 720 | "attributes": { 721 | "argumentTypes": null, 722 | "isConstant": false, 723 | "isLValue": false, 724 | "isPure": false, 725 | "isStructConstructorCall": false, 726 | "lValueRequested": false, 727 | "names": [ 728 | null 729 | ], 730 | "type": "tuple()", 731 | "type_conversion": false 732 | }, 733 | "children": [ 734 | { 735 | "attributes": { 736 | "argumentTypes": [ 737 | { 738 | "typeIdentifier": "t_uint256", 739 | "typeString": "uint256" 740 | } 741 | ], 742 | "isConstant": false, 743 | "isLValue": false, 744 | "isPure": false, 745 | "lValueRequested": false, 746 | "member_name": "setCompleted", 747 | "referencedDeclaration": 148, 748 | "type": "function (uint256) external" 749 | }, 750 | "children": [ 751 | { 752 | "attributes": { 753 | "argumentTypes": null, 754 | "overloadedDeclarations": [ 755 | null 756 | ], 757 | "referencedDeclaration": 156, 758 | "type": "contract Migrations", 759 | "value": "upgraded" 760 | }, 761 | "id": 161, 762 | "name": "Identifier", 763 | "src": "460:8:2" 764 | } 765 | ], 766 | "id": 163, 767 | "name": "MemberAccess", 768 | "src": "460:21:2" 769 | }, 770 | { 771 | "attributes": { 772 | "argumentTypes": null, 773 | "overloadedDeclarations": [ 774 | null 775 | ], 776 | "referencedDeclaration": 118, 777 | "type": "uint256", 778 | "value": "last_completed_migration" 779 | }, 780 | "id": 164, 781 | "name": "Identifier", 782 | "src": "482:24:2" 783 | } 784 | ], 785 | "id": 165, 786 | "name": "FunctionCall", 787 | "src": "460:47:2" 788 | } 789 | ], 790 | "id": 166, 791 | "name": "ExpressionStatement", 792 | "src": "460:47:2" 793 | } 794 | ], 795 | "id": 167, 796 | "name": "Block", 797 | "src": "403:109:2" 798 | } 799 | ], 800 | "id": 168, 801 | "name": "FunctionDefinition", 802 | "src": "347:165:2" 803 | } 804 | ], 805 | "id": 169, 806 | "name": "ContractDefinition", 807 | "src": "26:488:2" 808 | } 809 | ], 810 | "id": 170, 811 | "name": "SourceUnit", 812 | "src": "0:515:2" 813 | }, 814 | "compiler": { 815 | "name": "solc", 816 | "version": "0.4.19+commit.c4cbbb05.Emscripten.clang" 817 | }, 818 | "networks": { 819 | "1520343466351": { 820 | "events": {}, 821 | "links": {}, 822 | "address": "0x2e817544413d7205d7652ce86f112008e6d497a0" 823 | } 824 | }, 825 | "schemaVersion": "1.0.1", 826 | "updatedAt": "2018-03-06T13:42:17.605Z" 827 | } -------------------------------------------------------------------------------- /Chapter01/contracts/ConvertLib.sol: -------------------------------------------------------------------------------- 1 | pragma solidity ^0.4.17; 2 | 3 | library ConvertLib{ 4 | function convert(uint amount,uint conversionRate) public pure returns (uint convertedAmount) 5 | { 6 | return amount * conversionRate; 7 | } 8 | } 9 | -------------------------------------------------------------------------------- /Chapter01/contracts/MetaCoin.sol: -------------------------------------------------------------------------------- 1 | pragma solidity ^0.4.17; 2 | 3 | import "./ConvertLib.sol"; 4 | 5 | // This is just a simple example of a coin-like contract. 6 | // It is not standards compatible and cannot be expected to talk to other 7 | // coin/token contracts. If you want to create a standards-compliant 8 | // token, see: https://github.com/ConsenSys/Tokens. Cheers! 9 | 10 | contract MetaCoin { 11 | mapping (address => uint) balances; 12 | 13 | event Transfer(address indexed _from, address indexed _to, uint256 _value); 14 | 15 | function MetaCoin() public { 16 | balances[tx.origin] = 10000; 17 | } 18 | 19 | function sendCoin(address receiver, uint amount) public returns(bool sufficient) { 20 | if (balances[msg.sender] < amount) return false; 21 | balances[msg.sender] -= amount; 22 | balances[receiver] += amount; 23 | Transfer(msg.sender, receiver, amount); 24 | return true; 25 | } 26 | 27 | function getBalanceInEth(address addr) public view returns(uint){ 28 | return ConvertLib.convert(getBalance(addr),2); 29 | } 30 | 31 | function getBalance(address addr) public view returns(uint) { 32 | return balances[addr]; 33 | } 34 | } 35 | -------------------------------------------------------------------------------- /Chapter01/contracts/Migrations.sol: -------------------------------------------------------------------------------- 1 | pragma solidity ^0.4.17; 2 | 3 | contract Migrations { 4 | address public owner; 5 | uint public last_completed_migration; 6 | 7 | modifier restricted() { 8 | if (msg.sender == owner) _; 9 | } 10 | 11 | function Migrations() public { 12 | owner = msg.sender; 13 | } 14 | 15 | function setCompleted(uint completed) public restricted { 16 | last_completed_migration = completed; 17 | } 18 | 19 | function upgrade(address new_address) public restricted { 20 | Migrations upgraded = Migrations(new_address); 21 | upgraded.setCompleted(last_completed_migration); 22 | } 23 | } 24 | -------------------------------------------------------------------------------- /Chapter01/migrations/1_initial_migration.js: -------------------------------------------------------------------------------- 1 | var Migrations = artifacts.require("./Migrations.sol"); 2 | 3 | module.exports = function(deployer) { 4 | deployer.deploy(Migrations); 5 | }; 6 | -------------------------------------------------------------------------------- /Chapter01/migrations/2_deploy_contracts.js: -------------------------------------------------------------------------------- 1 | var ConvertLib = artifacts.require("./ConvertLib.sol"); 2 | var MetaCoin = artifacts.require("./MetaCoin.sol"); 3 | 4 | module.exports = function(deployer) { 5 | deployer.deploy(ConvertLib); 6 | deployer.link(ConvertLib, MetaCoin); 7 | deployer.deploy(MetaCoin); 8 | }; 9 | -------------------------------------------------------------------------------- /Chapter01/package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "truffle-init-webpack", 3 | "version": "0.0.2", 4 | "description": "Frontend example using truffle v3", 5 | "scripts": { 6 | "lint": "eslint ./", 7 | "build": "webpack", 8 | "dev": "webpack-dev-server" 9 | }, 10 | "author": "Douglas von Kohorn", 11 | "license": "MIT", 12 | "devDependencies": { 13 | "babel-cli": "^6.22.2", 14 | "babel-core": "^6.22.1", 15 | "babel-eslint": "^6.1.2", 16 | "babel-loader": "^6.2.10", 17 | "babel-plugin-transform-runtime": "^6.22.0", 18 | "babel-preset-env": "^1.1.8", 19 | "babel-preset-es2015": "^6.22.0", 20 | "babel-register": "^6.22.0", 21 | "copy-webpack-plugin": "^4.0.1", 22 | "css-loader": "^0.26.1", 23 | "eslint": "^3.14.0", 24 | "eslint-config-standard": "^6.0.0", 25 | "eslint-plugin-babel": "^4.0.0", 26 | "eslint-plugin-mocha": "^4.8.0", 27 | "eslint-plugin-promise": "^3.0.0", 28 | "eslint-plugin-standard": "^2.0.0", 29 | "html-webpack-plugin": "^2.28.0", 30 | "json-loader": "^0.5.4", 31 | "style-loader": "^0.13.1", 32 | "truffle-contract": "^1.1.11", 33 | "web3": "^0.20.0", 34 | "webpack": "^2.2.1", 35 | "webpack-dev-server": "^2.3.0" 36 | } 37 | } 38 | -------------------------------------------------------------------------------- /Chapter01/test/TestMetacoin.sol: -------------------------------------------------------------------------------- 1 | pragma solidity ^0.4.2; 2 | 3 | import "truffle/Assert.sol"; 4 | import "truffle/DeployedAddresses.sol"; 5 | import "../contracts/MetaCoin.sol"; 6 | 7 | contract TestMetacoin { 8 | 9 | function testInitialBalanceUsingDeployedContract() { 10 | MetaCoin meta = MetaCoin(DeployedAddresses.MetaCoin()); 11 | 12 | uint expected = 10000; 13 | 14 | Assert.equal(meta.getBalance(tx.origin), expected, "Owner should have 10000 MetaCoin initially"); 15 | } 16 | 17 | function testInitialBalanceWithNewMetaCoin() { 18 | MetaCoin meta = new MetaCoin(); 19 | 20 | uint expected = 10000; 21 | 22 | Assert.equal(meta.getBalance(tx.origin), expected, "Owner should have 10000 MetaCoin initially"); 23 | } 24 | 25 | } 26 | -------------------------------------------------------------------------------- /Chapter01/test/metacoin.js: -------------------------------------------------------------------------------- 1 | var MetaCoin = artifacts.require("./MetaCoin.sol"); 2 | 3 | contract('MetaCoin', function(accounts) { 4 | it("should put 10000 MetaCoin in the first account", function() { 5 | return MetaCoin.deployed().then(function(instance) { 6 | return instance.getBalance.call(accounts[0]); 7 | }).then(function(balance) { 8 | assert.equal(balance.valueOf(), 10000, "10000 wasn't in the first account"); 9 | }); 10 | }); 11 | it("should call a function that depends on a linked library", function() { 12 | var meta; 13 | var metaCoinBalance; 14 | var metaCoinEthBalance; 15 | 16 | return MetaCoin.deployed().then(function(instance) { 17 | meta = instance; 18 | return meta.getBalance.call(accounts[0]); 19 | }).then(function(outCoinBalance) { 20 | metaCoinBalance = outCoinBalance.toNumber(); 21 | return meta.getBalanceInEth.call(accounts[0]); 22 | }).then(function(outCoinBalanceEth) { 23 | metaCoinEthBalance = outCoinBalanceEth.toNumber(); 24 | }).then(function() { 25 | assert.equal(metaCoinEthBalance, 2 * metaCoinBalance, "Library function returned unexpeced function, linkage may be broken"); 26 | }); 27 | }); 28 | 29 | it("should send coin correctly", function() { 30 | var meta; 31 | 32 | // Get initial balances of first and second account. 33 | var account_one = accounts[0]; 34 | var account_two = accounts[1]; 35 | 36 | var account_one_starting_balance; 37 | var account_two_starting_balance; 38 | var account_one_ending_balance; 39 | var account_two_ending_balance; 40 | 41 | var amount = 10; 42 | 43 | return MetaCoin.deployed().then(function(instance) { 44 | meta = instance; 45 | return meta.getBalance.call(account_one); 46 | }).then(function(balance) { 47 | account_one_starting_balance = balance.toNumber(); 48 | return meta.getBalance.call(account_two); 49 | }).then(function(balance) { 50 | account_two_starting_balance = balance.toNumber(); 51 | return meta.sendCoin(account_two, amount, {from: account_one}); 52 | }).then(function() { 53 | return meta.getBalance.call(account_one); 54 | }).then(function(balance) { 55 | account_one_ending_balance = balance.toNumber(); 56 | return meta.getBalance.call(account_two); 57 | }).then(function(balance) { 58 | account_two_ending_balance = balance.toNumber(); 59 | 60 | assert.equal(account_one_ending_balance, account_one_starting_balance - amount, "Amount wasn't correctly taken from the sender"); 61 | assert.equal(account_two_ending_balance, account_two_starting_balance + amount, "Amount wasn't correctly sent to the receiver"); 62 | }); 63 | }); 64 | }); 65 | -------------------------------------------------------------------------------- /Chapter01/truffle.js: -------------------------------------------------------------------------------- 1 | // Allows us to use ES6 in our migrations and tests. 2 | require('babel-register') 3 | 4 | module.exports = { 5 | networks: { 6 | development: { 7 | host: '127.0.0.1', 8 | port: 8545, 9 | network_id: '*' // Match any network id 10 | } 11 | } 12 | } 13 | -------------------------------------------------------------------------------- /Chapter01/webpack.config.js: -------------------------------------------------------------------------------- 1 | const path = require('path'); 2 | const CopyWebpackPlugin = require('copy-webpack-plugin'); 3 | 4 | module.exports = { 5 | entry: './app/javascripts/app.js', 6 | output: { 7 | path: path.resolve(__dirname, 'build'), 8 | filename: 'app.js' 9 | }, 10 | plugins: [ 11 | // Copy our app's index.html to the build folder. 12 | new CopyWebpackPlugin([ 13 | { from: './app/index.html', to: "index.html" } 14 | ]) 15 | ], 16 | module: { 17 | rules: [ 18 | { 19 | test: /\.css$/, 20 | use: [ 'style-loader', 'css-loader' ] 21 | } 22 | ], 23 | loaders: [ 24 | { test: /\.json$/, use: 'json-loader' }, 25 | { 26 | test: /\.js$/, 27 | exclude: /(node_modules|bower_components)/, 28 | loader: 'babel-loader', 29 | query: { 30 | presets: ['es2015'], 31 | plugins: ['transform-runtime'] 32 | } 33 | } 34 | ] 35 | } 36 | } 37 | -------------------------------------------------------------------------------- /Chapter02/app/index.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | MetaCoin - Truffle Webpack Demo w/ Frontend 5 | 6 | 7 | 8 | 9 |

MetaCoin

10 |

Example Truffle Dapp

11 |

You have META

12 | 13 |
14 |

Send MetaCoin

15 |
16 |
17 |

18 |

19 | 20 |
21 | Hint: open the browser developer console to view any errors and warnings. 22 | 23 | 24 | -------------------------------------------------------------------------------- /Chapter02/app/javascripts/app.js: -------------------------------------------------------------------------------- 1 | // Import the page's CSS. Webpack will know what to do with it. 2 | import "../stylesheets/app.css"; 3 | 4 | // Import libraries we need. 5 | import { default as Web3} from 'web3'; 6 | import { default as contract } from 'truffle-contract' 7 | 8 | // Import our contract artifacts and turn them into usable abstractions. 9 | import metacoin_artifacts from '../../build/contracts/MetaCoin.json' 10 | 11 | // MetaCoin is our usable abstraction, which we'll use through the code below. 12 | var MetaCoin = contract(metacoin_artifacts); 13 | 14 | // The following code is simple to show off interacting with your contracts. 15 | // As your needs grow you will likely need to change its form and structure. 16 | // For application bootstrapping, check out window.addEventListener below. 17 | var accounts; 18 | var account; 19 | 20 | window.App = { 21 | start: function() { 22 | var self = this; 23 | 24 | // Bootstrap the MetaCoin abstraction for Use. 25 | MetaCoin.setProvider(web3.currentProvider); 26 | 27 | // Get the initial account balance so it can be displayed. 28 | web3.eth.getAccounts(function(err, accs) { 29 | if (err != null) { 30 | alert("There was an error fetching your accounts."); 31 | return; 32 | } 33 | 34 | if (accs.length == 0) { 35 | alert("Couldn't get any accounts! Make sure your Ethereum client is configured correctly."); 36 | return; 37 | } 38 | 39 | accounts = accs; 40 | account = accounts[0]; 41 | 42 | self.refreshBalance(); 43 | }); 44 | }, 45 | 46 | setStatus: function(message) { 47 | var status = document.getElementById("status"); 48 | status.innerHTML = message; 49 | }, 50 | 51 | refreshBalance: function() { 52 | var self = this; 53 | 54 | var meta; 55 | MetaCoin.deployed().then(function(instance) { 56 | meta = instance; 57 | return meta.getBalance.call(account, {from: account}); 58 | }).then(function(value) { 59 | var balance_element = document.getElementById("balance"); 60 | balance_element.innerHTML = value.valueOf(); 61 | }).catch(function(e) { 62 | console.log(e); 63 | self.setStatus("Error getting balance; see log."); 64 | }); 65 | }, 66 | 67 | sendCoin: function() { 68 | var self = this; 69 | 70 | var amount = parseInt(document.getElementById("amount").value); 71 | var receiver = document.getElementById("receiver").value; 72 | 73 | this.setStatus("Initiating transaction... (please wait)"); 74 | 75 | var meta; 76 | MetaCoin.deployed().then(function(instance) { 77 | meta = instance; 78 | return meta.sendCoin(receiver, amount, {from: account}); 79 | }).then(function() { 80 | self.setStatus("Transaction complete!"); 81 | self.refreshBalance(); 82 | }).catch(function(e) { 83 | console.log(e); 84 | self.setStatus("Error sending coin; see log."); 85 | }); 86 | } 87 | }; 88 | 89 | window.addEventListener('load', function() { 90 | // Checking if Web3 has been injected by the browser (Mist/MetaMask) 91 | if (typeof web3 !== 'undefined') { 92 | console.warn("Using web3 detected from external source. If you find that your accounts don't appear or you have 0 MetaCoin, ensure you've configured that source properly. If using MetaMask, see the following link. Feel free to delete this warning. :) http://truffleframework.com/tutorials/truffle-and-metamask") 93 | // Use Mist/MetaMask's provider 94 | window.web3 = new Web3(web3.currentProvider); 95 | } else { 96 | console.warn("No web3 detected. Falling back to http://127.0.0.1:9545. You should remove this fallback when you deploy live, as it's inherently insecure. Consider switching to Metamask for development. More info here: http://truffleframework.com/tutorials/truffle-and-metamask"); 97 | // fallback - use your fallback strategy (local node / hosted node + in-dapp id mgmt / fail) 98 | window.web3 = new Web3(new Web3.providers.HttpProvider("http://127.0.0.1:9545")); 99 | } 100 | 101 | App.start(); 102 | }); 103 | -------------------------------------------------------------------------------- /Chapter02/app/stylesheets/app.css: -------------------------------------------------------------------------------- 1 | body { 2 | margin-left: 25%; 3 | margin-right: 25%; 4 | margin-top: 10%; 5 | font-family: "Open Sans", sans-serif; 6 | } 7 | 8 | label { 9 | display: inline-block; 10 | width: 100px; 11 | } 12 | 13 | input { 14 | width: 500px; 15 | padding: 5px; 16 | font-size: 16px; 17 | } 18 | 19 | button { 20 | font-size: 16px; 21 | padding: 5px; 22 | } 23 | 24 | h1, h2 { 25 | display: inline-block; 26 | vertical-align: middle; 27 | margin-top: 0px; 28 | margin-bottom: 10px; 29 | } 30 | 31 | h2 { 32 | color: #AAA; 33 | font-size: 32px; 34 | } 35 | 36 | h3 { 37 | font-weight: normal; 38 | color: #AAA; 39 | font-size: 24px; 40 | } 41 | 42 | .black { 43 | color: black; 44 | } 45 | 46 | #balance { 47 | color: black; 48 | } 49 | 50 | .hint { 51 | color: #666; 52 | } 53 | -------------------------------------------------------------------------------- /Chapter02/box-img-lg.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/PacktPublishing/Ethereum-Projects-for-Beginners/96e2448b3061b45b6a77e54d527524cd0c23d4be/Chapter02/box-img-lg.png -------------------------------------------------------------------------------- /Chapter02/box-img-sm.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/PacktPublishing/Ethereum-Projects-for-Beginners/96e2448b3061b45b6a77e54d527524cd0c23d4be/Chapter02/box-img-sm.png -------------------------------------------------------------------------------- /Chapter02/build/contracts/ConvertLib.json: -------------------------------------------------------------------------------- 1 | { 2 | "contractName": "ConvertLib", 3 | "abi": [ 4 | { 5 | "constant": true, 6 | "inputs": [ 7 | { 8 | "name": "amount", 9 | "type": "uint256" 10 | }, 11 | { 12 | "name": "conversionRate", 13 | "type": "uint256" 14 | } 15 | ], 16 | "name": "convert", 17 | "outputs": [ 18 | { 19 | "name": "convertedAmount", 20 | "type": "uint256" 21 | } 22 | ], 23 | "payable": false, 24 | "stateMutability": "pure", 25 | "type": "function" 26 | } 27 | ], 28 | "bytecode": "0x6060604052341561000f57600080fd5b60b08061001d6000396000f300606060405260043610603f576000357c0100000000000000000000000000000000000000000000000000000000900463ffffffff16806396e4ee3d146044575b600080fd5b606160048080359060200190919080359060200190919050506077565b6040518082815260200191505060405180910390f35b60008183029050929150505600a165627a7a72305820c203807f080cb0220a38ed19317d8308a1da97f006865dcd0531aa87a1df060a0029", 29 | "deployedBytecode": "0x606060405260043610603f576000357c0100000000000000000000000000000000000000000000000000000000900463ffffffff16806396e4ee3d146044575b600080fd5b606160048080359060200190919080359060200190919050506077565b6040518082815260200191505060405180910390f35b60008183029050929150505600a165627a7a72305820c203807f080cb0220a38ed19317d8308a1da97f006865dcd0531aa87a1df060a0029", 30 | "sourceMap": "26:170:0:-;;;;;;;;;;;;;;;;;", 31 | "deployedSourceMap": "26:170:0:-;;;;;;;;;;;;;;;;;;;;;;;;50:144;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;121:20;173:14;164:6;:23;157:30;;50:144;;;;:::o", 32 | "source": "pragma solidity ^0.4.17;\n\nlibrary ConvertLib{\n function convert(uint amount,uint conversionRate) public pure returns (uint convertedAmount)\n {\n return amount * conversionRate;\n }\n}\n", 33 | "sourcePath": "F:\\Dev\\ethereum\\my_first_project\\contracts\\ConvertLib.sol", 34 | "ast": { 35 | "attributes": { 36 | "absolutePath": "/F/Dev/ethereum/my_first_project/contracts/ConvertLib.sol", 37 | "exportedSymbols": { 38 | "ConvertLib": [ 39 | 16 40 | ] 41 | } 42 | }, 43 | "children": [ 44 | { 45 | "attributes": { 46 | "literals": [ 47 | "solidity", 48 | "^", 49 | "0.4", 50 | ".17" 51 | ] 52 | }, 53 | "id": 1, 54 | "name": "PragmaDirective", 55 | "src": "0:24:0" 56 | }, 57 | { 58 | "attributes": { 59 | "baseContracts": [ 60 | null 61 | ], 62 | "contractDependencies": [ 63 | null 64 | ], 65 | "contractKind": "library", 66 | "documentation": null, 67 | "fullyImplemented": true, 68 | "linearizedBaseContracts": [ 69 | 16 70 | ], 71 | "name": "ConvertLib", 72 | "scope": 17 73 | }, 74 | "children": [ 75 | { 76 | "attributes": { 77 | "constant": true, 78 | "implemented": true, 79 | "isConstructor": false, 80 | "modifiers": [ 81 | null 82 | ], 83 | "name": "convert", 84 | "payable": false, 85 | "scope": 16, 86 | "stateMutability": "pure", 87 | "superFunction": null, 88 | "visibility": "public" 89 | }, 90 | "children": [ 91 | { 92 | "children": [ 93 | { 94 | "attributes": { 95 | "constant": false, 96 | "name": "amount", 97 | "scope": 15, 98 | "stateVariable": false, 99 | "storageLocation": "default", 100 | "type": "uint256", 101 | "value": null, 102 | "visibility": "internal" 103 | }, 104 | "children": [ 105 | { 106 | "attributes": { 107 | "name": "uint", 108 | "type": "uint256" 109 | }, 110 | "id": 2, 111 | "name": "ElementaryTypeName", 112 | "src": "67:4:0" 113 | } 114 | ], 115 | "id": 3, 116 | "name": "VariableDeclaration", 117 | "src": "67:11:0" 118 | }, 119 | { 120 | "attributes": { 121 | "constant": false, 122 | "name": "conversionRate", 123 | "scope": 15, 124 | "stateVariable": false, 125 | "storageLocation": "default", 126 | "type": "uint256", 127 | "value": null, 128 | "visibility": "internal" 129 | }, 130 | "children": [ 131 | { 132 | "attributes": { 133 | "name": "uint", 134 | "type": "uint256" 135 | }, 136 | "id": 4, 137 | "name": "ElementaryTypeName", 138 | "src": "79:4:0" 139 | } 140 | ], 141 | "id": 5, 142 | "name": "VariableDeclaration", 143 | "src": "79:19:0" 144 | } 145 | ], 146 | "id": 6, 147 | "name": "ParameterList", 148 | "src": "66:33:0" 149 | }, 150 | { 151 | "children": [ 152 | { 153 | "attributes": { 154 | "constant": false, 155 | "name": "convertedAmount", 156 | "scope": 15, 157 | "stateVariable": false, 158 | "storageLocation": "default", 159 | "type": "uint256", 160 | "value": null, 161 | "visibility": "internal" 162 | }, 163 | "children": [ 164 | { 165 | "attributes": { 166 | "name": "uint", 167 | "type": "uint256" 168 | }, 169 | "id": 7, 170 | "name": "ElementaryTypeName", 171 | "src": "121:4:0" 172 | } 173 | ], 174 | "id": 8, 175 | "name": "VariableDeclaration", 176 | "src": "121:20:0" 177 | } 178 | ], 179 | "id": 9, 180 | "name": "ParameterList", 181 | "src": "120:22:0" 182 | }, 183 | { 184 | "children": [ 185 | { 186 | "attributes": { 187 | "functionReturnParameters": 9 188 | }, 189 | "children": [ 190 | { 191 | "attributes": { 192 | "argumentTypes": null, 193 | "commonType": { 194 | "typeIdentifier": "t_uint256", 195 | "typeString": "uint256" 196 | }, 197 | "isConstant": false, 198 | "isLValue": false, 199 | "isPure": false, 200 | "lValueRequested": false, 201 | "operator": "*", 202 | "type": "uint256" 203 | }, 204 | "children": [ 205 | { 206 | "attributes": { 207 | "argumentTypes": null, 208 | "overloadedDeclarations": [ 209 | null 210 | ], 211 | "referencedDeclaration": 3, 212 | "type": "uint256", 213 | "value": "amount" 214 | }, 215 | "id": 10, 216 | "name": "Identifier", 217 | "src": "164:6:0" 218 | }, 219 | { 220 | "attributes": { 221 | "argumentTypes": null, 222 | "overloadedDeclarations": [ 223 | null 224 | ], 225 | "referencedDeclaration": 5, 226 | "type": "uint256", 227 | "value": "conversionRate" 228 | }, 229 | "id": 11, 230 | "name": "Identifier", 231 | "src": "173:14:0" 232 | } 233 | ], 234 | "id": 12, 235 | "name": "BinaryOperation", 236 | "src": "164:23:0" 237 | } 238 | ], 239 | "id": 13, 240 | "name": "Return", 241 | "src": "157:30:0" 242 | } 243 | ], 244 | "id": 14, 245 | "name": "Block", 246 | "src": "147:47:0" 247 | } 248 | ], 249 | "id": 15, 250 | "name": "FunctionDefinition", 251 | "src": "50:144:0" 252 | } 253 | ], 254 | "id": 16, 255 | "name": "ContractDefinition", 256 | "src": "26:170:0" 257 | } 258 | ], 259 | "id": 17, 260 | "name": "SourceUnit", 261 | "src": "0:197:0" 262 | }, 263 | "compiler": { 264 | "name": "solc", 265 | "version": "0.4.19+commit.c4cbbb05.Emscripten.clang" 266 | }, 267 | "networks": { 268 | "1520343466351": { 269 | "events": {}, 270 | "links": {}, 271 | "address": "0x65bf225df375be248775020ab921c0159c2a4152" 272 | } 273 | }, 274 | "schemaVersion": "1.0.1", 275 | "updatedAt": "2018-03-06T13:42:17.603Z" 276 | } -------------------------------------------------------------------------------- /Chapter02/build/contracts/Migrations.json: -------------------------------------------------------------------------------- 1 | { 2 | "contractName": "Migrations", 3 | "abi": [ 4 | { 5 | "constant": true, 6 | "inputs": [], 7 | "name": "last_completed_migration", 8 | "outputs": [ 9 | { 10 | "name": "", 11 | "type": "uint256" 12 | } 13 | ], 14 | "payable": false, 15 | "stateMutability": "view", 16 | "type": "function" 17 | }, 18 | { 19 | "constant": true, 20 | "inputs": [], 21 | "name": "owner", 22 | "outputs": [ 23 | { 24 | "name": "", 25 | "type": "address" 26 | } 27 | ], 28 | "payable": false, 29 | "stateMutability": "view", 30 | "type": "function" 31 | }, 32 | { 33 | "inputs": [], 34 | "payable": false, 35 | "stateMutability": "nonpayable", 36 | "type": "constructor" 37 | }, 38 | { 39 | "constant": false, 40 | "inputs": [ 41 | { 42 | "name": "completed", 43 | "type": "uint256" 44 | } 45 | ], 46 | "name": "setCompleted", 47 | "outputs": [], 48 | "payable": false, 49 | "stateMutability": "nonpayable", 50 | "type": "function" 51 | }, 52 | { 53 | "constant": false, 54 | "inputs": [ 55 | { 56 | "name": "new_address", 57 | "type": "address" 58 | } 59 | ], 60 | "name": "upgrade", 61 | "outputs": [], 62 | "payable": false, 63 | "stateMutability": "nonpayable", 64 | "type": "function" 65 | } 66 | ], 67 | "bytecode": "0x6060604052341561000f57600080fd5b336000806101000a81548173ffffffffffffffffffffffffffffffffffffffff021916908373ffffffffffffffffffffffffffffffffffffffff1602179055506102db8061005e6000396000f300606060405260043610610062576000357c0100000000000000000000000000000000000000000000000000000000900463ffffffff1680630900f01014610067578063445df0ac146100a05780638da5cb5b146100c9578063fdacd5761461011e575b600080fd5b341561007257600080fd5b61009e600480803573ffffffffffffffffffffffffffffffffffffffff16906020019091905050610141565b005b34156100ab57600080fd5b6100b3610224565b6040518082815260200191505060405180910390f35b34156100d457600080fd5b6100dc61022a565b604051808273ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff16815260200191505060405180910390f35b341561012957600080fd5b61013f600480803590602001909190505061024f565b005b60008060009054906101000a900473ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff163373ffffffffffffffffffffffffffffffffffffffff161415610220578190508073ffffffffffffffffffffffffffffffffffffffff1663fdacd5766001546040518263ffffffff167c010000000000000000000000000000000000000000000000000000000002815260040180828152602001915050600060405180830381600087803b151561020b57600080fd5b6102c65a03f1151561021c57600080fd5b5050505b5050565b60015481565b6000809054906101000a900473ffffffffffffffffffffffffffffffffffffffff1681565b6000809054906101000a900473ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff163373ffffffffffffffffffffffffffffffffffffffff1614156102ac57806001819055505b505600a165627a7a7230582033a36094f4743c7313f2088c3792f043703667e5275960cb7f17e71630f261e80029", 68 | "deployedBytecode": "0x606060405260043610610062576000357c0100000000000000000000000000000000000000000000000000000000900463ffffffff1680630900f01014610067578063445df0ac146100a05780638da5cb5b146100c9578063fdacd5761461011e575b600080fd5b341561007257600080fd5b61009e600480803573ffffffffffffffffffffffffffffffffffffffff16906020019091905050610141565b005b34156100ab57600080fd5b6100b3610224565b6040518082815260200191505060405180910390f35b34156100d457600080fd5b6100dc61022a565b604051808273ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff16815260200191505060405180910390f35b341561012957600080fd5b61013f600480803590602001909190505061024f565b005b60008060009054906101000a900473ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff163373ffffffffffffffffffffffffffffffffffffffff161415610220578190508073ffffffffffffffffffffffffffffffffffffffff1663fdacd5766001546040518263ffffffff167c010000000000000000000000000000000000000000000000000000000002815260040180828152602001915050600060405180830381600087803b151561020b57600080fd5b6102c65a03f1151561021c57600080fd5b5050505b5050565b60015481565b6000809054906101000a900473ffffffffffffffffffffffffffffffffffffffff1681565b6000809054906101000a900473ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff163373ffffffffffffffffffffffffffffffffffffffff1614156102ac57806001819055505b505600a165627a7a7230582033a36094f4743c7313f2088c3792f043703667e5275960cb7f17e71630f261e80029", 69 | "sourceMap": "26:488:2:-;;;178:58;;;;;;;;221:10;213:5;;:18;;;;;;;;;;;;;;;;;;26:488;;;;;;", 70 | "deployedSourceMap": "26:488:2:-;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;347:165;;;;;;;;;;;;;;;;;;;;;;;;;;;;74:36;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;50:20;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;240:103;;;;;;;;;;;;;;;;;;;;;;;;;;347:165;409:19;161:5;;;;;;;;;;;147:19;;:10;:19;;;143:26;;;442:11;409:45;;460:8;:21;;;482:24;;460:47;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;143:26;347:165;;:::o;74:36::-;;;;:::o;50:20::-;;;;;;;;;;;;;:::o;240:103::-;161:5;;;;;;;;;;;147:19;;:10;:19;;;143:26;;;329:9;302:24;:36;;;;143:26;240:103;:::o", 71 | "source": "pragma solidity ^0.4.17;\n\ncontract Migrations {\n address public owner;\n uint public last_completed_migration;\n\n modifier restricted() {\n if (msg.sender == owner) _;\n }\n\n function Migrations() public {\n owner = msg.sender;\n }\n\n function setCompleted(uint completed) public restricted {\n last_completed_migration = completed;\n }\n\n function upgrade(address new_address) public restricted {\n Migrations upgraded = Migrations(new_address);\n upgraded.setCompleted(last_completed_migration);\n }\n}\n", 72 | "sourcePath": "F:\\Dev\\ethereum\\my_first_project\\contracts\\Migrations.sol", 73 | "ast": { 74 | "attributes": { 75 | "absolutePath": "/F/Dev/ethereum/my_first_project/contracts/Migrations.sol", 76 | "exportedSymbols": { 77 | "Migrations": [ 78 | 169 79 | ] 80 | } 81 | }, 82 | "children": [ 83 | { 84 | "attributes": { 85 | "literals": [ 86 | "solidity", 87 | "^", 88 | "0.4", 89 | ".17" 90 | ] 91 | }, 92 | "id": 114, 93 | "name": "PragmaDirective", 94 | "src": "0:24:2" 95 | }, 96 | { 97 | "attributes": { 98 | "baseContracts": [ 99 | null 100 | ], 101 | "contractDependencies": [ 102 | null 103 | ], 104 | "contractKind": "contract", 105 | "documentation": null, 106 | "fullyImplemented": true, 107 | "linearizedBaseContracts": [ 108 | 169 109 | ], 110 | "name": "Migrations", 111 | "scope": 170 112 | }, 113 | "children": [ 114 | { 115 | "attributes": { 116 | "constant": false, 117 | "name": "owner", 118 | "scope": 169, 119 | "stateVariable": true, 120 | "storageLocation": "default", 121 | "type": "address", 122 | "value": null, 123 | "visibility": "public" 124 | }, 125 | "children": [ 126 | { 127 | "attributes": { 128 | "name": "address", 129 | "type": "address" 130 | }, 131 | "id": 115, 132 | "name": "ElementaryTypeName", 133 | "src": "50:7:2" 134 | } 135 | ], 136 | "id": 116, 137 | "name": "VariableDeclaration", 138 | "src": "50:20:2" 139 | }, 140 | { 141 | "attributes": { 142 | "constant": false, 143 | "name": "last_completed_migration", 144 | "scope": 169, 145 | "stateVariable": true, 146 | "storageLocation": "default", 147 | "type": "uint256", 148 | "value": null, 149 | "visibility": "public" 150 | }, 151 | "children": [ 152 | { 153 | "attributes": { 154 | "name": "uint", 155 | "type": "uint256" 156 | }, 157 | "id": 117, 158 | "name": "ElementaryTypeName", 159 | "src": "74:4:2" 160 | } 161 | ], 162 | "id": 118, 163 | "name": "VariableDeclaration", 164 | "src": "74:36:2" 165 | }, 166 | { 167 | "attributes": { 168 | "name": "restricted", 169 | "visibility": "internal" 170 | }, 171 | "children": [ 172 | { 173 | "attributes": { 174 | "parameters": [ 175 | null 176 | ] 177 | }, 178 | "children": [], 179 | "id": 119, 180 | "name": "ParameterList", 181 | "src": "134:2:2" 182 | }, 183 | { 184 | "children": [ 185 | { 186 | "attributes": { 187 | "falseBody": null 188 | }, 189 | "children": [ 190 | { 191 | "attributes": { 192 | "argumentTypes": null, 193 | "commonType": { 194 | "typeIdentifier": "t_address", 195 | "typeString": "address" 196 | }, 197 | "isConstant": false, 198 | "isLValue": false, 199 | "isPure": false, 200 | "lValueRequested": false, 201 | "operator": "==", 202 | "type": "bool" 203 | }, 204 | "children": [ 205 | { 206 | "attributes": { 207 | "argumentTypes": null, 208 | "isConstant": false, 209 | "isLValue": false, 210 | "isPure": false, 211 | "lValueRequested": false, 212 | "member_name": "sender", 213 | "referencedDeclaration": null, 214 | "type": "address" 215 | }, 216 | "children": [ 217 | { 218 | "attributes": { 219 | "argumentTypes": null, 220 | "overloadedDeclarations": [ 221 | null 222 | ], 223 | "referencedDeclaration": 181, 224 | "type": "msg", 225 | "value": "msg" 226 | }, 227 | "id": 120, 228 | "name": "Identifier", 229 | "src": "147:3:2" 230 | } 231 | ], 232 | "id": 121, 233 | "name": "MemberAccess", 234 | "src": "147:10:2" 235 | }, 236 | { 237 | "attributes": { 238 | "argumentTypes": null, 239 | "overloadedDeclarations": [ 240 | null 241 | ], 242 | "referencedDeclaration": 116, 243 | "type": "address", 244 | "value": "owner" 245 | }, 246 | "id": 122, 247 | "name": "Identifier", 248 | "src": "161:5:2" 249 | } 250 | ], 251 | "id": 123, 252 | "name": "BinaryOperation", 253 | "src": "147:19:2" 254 | }, 255 | { 256 | "id": 124, 257 | "name": "PlaceholderStatement", 258 | "src": "168:1:2" 259 | } 260 | ], 261 | "id": 125, 262 | "name": "IfStatement", 263 | "src": "143:26:2" 264 | } 265 | ], 266 | "id": 126, 267 | "name": "Block", 268 | "src": "137:37:2" 269 | } 270 | ], 271 | "id": 127, 272 | "name": "ModifierDefinition", 273 | "src": "115:59:2" 274 | }, 275 | { 276 | "attributes": { 277 | "constant": false, 278 | "implemented": true, 279 | "isConstructor": true, 280 | "modifiers": [ 281 | null 282 | ], 283 | "name": "Migrations", 284 | "payable": false, 285 | "scope": 169, 286 | "stateMutability": "nonpayable", 287 | "superFunction": null, 288 | "visibility": "public" 289 | }, 290 | "children": [ 291 | { 292 | "attributes": { 293 | "parameters": [ 294 | null 295 | ] 296 | }, 297 | "children": [], 298 | "id": 128, 299 | "name": "ParameterList", 300 | "src": "197:2:2" 301 | }, 302 | { 303 | "attributes": { 304 | "parameters": [ 305 | null 306 | ] 307 | }, 308 | "children": [], 309 | "id": 129, 310 | "name": "ParameterList", 311 | "src": "207:0:2" 312 | }, 313 | { 314 | "children": [ 315 | { 316 | "children": [ 317 | { 318 | "attributes": { 319 | "argumentTypes": null, 320 | "isConstant": false, 321 | "isLValue": false, 322 | "isPure": false, 323 | "lValueRequested": false, 324 | "operator": "=", 325 | "type": "address" 326 | }, 327 | "children": [ 328 | { 329 | "attributes": { 330 | "argumentTypes": null, 331 | "overloadedDeclarations": [ 332 | null 333 | ], 334 | "referencedDeclaration": 116, 335 | "type": "address", 336 | "value": "owner" 337 | }, 338 | "id": 130, 339 | "name": "Identifier", 340 | "src": "213:5:2" 341 | }, 342 | { 343 | "attributes": { 344 | "argumentTypes": null, 345 | "isConstant": false, 346 | "isLValue": false, 347 | "isPure": false, 348 | "lValueRequested": false, 349 | "member_name": "sender", 350 | "referencedDeclaration": null, 351 | "type": "address" 352 | }, 353 | "children": [ 354 | { 355 | "attributes": { 356 | "argumentTypes": null, 357 | "overloadedDeclarations": [ 358 | null 359 | ], 360 | "referencedDeclaration": 181, 361 | "type": "msg", 362 | "value": "msg" 363 | }, 364 | "id": 131, 365 | "name": "Identifier", 366 | "src": "221:3:2" 367 | } 368 | ], 369 | "id": 132, 370 | "name": "MemberAccess", 371 | "src": "221:10:2" 372 | } 373 | ], 374 | "id": 133, 375 | "name": "Assignment", 376 | "src": "213:18:2" 377 | } 378 | ], 379 | "id": 134, 380 | "name": "ExpressionStatement", 381 | "src": "213:18:2" 382 | } 383 | ], 384 | "id": 135, 385 | "name": "Block", 386 | "src": "207:29:2" 387 | } 388 | ], 389 | "id": 136, 390 | "name": "FunctionDefinition", 391 | "src": "178:58:2" 392 | }, 393 | { 394 | "attributes": { 395 | "constant": false, 396 | "implemented": true, 397 | "isConstructor": false, 398 | "name": "setCompleted", 399 | "payable": false, 400 | "scope": 169, 401 | "stateMutability": "nonpayable", 402 | "superFunction": null, 403 | "visibility": "public" 404 | }, 405 | "children": [ 406 | { 407 | "children": [ 408 | { 409 | "attributes": { 410 | "constant": false, 411 | "name": "completed", 412 | "scope": 148, 413 | "stateVariable": false, 414 | "storageLocation": "default", 415 | "type": "uint256", 416 | "value": null, 417 | "visibility": "internal" 418 | }, 419 | "children": [ 420 | { 421 | "attributes": { 422 | "name": "uint", 423 | "type": "uint256" 424 | }, 425 | "id": 137, 426 | "name": "ElementaryTypeName", 427 | "src": "262:4:2" 428 | } 429 | ], 430 | "id": 138, 431 | "name": "VariableDeclaration", 432 | "src": "262:14:2" 433 | } 434 | ], 435 | "id": 139, 436 | "name": "ParameterList", 437 | "src": "261:16:2" 438 | }, 439 | { 440 | "attributes": { 441 | "parameters": [ 442 | null 443 | ] 444 | }, 445 | "children": [], 446 | "id": 142, 447 | "name": "ParameterList", 448 | "src": "296:0:2" 449 | }, 450 | { 451 | "attributes": { 452 | "arguments": [ 453 | null 454 | ] 455 | }, 456 | "children": [ 457 | { 458 | "attributes": { 459 | "argumentTypes": null, 460 | "overloadedDeclarations": [ 461 | null 462 | ], 463 | "referencedDeclaration": 127, 464 | "type": "modifier ()", 465 | "value": "restricted" 466 | }, 467 | "id": 140, 468 | "name": "Identifier", 469 | "src": "285:10:2" 470 | } 471 | ], 472 | "id": 141, 473 | "name": "ModifierInvocation", 474 | "src": "285:10:2" 475 | }, 476 | { 477 | "children": [ 478 | { 479 | "children": [ 480 | { 481 | "attributes": { 482 | "argumentTypes": null, 483 | "isConstant": false, 484 | "isLValue": false, 485 | "isPure": false, 486 | "lValueRequested": false, 487 | "operator": "=", 488 | "type": "uint256" 489 | }, 490 | "children": [ 491 | { 492 | "attributes": { 493 | "argumentTypes": null, 494 | "overloadedDeclarations": [ 495 | null 496 | ], 497 | "referencedDeclaration": 118, 498 | "type": "uint256", 499 | "value": "last_completed_migration" 500 | }, 501 | "id": 143, 502 | "name": "Identifier", 503 | "src": "302:24:2" 504 | }, 505 | { 506 | "attributes": { 507 | "argumentTypes": null, 508 | "overloadedDeclarations": [ 509 | null 510 | ], 511 | "referencedDeclaration": 138, 512 | "type": "uint256", 513 | "value": "completed" 514 | }, 515 | "id": 144, 516 | "name": "Identifier", 517 | "src": "329:9:2" 518 | } 519 | ], 520 | "id": 145, 521 | "name": "Assignment", 522 | "src": "302:36:2" 523 | } 524 | ], 525 | "id": 146, 526 | "name": "ExpressionStatement", 527 | "src": "302:36:2" 528 | } 529 | ], 530 | "id": 147, 531 | "name": "Block", 532 | "src": "296:47:2" 533 | } 534 | ], 535 | "id": 148, 536 | "name": "FunctionDefinition", 537 | "src": "240:103:2" 538 | }, 539 | { 540 | "attributes": { 541 | "constant": false, 542 | "implemented": true, 543 | "isConstructor": false, 544 | "name": "upgrade", 545 | "payable": false, 546 | "scope": 169, 547 | "stateMutability": "nonpayable", 548 | "superFunction": null, 549 | "visibility": "public" 550 | }, 551 | "children": [ 552 | { 553 | "children": [ 554 | { 555 | "attributes": { 556 | "constant": false, 557 | "name": "new_address", 558 | "scope": 168, 559 | "stateVariable": false, 560 | "storageLocation": "default", 561 | "type": "address", 562 | "value": null, 563 | "visibility": "internal" 564 | }, 565 | "children": [ 566 | { 567 | "attributes": { 568 | "name": "address", 569 | "type": "address" 570 | }, 571 | "id": 149, 572 | "name": "ElementaryTypeName", 573 | "src": "364:7:2" 574 | } 575 | ], 576 | "id": 150, 577 | "name": "VariableDeclaration", 578 | "src": "364:19:2" 579 | } 580 | ], 581 | "id": 151, 582 | "name": "ParameterList", 583 | "src": "363:21:2" 584 | }, 585 | { 586 | "attributes": { 587 | "parameters": [ 588 | null 589 | ] 590 | }, 591 | "children": [], 592 | "id": 154, 593 | "name": "ParameterList", 594 | "src": "403:0:2" 595 | }, 596 | { 597 | "attributes": { 598 | "arguments": [ 599 | null 600 | ] 601 | }, 602 | "children": [ 603 | { 604 | "attributes": { 605 | "argumentTypes": null, 606 | "overloadedDeclarations": [ 607 | null 608 | ], 609 | "referencedDeclaration": 127, 610 | "type": "modifier ()", 611 | "value": "restricted" 612 | }, 613 | "id": 152, 614 | "name": "Identifier", 615 | "src": "392:10:2" 616 | } 617 | ], 618 | "id": 153, 619 | "name": "ModifierInvocation", 620 | "src": "392:10:2" 621 | }, 622 | { 623 | "children": [ 624 | { 625 | "attributes": { 626 | "assignments": [ 627 | 156 628 | ] 629 | }, 630 | "children": [ 631 | { 632 | "attributes": { 633 | "constant": false, 634 | "name": "upgraded", 635 | "scope": 168, 636 | "stateVariable": false, 637 | "storageLocation": "default", 638 | "type": "contract Migrations", 639 | "value": null, 640 | "visibility": "internal" 641 | }, 642 | "children": [ 643 | { 644 | "attributes": { 645 | "contractScope": null, 646 | "name": "Migrations", 647 | "referencedDeclaration": 169, 648 | "type": "contract Migrations" 649 | }, 650 | "id": 155, 651 | "name": "UserDefinedTypeName", 652 | "src": "409:10:2" 653 | } 654 | ], 655 | "id": 156, 656 | "name": "VariableDeclaration", 657 | "src": "409:19:2" 658 | }, 659 | { 660 | "attributes": { 661 | "argumentTypes": null, 662 | "isConstant": false, 663 | "isLValue": false, 664 | "isPure": false, 665 | "isStructConstructorCall": false, 666 | "lValueRequested": false, 667 | "names": [ 668 | null 669 | ], 670 | "type": "contract Migrations", 671 | "type_conversion": true 672 | }, 673 | "children": [ 674 | { 675 | "attributes": { 676 | "argumentTypes": [ 677 | { 678 | "typeIdentifier": "t_address", 679 | "typeString": "address" 680 | } 681 | ], 682 | "overloadedDeclarations": [ 683 | null 684 | ], 685 | "referencedDeclaration": 169, 686 | "type": "type(contract Migrations)", 687 | "value": "Migrations" 688 | }, 689 | "id": 157, 690 | "name": "Identifier", 691 | "src": "431:10:2" 692 | }, 693 | { 694 | "attributes": { 695 | "argumentTypes": null, 696 | "overloadedDeclarations": [ 697 | null 698 | ], 699 | "referencedDeclaration": 150, 700 | "type": "address", 701 | "value": "new_address" 702 | }, 703 | "id": 158, 704 | "name": "Identifier", 705 | "src": "442:11:2" 706 | } 707 | ], 708 | "id": 159, 709 | "name": "FunctionCall", 710 | "src": "431:23:2" 711 | } 712 | ], 713 | "id": 160, 714 | "name": "VariableDeclarationStatement", 715 | "src": "409:45:2" 716 | }, 717 | { 718 | "children": [ 719 | { 720 | "attributes": { 721 | "argumentTypes": null, 722 | "isConstant": false, 723 | "isLValue": false, 724 | "isPure": false, 725 | "isStructConstructorCall": false, 726 | "lValueRequested": false, 727 | "names": [ 728 | null 729 | ], 730 | "type": "tuple()", 731 | "type_conversion": false 732 | }, 733 | "children": [ 734 | { 735 | "attributes": { 736 | "argumentTypes": [ 737 | { 738 | "typeIdentifier": "t_uint256", 739 | "typeString": "uint256" 740 | } 741 | ], 742 | "isConstant": false, 743 | "isLValue": false, 744 | "isPure": false, 745 | "lValueRequested": false, 746 | "member_name": "setCompleted", 747 | "referencedDeclaration": 148, 748 | "type": "function (uint256) external" 749 | }, 750 | "children": [ 751 | { 752 | "attributes": { 753 | "argumentTypes": null, 754 | "overloadedDeclarations": [ 755 | null 756 | ], 757 | "referencedDeclaration": 156, 758 | "type": "contract Migrations", 759 | "value": "upgraded" 760 | }, 761 | "id": 161, 762 | "name": "Identifier", 763 | "src": "460:8:2" 764 | } 765 | ], 766 | "id": 163, 767 | "name": "MemberAccess", 768 | "src": "460:21:2" 769 | }, 770 | { 771 | "attributes": { 772 | "argumentTypes": null, 773 | "overloadedDeclarations": [ 774 | null 775 | ], 776 | "referencedDeclaration": 118, 777 | "type": "uint256", 778 | "value": "last_completed_migration" 779 | }, 780 | "id": 164, 781 | "name": "Identifier", 782 | "src": "482:24:2" 783 | } 784 | ], 785 | "id": 165, 786 | "name": "FunctionCall", 787 | "src": "460:47:2" 788 | } 789 | ], 790 | "id": 166, 791 | "name": "ExpressionStatement", 792 | "src": "460:47:2" 793 | } 794 | ], 795 | "id": 167, 796 | "name": "Block", 797 | "src": "403:109:2" 798 | } 799 | ], 800 | "id": 168, 801 | "name": "FunctionDefinition", 802 | "src": "347:165:2" 803 | } 804 | ], 805 | "id": 169, 806 | "name": "ContractDefinition", 807 | "src": "26:488:2" 808 | } 809 | ], 810 | "id": 170, 811 | "name": "SourceUnit", 812 | "src": "0:515:2" 813 | }, 814 | "compiler": { 815 | "name": "solc", 816 | "version": "0.4.19+commit.c4cbbb05.Emscripten.clang" 817 | }, 818 | "networks": { 819 | "1520343466351": { 820 | "events": {}, 821 | "links": {}, 822 | "address": "0x2e817544413d7205d7652ce86f112008e6d497a0" 823 | } 824 | }, 825 | "schemaVersion": "1.0.1", 826 | "updatedAt": "2018-03-06T13:42:17.605Z" 827 | } -------------------------------------------------------------------------------- /Chapter02/contracts/ConvertLib.sol: -------------------------------------------------------------------------------- 1 | pragma solidity ^0.4.17; 2 | 3 | library ConvertLib{ 4 | function convert(uint amount,uint conversionRate) public pure returns (uint convertedAmount) 5 | { 6 | return amount * conversionRate; 7 | } 8 | } 9 | -------------------------------------------------------------------------------- /Chapter02/contracts/MetaCoin.sol: -------------------------------------------------------------------------------- 1 | pragma solidity ^0.4.17; 2 | 3 | import "./ConvertLib.sol"; 4 | 5 | // This is just a simple example of a coin-like contract. 6 | // It is not standards compatible and cannot be expected to talk to other 7 | // coin/token contracts. If you want to create a standards-compliant 8 | // token, see: https://github.com/ConsenSys/Tokens. Cheers! 9 | 10 | contract MetaCoin { 11 | mapping (address => uint) balances; 12 | 13 | event Transfer(address indexed _from, address indexed _to, uint256 _value); 14 | 15 | function MetaCoin() public { 16 | balances[tx.origin] = 10000; 17 | } 18 | 19 | function sendCoin(address receiver, uint amount) public returns(bool sufficient) { 20 | if (balances[msg.sender] < amount) return false; 21 | balances[msg.sender] -= amount; 22 | balances[receiver] += amount; 23 | Transfer(msg.sender, receiver, amount); 24 | return true; 25 | } 26 | 27 | function getBalanceInEth(address addr) public view returns(uint){ 28 | return ConvertLib.convert(getBalance(addr),2); 29 | } 30 | 31 | function getBalance(address addr) public view returns(uint) { 32 | return balances[addr]; 33 | } 34 | } 35 | -------------------------------------------------------------------------------- /Chapter02/contracts/Migrations.sol: -------------------------------------------------------------------------------- 1 | pragma solidity ^0.4.17; 2 | 3 | contract Migrations { 4 | address public owner; 5 | uint public last_completed_migration; 6 | 7 | modifier restricted() { 8 | if (msg.sender == owner) _; 9 | } 10 | 11 | function Migrations() public { 12 | owner = msg.sender; 13 | } 14 | 15 | function setCompleted(uint completed) public restricted { 16 | last_completed_migration = completed; 17 | } 18 | 19 | function upgrade(address new_address) public restricted { 20 | Migrations upgraded = Migrations(new_address); 21 | upgraded.setCompleted(last_completed_migration); 22 | } 23 | } 24 | -------------------------------------------------------------------------------- /Chapter02/migrations/1_initial_migration.js: -------------------------------------------------------------------------------- 1 | var Migrations = artifacts.require("./Migrations.sol"); 2 | 3 | module.exports = function(deployer) { 4 | deployer.deploy(Migrations); 5 | }; 6 | -------------------------------------------------------------------------------- /Chapter02/migrations/2_deploy_contracts.js: -------------------------------------------------------------------------------- 1 | var ConvertLib = artifacts.require("./ConvertLib.sol"); 2 | var MetaCoin = artifacts.require("./MetaCoin.sol"); 3 | 4 | module.exports = function(deployer) { 5 | deployer.deploy(ConvertLib); 6 | deployer.link(ConvertLib, MetaCoin); 7 | deployer.deploy(MetaCoin); 8 | }; 9 | -------------------------------------------------------------------------------- /Chapter02/package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "truffle-init-webpack", 3 | "version": "0.0.2", 4 | "description": "Frontend example using truffle v3", 5 | "scripts": { 6 | "lint": "eslint ./", 7 | "build": "webpack", 8 | "dev": "webpack-dev-server" 9 | }, 10 | "author": "Douglas von Kohorn", 11 | "license": "MIT", 12 | "devDependencies": { 13 | "babel-cli": "^6.22.2", 14 | "babel-core": "^6.22.1", 15 | "babel-eslint": "^6.1.2", 16 | "babel-loader": "^6.2.10", 17 | "babel-plugin-transform-runtime": "^6.22.0", 18 | "babel-preset-env": "^1.1.8", 19 | "babel-preset-es2015": "^6.22.0", 20 | "babel-register": "^6.22.0", 21 | "copy-webpack-plugin": "^4.0.1", 22 | "css-loader": "^0.26.1", 23 | "eslint": "^3.14.0", 24 | "eslint-config-standard": "^6.0.0", 25 | "eslint-plugin-babel": "^4.0.0", 26 | "eslint-plugin-mocha": "^4.8.0", 27 | "eslint-plugin-promise": "^3.0.0", 28 | "eslint-plugin-standard": "^2.0.0", 29 | "html-webpack-plugin": "^2.28.0", 30 | "json-loader": "^0.5.4", 31 | "style-loader": "^0.13.1", 32 | "truffle-contract": "^1.1.11", 33 | "web3": "^0.20.0", 34 | "webpack": "^2.2.1", 35 | "webpack-dev-server": "^2.3.0" 36 | } 37 | } 38 | -------------------------------------------------------------------------------- /Chapter02/test/TestMetacoin.sol: -------------------------------------------------------------------------------- 1 | pragma solidity ^0.4.2; 2 | 3 | import "truffle/Assert.sol"; 4 | import "truffle/DeployedAddresses.sol"; 5 | import "../contracts/MetaCoin.sol"; 6 | 7 | contract TestMetacoin { 8 | 9 | function testInitialBalanceUsingDeployedContract() { 10 | MetaCoin meta = MetaCoin(DeployedAddresses.MetaCoin()); 11 | 12 | uint expected = 10000; 13 | 14 | Assert.equal(meta.getBalance(tx.origin), expected, "Owner should have 10000 MetaCoin initially"); 15 | } 16 | 17 | function testInitialBalanceWithNewMetaCoin() { 18 | MetaCoin meta = new MetaCoin(); 19 | 20 | uint expected = 10000; 21 | 22 | Assert.equal(meta.getBalance(tx.origin), expected, "Owner should have 10000 MetaCoin initially"); 23 | } 24 | 25 | } 26 | -------------------------------------------------------------------------------- /Chapter02/test/metacoin.js: -------------------------------------------------------------------------------- 1 | var MetaCoin = artifacts.require("./MetaCoin.sol"); 2 | 3 | contract('MetaCoin', function(accounts) { 4 | it("should put 10000 MetaCoin in the first account", function() { 5 | return MetaCoin.deployed().then(function(instance) { 6 | return instance.getBalance.call(accounts[0]); 7 | }).then(function(balance) { 8 | assert.equal(balance.valueOf(), 10000, "10000 wasn't in the first account"); 9 | }); 10 | }); 11 | it("should call a function that depends on a linked library", function() { 12 | var meta; 13 | var metaCoinBalance; 14 | var metaCoinEthBalance; 15 | 16 | return MetaCoin.deployed().then(function(instance) { 17 | meta = instance; 18 | return meta.getBalance.call(accounts[0]); 19 | }).then(function(outCoinBalance) { 20 | metaCoinBalance = outCoinBalance.toNumber(); 21 | return meta.getBalanceInEth.call(accounts[0]); 22 | }).then(function(outCoinBalanceEth) { 23 | metaCoinEthBalance = outCoinBalanceEth.toNumber(); 24 | }).then(function() { 25 | assert.equal(metaCoinEthBalance, 2 * metaCoinBalance, "Library function returned unexpeced function, linkage may be broken"); 26 | }); 27 | }); 28 | 29 | it("should send coin correctly", function() { 30 | var meta; 31 | 32 | // Get initial balances of first and second account. 33 | var account_one = accounts[0]; 34 | var account_two = accounts[1]; 35 | 36 | var account_one_starting_balance; 37 | var account_two_starting_balance; 38 | var account_one_ending_balance; 39 | var account_two_ending_balance; 40 | 41 | var amount = 10; 42 | 43 | return MetaCoin.deployed().then(function(instance) { 44 | meta = instance; 45 | return meta.getBalance.call(account_one); 46 | }).then(function(balance) { 47 | account_one_starting_balance = balance.toNumber(); 48 | return meta.getBalance.call(account_two); 49 | }).then(function(balance) { 50 | account_two_starting_balance = balance.toNumber(); 51 | return meta.sendCoin(account_two, amount, {from: account_one}); 52 | }).then(function() { 53 | return meta.getBalance.call(account_one); 54 | }).then(function(balance) { 55 | account_one_ending_balance = balance.toNumber(); 56 | return meta.getBalance.call(account_two); 57 | }).then(function(balance) { 58 | account_two_ending_balance = balance.toNumber(); 59 | 60 | assert.equal(account_one_ending_balance, account_one_starting_balance - amount, "Amount wasn't correctly taken from the sender"); 61 | assert.equal(account_two_ending_balance, account_two_starting_balance + amount, "Amount wasn't correctly sent to the receiver"); 62 | }); 63 | }); 64 | }); 65 | -------------------------------------------------------------------------------- /Chapter02/truffle.js: -------------------------------------------------------------------------------- 1 | // Allows us to use ES6 in our migrations and tests. 2 | require('babel-register') 3 | 4 | module.exports = { 5 | networks: { 6 | development: { 7 | host: '127.0.0.1', 8 | port: 8545, 9 | network_id: '*' // Match any network id 10 | } 11 | } 12 | } 13 | -------------------------------------------------------------------------------- /Chapter02/webpack.config.js: -------------------------------------------------------------------------------- 1 | const path = require('path'); 2 | const CopyWebpackPlugin = require('copy-webpack-plugin'); 3 | 4 | module.exports = { 5 | entry: './app/javascripts/app.js', 6 | output: { 7 | path: path.resolve(__dirname, 'build'), 8 | filename: 'app.js' 9 | }, 10 | plugins: [ 11 | // Copy our app's index.html to the build folder. 12 | new CopyWebpackPlugin([ 13 | { from: './app/index.html', to: "index.html" } 14 | ]) 15 | ], 16 | module: { 17 | rules: [ 18 | { 19 | test: /\.css$/, 20 | use: [ 'style-loader', 'css-loader' ] 21 | } 22 | ], 23 | loaders: [ 24 | { test: /\.json$/, use: 'json-loader' }, 25 | { 26 | test: /\.js$/, 27 | exclude: /(node_modules|bower_components)/, 28 | loader: 'babel-loader', 29 | query: { 30 | presets: ['es2015'], 31 | plugins: ['transform-runtime'] 32 | } 33 | } 34 | ] 35 | } 36 | } 37 | -------------------------------------------------------------------------------- /Chapter03/app/index.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | MetaCoin - Truffle Webpack Demo w/ Frontend 5 | 6 | 7 | 8 | 9 |

MetaCoin

10 |

Example Truffle Dapp

11 |

You have META

12 | 13 |
14 |

Send MetaCoin

15 |
16 |
17 |

18 |

19 | 20 |
21 | Hint: open the browser developer console to view any errors and warnings. 22 | 23 | 24 | -------------------------------------------------------------------------------- /Chapter03/app/javascripts/app.js: -------------------------------------------------------------------------------- 1 | // Import the page's CSS. Webpack will know what to do with it. 2 | import "../stylesheets/app.css"; 3 | 4 | // Import libraries we need. 5 | import { default as Web3} from 'web3'; 6 | import { default as contract } from 'truffle-contract' 7 | 8 | // Import our contract artifacts and turn them into usable abstractions. 9 | import metacoin_artifacts from '../../build/contracts/MetaCoin.json' 10 | 11 | // MetaCoin is our usable abstraction, which we'll use through the code below. 12 | var MetaCoin = contract(metacoin_artifacts); 13 | 14 | // The following code is simple to show off interacting with your contracts. 15 | // As your needs grow you will likely need to change its form and structure. 16 | // For application bootstrapping, check out window.addEventListener below. 17 | var accounts; 18 | var account; 19 | 20 | window.App = { 21 | start: function() { 22 | var self = this; 23 | 24 | // Bootstrap the MetaCoin abstraction for Use. 25 | MetaCoin.setProvider(web3.currentProvider); 26 | 27 | // Get the initial account balance so it can be displayed. 28 | web3.eth.getAccounts(function(err, accs) { 29 | if (err != null) { 30 | alert("There was an error fetching your accounts."); 31 | return; 32 | } 33 | 34 | if (accs.length == 0) { 35 | alert("Couldn't get any accounts! Make sure your Ethereum client is configured correctly."); 36 | return; 37 | } 38 | 39 | accounts = accs; 40 | account = accounts[0]; 41 | 42 | self.refreshBalance(); 43 | }); 44 | }, 45 | 46 | setStatus: function(message) { 47 | var status = document.getElementById("status"); 48 | status.innerHTML = message; 49 | }, 50 | 51 | refreshBalance: function() { 52 | var self = this; 53 | 54 | var meta; 55 | MetaCoin.deployed().then(function(instance) { 56 | meta = instance; 57 | return meta.balanceOf.call(account, {from: account}); 58 | }).then(function(value) { 59 | var balance_element = document.getElementById("balance"); 60 | balance_element.innerHTML = value.valueOf(); 61 | }).catch(function(e) { 62 | console.log(e); 63 | self.setStatus("Error getting balance; see log."); 64 | }); 65 | }, 66 | 67 | sendCoin: function() { 68 | var self = this; 69 | 70 | var amount = parseInt(document.getElementById("amount").value); 71 | var receiver = document.getElementById("receiver").value; 72 | 73 | this.setStatus("Initiating transaction... (please wait)"); 74 | 75 | var meta; 76 | MetaCoin.deployed().then(function(instance) { 77 | meta = instance; 78 | return meta.transfer(receiver, amount, {from: account}); 79 | }).then(function() { 80 | self.setStatus("Transaction complete!"); 81 | self.refreshBalance(); 82 | }).catch(function(e) { 83 | console.log(e); 84 | self.setStatus("Error sending coin; see log."); 85 | }); 86 | } 87 | }; 88 | 89 | window.addEventListener('load', function() { 90 | // Checking if Web3 has been injected by the browser (Mist/MetaMask) 91 | if (typeof web3 !== 'undefined') { 92 | console.warn("Using web3 detected from external source. If you find that your accounts don't appear or you have 0 MetaCoin, ensure you've configured that source properly. If using MetaMask, see the following link. Feel free to delete this warning. :) http://truffleframework.com/tutorials/truffle-and-metamask") 93 | // Use Mist/MetaMask's provider 94 | window.web3 = new Web3(web3.currentProvider); 95 | } else { 96 | console.warn("No web3 detected. Falling back to http://127.0.0.1:9545. You should remove this fallback when you deploy live, as it's inherently insecure. Consider switching to Metamask for development. More info here: http://truffleframework.com/tutorials/truffle-and-metamask"); 97 | // fallback - use your fallback strategy (local node / hosted node + in-dapp id mgmt / fail) 98 | window.web3 = new Web3(new Web3.providers.HttpProvider("http://127.0.0.1:9545")); 99 | } 100 | 101 | App.start(); 102 | }); 103 | -------------------------------------------------------------------------------- /Chapter03/app/stylesheets/app.css: -------------------------------------------------------------------------------- 1 | body { 2 | margin-left: 25%; 3 | margin-right: 25%; 4 | margin-top: 10%; 5 | font-family: "Open Sans", sans-serif; 6 | } 7 | 8 | label { 9 | display: inline-block; 10 | width: 100px; 11 | } 12 | 13 | input { 14 | width: 500px; 15 | padding: 5px; 16 | font-size: 16px; 17 | } 18 | 19 | button { 20 | font-size: 16px; 21 | padding: 5px; 22 | } 23 | 24 | h1, h2 { 25 | display: inline-block; 26 | vertical-align: middle; 27 | margin-top: 0px; 28 | margin-bottom: 10px; 29 | } 30 | 31 | h2 { 32 | color: #AAA; 33 | font-size: 32px; 34 | } 35 | 36 | h3 { 37 | font-weight: normal; 38 | color: #AAA; 39 | font-size: 24px; 40 | } 41 | 42 | .black { 43 | color: black; 44 | } 45 | 46 | #balance { 47 | color: black; 48 | } 49 | 50 | .hint { 51 | color: #666; 52 | } 53 | -------------------------------------------------------------------------------- /Chapter03/box-img-lg.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/PacktPublishing/Ethereum-Projects-for-Beginners/96e2448b3061b45b6a77e54d527524cd0c23d4be/Chapter03/box-img-lg.png -------------------------------------------------------------------------------- /Chapter03/box-img-sm.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/PacktPublishing/Ethereum-Projects-for-Beginners/96e2448b3061b45b6a77e54d527524cd0c23d4be/Chapter03/box-img-sm.png -------------------------------------------------------------------------------- /Chapter03/build/contracts/ERC20.json: -------------------------------------------------------------------------------- 1 | { 2 | "contractName": "ERC20", 3 | "abi": [ 4 | { 5 | "constant": true, 6 | "inputs": [], 7 | "name": "totalSupply", 8 | "outputs": [ 9 | { 10 | "name": "", 11 | "type": "uint256" 12 | } 13 | ], 14 | "payable": false, 15 | "stateMutability": "view", 16 | "type": "function" 17 | }, 18 | { 19 | "constant": true, 20 | "inputs": [ 21 | { 22 | "name": "who", 23 | "type": "address" 24 | } 25 | ], 26 | "name": "balanceOf", 27 | "outputs": [ 28 | { 29 | "name": "", 30 | "type": "uint256" 31 | } 32 | ], 33 | "payable": false, 34 | "stateMutability": "view", 35 | "type": "function" 36 | }, 37 | { 38 | "constant": false, 39 | "inputs": [ 40 | { 41 | "name": "to", 42 | "type": "address" 43 | }, 44 | { 45 | "name": "value", 46 | "type": "uint256" 47 | } 48 | ], 49 | "name": "transfer", 50 | "outputs": [ 51 | { 52 | "name": "", 53 | "type": "bool" 54 | } 55 | ], 56 | "payable": false, 57 | "stateMutability": "nonpayable", 58 | "type": "function" 59 | }, 60 | { 61 | "anonymous": false, 62 | "inputs": [ 63 | { 64 | "indexed": true, 65 | "name": "owner", 66 | "type": "address" 67 | }, 68 | { 69 | "indexed": true, 70 | "name": "spender", 71 | "type": "address" 72 | }, 73 | { 74 | "indexed": false, 75 | "name": "value", 76 | "type": "uint256" 77 | } 78 | ], 79 | "name": "Approval", 80 | "type": "event" 81 | }, 82 | { 83 | "anonymous": false, 84 | "inputs": [ 85 | { 86 | "indexed": true, 87 | "name": "from", 88 | "type": "address" 89 | }, 90 | { 91 | "indexed": true, 92 | "name": "to", 93 | "type": "address" 94 | }, 95 | { 96 | "indexed": false, 97 | "name": "value", 98 | "type": "uint256" 99 | } 100 | ], 101 | "name": "Transfer", 102 | "type": "event" 103 | }, 104 | { 105 | "constant": true, 106 | "inputs": [ 107 | { 108 | "name": "owner", 109 | "type": "address" 110 | }, 111 | { 112 | "name": "spender", 113 | "type": "address" 114 | } 115 | ], 116 | "name": "allowance", 117 | "outputs": [ 118 | { 119 | "name": "", 120 | "type": "uint256" 121 | } 122 | ], 123 | "payable": false, 124 | "stateMutability": "view", 125 | "type": "function" 126 | }, 127 | { 128 | "constant": false, 129 | "inputs": [ 130 | { 131 | "name": "from", 132 | "type": "address" 133 | }, 134 | { 135 | "name": "to", 136 | "type": "address" 137 | }, 138 | { 139 | "name": "value", 140 | "type": "uint256" 141 | } 142 | ], 143 | "name": "transferFrom", 144 | "outputs": [ 145 | { 146 | "name": "", 147 | "type": "bool" 148 | } 149 | ], 150 | "payable": false, 151 | "stateMutability": "nonpayable", 152 | "type": "function" 153 | }, 154 | { 155 | "constant": false, 156 | "inputs": [ 157 | { 158 | "name": "spender", 159 | "type": "address" 160 | }, 161 | { 162 | "name": "value", 163 | "type": "uint256" 164 | } 165 | ], 166 | "name": "approve", 167 | "outputs": [ 168 | { 169 | "name": "", 170 | "type": "bool" 171 | } 172 | ], 173 | "payable": false, 174 | "stateMutability": "nonpayable", 175 | "type": "function" 176 | } 177 | ], 178 | "bytecode": "0x", 179 | "deployedBytecode": "0x", 180 | "sourceMap": "", 181 | "deployedSourceMap": "", 182 | "source": "pragma solidity ^0.4.18;\n\nimport \"./ERC20Basic.sol\";\n\n\n/**\n * @title ERC20 interface\n * @dev see https://github.com/ethereum/EIPs/issues/20\n */\ncontract ERC20 is ERC20Basic {\n function allowance(address owner, address spender) public view returns (uint256);\n function transferFrom(address from, address to, uint256 value) public returns (bool);\n function approve(address spender, uint256 value) public returns (bool);\n event Approval(address indexed owner, address indexed spender, uint256 value);\n}\n", 183 | "sourcePath": "zeppelin-solidity\\contracts\\token\\ERC20\\ERC20.sol", 184 | "ast": { 185 | "attributes": { 186 | "absolutePath": "zeppelin-solidity/contracts/token/ERC20/ERC20.sol", 187 | "exportedSymbols": { 188 | "ERC20": [ 189 | 269 190 | ] 191 | } 192 | }, 193 | "children": [ 194 | { 195 | "attributes": { 196 | "literals": [ 197 | "solidity", 198 | "^", 199 | "0.4", 200 | ".18" 201 | ] 202 | }, 203 | "id": 228, 204 | "name": "PragmaDirective", 205 | "src": "0:24:3" 206 | }, 207 | { 208 | "attributes": { 209 | "SourceUnit": 302, 210 | "absolutePath": "zeppelin-solidity/contracts/token/ERC20/ERC20Basic.sol", 211 | "file": "./ERC20Basic.sol", 212 | "scope": 270, 213 | "symbolAliases": [ 214 | null 215 | ], 216 | "unitAlias": "" 217 | }, 218 | "id": 229, 219 | "name": "ImportDirective", 220 | "src": "26:26:3" 221 | }, 222 | { 223 | "attributes": { 224 | "contractDependencies": [ 225 | 301 226 | ], 227 | "contractKind": "contract", 228 | "documentation": "@title ERC20 interface\n@dev see https://github.com/ethereum/EIPs/issues/20", 229 | "fullyImplemented": false, 230 | "linearizedBaseContracts": [ 231 | 269, 232 | 301 233 | ], 234 | "name": "ERC20", 235 | "scope": 270 236 | }, 237 | "children": [ 238 | { 239 | "attributes": { 240 | "arguments": [ 241 | null 242 | ] 243 | }, 244 | "children": [ 245 | { 246 | "attributes": { 247 | "contractScope": null, 248 | "name": "ERC20Basic", 249 | "referencedDeclaration": 301, 250 | "type": "contract ERC20Basic" 251 | }, 252 | "id": 230, 253 | "name": "UserDefinedTypeName", 254 | "src": "162:10:3" 255 | } 256 | ], 257 | "id": 231, 258 | "name": "InheritanceSpecifier", 259 | "src": "162:10:3" 260 | }, 261 | { 262 | "attributes": { 263 | "body": null, 264 | "constant": true, 265 | "implemented": false, 266 | "isConstructor": false, 267 | "modifiers": [ 268 | null 269 | ], 270 | "name": "allowance", 271 | "payable": false, 272 | "scope": 269, 273 | "stateMutability": "view", 274 | "superFunction": null, 275 | "visibility": "public" 276 | }, 277 | "children": [ 278 | { 279 | "children": [ 280 | { 281 | "attributes": { 282 | "constant": false, 283 | "name": "owner", 284 | "scope": 240, 285 | "stateVariable": false, 286 | "storageLocation": "default", 287 | "type": "address", 288 | "value": null, 289 | "visibility": "internal" 290 | }, 291 | "children": [ 292 | { 293 | "attributes": { 294 | "name": "address", 295 | "type": "address" 296 | }, 297 | "id": 232, 298 | "name": "ElementaryTypeName", 299 | "src": "196:7:3" 300 | } 301 | ], 302 | "id": 233, 303 | "name": "VariableDeclaration", 304 | "src": "196:13:3" 305 | }, 306 | { 307 | "attributes": { 308 | "constant": false, 309 | "name": "spender", 310 | "scope": 240, 311 | "stateVariable": false, 312 | "storageLocation": "default", 313 | "type": "address", 314 | "value": null, 315 | "visibility": "internal" 316 | }, 317 | "children": [ 318 | { 319 | "attributes": { 320 | "name": "address", 321 | "type": "address" 322 | }, 323 | "id": 234, 324 | "name": "ElementaryTypeName", 325 | "src": "211:7:3" 326 | } 327 | ], 328 | "id": 235, 329 | "name": "VariableDeclaration", 330 | "src": "211:15:3" 331 | } 332 | ], 333 | "id": 236, 334 | "name": "ParameterList", 335 | "src": "195:32:3" 336 | }, 337 | { 338 | "children": [ 339 | { 340 | "attributes": { 341 | "constant": false, 342 | "name": "", 343 | "scope": 240, 344 | "stateVariable": false, 345 | "storageLocation": "default", 346 | "type": "uint256", 347 | "value": null, 348 | "visibility": "internal" 349 | }, 350 | "children": [ 351 | { 352 | "attributes": { 353 | "name": "uint256", 354 | "type": "uint256" 355 | }, 356 | "id": 237, 357 | "name": "ElementaryTypeName", 358 | "src": "249:7:3" 359 | } 360 | ], 361 | "id": 238, 362 | "name": "VariableDeclaration", 363 | "src": "249:7:3" 364 | } 365 | ], 366 | "id": 239, 367 | "name": "ParameterList", 368 | "src": "248:9:3" 369 | } 370 | ], 371 | "id": 240, 372 | "name": "FunctionDefinition", 373 | "src": "177:81:3" 374 | }, 375 | { 376 | "attributes": { 377 | "body": null, 378 | "constant": false, 379 | "implemented": false, 380 | "isConstructor": false, 381 | "modifiers": [ 382 | null 383 | ], 384 | "name": "transferFrom", 385 | "payable": false, 386 | "scope": 269, 387 | "stateMutability": "nonpayable", 388 | "superFunction": null, 389 | "visibility": "public" 390 | }, 391 | "children": [ 392 | { 393 | "children": [ 394 | { 395 | "attributes": { 396 | "constant": false, 397 | "name": "from", 398 | "scope": 251, 399 | "stateVariable": false, 400 | "storageLocation": "default", 401 | "type": "address", 402 | "value": null, 403 | "visibility": "internal" 404 | }, 405 | "children": [ 406 | { 407 | "attributes": { 408 | "name": "address", 409 | "type": "address" 410 | }, 411 | "id": 241, 412 | "name": "ElementaryTypeName", 413 | "src": "283:7:3" 414 | } 415 | ], 416 | "id": 242, 417 | "name": "VariableDeclaration", 418 | "src": "283:12:3" 419 | }, 420 | { 421 | "attributes": { 422 | "constant": false, 423 | "name": "to", 424 | "scope": 251, 425 | "stateVariable": false, 426 | "storageLocation": "default", 427 | "type": "address", 428 | "value": null, 429 | "visibility": "internal" 430 | }, 431 | "children": [ 432 | { 433 | "attributes": { 434 | "name": "address", 435 | "type": "address" 436 | }, 437 | "id": 243, 438 | "name": "ElementaryTypeName", 439 | "src": "297:7:3" 440 | } 441 | ], 442 | "id": 244, 443 | "name": "VariableDeclaration", 444 | "src": "297:10:3" 445 | }, 446 | { 447 | "attributes": { 448 | "constant": false, 449 | "name": "value", 450 | "scope": 251, 451 | "stateVariable": false, 452 | "storageLocation": "default", 453 | "type": "uint256", 454 | "value": null, 455 | "visibility": "internal" 456 | }, 457 | "children": [ 458 | { 459 | "attributes": { 460 | "name": "uint256", 461 | "type": "uint256" 462 | }, 463 | "id": 245, 464 | "name": "ElementaryTypeName", 465 | "src": "309:7:3" 466 | } 467 | ], 468 | "id": 246, 469 | "name": "VariableDeclaration", 470 | "src": "309:13:3" 471 | } 472 | ], 473 | "id": 247, 474 | "name": "ParameterList", 475 | "src": "282:41:3" 476 | }, 477 | { 478 | "children": [ 479 | { 480 | "attributes": { 481 | "constant": false, 482 | "name": "", 483 | "scope": 251, 484 | "stateVariable": false, 485 | "storageLocation": "default", 486 | "type": "bool", 487 | "value": null, 488 | "visibility": "internal" 489 | }, 490 | "children": [ 491 | { 492 | "attributes": { 493 | "name": "bool", 494 | "type": "bool" 495 | }, 496 | "id": 248, 497 | "name": "ElementaryTypeName", 498 | "src": "340:4:3" 499 | } 500 | ], 501 | "id": 249, 502 | "name": "VariableDeclaration", 503 | "src": "340:4:3" 504 | } 505 | ], 506 | "id": 250, 507 | "name": "ParameterList", 508 | "src": "339:6:3" 509 | } 510 | ], 511 | "id": 251, 512 | "name": "FunctionDefinition", 513 | "src": "261:85:3" 514 | }, 515 | { 516 | "attributes": { 517 | "body": null, 518 | "constant": false, 519 | "implemented": false, 520 | "isConstructor": false, 521 | "modifiers": [ 522 | null 523 | ], 524 | "name": "approve", 525 | "payable": false, 526 | "scope": 269, 527 | "stateMutability": "nonpayable", 528 | "superFunction": null, 529 | "visibility": "public" 530 | }, 531 | "children": [ 532 | { 533 | "children": [ 534 | { 535 | "attributes": { 536 | "constant": false, 537 | "name": "spender", 538 | "scope": 260, 539 | "stateVariable": false, 540 | "storageLocation": "default", 541 | "type": "address", 542 | "value": null, 543 | "visibility": "internal" 544 | }, 545 | "children": [ 546 | { 547 | "attributes": { 548 | "name": "address", 549 | "type": "address" 550 | }, 551 | "id": 252, 552 | "name": "ElementaryTypeName", 553 | "src": "366:7:3" 554 | } 555 | ], 556 | "id": 253, 557 | "name": "VariableDeclaration", 558 | "src": "366:15:3" 559 | }, 560 | { 561 | "attributes": { 562 | "constant": false, 563 | "name": "value", 564 | "scope": 260, 565 | "stateVariable": false, 566 | "storageLocation": "default", 567 | "type": "uint256", 568 | "value": null, 569 | "visibility": "internal" 570 | }, 571 | "children": [ 572 | { 573 | "attributes": { 574 | "name": "uint256", 575 | "type": "uint256" 576 | }, 577 | "id": 254, 578 | "name": "ElementaryTypeName", 579 | "src": "383:7:3" 580 | } 581 | ], 582 | "id": 255, 583 | "name": "VariableDeclaration", 584 | "src": "383:13:3" 585 | } 586 | ], 587 | "id": 256, 588 | "name": "ParameterList", 589 | "src": "365:32:3" 590 | }, 591 | { 592 | "children": [ 593 | { 594 | "attributes": { 595 | "constant": false, 596 | "name": "", 597 | "scope": 260, 598 | "stateVariable": false, 599 | "storageLocation": "default", 600 | "type": "bool", 601 | "value": null, 602 | "visibility": "internal" 603 | }, 604 | "children": [ 605 | { 606 | "attributes": { 607 | "name": "bool", 608 | "type": "bool" 609 | }, 610 | "id": 257, 611 | "name": "ElementaryTypeName", 612 | "src": "414:4:3" 613 | } 614 | ], 615 | "id": 258, 616 | "name": "VariableDeclaration", 617 | "src": "414:4:3" 618 | } 619 | ], 620 | "id": 259, 621 | "name": "ParameterList", 622 | "src": "413:6:3" 623 | } 624 | ], 625 | "id": 260, 626 | "name": "FunctionDefinition", 627 | "src": "349:71:3" 628 | }, 629 | { 630 | "attributes": { 631 | "anonymous": false, 632 | "name": "Approval" 633 | }, 634 | "children": [ 635 | { 636 | "children": [ 637 | { 638 | "attributes": { 639 | "constant": false, 640 | "indexed": true, 641 | "name": "owner", 642 | "scope": 268, 643 | "stateVariable": false, 644 | "storageLocation": "default", 645 | "type": "address", 646 | "value": null, 647 | "visibility": "internal" 648 | }, 649 | "children": [ 650 | { 651 | "attributes": { 652 | "name": "address", 653 | "type": "address" 654 | }, 655 | "id": 261, 656 | "name": "ElementaryTypeName", 657 | "src": "438:7:3" 658 | } 659 | ], 660 | "id": 262, 661 | "name": "VariableDeclaration", 662 | "src": "438:21:3" 663 | }, 664 | { 665 | "attributes": { 666 | "constant": false, 667 | "indexed": true, 668 | "name": "spender", 669 | "scope": 268, 670 | "stateVariable": false, 671 | "storageLocation": "default", 672 | "type": "address", 673 | "value": null, 674 | "visibility": "internal" 675 | }, 676 | "children": [ 677 | { 678 | "attributes": { 679 | "name": "address", 680 | "type": "address" 681 | }, 682 | "id": 263, 683 | "name": "ElementaryTypeName", 684 | "src": "461:7:3" 685 | } 686 | ], 687 | "id": 264, 688 | "name": "VariableDeclaration", 689 | "src": "461:23:3" 690 | }, 691 | { 692 | "attributes": { 693 | "constant": false, 694 | "indexed": false, 695 | "name": "value", 696 | "scope": 268, 697 | "stateVariable": false, 698 | "storageLocation": "default", 699 | "type": "uint256", 700 | "value": null, 701 | "visibility": "internal" 702 | }, 703 | "children": [ 704 | { 705 | "attributes": { 706 | "name": "uint256", 707 | "type": "uint256" 708 | }, 709 | "id": 265, 710 | "name": "ElementaryTypeName", 711 | "src": "486:7:3" 712 | } 713 | ], 714 | "id": 266, 715 | "name": "VariableDeclaration", 716 | "src": "486:13:3" 717 | } 718 | ], 719 | "id": 267, 720 | "name": "ParameterList", 721 | "src": "437:63:3" 722 | } 723 | ], 724 | "id": 268, 725 | "name": "EventDefinition", 726 | "src": "423:78:3" 727 | } 728 | ], 729 | "id": 269, 730 | "name": "ContractDefinition", 731 | "src": "144:359:3" 732 | } 733 | ], 734 | "id": 270, 735 | "name": "SourceUnit", 736 | "src": "0:504:3" 737 | }, 738 | "compiler": { 739 | "name": "solc", 740 | "version": "0.4.19+commit.c4cbbb05.Emscripten.clang" 741 | }, 742 | "networks": {}, 743 | "schemaVersion": "1.0.1", 744 | "updatedAt": "2018-03-29T14:08:50.584Z" 745 | } -------------------------------------------------------------------------------- /Chapter03/build/contracts/ERC20Basic.json: -------------------------------------------------------------------------------- 1 | { 2 | "contractName": "ERC20Basic", 3 | "abi": [ 4 | { 5 | "anonymous": false, 6 | "inputs": [ 7 | { 8 | "indexed": true, 9 | "name": "from", 10 | "type": "address" 11 | }, 12 | { 13 | "indexed": true, 14 | "name": "to", 15 | "type": "address" 16 | }, 17 | { 18 | "indexed": false, 19 | "name": "value", 20 | "type": "uint256" 21 | } 22 | ], 23 | "name": "Transfer", 24 | "type": "event" 25 | }, 26 | { 27 | "constant": true, 28 | "inputs": [], 29 | "name": "totalSupply", 30 | "outputs": [ 31 | { 32 | "name": "", 33 | "type": "uint256" 34 | } 35 | ], 36 | "payable": false, 37 | "stateMutability": "view", 38 | "type": "function" 39 | }, 40 | { 41 | "constant": true, 42 | "inputs": [ 43 | { 44 | "name": "who", 45 | "type": "address" 46 | } 47 | ], 48 | "name": "balanceOf", 49 | "outputs": [ 50 | { 51 | "name": "", 52 | "type": "uint256" 53 | } 54 | ], 55 | "payable": false, 56 | "stateMutability": "view", 57 | "type": "function" 58 | }, 59 | { 60 | "constant": false, 61 | "inputs": [ 62 | { 63 | "name": "to", 64 | "type": "address" 65 | }, 66 | { 67 | "name": "value", 68 | "type": "uint256" 69 | } 70 | ], 71 | "name": "transfer", 72 | "outputs": [ 73 | { 74 | "name": "", 75 | "type": "bool" 76 | } 77 | ], 78 | "payable": false, 79 | "stateMutability": "nonpayable", 80 | "type": "function" 81 | } 82 | ], 83 | "bytecode": "0x", 84 | "deployedBytecode": "0x", 85 | "sourceMap": "", 86 | "deployedSourceMap": "", 87 | "source": "pragma solidity ^0.4.18;\n\n\n/**\n * @title ERC20Basic\n * @dev Simpler version of ERC20 interface\n * @dev see https://github.com/ethereum/EIPs/issues/179\n */\ncontract ERC20Basic {\n function totalSupply() public view returns (uint256);\n function balanceOf(address who) public view returns (uint256);\n function transfer(address to, uint256 value) public returns (bool);\n event Transfer(address indexed from, address indexed to, uint256 value);\n}\n", 88 | "sourcePath": "zeppelin-solidity\\contracts\\token\\ERC20\\ERC20Basic.sol", 89 | "ast": { 90 | "attributes": { 91 | "absolutePath": "zeppelin-solidity/contracts/token/ERC20/ERC20Basic.sol", 92 | "exportedSymbols": { 93 | "ERC20Basic": [ 94 | 301 95 | ] 96 | } 97 | }, 98 | "children": [ 99 | { 100 | "attributes": { 101 | "literals": [ 102 | "solidity", 103 | "^", 104 | "0.4", 105 | ".18" 106 | ] 107 | }, 108 | "id": 271, 109 | "name": "PragmaDirective", 110 | "src": "0:24:4" 111 | }, 112 | { 113 | "attributes": { 114 | "baseContracts": [ 115 | null 116 | ], 117 | "contractDependencies": [ 118 | null 119 | ], 120 | "contractKind": "contract", 121 | "documentation": "@title ERC20Basic\n@dev Simpler version of ERC20 interface\n@dev see https://github.com/ethereum/EIPs/issues/179", 122 | "fullyImplemented": false, 123 | "linearizedBaseContracts": [ 124 | 301 125 | ], 126 | "name": "ERC20Basic", 127 | "scope": 302 128 | }, 129 | "children": [ 130 | { 131 | "attributes": { 132 | "body": null, 133 | "constant": true, 134 | "implemented": false, 135 | "isConstructor": false, 136 | "modifiers": [ 137 | null 138 | ], 139 | "name": "totalSupply", 140 | "payable": false, 141 | "scope": 301, 142 | "stateMutability": "view", 143 | "superFunction": null, 144 | "visibility": "public" 145 | }, 146 | "children": [ 147 | { 148 | "attributes": { 149 | "parameters": [ 150 | null 151 | ] 152 | }, 153 | "children": [], 154 | "id": 272, 155 | "name": "ParameterList", 156 | "src": "199:2:4" 157 | }, 158 | { 159 | "children": [ 160 | { 161 | "attributes": { 162 | "constant": false, 163 | "name": "", 164 | "scope": 276, 165 | "stateVariable": false, 166 | "storageLocation": "default", 167 | "type": "uint256", 168 | "value": null, 169 | "visibility": "internal" 170 | }, 171 | "children": [ 172 | { 173 | "attributes": { 174 | "name": "uint256", 175 | "type": "uint256" 176 | }, 177 | "id": 273, 178 | "name": "ElementaryTypeName", 179 | "src": "223:7:4" 180 | } 181 | ], 182 | "id": 274, 183 | "name": "VariableDeclaration", 184 | "src": "223:7:4" 185 | } 186 | ], 187 | "id": 275, 188 | "name": "ParameterList", 189 | "src": "222:9:4" 190 | } 191 | ], 192 | "id": 276, 193 | "name": "FunctionDefinition", 194 | "src": "179:53:4" 195 | }, 196 | { 197 | "attributes": { 198 | "body": null, 199 | "constant": true, 200 | "implemented": false, 201 | "isConstructor": false, 202 | "modifiers": [ 203 | null 204 | ], 205 | "name": "balanceOf", 206 | "payable": false, 207 | "scope": 301, 208 | "stateMutability": "view", 209 | "superFunction": null, 210 | "visibility": "public" 211 | }, 212 | "children": [ 213 | { 214 | "children": [ 215 | { 216 | "attributes": { 217 | "constant": false, 218 | "name": "who", 219 | "scope": 283, 220 | "stateVariable": false, 221 | "storageLocation": "default", 222 | "type": "address", 223 | "value": null, 224 | "visibility": "internal" 225 | }, 226 | "children": [ 227 | { 228 | "attributes": { 229 | "name": "address", 230 | "type": "address" 231 | }, 232 | "id": 277, 233 | "name": "ElementaryTypeName", 234 | "src": "254:7:4" 235 | } 236 | ], 237 | "id": 278, 238 | "name": "VariableDeclaration", 239 | "src": "254:11:4" 240 | } 241 | ], 242 | "id": 279, 243 | "name": "ParameterList", 244 | "src": "253:13:4" 245 | }, 246 | { 247 | "children": [ 248 | { 249 | "attributes": { 250 | "constant": false, 251 | "name": "", 252 | "scope": 283, 253 | "stateVariable": false, 254 | "storageLocation": "default", 255 | "type": "uint256", 256 | "value": null, 257 | "visibility": "internal" 258 | }, 259 | "children": [ 260 | { 261 | "attributes": { 262 | "name": "uint256", 263 | "type": "uint256" 264 | }, 265 | "id": 280, 266 | "name": "ElementaryTypeName", 267 | "src": "288:7:4" 268 | } 269 | ], 270 | "id": 281, 271 | "name": "VariableDeclaration", 272 | "src": "288:7:4" 273 | } 274 | ], 275 | "id": 282, 276 | "name": "ParameterList", 277 | "src": "287:9:4" 278 | } 279 | ], 280 | "id": 283, 281 | "name": "FunctionDefinition", 282 | "src": "235:62:4" 283 | }, 284 | { 285 | "attributes": { 286 | "body": null, 287 | "constant": false, 288 | "implemented": false, 289 | "isConstructor": false, 290 | "modifiers": [ 291 | null 292 | ], 293 | "name": "transfer", 294 | "payable": false, 295 | "scope": 301, 296 | "stateMutability": "nonpayable", 297 | "superFunction": null, 298 | "visibility": "public" 299 | }, 300 | "children": [ 301 | { 302 | "children": [ 303 | { 304 | "attributes": { 305 | "constant": false, 306 | "name": "to", 307 | "scope": 292, 308 | "stateVariable": false, 309 | "storageLocation": "default", 310 | "type": "address", 311 | "value": null, 312 | "visibility": "internal" 313 | }, 314 | "children": [ 315 | { 316 | "attributes": { 317 | "name": "address", 318 | "type": "address" 319 | }, 320 | "id": 284, 321 | "name": "ElementaryTypeName", 322 | "src": "318:7:4" 323 | } 324 | ], 325 | "id": 285, 326 | "name": "VariableDeclaration", 327 | "src": "318:10:4" 328 | }, 329 | { 330 | "attributes": { 331 | "constant": false, 332 | "name": "value", 333 | "scope": 292, 334 | "stateVariable": false, 335 | "storageLocation": "default", 336 | "type": "uint256", 337 | "value": null, 338 | "visibility": "internal" 339 | }, 340 | "children": [ 341 | { 342 | "attributes": { 343 | "name": "uint256", 344 | "type": "uint256" 345 | }, 346 | "id": 286, 347 | "name": "ElementaryTypeName", 348 | "src": "330:7:4" 349 | } 350 | ], 351 | "id": 287, 352 | "name": "VariableDeclaration", 353 | "src": "330:13:4" 354 | } 355 | ], 356 | "id": 288, 357 | "name": "ParameterList", 358 | "src": "317:27:4" 359 | }, 360 | { 361 | "children": [ 362 | { 363 | "attributes": { 364 | "constant": false, 365 | "name": "", 366 | "scope": 292, 367 | "stateVariable": false, 368 | "storageLocation": "default", 369 | "type": "bool", 370 | "value": null, 371 | "visibility": "internal" 372 | }, 373 | "children": [ 374 | { 375 | "attributes": { 376 | "name": "bool", 377 | "type": "bool" 378 | }, 379 | "id": 289, 380 | "name": "ElementaryTypeName", 381 | "src": "361:4:4" 382 | } 383 | ], 384 | "id": 290, 385 | "name": "VariableDeclaration", 386 | "src": "361:4:4" 387 | } 388 | ], 389 | "id": 291, 390 | "name": "ParameterList", 391 | "src": "360:6:4" 392 | } 393 | ], 394 | "id": 292, 395 | "name": "FunctionDefinition", 396 | "src": "300:67:4" 397 | }, 398 | { 399 | "attributes": { 400 | "anonymous": false, 401 | "name": "Transfer" 402 | }, 403 | "children": [ 404 | { 405 | "children": [ 406 | { 407 | "attributes": { 408 | "constant": false, 409 | "indexed": true, 410 | "name": "from", 411 | "scope": 300, 412 | "stateVariable": false, 413 | "storageLocation": "default", 414 | "type": "address", 415 | "value": null, 416 | "visibility": "internal" 417 | }, 418 | "children": [ 419 | { 420 | "attributes": { 421 | "name": "address", 422 | "type": "address" 423 | }, 424 | "id": 293, 425 | "name": "ElementaryTypeName", 426 | "src": "385:7:4" 427 | } 428 | ], 429 | "id": 294, 430 | "name": "VariableDeclaration", 431 | "src": "385:20:4" 432 | }, 433 | { 434 | "attributes": { 435 | "constant": false, 436 | "indexed": true, 437 | "name": "to", 438 | "scope": 300, 439 | "stateVariable": false, 440 | "storageLocation": "default", 441 | "type": "address", 442 | "value": null, 443 | "visibility": "internal" 444 | }, 445 | "children": [ 446 | { 447 | "attributes": { 448 | "name": "address", 449 | "type": "address" 450 | }, 451 | "id": 295, 452 | "name": "ElementaryTypeName", 453 | "src": "407:7:4" 454 | } 455 | ], 456 | "id": 296, 457 | "name": "VariableDeclaration", 458 | "src": "407:18:4" 459 | }, 460 | { 461 | "attributes": { 462 | "constant": false, 463 | "indexed": false, 464 | "name": "value", 465 | "scope": 300, 466 | "stateVariable": false, 467 | "storageLocation": "default", 468 | "type": "uint256", 469 | "value": null, 470 | "visibility": "internal" 471 | }, 472 | "children": [ 473 | { 474 | "attributes": { 475 | "name": "uint256", 476 | "type": "uint256" 477 | }, 478 | "id": 297, 479 | "name": "ElementaryTypeName", 480 | "src": "427:7:4" 481 | } 482 | ], 483 | "id": 298, 484 | "name": "VariableDeclaration", 485 | "src": "427:13:4" 486 | } 487 | ], 488 | "id": 299, 489 | "name": "ParameterList", 490 | "src": "384:57:4" 491 | } 492 | ], 493 | "id": 300, 494 | "name": "EventDefinition", 495 | "src": "370:72:4" 496 | } 497 | ], 498 | "id": 301, 499 | "name": "ContractDefinition", 500 | "src": "155:289:4" 501 | } 502 | ], 503 | "id": 302, 504 | "name": "SourceUnit", 505 | "src": "0:445:4" 506 | }, 507 | "compiler": { 508 | "name": "solc", 509 | "version": "0.4.19+commit.c4cbbb05.Emscripten.clang" 510 | }, 511 | "networks": {}, 512 | "schemaVersion": "1.0.1", 513 | "updatedAt": "2018-03-29T14:08:50.582Z" 514 | } -------------------------------------------------------------------------------- /Chapter03/build/contracts/Migrations.json: -------------------------------------------------------------------------------- 1 | { 2 | "contractName": "Migrations", 3 | "abi": [ 4 | { 5 | "constant": true, 6 | "inputs": [], 7 | "name": "last_completed_migration", 8 | "outputs": [ 9 | { 10 | "name": "", 11 | "type": "uint256" 12 | } 13 | ], 14 | "payable": false, 15 | "stateMutability": "view", 16 | "type": "function" 17 | }, 18 | { 19 | "constant": true, 20 | "inputs": [], 21 | "name": "owner", 22 | "outputs": [ 23 | { 24 | "name": "", 25 | "type": "address" 26 | } 27 | ], 28 | "payable": false, 29 | "stateMutability": "view", 30 | "type": "function" 31 | }, 32 | { 33 | "inputs": [], 34 | "payable": false, 35 | "stateMutability": "nonpayable", 36 | "type": "constructor" 37 | }, 38 | { 39 | "constant": false, 40 | "inputs": [ 41 | { 42 | "name": "completed", 43 | "type": "uint256" 44 | } 45 | ], 46 | "name": "setCompleted", 47 | "outputs": [], 48 | "payable": false, 49 | "stateMutability": "nonpayable", 50 | "type": "function" 51 | }, 52 | { 53 | "constant": false, 54 | "inputs": [ 55 | { 56 | "name": "new_address", 57 | "type": "address" 58 | } 59 | ], 60 | "name": "upgrade", 61 | "outputs": [], 62 | "payable": false, 63 | "stateMutability": "nonpayable", 64 | "type": "function" 65 | } 66 | ], 67 | "bytecode": "0x6060604052341561000f57600080fd5b336000806101000a81548173ffffffffffffffffffffffffffffffffffffffff021916908373ffffffffffffffffffffffffffffffffffffffff1602179055506102db8061005e6000396000f300606060405260043610610062576000357c0100000000000000000000000000000000000000000000000000000000900463ffffffff1680630900f01014610067578063445df0ac146100a05780638da5cb5b146100c9578063fdacd5761461011e575b600080fd5b341561007257600080fd5b61009e600480803573ffffffffffffffffffffffffffffffffffffffff16906020019091905050610141565b005b34156100ab57600080fd5b6100b3610224565b6040518082815260200191505060405180910390f35b34156100d457600080fd5b6100dc61022a565b604051808273ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff16815260200191505060405180910390f35b341561012957600080fd5b61013f600480803590602001909190505061024f565b005b60008060009054906101000a900473ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff163373ffffffffffffffffffffffffffffffffffffffff161415610220578190508073ffffffffffffffffffffffffffffffffffffffff1663fdacd5766001546040518263ffffffff167c010000000000000000000000000000000000000000000000000000000002815260040180828152602001915050600060405180830381600087803b151561020b57600080fd5b6102c65a03f1151561021c57600080fd5b5050505b5050565b60015481565b6000809054906101000a900473ffffffffffffffffffffffffffffffffffffffff1681565b6000809054906101000a900473ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff163373ffffffffffffffffffffffffffffffffffffffff1614156102ac57806001819055505b505600a165627a7a723058200b8e751c2f263ddd593c89b1d83007e02531a2fda76114341b19955efc5172210029", 68 | "deployedBytecode": "0x606060405260043610610062576000357c0100000000000000000000000000000000000000000000000000000000900463ffffffff1680630900f01014610067578063445df0ac146100a05780638da5cb5b146100c9578063fdacd5761461011e575b600080fd5b341561007257600080fd5b61009e600480803573ffffffffffffffffffffffffffffffffffffffff16906020019091905050610141565b005b34156100ab57600080fd5b6100b3610224565b6040518082815260200191505060405180910390f35b34156100d457600080fd5b6100dc61022a565b604051808273ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff16815260200191505060405180910390f35b341561012957600080fd5b61013f600480803590602001909190505061024f565b005b60008060009054906101000a900473ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff163373ffffffffffffffffffffffffffffffffffffffff161415610220578190508073ffffffffffffffffffffffffffffffffffffffff1663fdacd5766001546040518263ffffffff167c010000000000000000000000000000000000000000000000000000000002815260040180828152602001915050600060405180830381600087803b151561020b57600080fd5b6102c65a03f1151561021c57600080fd5b5050505b5050565b60015481565b6000809054906101000a900473ffffffffffffffffffffffffffffffffffffffff1681565b6000809054906101000a900473ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff163373ffffffffffffffffffffffffffffffffffffffff1614156102ac57806001819055505b505600a165627a7a723058200b8e751c2f263ddd593c89b1d83007e02531a2fda76114341b19955efc5172210029", 69 | "sourceMap": "26:488:1:-;;;178:58;;;;;;;;221:10;213:5;;:18;;;;;;;;;;;;;;;;;;26:488;;;;;;", 70 | "deployedSourceMap": "26:488:1:-;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;347:165;;;;;;;;;;;;;;;;;;;;;;;;;;;;74:36;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;50:20;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;240:103;;;;;;;;;;;;;;;;;;;;;;;;;;347:165;409:19;161:5;;;;;;;;;;;147:19;;:10;:19;;;143:26;;;442:11;409:45;;460:8;:21;;;482:24;;460:47;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;143:26;347:165;;:::o;74:36::-;;;;:::o;50:20::-;;;;;;;;;;;;;:::o;240:103::-;161:5;;;;;;;;;;;147:19;;:10;:19;;;143:26;;;329:9;302:24;:36;;;;143:26;240:103;:::o", 71 | "source": "pragma solidity ^0.4.17;\n\ncontract Migrations {\n address public owner;\n uint public last_completed_migration;\n\n modifier restricted() {\n if (msg.sender == owner) _;\n }\n\n function Migrations() public {\n owner = msg.sender;\n }\n\n function setCompleted(uint completed) public restricted {\n last_completed_migration = completed;\n }\n\n function upgrade(address new_address) public restricted {\n Migrations upgraded = Migrations(new_address);\n upgraded.setCompleted(last_completed_migration);\n }\n}\n", 72 | "sourcePath": "F:\\Dev\\ethereum_packt\\erc20_mine\\contracts\\Migrations.sol", 73 | "ast": { 74 | "attributes": { 75 | "absolutePath": "/F/Dev/ethereum_packt/erc20_mine/contracts/Migrations.sol", 76 | "exportedSymbols": { 77 | "Migrations": [ 78 | 62 79 | ] 80 | } 81 | }, 82 | "children": [ 83 | { 84 | "attributes": { 85 | "literals": [ 86 | "solidity", 87 | "^", 88 | "0.4", 89 | ".17" 90 | ] 91 | }, 92 | "id": 7, 93 | "name": "PragmaDirective", 94 | "src": "0:24:1" 95 | }, 96 | { 97 | "attributes": { 98 | "baseContracts": [ 99 | null 100 | ], 101 | "contractDependencies": [ 102 | null 103 | ], 104 | "contractKind": "contract", 105 | "documentation": null, 106 | "fullyImplemented": true, 107 | "linearizedBaseContracts": [ 108 | 62 109 | ], 110 | "name": "Migrations", 111 | "scope": 63 112 | }, 113 | "children": [ 114 | { 115 | "attributes": { 116 | "constant": false, 117 | "name": "owner", 118 | "scope": 62, 119 | "stateVariable": true, 120 | "storageLocation": "default", 121 | "type": "address", 122 | "value": null, 123 | "visibility": "public" 124 | }, 125 | "children": [ 126 | { 127 | "attributes": { 128 | "name": "address", 129 | "type": "address" 130 | }, 131 | "id": 8, 132 | "name": "ElementaryTypeName", 133 | "src": "50:7:1" 134 | } 135 | ], 136 | "id": 9, 137 | "name": "VariableDeclaration", 138 | "src": "50:20:1" 139 | }, 140 | { 141 | "attributes": { 142 | "constant": false, 143 | "name": "last_completed_migration", 144 | "scope": 62, 145 | "stateVariable": true, 146 | "storageLocation": "default", 147 | "type": "uint256", 148 | "value": null, 149 | "visibility": "public" 150 | }, 151 | "children": [ 152 | { 153 | "attributes": { 154 | "name": "uint", 155 | "type": "uint256" 156 | }, 157 | "id": 10, 158 | "name": "ElementaryTypeName", 159 | "src": "74:4:1" 160 | } 161 | ], 162 | "id": 11, 163 | "name": "VariableDeclaration", 164 | "src": "74:36:1" 165 | }, 166 | { 167 | "attributes": { 168 | "name": "restricted", 169 | "visibility": "internal" 170 | }, 171 | "children": [ 172 | { 173 | "attributes": { 174 | "parameters": [ 175 | null 176 | ] 177 | }, 178 | "children": [], 179 | "id": 12, 180 | "name": "ParameterList", 181 | "src": "134:2:1" 182 | }, 183 | { 184 | "children": [ 185 | { 186 | "attributes": { 187 | "falseBody": null 188 | }, 189 | "children": [ 190 | { 191 | "attributes": { 192 | "argumentTypes": null, 193 | "commonType": { 194 | "typeIdentifier": "t_address", 195 | "typeString": "address" 196 | }, 197 | "isConstant": false, 198 | "isLValue": false, 199 | "isPure": false, 200 | "lValueRequested": false, 201 | "operator": "==", 202 | "type": "bool" 203 | }, 204 | "children": [ 205 | { 206 | "attributes": { 207 | "argumentTypes": null, 208 | "isConstant": false, 209 | "isLValue": false, 210 | "isPure": false, 211 | "lValueRequested": false, 212 | "member_name": "sender", 213 | "referencedDeclaration": null, 214 | "type": "address" 215 | }, 216 | "children": [ 217 | { 218 | "attributes": { 219 | "argumentTypes": null, 220 | "overloadedDeclarations": [ 221 | null 222 | ], 223 | "referencedDeclaration": 589, 224 | "type": "msg", 225 | "value": "msg" 226 | }, 227 | "id": 13, 228 | "name": "Identifier", 229 | "src": "147:3:1" 230 | } 231 | ], 232 | "id": 14, 233 | "name": "MemberAccess", 234 | "src": "147:10:1" 235 | }, 236 | { 237 | "attributes": { 238 | "argumentTypes": null, 239 | "overloadedDeclarations": [ 240 | null 241 | ], 242 | "referencedDeclaration": 9, 243 | "type": "address", 244 | "value": "owner" 245 | }, 246 | "id": 15, 247 | "name": "Identifier", 248 | "src": "161:5:1" 249 | } 250 | ], 251 | "id": 16, 252 | "name": "BinaryOperation", 253 | "src": "147:19:1" 254 | }, 255 | { 256 | "id": 17, 257 | "name": "PlaceholderStatement", 258 | "src": "168:1:1" 259 | } 260 | ], 261 | "id": 18, 262 | "name": "IfStatement", 263 | "src": "143:26:1" 264 | } 265 | ], 266 | "id": 19, 267 | "name": "Block", 268 | "src": "137:37:1" 269 | } 270 | ], 271 | "id": 20, 272 | "name": "ModifierDefinition", 273 | "src": "115:59:1" 274 | }, 275 | { 276 | "attributes": { 277 | "constant": false, 278 | "implemented": true, 279 | "isConstructor": true, 280 | "modifiers": [ 281 | null 282 | ], 283 | "name": "Migrations", 284 | "payable": false, 285 | "scope": 62, 286 | "stateMutability": "nonpayable", 287 | "superFunction": null, 288 | "visibility": "public" 289 | }, 290 | "children": [ 291 | { 292 | "attributes": { 293 | "parameters": [ 294 | null 295 | ] 296 | }, 297 | "children": [], 298 | "id": 21, 299 | "name": "ParameterList", 300 | "src": "197:2:1" 301 | }, 302 | { 303 | "attributes": { 304 | "parameters": [ 305 | null 306 | ] 307 | }, 308 | "children": [], 309 | "id": 22, 310 | "name": "ParameterList", 311 | "src": "207:0:1" 312 | }, 313 | { 314 | "children": [ 315 | { 316 | "children": [ 317 | { 318 | "attributes": { 319 | "argumentTypes": null, 320 | "isConstant": false, 321 | "isLValue": false, 322 | "isPure": false, 323 | "lValueRequested": false, 324 | "operator": "=", 325 | "type": "address" 326 | }, 327 | "children": [ 328 | { 329 | "attributes": { 330 | "argumentTypes": null, 331 | "overloadedDeclarations": [ 332 | null 333 | ], 334 | "referencedDeclaration": 9, 335 | "type": "address", 336 | "value": "owner" 337 | }, 338 | "id": 23, 339 | "name": "Identifier", 340 | "src": "213:5:1" 341 | }, 342 | { 343 | "attributes": { 344 | "argumentTypes": null, 345 | "isConstant": false, 346 | "isLValue": false, 347 | "isPure": false, 348 | "lValueRequested": false, 349 | "member_name": "sender", 350 | "referencedDeclaration": null, 351 | "type": "address" 352 | }, 353 | "children": [ 354 | { 355 | "attributes": { 356 | "argumentTypes": null, 357 | "overloadedDeclarations": [ 358 | null 359 | ], 360 | "referencedDeclaration": 589, 361 | "type": "msg", 362 | "value": "msg" 363 | }, 364 | "id": 24, 365 | "name": "Identifier", 366 | "src": "221:3:1" 367 | } 368 | ], 369 | "id": 25, 370 | "name": "MemberAccess", 371 | "src": "221:10:1" 372 | } 373 | ], 374 | "id": 26, 375 | "name": "Assignment", 376 | "src": "213:18:1" 377 | } 378 | ], 379 | "id": 27, 380 | "name": "ExpressionStatement", 381 | "src": "213:18:1" 382 | } 383 | ], 384 | "id": 28, 385 | "name": "Block", 386 | "src": "207:29:1" 387 | } 388 | ], 389 | "id": 29, 390 | "name": "FunctionDefinition", 391 | "src": "178:58:1" 392 | }, 393 | { 394 | "attributes": { 395 | "constant": false, 396 | "implemented": true, 397 | "isConstructor": false, 398 | "name": "setCompleted", 399 | "payable": false, 400 | "scope": 62, 401 | "stateMutability": "nonpayable", 402 | "superFunction": null, 403 | "visibility": "public" 404 | }, 405 | "children": [ 406 | { 407 | "children": [ 408 | { 409 | "attributes": { 410 | "constant": false, 411 | "name": "completed", 412 | "scope": 41, 413 | "stateVariable": false, 414 | "storageLocation": "default", 415 | "type": "uint256", 416 | "value": null, 417 | "visibility": "internal" 418 | }, 419 | "children": [ 420 | { 421 | "attributes": { 422 | "name": "uint", 423 | "type": "uint256" 424 | }, 425 | "id": 30, 426 | "name": "ElementaryTypeName", 427 | "src": "262:4:1" 428 | } 429 | ], 430 | "id": 31, 431 | "name": "VariableDeclaration", 432 | "src": "262:14:1" 433 | } 434 | ], 435 | "id": 32, 436 | "name": "ParameterList", 437 | "src": "261:16:1" 438 | }, 439 | { 440 | "attributes": { 441 | "parameters": [ 442 | null 443 | ] 444 | }, 445 | "children": [], 446 | "id": 35, 447 | "name": "ParameterList", 448 | "src": "296:0:1" 449 | }, 450 | { 451 | "attributes": { 452 | "arguments": [ 453 | null 454 | ] 455 | }, 456 | "children": [ 457 | { 458 | "attributes": { 459 | "argumentTypes": null, 460 | "overloadedDeclarations": [ 461 | null 462 | ], 463 | "referencedDeclaration": 20, 464 | "type": "modifier ()", 465 | "value": "restricted" 466 | }, 467 | "id": 33, 468 | "name": "Identifier", 469 | "src": "285:10:1" 470 | } 471 | ], 472 | "id": 34, 473 | "name": "ModifierInvocation", 474 | "src": "285:10:1" 475 | }, 476 | { 477 | "children": [ 478 | { 479 | "children": [ 480 | { 481 | "attributes": { 482 | "argumentTypes": null, 483 | "isConstant": false, 484 | "isLValue": false, 485 | "isPure": false, 486 | "lValueRequested": false, 487 | "operator": "=", 488 | "type": "uint256" 489 | }, 490 | "children": [ 491 | { 492 | "attributes": { 493 | "argumentTypes": null, 494 | "overloadedDeclarations": [ 495 | null 496 | ], 497 | "referencedDeclaration": 11, 498 | "type": "uint256", 499 | "value": "last_completed_migration" 500 | }, 501 | "id": 36, 502 | "name": "Identifier", 503 | "src": "302:24:1" 504 | }, 505 | { 506 | "attributes": { 507 | "argumentTypes": null, 508 | "overloadedDeclarations": [ 509 | null 510 | ], 511 | "referencedDeclaration": 31, 512 | "type": "uint256", 513 | "value": "completed" 514 | }, 515 | "id": 37, 516 | "name": "Identifier", 517 | "src": "329:9:1" 518 | } 519 | ], 520 | "id": 38, 521 | "name": "Assignment", 522 | "src": "302:36:1" 523 | } 524 | ], 525 | "id": 39, 526 | "name": "ExpressionStatement", 527 | "src": "302:36:1" 528 | } 529 | ], 530 | "id": 40, 531 | "name": "Block", 532 | "src": "296:47:1" 533 | } 534 | ], 535 | "id": 41, 536 | "name": "FunctionDefinition", 537 | "src": "240:103:1" 538 | }, 539 | { 540 | "attributes": { 541 | "constant": false, 542 | "implemented": true, 543 | "isConstructor": false, 544 | "name": "upgrade", 545 | "payable": false, 546 | "scope": 62, 547 | "stateMutability": "nonpayable", 548 | "superFunction": null, 549 | "visibility": "public" 550 | }, 551 | "children": [ 552 | { 553 | "children": [ 554 | { 555 | "attributes": { 556 | "constant": false, 557 | "name": "new_address", 558 | "scope": 61, 559 | "stateVariable": false, 560 | "storageLocation": "default", 561 | "type": "address", 562 | "value": null, 563 | "visibility": "internal" 564 | }, 565 | "children": [ 566 | { 567 | "attributes": { 568 | "name": "address", 569 | "type": "address" 570 | }, 571 | "id": 42, 572 | "name": "ElementaryTypeName", 573 | "src": "364:7:1" 574 | } 575 | ], 576 | "id": 43, 577 | "name": "VariableDeclaration", 578 | "src": "364:19:1" 579 | } 580 | ], 581 | "id": 44, 582 | "name": "ParameterList", 583 | "src": "363:21:1" 584 | }, 585 | { 586 | "attributes": { 587 | "parameters": [ 588 | null 589 | ] 590 | }, 591 | "children": [], 592 | "id": 47, 593 | "name": "ParameterList", 594 | "src": "403:0:1" 595 | }, 596 | { 597 | "attributes": { 598 | "arguments": [ 599 | null 600 | ] 601 | }, 602 | "children": [ 603 | { 604 | "attributes": { 605 | "argumentTypes": null, 606 | "overloadedDeclarations": [ 607 | null 608 | ], 609 | "referencedDeclaration": 20, 610 | "type": "modifier ()", 611 | "value": "restricted" 612 | }, 613 | "id": 45, 614 | "name": "Identifier", 615 | "src": "392:10:1" 616 | } 617 | ], 618 | "id": 46, 619 | "name": "ModifierInvocation", 620 | "src": "392:10:1" 621 | }, 622 | { 623 | "children": [ 624 | { 625 | "attributes": { 626 | "assignments": [ 627 | 49 628 | ] 629 | }, 630 | "children": [ 631 | { 632 | "attributes": { 633 | "constant": false, 634 | "name": "upgraded", 635 | "scope": 61, 636 | "stateVariable": false, 637 | "storageLocation": "default", 638 | "type": "contract Migrations", 639 | "value": null, 640 | "visibility": "internal" 641 | }, 642 | "children": [ 643 | { 644 | "attributes": { 645 | "contractScope": null, 646 | "name": "Migrations", 647 | "referencedDeclaration": 62, 648 | "type": "contract Migrations" 649 | }, 650 | "id": 48, 651 | "name": "UserDefinedTypeName", 652 | "src": "409:10:1" 653 | } 654 | ], 655 | "id": 49, 656 | "name": "VariableDeclaration", 657 | "src": "409:19:1" 658 | }, 659 | { 660 | "attributes": { 661 | "argumentTypes": null, 662 | "isConstant": false, 663 | "isLValue": false, 664 | "isPure": false, 665 | "isStructConstructorCall": false, 666 | "lValueRequested": false, 667 | "names": [ 668 | null 669 | ], 670 | "type": "contract Migrations", 671 | "type_conversion": true 672 | }, 673 | "children": [ 674 | { 675 | "attributes": { 676 | "argumentTypes": [ 677 | { 678 | "typeIdentifier": "t_address", 679 | "typeString": "address" 680 | } 681 | ], 682 | "overloadedDeclarations": [ 683 | null 684 | ], 685 | "referencedDeclaration": 62, 686 | "type": "type(contract Migrations)", 687 | "value": "Migrations" 688 | }, 689 | "id": 50, 690 | "name": "Identifier", 691 | "src": "431:10:1" 692 | }, 693 | { 694 | "attributes": { 695 | "argumentTypes": null, 696 | "overloadedDeclarations": [ 697 | null 698 | ], 699 | "referencedDeclaration": 43, 700 | "type": "address", 701 | "value": "new_address" 702 | }, 703 | "id": 51, 704 | "name": "Identifier", 705 | "src": "442:11:1" 706 | } 707 | ], 708 | "id": 52, 709 | "name": "FunctionCall", 710 | "src": "431:23:1" 711 | } 712 | ], 713 | "id": 53, 714 | "name": "VariableDeclarationStatement", 715 | "src": "409:45:1" 716 | }, 717 | { 718 | "children": [ 719 | { 720 | "attributes": { 721 | "argumentTypes": null, 722 | "isConstant": false, 723 | "isLValue": false, 724 | "isPure": false, 725 | "isStructConstructorCall": false, 726 | "lValueRequested": false, 727 | "names": [ 728 | null 729 | ], 730 | "type": "tuple()", 731 | "type_conversion": false 732 | }, 733 | "children": [ 734 | { 735 | "attributes": { 736 | "argumentTypes": [ 737 | { 738 | "typeIdentifier": "t_uint256", 739 | "typeString": "uint256" 740 | } 741 | ], 742 | "isConstant": false, 743 | "isLValue": false, 744 | "isPure": false, 745 | "lValueRequested": false, 746 | "member_name": "setCompleted", 747 | "referencedDeclaration": 41, 748 | "type": "function (uint256) external" 749 | }, 750 | "children": [ 751 | { 752 | "attributes": { 753 | "argumentTypes": null, 754 | "overloadedDeclarations": [ 755 | null 756 | ], 757 | "referencedDeclaration": 49, 758 | "type": "contract Migrations", 759 | "value": "upgraded" 760 | }, 761 | "id": 54, 762 | "name": "Identifier", 763 | "src": "460:8:1" 764 | } 765 | ], 766 | "id": 56, 767 | "name": "MemberAccess", 768 | "src": "460:21:1" 769 | }, 770 | { 771 | "attributes": { 772 | "argumentTypes": null, 773 | "overloadedDeclarations": [ 774 | null 775 | ], 776 | "referencedDeclaration": 11, 777 | "type": "uint256", 778 | "value": "last_completed_migration" 779 | }, 780 | "id": 57, 781 | "name": "Identifier", 782 | "src": "482:24:1" 783 | } 784 | ], 785 | "id": 58, 786 | "name": "FunctionCall", 787 | "src": "460:47:1" 788 | } 789 | ], 790 | "id": 59, 791 | "name": "ExpressionStatement", 792 | "src": "460:47:1" 793 | } 794 | ], 795 | "id": 60, 796 | "name": "Block", 797 | "src": "403:109:1" 798 | } 799 | ], 800 | "id": 61, 801 | "name": "FunctionDefinition", 802 | "src": "347:165:1" 803 | } 804 | ], 805 | "id": 62, 806 | "name": "ContractDefinition", 807 | "src": "26:488:1" 808 | } 809 | ], 810 | "id": 63, 811 | "name": "SourceUnit", 812 | "src": "0:515:1" 813 | }, 814 | "compiler": { 815 | "name": "solc", 816 | "version": "0.4.19+commit.c4cbbb05.Emscripten.clang" 817 | }, 818 | "networks": { 819 | "5777": { 820 | "events": {}, 821 | "links": {}, 822 | "address": "0x8cdaf0cd259887258bc13a92c0a6da92698644c0" 823 | } 824 | }, 825 | "schemaVersion": "1.0.1", 826 | "updatedAt": "2018-03-29T14:08:51.128Z" 827 | } -------------------------------------------------------------------------------- /Chapter03/contracts/MetaCoin.sol: -------------------------------------------------------------------------------- 1 | pragma solidity ^0.4.17; 2 | 3 | import 'zeppelin-solidity/contracts/token/ERC20/StandardToken.sol'; 4 | 5 | contract MetaCoin is StandardToken { 6 | 7 | string public name = 'MetaCoinToken'; 8 | string public symbol = 'MCT'; 9 | uint8 public decimals = 6; 10 | uint public INITIAL_SUPPLY = 10000000; 11 | 12 | function MetaCoin() public { 13 | totalSupply_ = INITIAL_SUPPLY; 14 | balances[msg.sender] = INITIAL_SUPPLY; 15 | } 16 | } 17 | -------------------------------------------------------------------------------- /Chapter03/contracts/Migrations.sol: -------------------------------------------------------------------------------- 1 | pragma solidity ^0.4.17; 2 | 3 | contract Migrations { 4 | address public owner; 5 | uint public last_completed_migration; 6 | 7 | modifier restricted() { 8 | if (msg.sender == owner) _; 9 | } 10 | 11 | function Migrations() public { 12 | owner = msg.sender; 13 | } 14 | 15 | function setCompleted(uint completed) public restricted { 16 | last_completed_migration = completed; 17 | } 18 | 19 | function upgrade(address new_address) public restricted { 20 | Migrations upgraded = Migrations(new_address); 21 | upgraded.setCompleted(last_completed_migration); 22 | } 23 | } 24 | -------------------------------------------------------------------------------- /Chapter03/migrations/1_initial_migration.js: -------------------------------------------------------------------------------- 1 | var Migrations = artifacts.require("./Migrations.sol"); 2 | 3 | module.exports = function(deployer) { 4 | deployer.deploy(Migrations); 5 | }; 6 | -------------------------------------------------------------------------------- /Chapter03/migrations/2_deploy_contracts.js: -------------------------------------------------------------------------------- 1 | var MetaCoin = artifacts.require("./MetaCoin.sol"); 2 | 3 | module.exports = function(deployer) { 4 | deployer.deploy(MetaCoin); 5 | }; 6 | -------------------------------------------------------------------------------- /Chapter03/package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "truffle-init-webpack", 3 | "version": "0.0.2", 4 | "description": "Frontend example using truffle v3", 5 | "scripts": { 6 | "lint": "eslint ./", 7 | "build": "webpack", 8 | "dev": "webpack-dev-server" 9 | }, 10 | "author": "Douglas von Kohorn", 11 | "license": "MIT", 12 | "devDependencies": { 13 | "babel-cli": "^6.22.2", 14 | "babel-core": "^6.22.1", 15 | "babel-eslint": "^6.1.2", 16 | "babel-loader": "^6.2.10", 17 | "babel-plugin-transform-runtime": "^6.22.0", 18 | "babel-preset-env": "^1.1.8", 19 | "babel-preset-es2015": "^6.22.0", 20 | "babel-register": "^6.22.0", 21 | "copy-webpack-plugin": "^4.0.1", 22 | "css-loader": "^0.26.1", 23 | "eslint": "^3.14.0", 24 | "eslint-config-standard": "^6.0.0", 25 | "eslint-plugin-babel": "^4.0.0", 26 | "eslint-plugin-mocha": "^4.8.0", 27 | "eslint-plugin-promise": "^3.0.0", 28 | "eslint-plugin-standard": "^2.0.0", 29 | "html-webpack-plugin": "^2.28.0", 30 | "json-loader": "^0.5.4", 31 | "style-loader": "^0.13.1", 32 | "truffle-contract": "^1.1.11", 33 | "web3": "^0.20.0", 34 | "webpack": "^2.2.1", 35 | "webpack-dev-server": "^2.3.0" 36 | }, 37 | "dependencies": { 38 | "zeppelin-solidity": "1.7.0" 39 | } 40 | } 41 | -------------------------------------------------------------------------------- /Chapter03/test/TestMetacoin.sol: -------------------------------------------------------------------------------- 1 | pragma solidity ^0.4.2; 2 | 3 | import "truffle/Assert.sol"; 4 | import "truffle/DeployedAddresses.sol"; 5 | import "../contracts/MetaCoin.sol"; 6 | 7 | contract TestMetacoin { 8 | 9 | function testInitialBalanceUsingDeployedContract() { 10 | MetaCoin meta = MetaCoin(DeployedAddresses.MetaCoin()); 11 | 12 | uint expected = 10000; 13 | 14 | Assert.equal(meta.getBalance(tx.origin), expected, "Owner should have 10000 MetaCoin initially"); 15 | } 16 | 17 | function testInitialBalanceWithNewMetaCoin() { 18 | MetaCoin meta = new MetaCoin(); 19 | 20 | uint expected = 10000; 21 | 22 | Assert.equal(meta.getBalance(tx.origin), expected, "Owner should have 10000 MetaCoin initially"); 23 | } 24 | 25 | } 26 | -------------------------------------------------------------------------------- /Chapter03/test/metacoin.js: -------------------------------------------------------------------------------- 1 | var MetaCoin = artifacts.require("./MetaCoin.sol"); 2 | 3 | contract('MetaCoin', function(accounts) { 4 | it("should put 10000 MetaCoin in the first account", function() { 5 | return MetaCoin.deployed().then(function(instance) { 6 | return instance.getBalance.call(accounts[0]); 7 | }).then(function(balance) { 8 | assert.equal(balance.valueOf(), 10000, "10000 wasn't in the first account"); 9 | }); 10 | }); 11 | it("should call a function that depends on a linked library", function() { 12 | var meta; 13 | var metaCoinBalance; 14 | var metaCoinEthBalance; 15 | 16 | return MetaCoin.deployed().then(function(instance) { 17 | meta = instance; 18 | return meta.getBalance.call(accounts[0]); 19 | }).then(function(outCoinBalance) { 20 | metaCoinBalance = outCoinBalance.toNumber(); 21 | return meta.getBalanceInEth.call(accounts[0]); 22 | }).then(function(outCoinBalanceEth) { 23 | metaCoinEthBalance = outCoinBalanceEth.toNumber(); 24 | }).then(function() { 25 | assert.equal(metaCoinEthBalance, 2 * metaCoinBalance, "Library function returned unexpeced function, linkage may be broken"); 26 | }); 27 | }); 28 | 29 | it("should send coin correctly", function() { 30 | var meta; 31 | 32 | // Get initial balances of first and second account. 33 | var account_one = accounts[0]; 34 | var account_two = accounts[1]; 35 | 36 | var account_one_starting_balance; 37 | var account_two_starting_balance; 38 | var account_one_ending_balance; 39 | var account_two_ending_balance; 40 | 41 | var amount = 10; 42 | 43 | return MetaCoin.deployed().then(function(instance) { 44 | meta = instance; 45 | return meta.getBalance.call(account_one); 46 | }).then(function(balance) { 47 | account_one_starting_balance = balance.toNumber(); 48 | return meta.getBalance.call(account_two); 49 | }).then(function(balance) { 50 | account_two_starting_balance = balance.toNumber(); 51 | return meta.sendCoin(account_two, amount, {from: account_one}); 52 | }).then(function() { 53 | return meta.getBalance.call(account_one); 54 | }).then(function(balance) { 55 | account_one_ending_balance = balance.toNumber(); 56 | return meta.getBalance.call(account_two); 57 | }).then(function(balance) { 58 | account_two_ending_balance = balance.toNumber(); 59 | 60 | assert.equal(account_one_ending_balance, account_one_starting_balance - amount, "Amount wasn't correctly taken from the sender"); 61 | assert.equal(account_two_ending_balance, account_two_starting_balance + amount, "Amount wasn't correctly sent to the receiver"); 62 | }); 63 | }); 64 | }); 65 | -------------------------------------------------------------------------------- /Chapter03/truffle.js: -------------------------------------------------------------------------------- 1 | // Allows us to use ES6 in our migrations and tests. 2 | require('babel-register') 3 | 4 | module.exports = { 5 | networks: { 6 | development: { 7 | host: '127.0.0.1', 8 | port: 7545, 9 | network_id: '*' // Match any network id 10 | } 11 | } 12 | } 13 | -------------------------------------------------------------------------------- /Chapter03/webpack.config.js: -------------------------------------------------------------------------------- 1 | const path = require('path'); 2 | const CopyWebpackPlugin = require('copy-webpack-plugin'); 3 | 4 | module.exports = { 5 | entry: './app/javascripts/app.js', 6 | output: { 7 | path: path.resolve(__dirname, 'build'), 8 | filename: 'app.js' 9 | }, 10 | plugins: [ 11 | // Copy our app's index.html to the build folder. 12 | new CopyWebpackPlugin([ 13 | { from: './app/index.html', to: "index.html" } 14 | ]) 15 | ], 16 | module: { 17 | rules: [ 18 | { 19 | test: /\.css$/, 20 | use: [ 'style-loader', 'css-loader' ] 21 | } 22 | ], 23 | loaders: [ 24 | { test: /\.json$/, use: 'json-loader' }, 25 | { 26 | test: /\.js$/, 27 | exclude: /(node_modules|bower_components)/, 28 | loader: 'babel-loader', 29 | query: { 30 | presets: ['es2015'], 31 | plugins: ['transform-runtime'] 32 | } 33 | } 34 | ] 35 | } 36 | } 37 | -------------------------------------------------------------------------------- /Chapter04/Section4/DocumentExample.sol: -------------------------------------------------------------------------------- 1 | pragma solidity ^0.4.17; 2 | 3 | contract DocumentExample { 4 | 5 | bytes32[] documentHashes; 6 | 7 | function addDocument(bytes32 documentHash) { 8 | documentHashes.push(documentHash); 9 | } 10 | 11 | } -------------------------------------------------------------------------------- /Chapter04/Section4/SensitiveData.sol: -------------------------------------------------------------------------------- 1 | pragma solidity ^0.4.17; 2 | 3 | contract SensitiveDataExample { 4 | 5 | bytes32[] userHashes; 6 | 7 | // The backend would take a user-object. For example a simple JSON 8 | // {name: "Kenny", familyName: "Vaneetvelde", address: "Some Address 123"} 9 | // After hashing this entire object, you can pass it to the function below. 10 | // Later, you can re-hash it, to verify the user and data if/when needed. 11 | function addUser(bytes32 userHash) { 12 | userHashes.push(userHash); 13 | } 14 | 15 | } -------------------------------------------------------------------------------- /Chapter04/Section4/UserExample.sol: -------------------------------------------------------------------------------- 1 | pragma solidity ^0.4.17; 2 | 3 | contract UserExample { 4 | 5 | mapping(address => bool) user_verified; 6 | mapping(address => bytes32) user_codes; 7 | 8 | mapping(bytes32 => address) to_sign; 9 | mapping(bytes32 => bool) signed; 10 | 11 | address owner; 12 | 13 | modifier onlyOwner() { 14 | require(msg.sender == owner); 15 | _; 16 | } 17 | 18 | function UserExample() public { 19 | owner = msg.sender; 20 | } 21 | 22 | function inviteUser(bytes32 documentHash, address user) public onlyOwner { 23 | to_sign[documentHash] = user; 24 | } 25 | 26 | function signDocument(bytes32 documentHash) public { 27 | if (msg.sender != to_sign[documentHash]) { 28 | revert(); 29 | } 30 | signed[documentHash] = true; 31 | } 32 | 33 | function addUser(address user, bytes32 hashed_verification) public onlyOwner { 34 | user_verified[user] = false; 35 | user_codes[user] = hashed_verification; 36 | } 37 | 38 | function verify(bytes32 verification_code) public { 39 | if (user_verified[msg.sender] == false && 40 | sha256(verification_code) == user_codes[msg.sender]) { 41 | user_verified[msg.sender] = true; 42 | } 43 | } 44 | } -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2018 Packt 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | # Ethereum Projects for Beginners 5 | Ethereum Projects for Beginners, by Packt 6 | 7 | Book Name 8 | 9 | This is the code repository for [Ethereum Projects for Beginners](https://www.packtpub.com/big-data-and-business-intelligence/ethereum-projects-beginners?utm_source=github&utm_medium=repository&utm_campaign=9781789537406), published by Packt. 10 | 11 | **Build blockchain-based cryptocurrencies, smart contracts, and DApps** 12 | 13 | ## What is this book about? 14 | Ethereum enables the development of efficient, smart contracts that contain code. These smart contracts can interact with other smart contracts to make decisions, store data, and send Ether to others.Ethereum Projects for Beginners provides you with a clear introduction to creating cryptocurrencies, smart contracts, and decentralized applications. As you make your way through the book, you’ll get to grips with detailed step-by-step processes to build advanced Ethereum projects. Each project will teach you enough about Ethereum to be productive right away. You will learn how tokenization works, think in a decentralized way, and build blockchain-based distributed computing systems. Towards the end of the book, you will develop interesting Ethereum projects such as creating wallets and secure data sharing.By the end of this book, you will be able to tackle blockchain challenges by implementing end-to-end projects using the full power of the Ethereum blockchain. 15 | 16 | This book covers the following exciting features: 17 | * Develop your ideas fast and efficiently using the Ethereum blockchain 18 | * Make writing and deploying smart contracts easy and manageable 19 | * Work with private data in blockchain applications 20 | * Handle large files in blockchain applications 21 | * Ensure your decentralized applications are safe 22 | * Explore how Ethereum development frameworks work 23 | 24 | If you feel this book is for you, get your [copy](https://www.amazon.com/dp/1789537401) today! 25 | 26 | https://www.packtpub.com/ 28 | 29 | 30 | ## Instructions and Navigations 31 | All of the code is organized into folders. 32 | 33 | The code will look like the following: 34 | 35 | function sendCoin(address receiver, uint amount) public returns(bool 36 | sufficient) { 37 | if (balances[msg.sender] < amount) return false; 38 | balances[msg.sender] -= amount; 39 | balances[receiver] += amount; 40 | 41 | 42 | **Following is what you need for this book:** 43 | This book is for individuals who want to build decentralized applications using blockchain technology and the power of Ethereum from scratch. Some prior knowledge of JavaScript is required, since most examples use a web frontend. 44 | 45 | With the following software and hardware list you can run all code files present in the book (Chapter 1-5). 46 | 47 | ### Software and Hardware List 48 | 49 | | Chapter | Software required | OS required | 50 | | -------- | ------------------------------------| -----------------------------------| 51 | | All | Golem | Windows, Mac OS X, and Linux (Any) | 52 | | | Metamask | Windows, Mac OS X, and Linux (Any) | 53 | | | Google Chrome | Windows, Mac OS X, and Linux (Any) | 54 | | | npm | Windows, Mac OS X, and Linux (Any) | 55 | | | truffle framework | Windows, Mac OS X, and Linux (Any) | 56 | | | Ethereum-JS ganache-cli | Windows, Mac OS X, and Linux (Any) | 57 | | | remix (remix.ethereum.org) | Windows, Mac OS X, and Linux (Any) | 58 | | | sublime editor | Windows, Mac OS X, and Linux (Any) | 59 | 60 | 61 | 62 | ### Related products 63 | * Hands-On Blockchain with Hyperledger [[Packt]](https://www.packtpub.com/big-data-and-business-intelligence/hands-blockchain-hyperledger?utm_source=github&utm_medium=repository&utm_campaign=9781788994521) [[Amazon]](https://www.amazon.com/dp/1788994523) 64 | 65 | * Mastering Blockchain - Second Edition [[Packt]](https://www.packtpub.com/big-data-and-business-intelligence/mastering-blockchain-second-edition?utm_source=github&utm_medium=repository&utm_campaign=9781788839044) [[Amazon]](https://www.amazon.com/dp/1788839048) 66 | 67 | ## Get to Know the Author 68 | **Kenny Vaneetvelde** 69 | 70 | Kenny Vaneetvelde had been trading Bitcoin for a few years when he heard about a new technology called Ethereum and was completely captivated by it. After learning all that he could about Ethereum and other blockchain technologies that had suddenly started sprouting up everywhere, he spent over a year doing R and D, developing prototypes, and training new employees in a consultancy firm so that they could start up a new blockchain branch. 71 | 72 | 73 | ### Suggestions and Feedback 74 | [Click here](https://docs.google.com/forms/d/e/1FAIpQLSdy7dATC6QmEL81FIUuymZ0Wy9vH1jHkvpY57OiMeKGqib_Ow/viewform) if you have any feedback or suggestions. 75 | ### Download a free PDF 76 | 77 | If you have already purchased a print or Kindle version of this book, you can get a DRM-free PDF version at no cost.
Simply click on the link to claim your free PDF.
78 |

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

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