From d048f27bef300801935fed9837a02c60fbde2edc Mon Sep 17 00:00:00 2001 From: ujez Date: Sat, 19 Nov 2022 20:43:47 +0100 Subject: [PATCH 1/2] added some comments for readability --- contracts/ColabBank.sol | 31 +++++++++++++++---------------- 1 file changed, 15 insertions(+), 16 deletions(-) diff --git a/contracts/ColabBank.sol b/contracts/ColabBank.sol index 9cfc6e2..ce86abf 100644 --- a/contracts/ColabBank.sol +++ b/contracts/ColabBank.sol @@ -1,22 +1,25 @@ // SPDX-License-Identifier: UNLICENSED pragma solidity ^0.8.9; - // Uncomment this line to use console.log // import "hardhat/console.sol"; contract ColabBank { - uint public unlockTime; + // The keyword "public" makes variables + // accessible from other contracts + uint256 public unlockTime; mapping(address => uint256) public balances; uint256 public totalColabBalance; - address payable public owner; - - event Deposit(uint amount, uint when, address caller); - event Withdrawal(uint amount, uint when); - - constructor(uint _unlockTime) payable { + // Events allow clients to react to specific + // contract changes you declare + event Deposit(uint256 amount, uint256 when, address caller); + event Withdrawal(uint256 amount, uint256 when); + + // Constructor code is only run when the contract + // is created + constructor(uint256 _unlockTime) payable { require( block.timestamp < _unlockTime, "Unlock time should be in the future" @@ -26,20 +29,16 @@ contract ColabBank { owner = payable(msg.sender); } - - - function deposit(uint amount) public { + function deposit(uint256 amount) public { // Uncomment this line, and the import of "hardhat/console.sol", to print a log in your terminal // console.log("Unlock time is %o and block timestamp is %o", unlockTime, block.timestamp); - require(amount != 0, "cannot deposit 0 amount"); - balances[msg.sender] = amount; + balances[msg.sender] = amount; totalColabBalance += amount; - emit Deposit(amount,block.timestamp, msg.sender); - - + emit Deposit(amount, block.timestamp, msg.sender); } + function withdraw() public { // Uncomment this line, and the import of "hardhat/console.sol", to print a log in your terminal // console.log("Unlock time is %o and block timestamp is %o", unlockTime, block.timestamp); From d1f418d9bfe1bb75ce5f40341bae6b7381982354 Mon Sep 17 00:00:00 2001 From: ujez Date: Sat, 19 Nov 2022 22:41:30 +0100 Subject: [PATCH 2/2] small inputs --- test/ColabBankV2.test.js | 43 +++++++++++++++++++++++++++++++++++++++- 1 file changed, 42 insertions(+), 1 deletion(-) diff --git a/test/ColabBankV2.test.js b/test/ColabBankV2.test.js index 743e14c..ae1149f 100644 --- a/test/ColabBankV2.test.js +++ b/test/ColabBankV2.test.js @@ -29,16 +29,57 @@ describe("ColabBank Test Suite", async () => { describe("Deposits", async () => { it("should successfully deposit", async () => { + const { colabBank, addr1 } = await loadFixture(deployOneYearLockFixture); + + const addr1BalanceBeforeDeposit = await colabBank.balances(addr1.address) + console.log("addr1 bal before__", addr1BalanceBeforeDeposit) + expect(addr1BalanceBeforeDeposit).to.eq(0) + expect(colabBank.connect(addr1).deposit(0)).to.be.reverted + const addr1DepositTxn = await colabBank.connect(addr1).deposit(5) + + const addr1BalanceAfterDeposit = await colabBank.balances(addr1.address) + expect(addr1BalanceAfterDeposit).to.eq(5) + console.log("addr1 bal after__", addr1BalanceAfterDeposit) + + const totalColabBalance = await colabBank.totalColabBalance() + console.log("total colab balance__", totalColabBalance) + expect(totalColabBalance).to.eq(5) + + await expect(addr1DepositTxn).to.emit(colabBank, "Deposit").withArgs(5, anyValue, addr1.address) + }) }) describe("Withdrawals", async () => { // write PoC for the vulnerability - it("", async () => { + it("Should withdraw from ColabBank", async () => { + const { colabBank, addr2 } = await loadFixture(deployOneYearLockFixture); + + const addr1BalanceBeforeWithdrawal = await colabBank.balances(addr2.address) + console.log("addr1 bal before__", addr1BalanceBeforeWithdrawal) + expect(Number(addr1BalanceBeforeWithdrawal)).to.eq(5) + expect(colabBank.connect(addr2).withdraw(5)).to.be.reverted + const addr1WithdrawalTxn = await colabBank.connect(addr2).withdraw(5) + + const addr1BalanceAfterWithdrawal = await colabBank.balances(addr2.address) + expect(Number(addr1BalanceAfterWithdrawal)).to.eq(0) + console.log("addr1 bal after__", addr1BalanceAfterWithdrawal) + + const totalColabBalance = await colabBank.totalColabBalance() + console.log("total colab balance__", totalColabBalance) + expect(Number(totalColabBalance)).to.eq(0) + await expect(Number(addr1WithdrawalTxn)).to.emit(colabBank, "Withdraw").withArgs(5, anyValue, addr2.address) }) }) + //GETTING THIS OUTPUT + // AssertionError: expected +0 to equal 5 + // + expected - actual + + // -0 + // +5 + }) \ No newline at end of file