-
Notifications
You must be signed in to change notification settings - Fork 33
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
test: Created unit tests to implement native ethers transfer #217
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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"; | ||
|
@@ -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 () { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Add "sent by the owner" |
||
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"); | ||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Whitespace can be removed |
||
const expiry = (await blocktimestamp()) + 1000000; | ||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Whitespace can be removed |
||
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, | ||
); | ||
}); | ||
0xneves marked this conversation as resolved.
Show resolved
Hide resolved
|
||
}); | ||
|
||
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 () { | ||
0xneves marked this conversation as resolved.
Show resolved
Hide resolved
|
||
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"); | ||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Whitespace can be removed |
||
const expiry = (await blocktimestamp()) + 1000000; | ||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Whitespace can be removed |
||
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", () => { | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Use "by the owner" refer, like in line 377