pragma solidity ^0.4.21; import "github.com/OpenZeppelin/zeppelin-solidity/contracts/math/SafeMath.sol"; contract Assignment2 { // 기존 버전은 Transaction 단위 없이 ( 입금, 출금 ) 잔액 정보만 존재했음, // 이를 struct 를 사용하여 deposit_id, 시간, deposit 양, 상태(deposit, return) 정보를 함께 하나의 단위로 저장 // 각 유저는 여러번 deposit 하여 여러 deposit_id 를 가질 수 있고, 각 단위에 대해 deposit_id 를 통해 claim 할 수 있도록 구현 using SafeMath for uint256; // 원장 struct Ledger { address deposit_id; uint time; // time of start burrow uint256 deposit_amount; string status; } Ledger[] ledgerList; event Logging(Ledger); // id별 입금 처리 function() public payable { // 전송한 ETH(Wei) 양이 0 보다 커야 하는 조건 require(msg.value > 0); // 원장목록에 존재하는 id면 deposit 양에 받은 값을 더한 뒤 저장 uint deposit_amount = 0; bool existId = false; for (uint i = 0; i < ledgerList.length; i++) { if(ledgerList[i].deposit_id == msg.sender) { deposit_amount = ledgerList[i].deposit_amount.add(msg.value); ledgerList[i].deposit_amount = deposit_amount; ledgerList[i].time = uint(now); ledgerList[i].status = "deposit"; existId = true; } } // 원장목록에 존재하지 않는 id면 기본값으로 신규 저장 if(!existId) { Ledger memory newLedger = Ledger({ deposit_id: msg.sender, time: uint(now), deposit_amount: msg.value, status: "deposit" }); ledgerList.push(newLedger); emit Logging(newLedger); } } // id별 출금 처리 function claim() public { // 원장목록에 존재하는 id면 deposit 양을 찾아 출금 처리 uint deposit_amount = 0; for (uint i = 0; i < ledgerList.length; i++) { if(ledgerList[i].deposit_id == msg.sender) { deposit_amount = ledgerList[i].deposit_amount; // 해당 Account 가 Deposit 한 양이 0 보다 커야하는 조건 require(deposit_amount > 0); // send 전 해당 id의 deposit 양을 먼저 0으로 변경시킴 ledgerList[i].deposit_amount = 0; ledgerList[i].time = uint(now); ledgerList[i].status = "return"; // address.send() 의 경우 실패시 false 반환, require 와 함께 써서 실패시 revert 처리 가능 require(msg.sender.send(deposit_amount)); } } } }
0.4.18