requireの第2引数にエラーメッセージを載せる(contract SimpleBank)
SimpleBankのaddress(ropsten)
1 2 3 |
ROPSTEN (Revival) TESTNET Contract 0x1A5a2b35E7AEde8cC54DEFd50F145B06181c10De |
https://ropsten.etherscan.io/address/0x1a5a2b35e7aede8cc54defd50f145b06181c10de
このやり方↓
require(false, “エラーメッセージ”);
がスマートかどうかは自信がない。
ところで、truffle consoleやetherscanではこの”エラーメッセージ”を読む方法がわからない。
remix(browser-solidity)では読める。
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 |
pragma solidity ^0.4.18; contract SimpleBank { /** storages */ // 預金残高 mapping(address => uint) public bank; /** events */ event Deposit( address indexed addr, uint oldBalance, uint value, uint newBalance ); event Withdraw( address indexed addr, uint oldBalance, uint value, uint newBalance ); /** methods */ // 残高確認 function getMyBalance() public view returns(uint) { return bank[msg.sender]; } // 預金 function deposit(uint _value) public { address msgSender = msg.sender; uint oldBalance = bank[msgSender]; uint newBalance = oldBalance + _value; // オーバーフローチェック if( newBalance < oldBalance || newBalance < _value ){ // オーバーフロー発生! string memory errmsg = "error! オーバーフロー! oldBalance:"; errmsg = _strConnect(errmsg, _uint2str(oldBalance)); errmsg = _strConnect(errmsg, ", _value:"); errmsg = _strConnect(errmsg, _uint2str(_value)); errmsg = _strConnect(errmsg, ", newBalance:"); errmsg = _strConnect(errmsg, _uint2str(newBalance)); require(false, errmsg); }else{ // オーバーフローセーフ bank[msgSender] = newBalance; emit Deposit(msgSender, oldBalance, _value, newBalance); } } // 出金 function withdraw(uint _value) public { address msgSender = msg.sender; uint oldBalance = bank[msgSender]; // 残高確認 if(oldBalance < _value){ // 残高不足 string memory errmsg = "error! 残高不足! oldBalance:"; errmsg = _strConnect(errmsg, _uint2str(oldBalance)); errmsg = _strConnect(errmsg, ", _value:"); errmsg = _strConnect(errmsg, _uint2str(_value)); require(false, errmsg); }else{ // 残高は足りている uint newBalance = oldBalance - _value; bank[msgSender] = newBalance; emit Withdraw(msgSender, oldBalance, _value, newBalance); } } function _strConnect(string _str1, string _str2) private pure returns(string) { bytes memory strbyte1 = bytes(_str1); bytes memory strbyte2 = bytes(_str2); bytes memory str = new bytes(strbyte1.length + strbyte2.length); uint8 point = 0; for(uint8 j = 0; j < strbyte1.length;j++){ str[point] = strbyte1[j]; point++; } for(uint8 k = 0; k < strbyte2.length;k++){ str[point] = strbyte2[k]; point++; } return string(str); } function _uint2str(uint i) private pure returns (string){ if (i == 0) return "0"; uint j = i; uint length; while (j != 0){ length++; j /= 10; } bytes memory bstr = new bytes(length); uint k = length - 1; while (i != 0){ bstr[k--] = byte(48 + i % 10); i /= 10; } return string(bstr); } } |