Blockchain: Hello worldWith Ethereum we can add maths functions onto the blockchain with a contract. For example [Intro]: First we create out code: pragma solidity ^0.4.0; contract mycontract { /* Owner of the type address*/ address owner; /* initialization and sets the owner of contract */ function mycontract() { owner = msg.sender; } /* Recover the funds on the contract */ function kill() { if (msg.sender == owner) selfdestruct(owner); } } contract showmessage is mycontract { /* Message in contract */ string message; /* Initialise on contract */ function showmessage(string _msg) public { message= _msg; } /* show function */ function show() constant returns (string) { return message; } } We then compile this with the Solidity compiler to give: var browser_hello_sol_mycontractContract = web3.eth.contract([{"constant":false,"inputs":[],"name":"kill","outputs":[],"payable":false,"type":"function"},{"inputs":[],"payable":false,"type":"constructor"}]); var browser_hello_sol_mycontract = browser_hello_sol_mycontractContract.new( { from: web3.eth.accounts[0], data: '0x606060405234....de13a65c0029', gas: '4700000' }, function (e, contract){ console.log(e, contract); if (typeof contract.address !== 'undefined') { console.log('Contract mined! address: ' + contract.address + ' transactionHash: ' + contract.transactionHash); } }) var _msg = "Hello" ; var browser_hello_sol_showmessageContract = web3.eth.contract([{"constant":false,"inputs":[],"name":"kill","outputs":[],"payable":false,"type":"function"},{"constant":true,"inputs":[],"name":"show","outputs":[{"name":"","type":"string"}],"payable":false,"type":"function"},{"inputs":[{"name":"_msg","type":"string"}],"payable":false,"type":"constructor"}]); var browser_hello_sol_showmessage = browser_hello_sol_showmessageContract.new( _msg, { from: web3.eth.accounts[0], data: '0x606060405234156100....13970029', gas: '4700000' }, function (e, contract){ console.log(e, contract); if (typeof contract.address !== 'undefined') { console.log('Contract mined! address: ' + contract.address + ' transactionHash: ' + contract.transactionHash); } }) We can then add this onto the Blockchain with: > personal.unlockAccount(web3.eth.accounts[0],'Qwerty') true > loadScript('hello.js') null [object Object] null [object Object] true > null [object Object] Contract mined! address: 0x3023606f3c8d9fe9f521aeed92a4500c9f026cd7 transactionH ash: 0xdaa242a5d0c628ad339491176f3c8c08aff9bf87014c6259d5897424b5d1fccf null [object Object] Contract mined! address: 0x8422605c83fc69bb3569587857d164461bbe105c transactionH ash: 0x669a43d64b210ee4c032694d34923e134f164987abba17bc86331cd6956c8abc We can view the mined contract: > browser_hello_sol_showmessage { abi: [{ constant: false, inputs: [], name: "kill", outputs: [], payable: false, type: "function" }, { constant: true, inputs: [], name: "show", outputs: [{...}], payable: false, type: "function" }, { inputs: [{...}], payable: false, type: "constructor" }], address: "0x8422605c83fc69bb3569587857d164461bbe105c", transactionHash: "0x669a43d64b210ee4c032694d34923e134f164987abba17bc86331cd695 6c8abc", allEvents: function(), kill: function(), show: function() } We now run with: > browser_hello_sol_showmessage.show() "Hello" |