Skip to content

Commit

Permalink
Add events for ETHLocker
Browse files Browse the repository at this point in the history
  • Loading branch information
pawurb committed Feb 19, 2024
1 parent b929db0 commit f62210a
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 0 deletions.
10 changes: 10 additions & 0 deletions contracts/ETHLocker.sol
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,9 @@ contract ETHLocker {
uint256 balance;
}

event Deposit(address indexed by, uint256 indexed amount);
event Withdrawal(address indexed by, uint256 indexed amount);

address private constant ZERO_ADDRESS = address(0x0);
string private constant ERRNOTCONFIGURED = "Deposit not configured.";

Expand Down Expand Up @@ -60,14 +63,19 @@ contract ETHLocker {

uint256 newDepositId = lockerPass.nextId();
lockerPass.mint(msg.sender);

deposits[newDepositId] = newDeposit;

emit Deposit(msg.sender, msg.value);
}

function deposit(
uint256 _depositId
) external payable onlyDepositOwner(msg.sender, _depositId) {
DepositData storage depositData = deposits[_depositId];
depositData.balance = depositData.balance + msg.value;

emit Deposit(msg.sender, msg.value);
}

function canWithdraw(uint256 _depositId) public view returns (bool) {
Expand Down Expand Up @@ -98,6 +106,8 @@ contract ETHLocker {
delete deposits[_depositId];
lockerPass.burn(_depositId);
payable(msg.sender).transfer(balance);

emit Withdrawal(msg.sender, balance);
}

function getETHPrice() public view returns (int256) {
Expand Down
20 changes: 20 additions & 0 deletions test/ETHLockerTest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,16 @@ describe("ETHLocker", () => {
)
})

it("emits a correct event", async () => {
await expect(locker.configureDeposit(10, 0, { value: oneEther.toString() }))
.to.emit(locker, "Deposit")
.withArgs(user1.address, oneEther.toString())

await expect(locker.deposit(DEPOSIT_ID, { value: twoEther.toString() }))
.to.emit(locker, "Deposit")
.withArgs(user1.address, twoEther.toString())
})

it("accepts funds and increases correct account balance", async () => {
await locker.configureDeposit(10, 0, { value: oneEther.toString() })
expect((await locker.deposits(DEPOSIT_ID)).balance).to.equal(oneEther)
Expand Down Expand Up @@ -282,6 +292,16 @@ describe("ETHLocker", () => {
expect((await locker.deposits(DEPOSIT_ID + 1)).balance).to.equal(0)
})

it("emits a correct event", async () => {
await locker.configureDeposit(10, 1100, { value: oneEther.toString() })
await advanceByDays(11)

await expect(
locker.withdraw(DEPOSIT_ID)
).to.emit(locker, "Withdrawal")
.withArgs(user1.address, oneEther.toString())
})

it("does not allow withdrawals by account that don't own correct LockerPass NFT", async () => {
await locker.configureDeposit(10, 1100, { value: oneEther.toString() })
await advanceByDays(11)
Expand Down

0 comments on commit f62210a

Please sign in to comment.