Skip to content

Commit

Permalink
refactor: use psi mint qantity functions
Browse files Browse the repository at this point in the history
  • Loading branch information
nattb8 committed May 29, 2024
1 parent 96af45d commit fbc5611
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 71 deletions.
14 changes: 0 additions & 14 deletions contracts/contracts/RunnerFox.sol
Original file line number Diff line number Diff line change
Expand Up @@ -27,18 +27,4 @@ contract RunnerFox is ImmutableERC721 {
feeNumerator
)
{}

// Allows minter to mint the next token to a specified address
function mintNextToken(address to) external onlyRole(MINTER_ROLE) returns (uint256) {
uint256 tokenId = ++_currentTokenId;
_mintByID(to, tokenId);
return tokenId;
}

// Allows minter to mint number of tokens specified to the address
function mintNextTokenByQuantity(address to, uint256 quantity) external onlyRole(MINTER_ROLE) {
for (uint256 i = 0; i < quantity; i++) {
_mintByID(to, ++_currentTokenId);
}
}
}
14 changes: 0 additions & 14 deletions contracts/contracts/RunnerSkin.sol
Original file line number Diff line number Diff line change
Expand Up @@ -27,18 +27,4 @@ contract RunnerSkin is ImmutableERC721 {
feeNumerator
)
{}

// Allows minter to mint the next token to a specified address
function mintNextToken(address to) external onlyRole(MINTER_ROLE) returns (uint256) {
uint256 tokenId = ++_currentTokenId;
_mintByID(to, tokenId);
return tokenId;
}

// Allows minter to mint number of tokens specified to the address
function mintNextTokenByQuantity(address to, uint256 quantity) external onlyRole(MINTER_ROLE) {
for (uint256 i = 0; i < quantity; i++) {
_mintByID(to, ++_currentTokenId);
}
}
}
44 changes: 23 additions & 21 deletions contracts/test/RunnerFox.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import { ethers } from "hardhat";
import { expect } from "chai";
import { RunnerFox, OperatorAllowlist__factory, RunnerFox__factory } from "../typechain-types";
import { BigNumber, constants } from "ethers";
import { token } from "../typechain-types/@openzeppelin/contracts";

