From 254c27af135e08991475570e5553fc67e75c0929 Mon Sep 17 00:00:00 2001 From: Eduardo Melo Date: Fri, 20 Sep 2024 11:24:58 -0300 Subject: [PATCH] fix: comments from PR, revert hardhat config --- contracts/ERC20Splitter.sol | 7 +++++-- hardhat.config.ts | 2 +- test/SplitterContract.test.ts | 22 ++++++---------------- 3 files changed, 12 insertions(+), 19 deletions(-) diff --git a/contracts/ERC20Splitter.sol b/contracts/ERC20Splitter.sol index 609f9eb..02a25af 100644 --- a/contracts/ERC20Splitter.sol +++ b/contracts/ERC20Splitter.sol @@ -57,6 +57,7 @@ contract ERC20Splitter is ReentrancyGuard { /// @notice Withdraw all tokens that the caller is entitled to. /// Tokens are automatically determined based on previous deposits. + /// @param tokenAddresses Array of token addresses (use address(0) for native tokens). function withdraw(address[] calldata tokenAddresses) external nonReentrant { uint256 tokenCount = tokenAddresses.length; require(tokenCount > 0, 'ERC20Splitter: No tokens specified'); @@ -68,7 +69,7 @@ contract ERC20Splitter is ReentrancyGuard { uint256 amount = balances[tokenAddress][msg.sender]; if (amount == 0) { - continue; + return; } delete balances[tokenAddress][msg.sender]; @@ -102,7 +103,9 @@ contract ERC20Splitter is ReentrancyGuard { address[] calldata recipients ) internal { require(shares.length == recipients.length, 'ERC20Splitter: Shares and recipients length mismatch'); - require(amount > 0, 'ERC20Splitter: Amount must be greater than zero'); + if(amount == 0 ) { + return; + } uint256 totalSharePercentage = 0; diff --git a/hardhat.config.ts b/hardhat.config.ts index 16a51e0..2664c0f 100644 --- a/hardhat.config.ts +++ b/hardhat.config.ts @@ -67,7 +67,7 @@ const BASE_CONFIG = { forking: { url: POLYGON_PROVIDER_URL, blockNumber: 55899875, - timeout: 9999900, + timeout: 20000, }, }, }, diff --git a/test/SplitterContract.test.ts b/test/SplitterContract.test.ts index c47389f..2d651b9 100644 --- a/test/SplitterContract.test.ts +++ b/test/SplitterContract.test.ts @@ -122,6 +122,12 @@ describe('ERC20Splitter', () => { expect(await splitter.balances(mockERC20.getAddress(), recipient1.address)).to.equal(ethers.parseEther('100')) }) + it('Should handle deposit when user has no tokens', async () => { + const recipients = [[recipient1.address]] + const shares = [[10000]] + await splitter.connect(owner).deposit([mockERC20.getAddress()], [0], shares, recipients) + }) + it('Should deposit four ERC20 tokens and split them between recipients', async () => { const tokenAmounts = [ ethers.parseEther('100'), @@ -238,7 +244,6 @@ describe('ERC20Splitter', () => { expect(await splitter.balances(mockERC20.getAddress(), recipient2.address)).to.equal(ethers.parseEther('30')) expect(await splitter.balances(mockERC20.getAddress(), recipient3.address)).to.equal(ethers.parseEther('20')) }) - it('Should deposit native tokens (ETH) and split them between recipients', async () => { const shares = [[5000, 3000, 2000]] const recipients = [[recipient1.address, recipient2.address, recipient3.address]] @@ -297,21 +302,6 @@ describe('ERC20Splitter', () => { }), ).to.be.revertedWith('ERC20Splitter: Incorrect native token amount sent') }) - it('Should revert when amount is 0', async () => { - const incorrectMsgValue = ethers.parseEther('1') // Incorrect Ether amount - const correctEtherAmount = ethers.parseEther('2') // Correct Ether amount to be split - const tokenAddresses = [ethers.ZeroAddress] // Using address(0) for Ether - const amounts = [correctEtherAmount] // Amount to split among recipients - const shares = [[5000, 3000, 2000]] // Shares summing up to 100% - const recipients = [[recipient1.address, recipient2.address, recipient3.address]] - - await expect( - splitter.connect(owner).deposit(tokenAddresses, [0], shares, recipients, { - value: incorrectMsgValue, // Sending incorrect msg.value - }), - ).to.be.revertedWith('ERC20Splitter: Amount must be greater than zero') - }) - it('Should revert when tokenAddresses and amounts lengths mismatch', async () => { const tokenAddresses = [mockERC20.getAddress(), ethers.ZeroAddress] const amounts = [ethers.parseEther('100')] // Length 1, intentional mismatch