-
Notifications
You must be signed in to change notification settings - Fork 52
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #134 from 1inch/feature/cheaperEthSend
[SC-1103] add cheapEthSender
- Loading branch information
Showing
14 changed files
with
1,103 additions
and
1,327 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
// SPDX-License-Identifier: MIT | ||
|
||
pragma solidity ^0.8.25; | ||
|
||
|
||
abstract contract SelfdestructEthSender { | ||
constructor() { | ||
// tload is done to verify that the EVM is cancun-compatible | ||
// solhint-disable-next-line no-inline-assembly | ||
assembly ("memory-safe") { | ||
pop(tload(0)) | ||
} | ||
} | ||
|
||
function stopAndTransferBalance(address payable receiver) external { | ||
selfdestruct(receiver); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
// SPDX-License-Identifier: MIT | ||
|
||
pragma solidity ^0.8.25; | ||
|
||
import "../mixins/SelfdestructEthSender.sol"; | ||
|
||
contract SelfdestructEthSenderMock is SelfdestructEthSender { | ||
error ETHTransferFailed(); | ||
|
||
receive() 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(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
# SelfdestructEthSender | ||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
## Functions | ||
### constructor | ||
```solidity | ||
function constructor( | ||
) internal | ||
``` | ||
|
||
|
||
|
||
|
||
### stopAndTransferBalance | ||
```solidity | ||
function stopAndTransferBalance( | ||
address payable receiver | ||
) external | ||
``` | ||
|
||
|
||
#### Parameters: | ||
| Name | Type | Description | | ||
| :--- | :--- | :------------------------------------------------------------------- | | ||
|`receiver` | address payable | | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
import { expect } from '../../src/expect'; | ||
import { ether } from '../../src/prelude'; | ||
import { SignerWithAddress } from '@nomicfoundation/hardhat-ethers/signers'; | ||
import { loadFixture } from '@nomicfoundation/hardhat-network-helpers'; | ||
import { ethers } from 'hardhat'; | ||
|
||
describe('SelfdestructEthSender', function () { | ||
let signer0: SignerWithAddress; | ||
let signer1: SignerWithAddress; | ||
|
||
before(async function () { | ||
[signer0, signer1] = await ethers.getSigners(); | ||
}); | ||
|
||
async function deployMocks() { | ||
const EthSender = await ethers.getContractFactory('SelfdestructEthSenderMock'); | ||
const ethSender = await EthSender.deploy(); | ||
|
||
return { ethSender }; | ||
} | ||
|
||
it('should send Ethers with selfdestruct', async function () { | ||
const { ethSender } = await loadFixture(deployMocks); | ||
const ethSenderAddress = await ethSender.getAddress(); | ||
|
||
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: ethSenderAddress, value: ether('1') }); | ||
const balanceOfAddr1 = await ethers.provider.getBalance(signer1.address); | ||
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(ethSenderAddress)).to.be.eq(ether('0')); | ||
expect(await ethers.provider.getCode(ethSenderAddress)).to.be.not.eq('0x'); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,29 +1,29 @@ | ||
// Jest Snapshot v1, https://goo.gl/fbAQLP | ||
|
||
exports[`StringUtil Gas usage Empty bytes 1`] = `22305n`; | ||
exports[`StringUtil Gas usage Empty bytes 1`] = `22212n`; | ||
|
||
exports[`StringUtil Gas usage Empty bytes naive 1`] = `22651n`; | ||
exports[`StringUtil Gas usage Empty bytes naive 1`] = `22558n`; | ||
|
||
exports[`StringUtil Gas usage Extremely long byte array gas 1`] = `60428n`; | ||
exports[`StringUtil Gas usage Extremely long byte array gas 1`] = `55747n`; | ||
|
||
exports[`StringUtil Gas usage Extremely long byte array gas naive 1`] = `536349n`; | ||
exports[`StringUtil Gas usage Extremely long byte array gas naive 1`] = `531668n`; | ||
|
||
exports[`StringUtil Gas usage Single byte 1`] = `22982n`; | ||
exports[`StringUtil Gas usage Single byte 1`] = `22889n`; | ||
|
||
exports[`StringUtil Gas usage Single byte naive 1`] = `23301n`; | ||
exports[`StringUtil Gas usage Single byte naive 1`] = `23208n`; | ||
|
||
exports[`StringUtil Gas usage Uint 128 1`] = `22815n`; | ||
exports[`StringUtil Gas usage Uint 128 1`] = `22574n`; | ||
|
||
exports[`StringUtil Gas usage Uint 128 naive 1`] = `38302n`; | ||
exports[`StringUtil Gas usage Uint 128 naive 1`] = `38061n`; | ||
|
||
exports[`StringUtil Gas usage Uint 256 1`] = `23007n`; | ||
exports[`StringUtil Gas usage Uint 256 1`] = `22766n`; | ||
|
||
exports[`StringUtil Gas usage Uint 256 as bytes 1`] = `23520n`; | ||
exports[`StringUtil Gas usage Uint 256 as bytes 1`] = `23279n`; | ||
|
||
exports[`StringUtil Gas usage Uint 256 as bytes naive 1`] = `39097n`; | ||
exports[`StringUtil Gas usage Uint 256 as bytes naive 1`] = `38856n`; | ||
|
||
exports[`StringUtil Gas usage Uint 256 naive 1`] = `38494n`; | ||
exports[`StringUtil Gas usage Uint 256 naive 1`] = `38253n`; | ||
|
||
exports[`StringUtil Gas usage Very long byte array gas 1`] = `25412n`; | ||
exports[`StringUtil Gas usage Very long byte array gas 1`] = `25023n`; | ||
|
||
exports[`StringUtil Gas usage Very long byte array gas naive 1`] = `57177n`; | ||
exports[`StringUtil Gas usage Very long byte array gas naive 1`] = `56788n`; |
Empty file.
Empty file.
Oops, something went wrong.