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 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150
| // SPDX-License-Identifier: MIT pragma solidity 0.4.20;
contract Bank {
struct Account { address owner; uint balance; uint time; }
mapping (address => Account) public accounts;
// 事件声明 event AccountCreate(address account, uint time); event WithdrawSuccess(address account, uint amount, uint time); event WithdrawFailed(address account, uint amount, string reason, uint time);
// 创建账户 function createAccount() public { address account = msg.sender;
// 不允许重复创建账户 if (checkAccount(account)) { revert(); }
accounts[account] = Account({ owner: account, balance: 0, time: now }); AccountCreate(account, now); }
// 检查账户是否存在 function checkAccount(address account) public constant returns(bool) { return accounts[account].owner != address(0); }
// 存款 function deposit() public payable { if (!checkAccount(msg.sender)) { createAccount(); } accounts[msg.sender].balance += msg.value; accounts[msg.sender].time = now; }
// 检查余额 function checkBalance() public constant returns (uint) { address account = msg.sender; if (!checkAccount(account)) { return 0; } return accounts[account].balance; }
// 完善的取款函数 function withdraw(uint amount) public { address account = msg.sender;
// 1. 检查账户是否存在 if (!checkAccount(account)) { WithdrawFailed(account, amount, "用户不存在", now); revert(); }
// 2. 检查余额是否充足 if (accounts[account].balance < amount) { WithdrawFailed(account, amount, "余额不足", now); revert(); }
// 3. 检查取款金额是否有效 if (amount == 0) { WithdrawFailed(account, amount, "取款金额必须大于0", now); revert(); }
// 4. 更新账户余额 accounts[account].balance -= amount; accounts[account].time = now;
// 5. 转账给用户 if (!account.send(amount)) { // 如果转账失败,恢复余额 accounts[account].balance += amount; WithdrawFailed(account, amount, "转账失败", now); revert(); }
// 6. 记录成功事件 WithdrawSuccess(account, amount, now); }
function transfer(address to, uint amount) public { address from = msg.sender; // 1. 检查发送方账户 if (!checkAccount(from)) { WithdrawFailed(from, amount, "发送方账户不存在", now); revert(); } // 2. 检查接收方账户 if (!checkAccount(to)) { WithdrawFailed(from, amount, "接收方账户不存在", now); revert(); } // 3. 检查转账金额是否有效 if (amount == 0) { WithdrawFailed(from, amount, "转账金额必须大于0", now); revert(); } // 4. 检查发送方余额是否充足 if (accounts[from].balance < amount) { WithdrawFailed(from, amount, "发送方余额不足", now); revert(); } // 5. 更新余额 accounts[from].balance -= amount; accounts[to].balance += amount; // 关键:增加接收方余额 // 3. 更新时间戳 accounts[from].time = now; accounts[to].time = now; // 4. 记录事件 TransferSuccess(from, to, amount, now); }
// 转账成功事件 event TransferSuccess(address from, address to, uint amount, uint time);
// 获取账户信息 function getAccountInfo() public constant returns (address, uint, uint) { address account = msg.sender; if (!checkAccount(account)) { return (address(0), 0, 0); } return (accounts[account].owner, accounts[account].balance, accounts[account].time); }
}
|