Section 4 Exercise/ Ethernaut Level 9 (King): transfer() and send() fails in attaining Kingship, but call() succeeds #84
-
I was trying to solve the King challenge in Ethernaut, but taking Kingship proved to be a bit tricky: // Transaction fails
function takeTheThroneWithTransfer() public payable{
payable(kingContractAddress).transfer(msg.value);
}
// Transaction fails
function takeTheThroneWithSend() public payable {
bool success = payable(kingContractAddress).send(msg.value);
require(success, "Failed to send Ether");
}
// Transaction succeeds, attains Kingship
function takeTheThroneWithCall() public payable{
(bool success, ) = kingContractAddress.call{value: msg.value}("");
require(success, "Failed to send Ether");
} I want to understand why this is the case, why transfer() and send() doesn't work but call() does. Once Kingship was attained, I was able to submit the level successfully, solving the challenge. I asked the same question on stackexchange as well. Thanks in advance! |
Beta Was this translation helpful? Give feedback.
Answered by
johnsonstephan
Jan 10, 2024
Replies: 1 comment 3 replies
-
Good walkthrough here to help understand why |
Beta Was this translation helpful? Give feedback.
3 replies
Answer selected by
RichuAK
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Good walkthrough here to help understand why
transfer()
andsend()
don't work butcall()
does - https://dev.to/nvn/ethernaut-hacks-level-9-king-12