feat: event

This commit is contained in:
Ting-Jun Wang 2023-04-06 16:57:38 +08:00
parent 83e0c67ea6
commit 2f70767578
Signed by: snsd0805
GPG Key ID: 8DB0D22BC1217D33
2 changed files with 13 additions and 4 deletions

View File

@ -6,16 +6,17 @@ import "./Bank.sol";
contract ATM is BaseContract{ contract ATM is BaseContract{
Bank bank; Bank bank;
event Withdraw(address from, uint amount, uint balance );
constructor(address bank_addr) { constructor(address bank_addr) {
bank = Bank(payable(bank_addr)); bank = Bank(payable(bank_addr));
} }
function withdraw(uint amount) public { 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) { if(address(this).balance <= 0.1 ether) {
bank.withdraw(0.5 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); payable(msg.sender).transfer(amount);
emit Withdraw(msg.sender, amount, address(this).balance);
} }
} }

View File

@ -4,7 +4,10 @@ pragma solidity >=0.4.22 <0.9.0;
import "./BaseContract.sol"; import "./BaseContract.sol";
contract Bank is BaseContract{ 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() {} constructor() {}
modifier onlyATM() { modifier onlyATM() {
@ -21,10 +24,15 @@ contract Bank is BaseContract{
function addATM(address atm) public onlyOwner { function addATM(address atm) public onlyOwner {
atms.push(atm); atms.push(atm);
emit AddATM(atm);
} }
function withdraw(uint amount) public onlyATM { 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); payable(msg.sender).transfer(amount);
emit Withdraw(msg.sender, amount, address(this).balance);
} }
} }