feature: expose last token id function in the contract (#124)

* feat: add function to get the last token id

* test: add getLastTokenId tests
This commit is contained in:
Felipe Mendes 2023-02-20 11:44:59 -03:00 committed by GitHub
parent a86c30a8da
commit 70e9c14b05
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 102 additions and 0 deletions

View File

@ -9,6 +9,8 @@ import "@openzeppelin/contracts/utils/Strings.sol";
import "./FleekAccessControl.sol";
import "./util/FleekStrings.sol";
error ThereIsNoTokenMinted();
contract FleekERC721 is Initializable, ERC721Upgradeable, FleekAccessControl {
using Strings for uint256;
using Counters for Counters.Counter;
@ -173,6 +175,15 @@ contract FleekERC721 is Initializable, ERC721Upgradeable, FleekAccessControl {
return (app.name, app.description, app.externalURL, app.ENS, app.currentBuild, app.logo, app.color);
}
/**
* @dev Returns the last minted tokenId.
*/
function getLastTokenId() public view virtual returns (uint256) {
uint256 current = _appIds.current();
if (current == 0) revert ThereIsNoTokenMinted();
return current - 1;
}
/**
* @dev See {IERC165-supportsInterface}.
*/

View File

@ -0,0 +1,31 @@
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.17;
import "./TestBase.sol";
import "contracts/FleekERC721.sol";
contract Test_FleekERC721_GetLastTokenId is Test_FleekERC721_Base {
function setUp() public {
baseSetUp();
}
function test_cannotGetLastTokenIdWhenThereIsNoTokenMinted() public {
vm.expectRevert(ThereIsNoTokenMinted.selector);
CuT.getLastTokenId();
}
function test_getLastTokenIdForOneToken() public {
uint256 mint = mintDefault(deployer);
assertEq(mint, CuT.getLastTokenId());
}
function test_getLastTokenIdAfterThreeMints() public {
mintDefault(deployer);
mintDefault(deployer);
uint256 lastMint = mintDefault(deployer);
assertEq(lastMint, CuT.getLastTokenId());
}
}

View File

@ -0,0 +1,56 @@
import { loadFixture } from '@nomicfoundation/hardhat-network-helpers';
import { expect } from 'chai';
import { TestConstants, Fixtures, Errors } from './helpers';
import { ethers } from 'hardhat';
const { MintParams, Roles } = TestConstants;
describe('FleekERC721.GetLastTokenId', () => {
let fixture: Awaited<ReturnType<typeof Fixtures.default>>;
const mint = async () => {
const response = await fixture.contract.mint(
fixture.owner.address,
TestConstants.MintParams.name,
TestConstants.MintParams.description,
TestConstants.MintParams.externalUrl,
TestConstants.MintParams.ens,
TestConstants.MintParams.commitHash,
TestConstants.MintParams.gitRepository,
TestConstants.MintParams.logo,
TestConstants.MintParams.color
);
return response;
};
beforeEach(async () => {
fixture = await loadFixture(Fixtures.default);
});
it('should revert if there is no token minted', async () => {
const { contract } = fixture;
await expect(contract.getLastTokenId()).to.be.revertedWithCustomError(
contract,
Errors.ThereIsNoTokenMinted
);
});
it('should return the last token id', async () => {
const { contract } = fixture;
await mint();
expect(await contract.getLastTokenId()).to.be.equal(0);
});
it('should return the last token id after minting multiple tokens', async () => {
const { contract } = fixture;
await mint();
await mint();
await mint();
expect(await contract.getLastTokenId()).to.be.equal(2);
});
});

View File

@ -0,0 +1,3 @@
export const Errors = Object.freeze({
ThereIsNoTokenMinted: 'ThereIsNoTokenMinted',
});

View File

@ -1,4 +1,5 @@
export * from './constants';
export * from './fixture';
export * from './utils';
export * from './errors';
export * from './events';