Skip to content

Commit

Permalink
closes #206
Browse files Browse the repository at this point in the history
  • Loading branch information
blackbeard002 committed May 10, 2024
1 parent e1832ed commit 18d04d1
Showing 1 changed file with 234 additions and 1 deletion.
235 changes: 234 additions & 1 deletion test/TestSwaplace.test.ts
Original file line number Diff line number Diff line change
@@ -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";
Expand Down Expand Up @@ -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", () => {
Expand All @@ -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`);
});
});
});

Expand Down Expand Up @@ -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", () => {
Expand Down Expand Up @@ -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");
});
});
});

Expand All @@ -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", () => {
Expand Down

0 comments on commit 18d04d1

Please sign in to comment.