Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Test trial #3

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 15 additions & 16 deletions contracts/ColabBank.sol
Original file line number Diff line number Diff line change
@@ -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"
Expand All @@ -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);
Expand Down
43 changes: 42 additions & 1 deletion test/ColabBankV2.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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

})