From 18d04d18a1b701ca88aa1a5efd711a1916f5b603 Mon Sep 17 00:00:00 2001 From: Deepu Date: Fri, 10 May 2024 18:38:45 +0530 Subject: [PATCH 1/3] closes #206 --- test/TestSwaplace.test.ts | 235 +++++++++++++++++++++++++++++++++++++- 1 file changed, 234 insertions(+), 1 deletion(-) diff --git a/test/TestSwaplace.test.ts b/test/TestSwaplace.test.ts index 35e6c5b..414307b 100644 --- a/test/TestSwaplace.test.ts +++ b/test/TestSwaplace.test.ts @@ -1,5 +1,5 @@ import { expect } from "chai"; -import { Contract } from "ethers"; +import { BigNumber, Contract } from "ethers"; import { ethers, network } from "hardhat"; import { SignerWithAddress } from "@nomiclabs/hardhat-ethers/signers"; import { Asset, Swap, composeSwap } from "./utils/SwapFactory"; @@ -326,6 +326,44 @@ describe("Swaplace", async function () { .to.emit(Swaplace, "SwapCreated") .withArgs(await Swaplace.totalSwaps(), owner.address, zeroAddress); }); + + it("Should be able to create a swap with native ethers", async function () { + const bidingAddr = [MockERC20.address]; + const bidingAmountOrId = [50]; + + const askingAddr = [ + MockERC20.address, + MockERC20.address, + MockERC20.address, + ]; + const askingAmountOrId = [50, 100, 150]; + + const valueToSend: BigNumber = ethers.utils.parseEther("0.5"); + + const currentTimestamp = (await blocktimestamp()) + 1000000; + const config = await Swaplace.encodeConfig( + zeroAddress, + currentTimestamp, + 0, + valueToSend.div(1e12), + ); + + const swap: Swap = await composeSwap( + owner.address, + config, + bidingAddr, + bidingAmountOrId, + askingAddr, + askingAmountOrId, + ); + await expect( + await Swaplace.connect(owner).createSwap(swap, { + value: valueToSend, + }), + ) + .to.emit(Swaplace, "SwapCreated") + .withArgs(await Swaplace.totalSwaps(), owner.address, zeroAddress); + }); }); context("Reverts when creating Swaps", () => { @@ -335,6 +373,40 @@ describe("Swaplace", async function () { Swaplace.connect(allowed).createSwap(swap), ).to.be.revertedWithCustomError(Swaplace, `InvalidAddress`); }); + + it("Should revert when the wrong amount of ethers is sent by the {owner}", async function () { + const bidingAddr = [MockERC20.address]; + const bidingAmountOrId = [50]; + + const askingAddr = [ + MockERC20.address, + MockERC20.address, + MockERC20.address, + ]; + const askingAmountOrId = [50, 100, 150]; + + const valueToSend: BigNumber = ethers.utils.parseEther("0.5"); + + const currentTimestamp = (await blocktimestamp()) + 1000000; + const config = await Swaplace.encodeConfig( + zeroAddress, + currentTimestamp, + 0, + valueToSend.div(1e12), + ); + + const swap: Swap = await composeSwap( + owner.address, + config, + bidingAddr, + bidingAmountOrId, + askingAddr, + askingAmountOrId, + ); + await expect( + Swaplace.connect(owner).createSwap(swap, { value: 69 }), + ).to.be.revertedWithCustomError(Swaplace, `InvalidValue`); + }); }); }); @@ -466,6 +538,69 @@ describe("Swaplace", async function () { allowed.address, ); }); + + it("Should be able to {acceptSwap} with native ethers", async function () { + await MockERC20.mint(owner.address, 1000); + await MockERC721.mint(allowed.address, 10); + + await MockERC20.connect(owner).approve(Swaplace.address, 1000); + await MockERC721.connect(allowed).approve(Swaplace.address, 10); + + const bidingAddr = [MockERC20.address]; + const bidingAmountOrId = [50]; + + const askingAddr = [ + MockERC20.address, + MockERC20.address, + MockERC20.address, + ]; + const askingAmountOrId = [50, 100, 150]; + + const valueToSend: BigNumber = ethers.utils.parseEther("0.5"); + + const expiry = (await blocktimestamp()) + 1000000; + + const config = await Swaplace.encodeConfig( + allowed.address, + expiry, + 0, + valueToSend.div(1e12), + ); + + const swap: Swap = await composeSwap( + owner.address, + config, + bidingAddr, + bidingAmountOrId, + askingAddr, + askingAmountOrId, + ); + + await expect( + await Swaplace.connect(owner).createSwap(swap, { + value: valueToSend, + }), + ) + .to.emit(Swaplace, "SwapCreated") + .withArgs( + await Swaplace.totalSwaps(), + owner.address, + allowed.address, + ); + + await expect( + await Swaplace.connect(allowed).acceptSwap( + await Swaplace.totalSwaps(), + receiver.address, + ), + ) + .to.emit(Swaplace, "SwapAccepted") + .withArgs( + await Swaplace.totalSwaps(), + owner.address, + allowed.address, + ); + }); }); context("Reverts when accepting Swaps", () => { @@ -553,6 +688,60 @@ describe("Swaplace", async function () { ), ).to.be.revertedWithCustomError(Swaplace, "InvalidAddress"); }); + + it("Should revert when wrong amount of ethers are sent by the {acceptee}", async function () { + await MockERC20.mint(owner.address, 1000); + await MockERC721.mint(allowed.address, 10); + + await MockERC20.connect(owner).approve(Swaplace.address, 1000); + await MockERC721.connect(allowed).approve(Swaplace.address, 10); + + const bidingAddr = [MockERC20.address]; + const bidingAmountOrId = [50]; + + const askingAddr = [ + MockERC20.address, + MockERC20.address, + MockERC20.address, + ]; + const askingAmountOrId = [50, 100, 150]; + + const valueToSend: BigNumber = ethers.utils.parseEther("0.5"); + + const expiry = (await blocktimestamp()) + 1000000; + + const config = await Swaplace.encodeConfig( + allowed.address, + expiry, + 1, + valueToSend.div(1e12), + ); + + const swap: Swap = await composeSwap( + owner.address, + config, + bidingAddr, + bidingAmountOrId, + askingAddr, + askingAmountOrId, + ); + + await expect(await Swaplace.connect(owner).createSwap(swap)) + .to.emit(Swaplace, "SwapCreated") + .withArgs( + await Swaplace.totalSwaps(), + owner.address, + allowed.address, + ); + + await expect( + Swaplace.connect(allowed).acceptSwap( + await Swaplace.totalSwaps(), + receiver.address, + { value: 69 }, + ), + ).to.be.revertedWithCustomError(Swaplace, "InvalidValue"); + }); }); }); @@ -578,6 +767,50 @@ describe("Swaplace", async function () { Swaplace.connect(owner).acceptSwap(lastSwap, receiver.address), ).to.be.revertedWithCustomError(Swaplace, `InvalidExpiry`); }); + + it("Should be able to {cancelSwap} and return ethers to {owner}", async function () { + const bidingAddr = [MockERC20.address]; + const bidingAmountOrId = [50]; + + const askingAddr = [ + MockERC20.address, + MockERC20.address, + MockERC20.address, + ]; + const askingAmountOrId = [50, 100, 150]; + + const valueToSend: BigNumber = ethers.utils.parseEther("0.5"); + + const currentTimestamp = (await blocktimestamp()) + 1000000; + const config = await Swaplace.encodeConfig( + zeroAddress, + currentTimestamp, + 0, + valueToSend.div(1e12), + ); + + const swap: Swap = await composeSwap( + owner.address, + config, + bidingAddr, + bidingAmountOrId, + askingAddr, + askingAmountOrId, + ); + + const lastSwap = await Swaplace.totalSwaps(); + await expect( + await Swaplace.connect(owner).createSwap(swap, { + value: valueToSend, + }), + ) + .to.emit(Swaplace, "SwapCreated") + .withArgs(await Swaplace.totalSwaps(), owner.address, zeroAddress); + + await expect(Swaplace.connect(owner).cancelSwap(lastSwap)) + .to.emit(Swaplace, "SwapCanceled") + .withArgs(lastSwap, owner.address); + }); }); context("Reverts when canceling Swaps", () => { From 247965f6df1a06d8b12d0cc588786142df3c1ce0 Mon Sep 17 00:00:00 2001 From: Deepu Date: Sat, 18 May 2024 15:23:16 +0530 Subject: [PATCH 2/3] Requested changes made #217 --- test/TestSwaplace.test.ts | 83 ++++++++++++++++++++++++++++++++++----- 1 file changed, 73 insertions(+), 10 deletions(-) diff --git a/test/TestSwaplace.test.ts b/test/TestSwaplace.test.ts index 414307b..8f99819 100644 --- a/test/TestSwaplace.test.ts +++ b/test/TestSwaplace.test.ts @@ -327,7 +327,7 @@ describe("Swaplace", async function () { .withArgs(await Swaplace.totalSwaps(), owner.address, zeroAddress); }); - it("Should be able to create a swap with native ethers", async function () { + it("Should be able to {createSwap} with native ethers sent by the {owner}", async function () { const bidingAddr = [MockERC20.address]; const bidingAmountOrId = [50]; @@ -339,7 +339,7 @@ describe("Swaplace", async function () { const askingAmountOrId = [50, 100, 150]; const valueToSend: BigNumber = ethers.utils.parseEther("0.5"); - + const currentTimestamp = (await blocktimestamp()) + 1000000; const config = await Swaplace.encodeConfig( zeroAddress, @@ -374,7 +374,7 @@ describe("Swaplace", async function () { ).to.be.revertedWithCustomError(Swaplace, `InvalidAddress`); }); - it("Should revert when the wrong amount of ethers is sent by the {owner}", async function () { + it("Should revert when the wrong amount of ethers are sent by the {owner}", async function () { const bidingAddr = [MockERC20.address]; const bidingAmountOrId = [50]; @@ -539,7 +539,7 @@ describe("Swaplace", async function () { ); }); - it("Should be able to {acceptSwap} with native ethers", async function () { + it("Should be able to {acceptSwap} with native ethers sent by the {owner}", async function () { await MockERC20.mint(owner.address, 1000); await MockERC721.mint(allowed.address, 10); @@ -555,11 +555,8 @@ describe("Swaplace", async function () { MockERC20.address, ]; const askingAmountOrId = [50, 100, 150]; - const valueToSend: BigNumber = ethers.utils.parseEther("0.5"); - const expiry = (await blocktimestamp()) + 1000000; - const config = await Swaplace.encodeConfig( allowed.address, expiry, @@ -576,6 +573,9 @@ describe("Swaplace", async function () { askingAmountOrId, ); + const balanceBefore: BigNumber = await receiver.getBalance(); + const expectedBalance: BigNumber = balanceBefore.add(valueToSend); + await expect( await Swaplace.connect(owner).createSwap(swap, { value: valueToSend, @@ -600,6 +600,72 @@ describe("Swaplace", async function () { owner.address, allowed.address, ); + + const balanceAfter: BigNumber = await receiver.getBalance(); + await expect(balanceAfter).to.be.equals(expectedBalance); + }); + + it("Should be able to {acceptSwap} with native ethers sent by the {acceptee}", async function () { + await MockERC20.mint(owner.address, 1000); + await MockERC721.mint(allowed.address, 10); + + await MockERC20.connect(owner).approve(Swaplace.address, 1000); + await MockERC721.connect(allowed).approve(Swaplace.address, 10); + + const bidingAddr = [MockERC20.address]; + const bidingAmountOrId = [50]; + + const askingAddr = [ + MockERC20.address, + MockERC20.address, + MockERC20.address, + ]; + const askingAmountOrId = [50, 100, 150]; + const valueToSend: BigNumber = ethers.utils.parseEther("0.5"); + const expiry = (await blocktimestamp()) + 1000000; + const config = await Swaplace.encodeConfig( + allowed.address, + expiry, + 1, + valueToSend.div(1e12), + ); + + const swap: Swap = await composeSwap( + owner.address, + config, + bidingAddr, + bidingAmountOrId, + askingAddr, + askingAmountOrId, + ); + + await expect(await Swaplace.connect(owner).createSwap(swap)) + .to.emit(Swaplace, "SwapCreated") + .withArgs( + await Swaplace.totalSwaps(), + owner.address, + allowed.address, + ); + + const balanceBefore: BigNumber = await owner.getBalance(); + const expectedBalance: BigNumber = balanceBefore.add(valueToSend); + + await expect( + Swaplace.connect(allowed).acceptSwap( + await Swaplace.totalSwaps(), + receiver.address, + { value: valueToSend }, + ), + ) + .to.emit(Swaplace, "SwapAccepted") + .withArgs( + await Swaplace.totalSwaps(), + owner.address, + allowed.address, + ); + + const balanceAfter: BigNumber = await owner.getBalance(); + await expect(balanceAfter).to.be.equals(expectedBalance); }); }); @@ -705,11 +771,8 @@ describe("Swaplace", async function () { MockERC20.address, ]; const askingAmountOrId = [50, 100, 150]; - const valueToSend: BigNumber = ethers.utils.parseEther("0.5"); - const expiry = (await blocktimestamp()) + 1000000; - const config = await Swaplace.encodeConfig( allowed.address, expiry, From c9f3df0093f36d33b010765e29a0339722447020 Mon Sep 17 00:00:00 2001 From: Deepu Date: Sun, 19 May 2024 15:20:33 +0530 Subject: [PATCH 3/3] removed white space #217 --- test/TestSwaplace.test.ts | 1 - 1 file changed, 1 deletion(-) diff --git a/test/TestSwaplace.test.ts b/test/TestSwaplace.test.ts index 8f99819..0f34d48 100644 --- a/test/TestSwaplace.test.ts +++ b/test/TestSwaplace.test.ts @@ -339,7 +339,6 @@ describe("Swaplace", async function () { const askingAmountOrId = [50, 100, 150]; const valueToSend: BigNumber = ethers.utils.parseEther("0.5"); - const currentTimestamp = (await blocktimestamp()) + 1000000; const config = await Swaplace.encodeConfig( zeroAddress,