Skip to content
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

fix: add totalSupply, bump hardhat-upgrades version #94

Merged
merged 5 commits into from
Dec 27, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions contracts/token/IZNSDomainToken.sol
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@ interface IZNSDomainToken is IERC2981Upgradeable, IERC721Upgradeable {
uint96 defaultRoyaltyFraction
) external;

function totalSupply() external view returns (uint256);

function register(
address to,
uint256 tokenId,
Expand Down
18 changes: 16 additions & 2 deletions contracts/token/ZNSDomainToken.sol
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,11 @@ contract ZNSDomainToken is
*/
string private baseURI;

/**
* @dev Total supply of all tokens
*/
uint256 private _totalSupply;

/// @custom:oz-upgrades-unsafe-allow constructor
constructor() {
_disableInitializers();
Expand All @@ -55,6 +60,13 @@ contract ZNSDomainToken is
_setDefaultRoyalty(defaultRoyaltyReceiver, defaultRoyaltyFraction);
}

/**
* @notice Returns the total supply of all tokens
*/
function totalSupply() external view override returns (uint256) {
return _totalSupply;
}

/**
* @notice Mints a token with a specified tokenId, using _safeMint, and sends it to the given address.
* Used ONLY as a part of the Register flow that starts from `ZNSRootRegistrar.registerRootDomain()`
Expand All @@ -65,6 +77,7 @@ contract ZNSDomainToken is
* @param _tokenURI The tokenURI to be set for the token minted.
*/
function register(address to, uint256 tokenId, string memory _tokenURI) external override onlyRegistrar {
++_totalSupply;
_safeMint(to, tokenId);
_setTokenURI(tokenId, _tokenURI);
}
Expand All @@ -85,7 +98,7 @@ contract ZNSDomainToken is
function tokenURI(uint256 tokenId)
public
view
override(ERC721Upgradeable, ERC721URIStorageUpgradeable, IZNSDomainToken)
override(ERC721URIStorageUpgradeable, ERC721Upgradeable, IZNSDomainToken)
returns (string memory)
{
return super.tokenURI(tokenId);
Expand Down Expand Up @@ -168,9 +181,10 @@ contract ZNSDomainToken is
*/
function _burn(uint256 tokenId)
internal
override(ERC721Upgradeable, ERC721URIStorageUpgradeable)
override(ERC721URIStorageUpgradeable, ERC721Upgradeable)
{
super._burn(tokenId);
--_totalSupply;
}

/**
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@
"@openzeppelin/contracts-upgradeable": "4.9.3",
"@openzeppelin/contracts-upgradeable-400": "npm:@openzeppelin/[email protected]",
"@openzeppelin/defender-sdk": "^1.7.0",
"@openzeppelin/hardhat-upgrades": "2.4.3",
"@openzeppelin/hardhat-upgrades": "2.5.0",
"@semantic-release/git": "^10.0.1",
"@tenderly/hardhat-tenderly": "^2.0.1",
"@typechain/ethers-v6": "^0.5.1",
Expand Down
1 change: 0 additions & 1 deletion test/DeployCampaign.integration.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,6 @@ describe("DeployCampaign - Integration", () => {

let deployer;
let provider;
let zeroVaultAddress;

if (hre.network.name === "hardhat") {
deployer = deployAdmin;
Expand Down
28 changes: 28 additions & 0 deletions test/ZNSDomainToken.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,34 @@ describe("ZNSDomainToken", () => {
expect(await zns.domainToken.ownerOf(tokenId)).to.equal(caller.address);
});

it("Should increment the totalSupply when a domain is registered", async () => {
const tokenId = BigInt("1");

const supplyBefore = await zns.domainToken.totalSupply();

await zns.domainToken
.connect(mockRegistrar)
.register(caller.address, tokenId, randomTokenURI);

const supplyAfter = await zns.domainToken.totalSupply();
expect(supplyAfter).to.equal(supplyBefore + BigInt(1));
});

it("Should decrement the totalSupply when a domain is revoked", async () => {
const tokenId = BigInt("1");

const supplyBefore = await zns.domainToken.totalSupply();

await zns.domainToken
.connect(mockRegistrar)
.register(caller.address, tokenId, randomTokenURI);

await zns.domainToken.connect(mockRegistrar).revoke(tokenId);

const supplyAfter = await zns.domainToken.totalSupply();
expect(supplyAfter).to.equal(supplyBefore);
});

it("Should revert when registering (minting) if caller does not have REGISTRAR_ROLE", async () => {
const tokenId = BigInt("1");
await expect(
Expand Down
4 changes: 2 additions & 2 deletions test/gas/gas-costs.json
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
{
"Root Domain Price": "469205",
"Subdomain Price": "462907"
"Root Domain Price": "475352",
"Subdomain Price": "469054"
}
11 changes: 11 additions & 0 deletions test/helpers/register-setup.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import { getDomainHashFromEvent } from "./events";
import { distrConfigEmpty, fullDistrConfigEmpty, DEFAULT_TOKEN_URI, paymentConfigEmpty } from "./constants";
import { getTokenContract } from "./tokens";
import { ICurvePriceConfig } from "../../src/deploy/missions/types";
import { expect } from "chai";

const { ZeroAddress } = ethers;

Expand All @@ -29,6 +30,8 @@ export const defaultRootRegistration = async ({
tokenURI ?: string;
distrConfig ?: IDistributionConfig;
}) : Promise<ContractTransactionReceipt | null> => {
const supplyBefore = await zns.domainToken.totalSupply();

const tx = await zns.rootRegistrar.connect(user).registerRootDomain(
domainName,
domainContent, // Arbitrary address value
Expand All @@ -37,6 +40,9 @@ export const defaultRootRegistration = async ({
paymentConfigEmpty
);

const supplyAfter = await zns.domainToken.totalSupply();
expect(supplyAfter).to.equal(supplyBefore + BigInt(1));

return tx.wait();
};

Expand Down Expand Up @@ -92,6 +98,8 @@ export const defaultSubdomainRegistration = async ({
tokenURI ?: string;
distrConfig : IDistributionConfig;
}) => {
const supplyBefore = await zns.domainToken.totalSupply();

const tx = await zns.subRegistrar.connect(user).registerSubdomain(
parentHash,
subdomainLabel,
Expand All @@ -101,6 +109,9 @@ export const defaultSubdomainRegistration = async ({
paymentConfigEmpty
);

const supplyAfter = await zns.domainToken.totalSupply();
expect(supplyAfter).to.equal(supplyBefore + BigInt(1));

return tx.wait();
};

Expand Down
Loading