Skip to content

Commit

Permalink
Merge pull request #16 from dev-protocol/fix-sbt-tokenids
Browse files Browse the repository at this point in the history
feat: make SBT start with token id 1.
  • Loading branch information
aggre authored Mar 7, 2024
2 parents c1d505a + b1c382a commit 109b98d
Show file tree
Hide file tree
Showing 4 changed files with 142 additions and 120 deletions.
24 changes: 20 additions & 4 deletions contracts/SBT.sol
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ contract SBT is ISBT, ERC721EnumerableUpgradeable {

/// @dev EOA with rights to allow(add)/disallow(remove) minter.
address private _minterUpdater;
/// @dev The counter to mint new NFTs and track supply.
uint256 private _tokenIdCounter;

/// @dev EOA with minting rights.
mapping(address => bool) private _minters;
Expand Down Expand Up @@ -81,23 +83,33 @@ contract SBT is ISBT, ERC721EnumerableUpgradeable {
uint256 tokenId,
bytes memory metadata
) external override onlyMinter {
require(tokenId < currentIndex(), "Token not found");
require(tokenId <= currentIndex(), "Token not found");
_setTokenURI(tokenId, metadata);
}

function mint(
address to,
bytes memory metadata
) external override onlyMinter returns (uint256 tokenId_) {
unchecked {
_tokenIdCounter++;
}
uint256 currentId = currentIndex();
_mint(to, currentId);
emit Minted(currentId, to);
_setTokenURI(currentId, metadata);
return currentId;
}

/**
* @dev See {IERC721Enumerable-totalSupply}.
*/
function totalSupply() public view override returns (uint256) {
return _tokenIdCounter;
}

function _tokenURI(uint256 tokenId) private view returns (string memory) {
require(tokenId < currentIndex(), "Token not found");
require(tokenId <= currentIndex(), "Token not found");

(
string memory name,
Expand Down Expand Up @@ -220,13 +232,17 @@ contract SBT is ISBT, ERC721EnumerableUpgradeable {
}

function currentIndex() public view override returns (uint256) {
return super.totalSupply();
return totalSupply();
}

function nextIndex() public view override returns (uint256) {
return currentIndex() + 1;
}

function metadataOf(
uint256 tokenId
) public view override returns (bytes memory) {
require(tokenId < currentIndex(), "Token not found");
require(tokenId <= currentIndex(), "Token not found");
return _sbtdata[tokenId];
}

Expand Down
6 changes: 6 additions & 0 deletions contracts/interfaces/ISBT.sol
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,12 @@ interface ISBT {
*/
function currentIndex() external view returns (uint256);

/*
* @dev get next id of token to be minted
* @return uint256 next token id
*/
function nextIndex() external view returns (uint256);

/*
* @dev get mapped metadata bytes of token id
* @param tokenId the token id of the NFT
Expand Down
Loading

0 comments on commit 109b98d

Please sign in to comment.