From a661cb21d3d4dcad84a0df112afe47dca4ab0b61 Mon Sep 17 00:00:00 2001 From: Richard Meissner Date: Wed, 17 Mar 2021 15:08:13 +0100 Subject: [PATCH] Added Received event --- contracts/common/EtherPaymentFallback.sol | 4 ++- test/core/GnosisSafe.Incoming.spec.ts | 38 +++++++++++++++++++---- 2 files changed, 35 insertions(+), 7 deletions(-) diff --git a/contracts/common/EtherPaymentFallback.sol b/contracts/common/EtherPaymentFallback.sol index 236443df8..5d47050a3 100644 --- a/contracts/common/EtherPaymentFallback.sol +++ b/contracts/common/EtherPaymentFallback.sol @@ -6,11 +6,13 @@ pragma solidity >=0.7.0 <0.9.0; /// @author Richard Meissner - contract EtherPaymentFallback { + event Received(address indexed sender, uint256 value); + /// @dev Fallback function accepts Ether transactions. receive() external payable { - + emit Received(msg.sender, msg.value); } } diff --git a/test/core/GnosisSafe.Incoming.spec.ts b/test/core/GnosisSafe.Incoming.spec.ts index 5754af432..373f1e7a5 100644 --- a/test/core/GnosisSafe.Incoming.spec.ts +++ b/test/core/GnosisSafe.Incoming.spec.ts @@ -18,6 +18,10 @@ describe("GnosisSafe", async () => { function sendEth(address payable safe) public payable returns (bool success) { require(safe.send(msg.value)); } + function callEth(address payable safe) public payable returns (bool success) { + (bool success,) = safe.call{ value: msg.value }(""); + require(success); + } }` return { safe: await getSafeWithOwners([user1.address]), @@ -30,23 +34,45 @@ describe("GnosisSafe", async () => { it('should be able to receive ETH via transfer', async () => { const { safe, caller } = await setupTests() // Notes: It is not possible to load storage + a call + emit event with 2300 gas - // Test Validator - await caller.transferEth(safe.address, { value: parseEther("1") }) - await expect(await hre.ethers.provider.getBalance(safe.address)).to.be.deep.eq(parseEther("1")) + await expect( + caller.transferEth(safe.address, { value: parseEther("1") }) + ).to.be.reverted }) it('should be able to receive ETH via send', async () => { const { safe, caller } = await setupTests() // Notes: It is not possible to load storage + a call + emit event with 2300 gas - // Test Validator - await caller.sendEth(safe.address, { value: parseEther("1") }) + await expect( + caller.sendEth(safe.address, { value: parseEther("1") }) + ).to.be.reverted + }) + + it('should be able to receive ETH via call', async () => { + const { safe, caller } = await setupTests() + await expect( + caller.callEth(safe.address, { + value: parseEther("1") + }) + ).to.emit(safe, "Received").withArgs(caller.address, parseEther("1")) + await expect(await hre.ethers.provider.getBalance(safe.address)).to.be.deep.eq(parseEther("1")) + }) + + + it('should be able to receive ETH via transaction', async () => { + const { safe } = await setupTests() + await expect( + user1.sendTransaction({ + to: safe.address, + value: parseEther("1") + }) + ).to.emit(safe, "Received").withArgs(user1.address, parseEther("1")) await expect(await hre.ethers.provider.getBalance(safe.address)).to.be.deep.eq(parseEther("1")) }) it('should throw for incoming eth with data', async () => { const { safe } = await setupTests() await expect( - user1.sendTransaction({to: safe.address, value: 23, data: "0xbaddad"}) + user1.sendTransaction({ to: safe.address, value: 23, data: "0xbaddad" }) ).to.be.revertedWith("fallback function is not payable and was called with value 23") }) })