[DApps]ゆずのは銀行(仮)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 |
// (Yuzubank.sol) pragma solidity ^0.4.18; contract Yuzubank { mapping(address => uint) yuzubank; event Transfer(address indexed from, address indexed to, uint value); // 振り込み function deposit() public payable { yuzubank[msg.sender] += msg.value; emit Transfer(msg.sender, address(this), msg.value); } // 確認 function check() public view returns(uint) { return yuzubank[msg.sender]; } // 銀行の総額 function checkTotal() public view returns(uint) { return address(this).balance; } // 引き出し function withdraw(uint _value) public { // 足りていること require(_value <= yuzubank[msg.sender]); // 引き落とす yuzubank[msg.sender] -= _value; // 送金 msg.sender.transfer(_value); emit Transfer(address(this), msg.sender, _value); } } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 |
// (index.html) <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>solidity2</title> <link rel="stylesheet" type="text/css" href="css.css"> </head> <body> <h2>ユズノハ銀行(仮)</h2> <p id="wallet_data"></p> <a href="#" class="square_btn" onclick="update();return false;">更新</a> <br> <br> value : <input id="value_deposit" type="number" > <a href="#" class="square_btn" onclick="deposit();return false;">預金</a> <br> <br> value : <input id="value_withdraw" type="number" > <a href="#" class="square_btn" onclick="withdraw();return false;">引き出し</a> <br> <script src="./const.js"></script> <script type="text/javascript"> // global start if (typeof web3 !== 'undefined') { window.web3 = new Web3(web3.currentProvider); } else { document.write("Please install metamask"); } // コントラクト var contract = web3.eth.contract(JSON.parse(ABI)).at(CONTRACT_ADDR); //イベント監視 contract.Transfer().watch(function (error, result) { console.log('result : '); console.log(result); // イベントキャッチのタイミングで自動更新 update(); }); // global end new Promise((resolve)=>{ window.web3.eth.getAccounts((err, accounts) => { resolve(accounts); }); }).then((accounts)=>{ update(); }); function update(){ var wallet_data = document.getElementById("wallet_data"); var address = web3.eth.defaultAccount; var s = ""; new Promise((resolve)=>{ web3.eth.getBalance(address, (err, value)=>{ s += "アドレス : " + address + "<br>"; s += "保持金額 : " + value + " wei<br>"; resolve(); }); }).then(()=>{ return new Promise((resolve)=>{ contract.check((err, value)=>{ s += "預金残高 : " + value + " wei<br>"; resolve(); }); }); }).then(()=>{ return new Promise((resolve)=>{ contract.checkTotal((err, value)=>{ s += "預金総額 : " + value + " wei<br>"; resolve(); }); }); }).then(()=>{ wallet_data.innerHTML = s; }); } function deposit(){ var value_deposit = document.getElementById("value_deposit").value; // ガード if(value_deposit<=0){ alert("valueは1以上の整数を入力してください"); return; } // 書き込み var txObj = {value: value_deposit}; contract.deposit(txObj, function(err, value){ console.log("deposit tx : " + value); }); } function withdraw(){ var value = document.getElementById("value_withdraw").value; // ガード if(value<=0){ alert("valueは1以上の整数を入力してください"); return; } // 書き込み contract.withdraw(value, function(err, value){ console.log("withdraw tx : " + value); }); } </script> </body> </html> |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 |
// (const.js) var CONTRACT_ADDR = "0xc02374f85917191281c308a1c0b6c5f63074434b"; var OWNER_ADDR = "0x5ed767cfebf5953835340e012fee4048661b7d1b"; var ABI = `[ { "constant": true, "inputs": [], "name": "checkTotal", "outputs": [ { "name": "", "type": "uint256" } ], "payable": false, "stateMutability": "view", "type": "function" }, { "constant": true, "inputs": [], "name": "check", "outputs": [ { "name": "", "type": "uint256" } ], "payable": false, "stateMutability": "view", "type": "function" }, { "anonymous": false, "inputs": [ { "indexed": true, "name": "from", "type": "address" }, { "indexed": true, "name": "to", "type": "address" }, { "indexed": false, "name": "value", "type": "uint256" } ], "name": "Transfer", "type": "event" }, { "constant": false, "inputs": [], "name": "deposit", "outputs": [], "payable": true, "stateMutability": "payable", "type": "function" }, { "constant": false, "inputs": [ { "name": "_value", "type": "uint256" } ], "name": "withdraw", "outputs": [], "payable": false, "stateMutability": "nonpayable", "type": "function" } ]`; |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
// (css.css) @charset "UTF-8"; .square_btn{ display: inline-block; padding: 0.5em 1em; text-decoration: none; color: #FFF; background-image: -webkit-linear-gradient(#6795fd 0%, #67ceff 100%); background-image: linear-gradient(#6795fd 0%, #67ceff 100%); transition: .4s; } .square_btn:hover{ background-image: -webkit-linear-gradient(#6795fd 0%, #67ceff 70%); background-image: linear-gradient(#6795fd 0%, #67ceff 70%); } |