Skip to content

Commit

Permalink
feat: add dirty money and dealer exercise and test
Browse files Browse the repository at this point in the history
  • Loading branch information
ialmanzaj committed Oct 20, 2023
1 parent f609334 commit a16aaf3
Show file tree
Hide file tree
Showing 3 changed files with 115 additions and 0 deletions.
42 changes: 42 additions & 0 deletions src/DirtyMoney/Dealer.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.19;

import "./DirtyMoney.sol";

contract Dealer {
address[] public hackers;
mapping(address => DirtyMoney) public userToDineroSucioContract;

modifier notContractCaller() {
if (msg.sender.code.length != 0) revert("Only EOA can call");
_;
}

event NewHacker(address indexed _newHacker);

function getNewInstance() public notContractCaller {
address sender = msg.sender;
userToDineroSucioContract[sender] = new DirtyMoney(sender);
}

function submitInstance() public {
address sender = msg.sender;
DirtyMoney userInstance = userToDineroSucioContract[sender];
uint256 userBalance = userInstance.balanceOf(sender);

if (isWinner(sender)) revert("You already won");
if (userBalance != 0) revert("You haven't got them yet");

hackers.push(sender);
emit NewHacker(sender);
}

function isWinner(address _hacker) public view returns (bool) {
return getWinner() == _hacker;
}

function getWinner() public view returns (address) {
if (hackers.length == 0) return address(0);
return hackers[0];
}
}
36 changes: 36 additions & 0 deletions src/DirtyMoney/DirtyMoney.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;

import "@openzeppelin/contracts/token/ERC20/ERC20.sol";

contract DirtyMoney is ERC20 {
uint256 public timeLock = block.timestamp + 10 * 365 days;
uint256 public INITIAL_SUPPLY;
address public player;

constructor(address _player) ERC20("DirtyMoney", "XXX") {
player = _player;
INITIAL_SUPPLY = 10000 * (10**uint256(decimals()));
_mint(player, INITIAL_SUPPLY);
emit Transfer(address(0), player, INITIAL_SUPPLY);
}

function transfer(address _to, uint256 _value)
public
override
lockTokens
returns (bool)
{
super.transfer(_to, _value);
return true;
}

modifier lockTokens() {
if (msg.sender == player) {
require(block.timestamp > timeLock);
_;
} else {
_;
}
}
}
37 changes: 37 additions & 0 deletions test/Dealer.t.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.13;

import "forge-std/console.sol";
import "forge-std/Test.sol";

import {Dealer} from "../src/DirtyMoney/Dealer.sol";
import {DirtyMoney} from "../src/DirtyMoney/DirtyMoney.sol";

contract DealerTest is Test {
Dealer dealer;
DirtyMoney money;

address bob = address(0x1);
address mary = address(0x22222222);

function setUp() public {
vm.startPrank(bob);
dealer = new Dealer();
vm.stopPrank();
}

function testSubmit() public {
vm.startPrank(bob);
dealer.getNewInstance();
money = dealer.userToDineroSucioContract(bob);
uint256 balance = money.balanceOf(bob);

money.approve(bob, balance);
money.transferFrom(bob, mary, balance);
dealer.submitInstance();

assertEq(money.balanceOf(bob), 0);
assertEq(money.balanceOf(mary), balance);
vm.stopPrank();
}
}

0 comments on commit a16aaf3

Please sign in to comment.