describe("RunnerFox", function () {
let contract: RunnerFox;
Expand Down Expand Up @@ -31,6 +33,11 @@ describe("RunnerFox", function () {
await contract.grantMinterRole(owner.address);
});

async function getTokenId(id: number): Promise<BigNumber> {
const index = (await contract.mintBatchByQuantityThreshold()).sub(constants.One).add(BigNumber.from(id));
return index;
}

it("Should be deployed with the correct arguments", async function () {
expect(await contract.name()).to.equal("Immutable Runner Fox");
expect(await contract.symbol()).to.equal("IMRC");
Expand All @@ -43,54 +50,49 @@ describe("RunnerFox", function () {
it("Account with minter role should be able to mint next NFT", async function () {
const [owner, recipient] = await ethers.getSigners();

await contract.connect(owner).mintNextToken(recipient.address);
await contract.connect(owner).mintByQuantity(recipient.address, 1);
expect(await contract.balanceOf(recipient.address)).to.equal(1);
expect(await contract.totalSupply()).to.equal(1);
expect(await contract.ownerOf(1)).to.equal(recipient.address);

await contract.connect(owner).mintNextToken(recipient.address);
expect(await contract.balanceOf(recipient.address)).to.equal(2);
expect(await contract.ownerOf(2)).to.equal(recipient.address);
});

it("Account with minter role should be able to mint next NFTs in batch", async function () {
const [owner, recipient] = await ethers.getSigners();
expect(await contract.ownerOf(await getTokenId(1))).to.equal(recipient.address);

await contract.connect(owner).mintNextTokenByQuantity(recipient.address, 3);
await contract.connect(owner).mintByQuantity(recipient.address, 2);
expect(await contract.balanceOf(recipient.address)).to.equal(3);
expect(await contract.ownerOf(1)).to.equal(recipient.address);
expect(await contract.ownerOf(2)).to.equal(recipient.address);
expect(await contract.ownerOf(3)).to.equal(recipient.address);
expect(await contract.totalSupply()).to.equal(3);
expect(await contract.ownerOf(await getTokenId(2))).to.equal(recipient.address);
expect(await contract.ownerOf(await getTokenId(3))).to.equal(recipient.address);
await expect(contract.ownerOf(await getTokenId(4))).to.be.revertedWith('ERC721Psi: owner query for nonexistent token');
});

it("Account without minter role should not be able to mint NFTs", async function () {
const [_, acc1] = await ethers.getSigners();
const minterRole = await contract.MINTER_ROLE();
await expect(
contract.connect(acc1).mintNextTokenByQuantity(acc1.address, 1)
contract.connect(acc1).mintByQuantity(acc1.address, 1)
).to.be.revertedWith(
`AccessControl: account ${acc1.address.toLowerCase()} is missing role ${minterRole}`
);
});

it("Account which owns the NFT, should be able to burn the NFT", async function () {
const [owner, recipient] = await ethers.getSigners();
const tokenId = await getTokenId(1);

await contract.connect(owner).mintNextTokenByQuantity(recipient.address, 1);
await contract.connect(owner).mintByQuantity(recipient.address, 1);
expect(await contract.balanceOf(recipient.address)).to.equal(1);
expect(await contract.ownerOf(1)).to.equal(recipient.address);
expect(await contract.ownerOf(tokenId)).to.equal(recipient.address);

await contract.connect(recipient).burn(1);
await contract.connect(recipient).burn(tokenId);
expect(await contract.balanceOf(recipient.address)).to.equal(0);
});

it("Account which doesn't own the NFT, should not be able to burn the NFT", async function () {
const [owner, acc1, recipient] = await ethers.getSigners();
const tokenId = await getTokenId(1);

await contract.connect(owner).mintNextTokenByQuantity(recipient.address, 1);
await contract.connect(owner).mintByQuantity(recipient.address, 1);
expect(await contract.balanceOf(recipient.address)).to.equal(1);
expect(await contract.ownerOf(1)).to.equal(recipient.address);
expect(await contract.ownerOf(tokenId)).to.equal(recipient.address);

await expect(contract.connect(acc1).burn(1)).to.be.rejectedWith('IImmutableERC721NotOwnerOrOperator(1)');
await expect(contract.connect(acc1).burn(tokenId)).to.be.rejectedWith(`IImmutableERC721NotOwnerOrOperator(${tokenId})`);
});
});
46 changes: 24 additions & 22 deletions contracts/test/RunnerSkin.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { ethers } from "hardhat";
import { expect } from "chai";
import { OperatorAllowlist__factory, RunnerSkin, RunnerSkin__factory } from "../typechain-types";
import { BigNumber, constants } from "ethers";

describe("RunnerSkin", function () {
let contract: RunnerSkin;
Expand Down Expand Up @@ -31,6 +32,11 @@ describe("RunnerSkin", function () {
await contract.grantMinterRole(owner.address);
});

async function getTokenId(id: number): Promise<BigNumber> {
const index = (await contract.mintBatchByQuantityThreshold()).sub(constants.One).add(BigNumber.from(id));
return index;
}

it("Should be deployed with the correct arguments", async function () {
expect(await contract.name()).to.equal("Immutable Runner Skin");
expect(await contract.symbol()).to.equal("IMRS");
Expand All @@ -43,53 +49,49 @@ describe("RunnerSkin", function () {
it("Account with minter role should be able to mint next NFT", async function () {
const [owner, recipient] = await ethers.getSigners();

await contract.connect(owner).mintNextToken(recipient.address);
await contract.connect(owner).mintByQuantity(recipient.address, 1);
expect(await contract.balanceOf(recipient.address)).to.equal(1);
expect(await contract.ownerOf(1)).to.equal(recipient.address);

await contract.connect(owner).mintNextToken(recipient.address);
expect(await contract.balanceOf(recipient.address)).to.equal(2);
expect(await contract.ownerOf(2)).to.equal(recipient.address);
});

it("Account with minter role should be able to mint next NFTs in batch", async function () {
const [owner, recipient] = await ethers.getSigners();
expect(await contract.totalSupply()).to.equal(1);
expect(await contract.ownerOf(await getTokenId(1))).to.equal(recipient.address);

await contract.connect(owner).mintNextTokenByQuantity(recipient.address, 3);
await contract.connect(owner).mintByQuantity(recipient.address, 2);
expect(await contract.balanceOf(recipient.address)).to.equal(3);
expect(await contract.ownerOf(1)).to.equal(recipient.address);
expect(await contract.ownerOf(2)).to.equal(recipient.address);
expect(await contract.ownerOf(3)).to.equal(recipient.address);
expect(await contract.totalSupply()).to.equal(3);
expect(await contract.ownerOf(await getTokenId(2))).to.equal(recipient.address);
expect(await contract.ownerOf(await getTokenId(3))).to.equal(recipient.address);
await expect(contract.ownerOf(await getTokenId(4))).to.be.revertedWith('ERC721Psi: owner query for nonexistent token');
});

it("Account without minter role should not be able to mint NFTs", async function () {
const [_, acc1] = await ethers.getSigners();
const minterRole = await contract.MINTER_ROLE();
await expect(
contract.connect(acc1).mintNextTokenByQuantity(acc1.address, 1)
).to.be.rejectedWith(
contract.connect(acc1).mintByQuantity(acc1.address, 1)
).to.be.revertedWith(
`AccessControl: account ${acc1.address.toLowerCase()} is missing role ${minterRole}`
);
});

it("Account which owns the NFT, should be able to burn the NFT", async function () {
const [owner, recipient] = await ethers.getSigners();
const tokenId = await getTokenId(1);

await contract.connect(owner).mintNextTokenByQuantity(recipient.address, 1);
await contract.connect(owner).mintByQuantity(recipient.address, 1);
expect(await contract.balanceOf(recipient.address)).to.equal(1);
expect(await contract.ownerOf(1)).to.equal(recipient.address);
expect(await contract.ownerOf(tokenId)).to.equal(recipient.address);

await contract.connect(recipient).burn(1);
await contract.connect(recipient).burn(tokenId);
expect(await contract.balanceOf(recipient.address)).to.equal(0);
});

it("Account which doesn't own the NFT, should not be able to burn the NFT", async function () {
const [owner, acc1, recipient] = await ethers.getSigners();
const tokenId = await getTokenId(1);

await contract.connect(owner).mintNextTokenByQuantity(recipient.address, 1);
await contract.connect(owner).mintByQuantity(recipient.address, 1);
expect(await contract.balanceOf(recipient.address)).to.equal(1);
expect(await contract.ownerOf(1)).to.equal(recipient.address);
expect(await contract.ownerOf(tokenId)).to.equal(recipient.address);

await expect(contract.connect(acc1).burn(1)).to.be.rejectedWith('IImmutableERC721NotOwnerOrOperator(1)');
await expect(contract.connect(acc1).burn(tokenId)).to.be.rejectedWith(`IImmutableERC721NotOwnerOrOperator(${tokenId})`);
});
});

0 comments on commit fbc5611

Please sign in to comment.