Setup Counter to start at 1

Hello guys, I need my NFT collection start with token number 1.
Sorry if it's something to simple, I'm just starting with code.

// SPDX-License-Identifier: MIT
pragma solidity ^0.8.2;

import "@openzeppelin/contracts/token/ERC721/ERC721.sol";
import "@openzeppelin/contracts/access/Ownable.sol";
import "@openzeppelin/contracts/utils/Counters.sol";

contract COUNTERTEST is ERC721, Ownable {
    using Counters for Counters.Counter;

    Counters.Counter private _tokenIdCounter;

    constructor() ERC721("COUNTERTEST", "CT") {}

    function safeMint(address to) public onlyOwner {
        _safeMint(to, _tokenIdCounter.current());
        _tokenIdCounter.increment();
    }
}

Inside @openzeppelin/contracts/utils/Counters.sol" says:

library Counters {
    struct Counter {
        // This variable should never be directly accessed by users of the library: interactions must be restricted to
        // the library's function. As of Solidity v0.5.2, this cannot be enforced, though there is a proposal to add
        // this feature: see https://github.com/ethereum/solidity/issues/4637
        uint256 _value; // default: 0
    }

How can I set the default value at 1?
Thanks!

You can add _tokenIdCounter.increment() in the constructor to skip 0.

Do you mean inside safeMint or another part of the constructor?

    constructor() ERC721("COUNTERTEST", "CT") {
        _tokenIdCounter.increment();
    }
2 Likes

It works perfectly! Thank you so much :raised_hands:

1 Like