Blockchain: Simple Purchasing SystemWith Ethereum we create a purchasing system by transferring credits from one account to another. For example [Intro]: Ethereum allows us to store transactions on a Blockchain and for these to be verifiable. So let's look at an example where we have a buyer (sender) and a seller (receiver). We first create a contract (market) and create a constructor (market) which will be initialised with a given number of tokens (supply). The findBalanceOf value will then be used to show the balance of an address (the user), and we'll implement a sendCoin() function to send a number of tokens from the buyer to the seller. First we create our code: pragma solidity ^0.4.0; contract market { mapping (address => uint) public findBalanceOf; event MoneyTransfer(address sender, address receiver, uint amount); /* Initializes contract with initial supply tokens to the creator of the contract */ function market(uint supply) { findBalanceOf[msg.sender] = supply; } /* Very simple trade function */ function sendCoin(address receiver, uint amount) returns(bool sufficient) { if (findBalanceOf[msg.sender] < amount) return false; findBalanceOf[msg.sender] -= amount; findBalanceOf[receiver] += amount; MoneyTransfer(msg.sender, receiver, amount); return true; } } We then compile this with the Solidity compiler to give: var supply = /* var of type uint256 here */ ; var browser_market_sol_marketContract = web3.eth.contract([{"constant":true,"inputs":[{"name":"","type":"address"}],"name":"findBalanceOf","outputs":[{"name":"","type":"uint256"}],"payable":false,"type":"function"},{"constant":false,"inputs":[{"name":"receiver","type":"address"},{"name":"amount","type":"uint256"}],"name":"sendCoin","outputs":[{"name":"sufficient","type":"bool"}],"payable":false,"type":"function"},{"inputs":[{"name":"supply","type":"uint256"}],"payable":false,"type":"constructor"},{"anonymous":false,"inputs":[{"indexed":false,"name":"sender","type":"address"},{"indexed":false,"name":"receiver","type":"address"},{"indexed":false,"name":"amount","type":"uint256"}],"name":"MoneyTransfer","type":"event"}]); var browser_market_sol_market = browser_market_sol_marketContract.new( supply, { from: web3.eth.accounts[0], data: '0x6060604052341561000c..4e067c3188f2b0029', gas: '4700000' }, function (e, contract){ console.log(e, contract); if (typeof contract.address !== 'undefined') { console.log('Contract mined! address: ' + contract.address + ' transactionHash: ' + contract.transactionHash); } }) First, we will view the accounts on the system, and log into one of them: > web3.eth.accounts ["0xc7552f45deb093cafb47286a0bc9415845ca3735", "0x0851db3e133a15cd1c32531ffff96b4526e3cbcd"] > loadScript('market.js') Error: authentication needed: password or unlock undefined true > personal.unlockAccount('0xc7552f45deb093cafb47286a0bc9415845ca3735', 'Qwerty') true Next, we'll load up the compiled contract and start the miner: > loadScript('market.js') null [object Object] true > miner.start() null Contract mined! address: 0xcddb2cda65a39a7cc7765a41b4d2be5fbe30e2fc transactionH ash: 0x4516a1611d3bae6729563791ba882c8619d6ba0b9b0ecd2cecceb16887bd0e32 Let's see the initial credits on the accounts when the contract is created: > browser_market_sol_market.findBalanceOf(eth.accounts[0]) + tokens" "10000 tokens" > browser_market_sol_market.findBalanceOf(eth.accounts[1]) + " tokens" "0 tokens" Now we see the contract being mined: > browser_market_sol_market.sendCoin.sendTransaction(eth.accounts[1], 1000, {from: eth.accounts[0]}) "0xf72e3e8ca69a62ce9380ae23d5a858e94213f2b5d8e96c7ea0a4da9c1ef33c77" > browser_market_sol_market.findBalanceOf(eth.accounts[0]) + " tokens" "10000 tokens" > browser_market_sol_market.findBalanceOf(eth.accounts[1]) + " tokens" "1000 tokens" > browser_market_sol_market.findBalanceOf(eth.accounts[0]) + " tokens" "9000 tokens" > browser_market_sol_market.sendCoin.sendTransaction(eth.accounts[1], 1000, {from: eth.accounts[0]}) "0xa26555a901fd994af362a5dd07e7da7c15c4e39cbfd85b98aab0b5c4c3c3b547" > browser_market_sol_market.findBalanceOf(eth.accounts[0]) + " tokens" "8000 tokens" > browser_market_sol_market.findBalanceOf(eth.accounts[1]) + " tokens" "2000 tokens" In the video I try to move tokens from the other account, but there are no credits to do the mining. |