|
| 1 | +import chai from "chai"; |
| 2 | +import { ethers, waffle } from "hardhat"; |
| 3 | +import { solidity } from "ethereum-waffle"; |
| 4 | +import { BigNumber } from "ethers"; |
| 5 | + |
| 6 | +import type { SignerWithAddress } from "@nomiclabs/hardhat-ethers/signers"; |
| 7 | + |
| 8 | +import { MultiSendETH } from "../../../typechain-types"; |
| 9 | +import { Artifacts } from "../../shared"; |
| 10 | + |
| 11 | +import { findEvent } from "../../shared/utils"; |
| 12 | + |
| 13 | +chai.use(solidity); |
| 14 | + |
| 15 | +const { expect } = chai; |
| 16 | +const { deployContract } = waffle; |
| 17 | + |
| 18 | +describe("MultiSendETH", () => { |
| 19 | + let deployer: SignerWithAddress; |
| 20 | + let sender: SignerWithAddress; |
| 21 | + let recipient1: SignerWithAddress; |
| 22 | + let recipient2: SignerWithAddress; |
| 23 | + let recipient3: SignerWithAddress; |
| 24 | + |
| 25 | + let multiSendETH: MultiSendETH; |
| 26 | + |
| 27 | + beforeEach(async () => { |
| 28 | + [deployer, sender, recipient1, recipient2, recipient3] = await ethers.getSigners(); |
| 29 | + }); |
| 30 | + |
| 31 | + async function deployMultiSendETH() { |
| 32 | + return deployContract(deployer, Artifacts.MultiSendETH, []); |
| 33 | + } |
| 34 | + |
| 35 | + describe("Contract deployment", () => { |
| 36 | + beforeEach(async () => { |
| 37 | + multiSendETH = (await deployMultiSendETH()) as MultiSendETH; |
| 38 | + }); |
| 39 | + |
| 40 | + it("should deploy with correct constants", async () => { |
| 41 | + expect(await multiSendETH.ARRAY_LIMIT()).to.eq(200); |
| 42 | + }); |
| 43 | + }); |
| 44 | + |
| 45 | + describe("multisendETH function", () => { |
| 46 | + beforeEach(async () => { |
| 47 | + multiSendETH = (await deployMultiSendETH()) as MultiSendETH; |
| 48 | + }); |
| 49 | + |
| 50 | + describe("Successful transfers", () => { |
| 51 | + it("should send ETH to single recipient", async () => { |
| 52 | + const recipients = [recipient1.address]; |
| 53 | + const amounts = [ethers.utils.parseEther("1")]; |
| 54 | + const totalAmount = ethers.utils.parseEther("1"); |
| 55 | + |
| 56 | + const initialBalance = await recipient1.getBalance(); |
| 57 | + |
| 58 | + const tx = await multiSendETH.connect(sender).multisendETH(recipients, amounts, { value: totalAmount }); |
| 59 | + |
| 60 | + const finalBalance = await recipient1.getBalance(); |
| 61 | + expect(finalBalance.sub(initialBalance)).to.eq(ethers.utils.parseEther("1")); |
| 62 | + |
| 63 | + // Check events |
| 64 | + const multisentEvent = await findEvent(tx, "Multisended"); |
| 65 | + expect(multisentEvent?.args?.total).to.eq(totalAmount); |
| 66 | + }); |
| 67 | + |
| 68 | + it("should send ETH to multiple recipients with different amounts", async () => { |
| 69 | + const recipients = [recipient1.address, recipient2.address, recipient3.address]; |
| 70 | + const amounts = [ethers.utils.parseEther("1"), ethers.utils.parseEther("2"), ethers.utils.parseEther("0.5")]; |
| 71 | + const totalAmount = ethers.utils.parseEther("3.5"); |
| 72 | + |
| 73 | + const initialBalances = [ |
| 74 | + await recipient1.getBalance(), |
| 75 | + await recipient2.getBalance(), |
| 76 | + await recipient3.getBalance(), |
| 77 | + ]; |
| 78 | + |
| 79 | + const tx = await multiSendETH.connect(sender).multisendETH(recipients, amounts, { value: totalAmount }); |
| 80 | + |
| 81 | + const finalBalances = [ |
| 82 | + await recipient1.getBalance(), |
| 83 | + await recipient2.getBalance(), |
| 84 | + await recipient3.getBalance(), |
| 85 | + ]; |
| 86 | + |
| 87 | + expect(finalBalances[0].sub(initialBalances[0])).to.eq(ethers.utils.parseEther("1")); |
| 88 | + expect(finalBalances[1].sub(initialBalances[1])).to.eq(ethers.utils.parseEther("2")); |
| 89 | + expect(finalBalances[2].sub(initialBalances[2])).to.eq(ethers.utils.parseEther("0.5")); |
| 90 | + |
| 91 | + // Check events |
| 92 | + const multisentEvent = await findEvent(tx, "Multisended"); |
| 93 | + expect(multisentEvent?.args?.total).to.eq(totalAmount); |
| 94 | + }); |
| 95 | + |
| 96 | + it("should handle maximum array length", async () => { |
| 97 | + const arrayLength = 200; // ARRAY_LIMIT |
| 98 | + const recipients: string[] = []; |
| 99 | + const amounts: BigNumber[] = []; |
| 100 | + const amountPerRecipient = ethers.utils.parseEther("0.01"); |
| 101 | + |
| 102 | + // Create arrays with max length |
| 103 | + for (let i = 0; i < arrayLength; i++) { |
| 104 | + recipients.push(ethers.Wallet.createRandom().address); |
| 105 | + amounts.push(amountPerRecipient); |
| 106 | + } |
| 107 | + |
| 108 | + const totalAmount = amountPerRecipient.mul(arrayLength); |
| 109 | + |
| 110 | + const tx = await multiSendETH.connect(sender).multisendETH(recipients, amounts, { value: totalAmount }); |
| 111 | + |
| 112 | + const multisentEvent = await findEvent(tx, "Multisended"); |
| 113 | + expect(multisentEvent?.args?.total).to.eq(totalAmount); |
| 114 | + }); |
| 115 | + }); |
| 116 | + |
| 117 | + describe("Validation failures", () => { |
| 118 | + it("should revert with mismatched arrays", async () => { |
| 119 | + const recipients = [recipient1.address, recipient2.address]; |
| 120 | + const amounts = [ethers.utils.parseEther("1")]; // Only one amount for two recipients |
| 121 | + |
| 122 | + await expect( |
| 123 | + multiSendETH.connect(sender).multisendETH(recipients, amounts, { value: ethers.utils.parseEther("1") }) |
| 124 | + ).to.be.revertedWith("Mismatched arrays"); |
| 125 | + }); |
| 126 | + |
| 127 | + it("should revert when array length exceeds limit", async () => { |
| 128 | + const arrayLength = 201; // Exceeds ARRAY_LIMIT |
| 129 | + const recipients: string[] = []; |
| 130 | + const amounts = []; |
| 131 | + |
| 132 | + for (let i = 0; i < arrayLength; i++) { |
| 133 | + recipients.push(ethers.Wallet.createRandom().address); |
| 134 | + amounts.push(ethers.utils.parseEther("0.01")); |
| 135 | + } |
| 136 | + |
| 137 | + await expect( |
| 138 | + multiSendETH.connect(sender).multisendETH(recipients, amounts, { value: ethers.utils.parseEther("2.01") }) |
| 139 | + ).to.be.revertedWith("Array length exceeds limit"); |
| 140 | + }); |
| 141 | + |
| 142 | + it("should revert when total doesn't match msg.value (too little sent)", async () => { |
| 143 | + const recipients = [recipient1.address, recipient2.address]; |
| 144 | + const amounts = [ethers.utils.parseEther("1"), ethers.utils.parseEther("1")]; |
| 145 | + const insufficientValue = ethers.utils.parseEther("1.5"); // Less than total (2 ETH) |
| 146 | + |
| 147 | + await expect( |
| 148 | + multiSendETH.connect(sender).multisendETH(recipients, amounts, { value: insufficientValue }) |
| 149 | + ).to.be.revertedWith("Transfer failed"); |
| 150 | + }); |
| 151 | + |
| 152 | + it("should revert when total doesn't match msg.value (too much sent)", async () => { |
| 153 | + const recipients = [recipient1.address]; |
| 154 | + const amounts = [ethers.utils.parseEther("1")]; |
| 155 | + const excessiveValue = ethers.utils.parseEther("2"); // More than total (1 ETH) |
| 156 | + |
| 157 | + await expect( |
| 158 | + multiSendETH.connect(sender).multisendETH(recipients, amounts, { value: excessiveValue }) |
| 159 | + ).to.be.revertedWith("Incorrect ETH amount sent"); |
| 160 | + }); |
| 161 | + }); |
| 162 | + }); |
| 163 | + |
| 164 | + describe("Gas optimization tests", () => { |
| 165 | + beforeEach(async () => { |
| 166 | + multiSendETH = (await deployMultiSendETH()) as MultiSendETH; |
| 167 | + }); |
| 168 | + |
| 169 | + it("should have reasonable gas consumption for different array sizes", async () => { |
| 170 | + const testSizes = [1, 5, 10, 50, 100]; |
| 171 | + |
| 172 | + for (const size of testSizes) { |
| 173 | + const recipients: string[] = []; |
| 174 | + const amounts = []; |
| 175 | + const amountPerRecipient = ethers.utils.parseEther("0.01"); |
| 176 | + |
| 177 | + for (let i = 0; i < size; i++) { |
| 178 | + recipients.push(ethers.Wallet.createRandom().address); |
| 179 | + amounts.push(amountPerRecipient); |
| 180 | + } |
| 181 | + |
| 182 | + const totalAmount = amountPerRecipient.mul(size); |
| 183 | + |
| 184 | + const tx = await multiSendETH.connect(sender).multisendETH(recipients, amounts, { value: totalAmount }); |
| 185 | + |
| 186 | + const receipt = await tx.wait(); |
| 187 | + console.log(`Gas used for ${size} recipients: ${receipt.gasUsed.toString()}`); |
| 188 | + |
| 189 | + // Ensure transaction succeeded |
| 190 | + expect(receipt.status).to.eq(1); |
| 191 | + } |
| 192 | + }); |
| 193 | + }); |
| 194 | +}); |
0 commit comments