# King

The contract below represents a very simple game: whoever sends it an amount of ether that is larger than the current prize becomes the new king. On such an event, the overthrown king gets paid the new prize, making a bit of ether in the process! As ponzi as it gets xD

Such a fun game. Your goal is to break it.

When you submit the instance back to the level, the level is going to reclaim kingship. See the require(... || msg.sender == owner); bit. You will beat the level if you can avoid such a self proclamation.

// SPDX-License-Identifier: MIT
pragma solidity ^0.6.0;

contract King {

  address payable public king;
  uint public prize;
  address payable public owner;

  constructor() public payable {
    owner = msg.sender;
    king = msg.sender;
    prize = msg.value;
  }

  receive() external payable {
    require(msg.value >= prize || msg.sender == owner);
    require(!isContract(msg.sender), "don't let a smart contract to be our king!");
    king.transfer(msg.value);
    king = msg.sender;
    prize = msg.value;
  }

  function isContract(address _account) internal view returns(bool) {
    uint256 size;
    assembly {
      size := extcodesize(_account)
    }
    return size > 0;
  }
}

TIP

You might want to read the explanation of DoS vulnerabilities (opens new window).

Most of Ethernaut's levels try to expose (in an oversimplified form of course) something that actually happened — a real hack or a real bug.

In this case, see: King of the Ether (opens new window) and King of the Ether Postmortem (opens new window).