From 2f7076757832e4c91db046a739c9221d587bb783 Mon Sep 17 00:00:00 2001 From: Ting-Jun Wang Date: Thu, 6 Apr 2023 16:57:38 +0800 Subject: [PATCH] feat: event --- contracts/ATM.sol | 5 +++-- contracts/Bank.sol | 12 ++++++++++-- 2 files changed, 13 insertions(+), 4 deletions(-) diff --git a/contracts/ATM.sol b/contracts/ATM.sol index ce6f5d7..0a85093 100644 --- a/contracts/ATM.sol +++ b/contracts/ATM.sol @@ -6,16 +6,17 @@ import "./Bank.sol"; contract ATM is BaseContract{ Bank bank; + event Withdraw(address from, uint amount, uint balance ); constructor(address bank_addr) { bank = Bank(payable(bank_addr)); } function withdraw(uint amount) public { + require(amount<=0.1 ether, "You cannot withdraw more than 0.1 ether"); if(address(this).balance <= 0.1 ether) { bank.withdraw(0.5 ether); } - require(amount<=0.1 ether, "You request amount cannot bigger than 0.1 ether"); - require(amount<=address(this).balance, "This ATM doesn't have enough ETH to withdraw"); payable(msg.sender).transfer(amount); + emit Withdraw(msg.sender, amount, address(this).balance); } } diff --git a/contracts/Bank.sol b/contracts/Bank.sol index 1207ca1..7d35734 100644 --- a/contracts/Bank.sol +++ b/contracts/Bank.sol @@ -4,7 +4,10 @@ pragma solidity >=0.4.22 <0.9.0; import "./BaseContract.sol"; contract Bank is BaseContract{ - address[] private atms; + address[] public atms; + event NoMoney(address from, uint amount, uint balance); + event Withdraw(address from, uint amount, uint balance); + event AddATM(address atm); constructor() {} modifier onlyATM() { @@ -21,10 +24,15 @@ contract Bank is BaseContract{ function addATM(address atm) public onlyOwner { atms.push(atm); + emit AddATM(atm); } function withdraw(uint amount) public onlyATM { - require(amount<=address(this).balance, "Bank doesn't have enough money to withdraw."); + if(amount<=address(this).balance){ + emit NoMoney(msg.sender, amount, address(this).balance); + revert("Bank doesn't have enough money to withdraw"); + } payable(msg.sender).transfer(amount); + emit Withdraw(msg.sender, amount, address(this).balance); } }