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

Krishna s #25

Open
wants to merge 3 commits into
base: master
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: 29 additions & 2 deletions contracts/Vault1.sol
Original file line number Diff line number Diff line change
@@ -1,6 +1,33 @@
//SPDX-License-Identifier: Unlicense
pragma solidity ^0.8.4;

contract Vault1 {

import "@openzeppelin/contracts/interfaces/IERC20.sol";
import "@openzeppelin/contracts/token/ERC20/ERC20.sol";
import "@openzeppelin/contracts/security/ReentrancyGuard.sol";

contract Vault1 is ReentrancyGuard {

IERC20 erc20Contract;
mapping(address => uint) balances;

constructor(IERC20 _erc20Contract) {
erc20Contract = _erc20Contract;
}

function deposit(uint _amount) external nonReentrant {
require(erc20Contract.transferFrom(msg.sender, address(this), _amount), "Transfer from sender failed");
balances[msg.sender] += _amount;
}

function withdraw(uint _amount) external nonReentrant {
require(balances[msg.sender] >= _amount, "Insufficient balance");
// first substract and then transfer to avoid reentry or other issues
balances[msg.sender] -= _amount;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since you are subtracting the amount before calling transfer, this reentrancy check might not be required

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I know :-) but wanted to be careful anyways.

require(erc20Contract.transfer(msg.sender, _amount), "Transfer to sender failed");
}

function getBalance(address addr) view external returns (uint) {
return balances[addr];
}

}
70 changes: 69 additions & 1 deletion contracts/Vault2.sol
Original file line number Diff line number Diff line change
@@ -1,6 +1,74 @@
//SPDX-License-Identifier: Unlicense
pragma solidity ^0.8.4;

contract Vault2 {
import "@openzeppelin/contracts/token/ERC20/ERC20.sol";
import "@openzeppelin/contracts/security/ReentrancyGuard.sol";
import "@openzeppelin/contracts/utils/Address.sol";

contract Vault2 is ERC20, ReentrancyGuard {

constructor() ERC20("Vault2", "VAULT") {}

function mint(uint _amount) payable external nonReentrant {
require(_amount == msg.value, "Amount of VAULT requested != Ether passed");

// Question says "user" but not does not explicitly say if a contract
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Both EOA and contracts should be able to call this function (they both can be users). Just calling _mint meets this purpose

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Cool.

// is allowed to mint or not. If we really only want EOA to mint and not
// other contracts uncomment tIhe below line though this is not fool proof
// as per openzepplin docs.
//Address.isContract(msg.sender);
_mint(msg.sender, _amount);
}

function burn(uint _amount) external nonReentrant {
// question says "user" but not does not explicitly say if a contract
// is allowed to mint or not. f we really only want EOA to mint and not
// other contracts uncomment the below line though this is not fool proof
// as per openzepplin docs.
//Address.isContract(msg.sender);
_burn(msg.sender, _amount);
(bool sent, ) = payable(msg.sender).call{value: _amount}("");
require(sent, "Failed to send Ether");
}

// Below functions I added for my convinience to try some
// exploit testing. Not needed for assignment.
function getBalance() public view returns (uint) {
return address(this).balance;
}
function load() external payable {}
}

// Below contract I added for my convinience to try some
// exploit testing. Not needed for assignment.
contract ReentrancyExploit {

Vault2 vault2Contract;

constructor(Vault2 _vault2Contract) payable {
vault2Contract = _vault2Contract;
}

function initiateAttack() external payable {
// first mint some VAULT by sending ether and
// then try burning VAULT and recieving as much
// ether as possible.
uint amount = address(this).balance;
vault2Contract.mint{value: amount}(amount);
vault2Contract.burn(amount);
}

// default function trying to steal / burn all ether
// of vault2Contract
fallback() external payable {
vault2Contract.burn(1);
}

receive() external payable {
vault2Contract.burn(1);
}

function getBalance() public view returns (uint) {
return address(this).balance;
}
}
11 changes: 10 additions & 1 deletion hardhat.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,5 +17,14 @@ task("accounts", "Prints the list of accounts", async (taskArgs, hre) => {
* @type import('hardhat/config').HardhatUserConfig
*/
module.exports = {
solidity: "0.8.4",
solidity: {
version: "0.8.4",
settings: {
outputSelection: {
"*": {
"*": ["storageLayout"]
}
}
}
}
};
Loading