-
Notifications
You must be signed in to change notification settings - Fork 66
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
krishnasantosh
wants to merge
3
commits into
DappCamp-Cohort-2:master
Choose a base branch
from
krishnasantosh:krishna-s
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Krishna s #25
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
require(erc20Contract.transfer(msg.sender, _amount), "Transfer to sender failed"); | ||
} | ||
|
||
function getBalance(address addr) view external returns (uint) { | ||
return balances[addr]; | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 There was a problem hiding this comment. Choose a reason for hiding this commentThe 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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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.