Skip to content

Commit

Permalink
cleanup and refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
ZumZoom committed Apr 2, 2024
1 parent 66fc4c5 commit 3cf5390
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 31 deletions.
13 changes: 0 additions & 13 deletions contracts/CheapEthSender.sol

This file was deleted.

17 changes: 17 additions & 0 deletions contracts/mixins/SelfdestructEthSender.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
// SPDX-License-Identifier: MIT

pragma solidity ^0.8.25;


abstract contract SelfdestructEthSender {
constructor() {
// tload is done to verify that the EVM is cancun-compatible
assembly ("memory-safe") {

Check warning on line 9 in contracts/mixins/SelfdestructEthSender.sol

View workflow job for this annotation

GitHub Actions / lint

Avoid to use inline assembly. It is acceptable only in rare cases
pop(tload(0))
}
}

function stopAndTransferBalance(address payable receiver) external {
selfdestruct(receiver);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,14 @@

pragma solidity ^0.8.25;

import "../CheapEthSender.sol";
import "../mixins/SelfdestructEthSender.sol";

contract CheapEthSenderMock is CheapEthSender {
contract SelfdestructEthSenderMock is SelfdestructEthSender {
error ETHTransferFailed();

function alive() external pure returns(uint256) {
return 1;
}
receive() external payable {}

function sendEthers(address payable receiver) external payable {
function transferBalance(address payable receiver) external payable {
// solhint-disable-next-line avoid-low-level-calls
(bool success, ) = receiver.call{value: address(this).balance}("");
if (!success) revert ETHTransferFailed();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { SignerWithAddress } from '@nomicfoundation/hardhat-ethers/signers';
import { loadFixture } from '@nomicfoundation/hardhat-network-helpers';
import { ethers } from 'hardhat';

describe('CheapEthSender', function () {
describe('SelfdestructEthSender', function () {
let signer0: SignerWithAddress;
let signer1: SignerWithAddress;

Expand All @@ -13,26 +13,26 @@ describe('CheapEthSender', function () {
});

async function deployMocks() {
const CheapEthSender = await ethers.getContractFactory('CheapEthSenderMock');
const cheapEthSender = await CheapEthSender.deploy();
const EthSender = await ethers.getContractFactory('SelfdestructEthSenderMock');
const ethSender = await EthSender.deploy();

return { cheapEthSender };
return { ethSender };
}

it('should send Ethers with selfdestruct', async function () {
const { cheapEthSender } = await loadFixture(deployMocks);
const cheapEthSenderAddress = await cheapEthSender.getAddress();
const { ethSender } = await loadFixture(deployMocks);
const ethSenderAddress = await ethSender.getAddress();

await signer0.sendTransaction({ to: cheapEthSenderAddress, value: ether('1') });
const receipt0 = await (await cheapEthSender.sendEthers(signer1.address)).wait();
await signer0.sendTransaction({ to: ethSenderAddress, value: ether('1') });
const receipt0 = await (await ethSender.transferBalance(signer1.address)).wait();
console.log('send ethers without selfdestruct', receipt0!.gasUsed.toString());

await signer0.sendTransaction({ to: cheapEthSenderAddress, value: ether('1') });
await signer0.sendTransaction({ to: ethSenderAddress, value: ether('1') });
const balanceOfAddr1 = await ethers.provider.getBalance(signer1.address);
const receipt1 = await (await cheapEthSender.sendAllEthers(signer1.address)).wait();
const receipt1 = await (await ethSender.stopAndTransferBalance(signer1.address)).wait();
console.log('send all ethers', receipt1!.gasUsed.toString());
expect(await ethers.provider.getBalance(signer1.address)).to.be.eq(balanceOfAddr1 + ether('1'));
expect(await ethers.provider.getBalance(cheapEthSenderAddress)).to.be.eq(ether('0'));
expect(await cheapEthSender.alive()).to.be.eq(1n);
expect(await ethers.provider.getBalance(ethSenderAddress)).to.be.eq(ether('0'));
expect(await ethers.provider.getCode(ethSenderAddress)).to.be.not.eq('0x');
});
});

0 comments on commit 3cf5390

Please sign in to comment.