Skip to content

Commit

Permalink
ON-468: Add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Daniel Lima committed Sep 22, 2023
1 parent e0be4eb commit a9bd462
Show file tree
Hide file tree
Showing 5 changed files with 90 additions and 38 deletions.
22 changes: 22 additions & 0 deletions contracts/mocks/MockERC1155.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
// SPDX-License-Identifier: CC0-1.0

pragma solidity 0.8.9;

import { ERC1155 } from "@openzeppelin/contracts/token/ERC1155/ERC1155.sol";

/**
* @title MockERC1155
* @dev Mock contract for testing purposes.
*/

contract MockERC1155 is ERC1155 {
constructor() ERC1155("") {}

function mint(address to, uint256 tokenId, uint256 amount, bytes memory data) external {
_mint(to, tokenId, amount, data);
}

function burn(address account, uint256 tokenId, uint256 amount) external {
_burn(account, tokenId, amount);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,11 @@ pragma solidity 0.8.9;
import { ERC20 } from "@openzeppelin/contracts/token/ERC20/ERC20.sol";

/**
* @title PaymentToken
* @title MockERC20
* @dev Mock contract for testing purposes.
*/

contract MockPaymentToken is ERC20 {
contract MockERC20 is ERC20 {
constructor() ERC20("PaymentToken", "PAY") {}

function mint(address to, uint256 amount) external {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,11 @@ pragma solidity 0.8.9;
import { ERC721 } from "@openzeppelin/contracts/token/ERC721/ERC721.sol";

/**
* @title MockNft
* @title MockERC721
* @dev Mock contract for testing purposes.
*/

contract MockNft is ERC721 {
contract MockERC721 is ERC721 {
constructor() ERC721("MockNft", "MOCK") {}

function mint(address to, uint256 tokenId) external {
Expand Down
78 changes: 52 additions & 26 deletions test/OriumMarketplace.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,9 @@ import { randomBytes } from 'crypto'

describe('OriumMarketplace', () => {
let marketplace: Contract
let nft: Contract
let paymentToken: Contract
let mockERC721: Contract
let mockERC20: Contract
let mockERC1155: Contract

// We are disabling this rule because hardhat uses first account as deployer by default, and we are separating deployer and operator
// eslint-disable-next-line @typescript-eslint/no-unused-vars
Expand Down Expand Up @@ -40,7 +41,7 @@ describe('OriumMarketplace', () => {
beforeEach(async () => {
// we are disabling this rule so ; may not be added automatically by prettier at the beginning of the line
// prettier-ignore
[marketplace, nft, paymentToken] = await loadFixture(deployMarketplaceContracts)
[marketplace, mockERC721, mockERC20, mockERC1155] = await loadFixture(deployMarketplaceContracts)
})

describe('Main Functions', async () => {
Expand All @@ -50,22 +51,41 @@ describe('OriumMarketplace', () => {
const tokenId = 1

beforeEach(async () => {
await nft.mint(lender.address, tokenId)
await mockERC721.mint(lender.address, tokenId)
const blockTimestamp = (await ethers.provider.getBlock('latest')).timestamp
rentalOffer = {
nonce: `0x${randomBytes(32).toString('hex')}`,
lender: lender.address,
borrower: borrower.address,
tokenAddress: nft.address,
tokenAddress: mockERC721.address,
tokenId,
feeTokenAddress: paymentToken.address,
feeTokenAddress: mockERC20.address,
feeAmountPerSecond: ethers.BigNumber.from(0),
deadline: blockTimestamp + ONE_DAY,
roles: [],
rolesData: [],
}
})
it('Should create a rental offer', async () => {
it('Should create a rental offer for ERC721', async () => {
await expect(marketplace.connect(lender).createRentalOffer(rentalOffer))
.to.emit(marketplace, 'RentalOfferCreated')
.withArgs(
rentalOffer.nonce,
rentalOffer.lender,
rentalOffer.borrower,
rentalOffer.tokenAddress,
rentalOffer.tokenId,
rentalOffer.feeTokenAddress,
rentalOffer.feeAmountPerSecond,
rentalOffer.deadline,
rentalOffer.roles,
rentalOffer.rolesData,
)
})
it('Should create a rental offer for ERC1155', async () => {
await mockERC1155.mint(lender.address, tokenId, 1, [])
rentalOffer.tokenAddress = mockERC1155.address
rentalOffer.tokenId = tokenId
await expect(marketplace.connect(lender).createRentalOffer(rentalOffer))
.to.emit(marketplace, 'RentalOfferCreated')
.withArgs(
Expand Down Expand Up @@ -117,6 +137,12 @@ describe('OriumMarketplace', () => {
'OriumMarketplace: Nonce already used',
)
})
it('Should NOT create a rental offer if NFT is neither ERC721 nor ERC1155', async () => {
rentalOffer.tokenAddress = ethers.constants.AddressZero
await expect(marketplace.connect(lender).createRentalOffer(rentalOffer)).to.be.revertedWith(
'OriumMarketplace: token address is not ERC1155 or ERC721',
)
})
})
})
describe('Core Functions', async () => {
Expand Down Expand Up @@ -160,25 +186,25 @@ describe('OriumMarketplace', () => {
await expect(
marketplace
.connect(operator)
.setMarketplaceFeeForCollection(nft.address, feeInfo.feePercentageInWei, feeInfo.isCustomFee),
.setMarketplaceFeeForCollection(mockERC721.address, feeInfo.feePercentageInWei, feeInfo.isCustomFee),
)
.to.emit(marketplace, 'MarketplaceFeeSet')
.withArgs(nft.address, feeInfo.feePercentageInWei, feeInfo.isCustomFee)
expect(await marketplace.feeInfo(nft.address)).to.have.deep.members([
.withArgs(mockERC721.address, feeInfo.feePercentageInWei, feeInfo.isCustomFee)
expect(await marketplace.feeInfo(mockERC721.address)).to.have.deep.members([
feeInfo.feePercentageInWei,
feeInfo.isCustomFee,
])
expect(await marketplace.marketplaceFeeOf(nft.address)).to.be.equal(feeInfo.feePercentageInWei)
expect(await marketplace.marketplaceFeeOf(mockERC721.address)).to.be.equal(feeInfo.feePercentageInWei)
})
it('Should NOT set the marketplace fee if caller is not the operator', async () => {
await expect(
marketplace
.connect(notOperator)
.setMarketplaceFeeForCollection(nft.address, feeInfo.feePercentageInWei, feeInfo.isCustomFee),
.setMarketplaceFeeForCollection(mockERC721.address, feeInfo.feePercentageInWei, feeInfo.isCustomFee),
).to.be.revertedWith('Ownable: caller is not the owner')
})
it("Should NOT set the marketplace fee if marketplace fee + creator royalty it's greater than 100%", async () => {
await marketplace.connect(operator).setCreator(nft.address, creator.address)
await marketplace.connect(operator).setCreator(mockERC721.address, creator.address)

const royaltyInfo: RoyaltyInfo = {
creator: creator.address,
Expand All @@ -188,7 +214,7 @@ describe('OriumMarketplace', () => {

await marketplace
.connect(creator)
.setRoyaltyInfo(nft.address, royaltyInfo.royaltyPercentageInWei, royaltyInfo.treasury)
.setRoyaltyInfo(mockERC721.address, royaltyInfo.royaltyPercentageInWei, royaltyInfo.treasury)

const feeInfo: FeeInfo = {
feePercentageInWei: toWei('95'),
Expand All @@ -197,7 +223,7 @@ describe('OriumMarketplace', () => {
await expect(
marketplace
.connect(operator)
.setMarketplaceFeeForCollection(nft.address, feeInfo.feePercentageInWei, feeInfo.isCustomFee),
.setMarketplaceFeeForCollection(mockERC721.address, feeInfo.feePercentageInWei, feeInfo.isCustomFee),
).to.be.revertedWith('OriumMarketplace: Royalty percentage + marketplace fee cannot be greater than 100%')
})
})
Expand All @@ -210,26 +236,26 @@ describe('OriumMarketplace', () => {
treasury: ethers.constants.AddressZero,
}

await expect(marketplace.connect(operator).setCreator(nft.address, creator.address))
await expect(marketplace.connect(operator).setCreator(mockERC721.address, creator.address))
.to.emit(marketplace, 'CreatorRoyaltySet')
.withArgs(nft.address, creator.address, royaltyInfo.royaltyPercentageInWei, royaltyInfo.treasury)
.withArgs(mockERC721.address, creator.address, royaltyInfo.royaltyPercentageInWei, royaltyInfo.treasury)

expect(await marketplace.royaltyInfo(nft.address)).to.have.deep.members([
expect(await marketplace.royaltyInfo(mockERC721.address)).to.have.deep.members([
royaltyInfo.creator,
royaltyInfo.royaltyPercentageInWei,
royaltyInfo.treasury,
])
})
it('Should NOT set the creator royalties if caller is not the operator', async () => {
await expect(marketplace.connect(notOperator).setCreator(nft.address, creator.address)).to.be.revertedWith(
'Ownable: caller is not the owner',
)
await expect(
marketplace.connect(notOperator).setCreator(mockERC721.address, creator.address),
).to.be.revertedWith('Ownable: caller is not the owner')
})
})

describe('Creator', async () => {
beforeEach(async () => {
await marketplace.connect(operator).setCreator(nft.address, creator.address)
await marketplace.connect(operator).setCreator(mockERC721.address, creator.address)
})
it("Should update the creator royalties for a collection if it's already set", async () => {
const royaltyInfo: RoyaltyInfo = {
Expand All @@ -241,10 +267,10 @@ describe('OriumMarketplace', () => {
await expect(
marketplace
.connect(creator)
.setRoyaltyInfo(nft.address, royaltyInfo.royaltyPercentageInWei, royaltyInfo.treasury),
.setRoyaltyInfo(mockERC721.address, royaltyInfo.royaltyPercentageInWei, royaltyInfo.treasury),
)
.to.emit(marketplace, 'CreatorRoyaltySet')
.withArgs(nft.address, creator.address, royaltyInfo.royaltyPercentageInWei, royaltyInfo.treasury)
.withArgs(mockERC721.address, creator.address, royaltyInfo.royaltyPercentageInWei, royaltyInfo.treasury)
})
it('Should NOT update the creator royalties for a collection if caller is not the creator', async () => {
const royaltyInfo: RoyaltyInfo = {
Expand All @@ -256,7 +282,7 @@ describe('OriumMarketplace', () => {
await expect(
marketplace
.connect(notOperator)
.setRoyaltyInfo(nft.address, royaltyInfo.royaltyPercentageInWei, royaltyInfo.treasury),
.setRoyaltyInfo(mockERC721.address, royaltyInfo.royaltyPercentageInWei, royaltyInfo.treasury),
).to.be.revertedWith('OriumMarketplace: Only creator can set royalty info')
})
it("Should NOT update the creator royalties for a collection if creator's royalty percentage + marketplace fee is greater than 100%", async () => {
Expand All @@ -269,7 +295,7 @@ describe('OriumMarketplace', () => {
await expect(
marketplace
.connect(creator)
.setRoyaltyInfo(nft.address, royaltyInfo.royaltyPercentageInWei, royaltyInfo.treasury),
.setRoyaltyInfo(mockERC721.address, royaltyInfo.royaltyPercentageInWei, royaltyInfo.treasury),
).to.be.revertedWith('OriumMarketplace: Royalty percentage + marketplace fee cannot be greater than 100%')
})
})
Expand Down
20 changes: 12 additions & 8 deletions test/fixtures/OriumMarketplaceFixture.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { Contract } from 'ethers'
/**
* @dev deployer and operator needs to be the first two accounts in the hardhat ethers.getSigners()
* list respectively. This should be considered to use this fixture in tests
* @returns [marketplace, nft, paymentToken] // TODO: TBD add rolesRegistry to the return
* @returns [marketplace, mockERC721, mockERC20, mockERC1155]
*/
export async function deployMarketplaceContracts() {
const [, operator] = await ethers.getSigners()
Expand All @@ -19,13 +19,17 @@ export async function deployMarketplaceContracts() {
])
await marketplace.deployed()

const NftFactory = await ethers.getContractFactory('MockNft')
const nft = await NftFactory.deploy()
await nft.deployed()
const MockERC721Factory = await ethers.getContractFactory('MockERC721')
const mockERC721 = await MockERC721Factory.deploy()
await mockERC721.deployed()

const PaymentTokenFactory = await ethers.getContractFactory('MockPaymentToken')
const paymentToken = await PaymentTokenFactory.deploy()
await paymentToken.deployed()
const MockERC20Factory = await ethers.getContractFactory('MockERC20')
const mockERC20 = await MockERC20Factory.deploy()
await mockERC20.deployed()

return [marketplace, nft, paymentToken] as Contract[]
const MockERC1155Factory = await ethers.getContractFactory('MockERC1155')
const mockERC1155 = await MockERC1155Factory.deploy()
await mockERC1155.deployed()

return [marketplace, mockERC721, mockERC20, mockERC1155] as Contract[]
}

0 comments on commit a9bd462

Please sign in to comment.