0

I'm currently working on an NFT project, using the openzeppelin ERC721 standard for the nfts.

When Inheriting the ERC721 NFT contract standard from openzeppelin you're required to pass in the "tokenName" and "tokenSymbol" for the nft in the constructor.

My issue with this is that in order to do this I need to hard-code the name of the nft and symbol into the contract before deploying. This code snippet is from an NFT contract that is going to be controlled from a different contract(A master contract), I need to be able to pass in information( e:g tokenName and tokenSymbol) from the master contract to the nft contract

hard-coding the tokenName and tokenSymbol into this nft contract is an issue.

Instead of hard coding, I want to be able to give the NFTs a dynamic name and symbol, when I deploy the contract from the master contract.

solidity version 0.8.4

This is a regular ERC721 constructor from openzeppelin; Token name "Metaverse Tokens" and symbol "METT" are hard-coded into the contract.

constructor (address marketPlaceAddress) ERC721 ("Metaverse Tokens", "METT") {
    contractAddress = marketPlaceAddress;
}

This is my modified code; Token name and symbol are collected as inputs from the constructor in the contract.

// import erc721 token standard import "@openzeppelin/contracts/token/ERC721/ERC721.sol";

// import extension to set token uri import "@openzeppelin/contracts/token/ERC721/extensions/ERC721URIStorage.sol";

contract NFT is ERC721URIStorage { // contract address of NFT dispenser address contractAddress; // keep track of tokens uint256 public tokenCount; string public tokenName; string public tokenSymbol;

constructor (address Dispenser, string memory _tokenName, string memory _tokenSymbol) 
ERC721 (  tokenName  ,  tokenSymbol) {
    // use Dispenser address to deploy NFT contract 
    contractAddress = Dispenser;
    // set the name of the token
    tokenName = _tokenName;
    // set the symbol of the token
    tokenSymbol = _tokenSymbol;
    // set the tokenCount to 0 on deployment 
    tokenCount = 0;
}

The issue with my code is that it doesn't pass in the name and symbol to the ERC721 contract that it inherits from, but the unmodified code passes them(name & symbol) in.

So my questions are as follows;

  1. How do you pass in a variable as a string to a constructor in solidity
  2. Am I on the right track with my code? and What do I need to change with my code to make this work
  3. Does the openZeppelin ERC721 contract not support this?
  4. Am I on the wrong track, and do you think I'm asking the wrong question

Please help

1
  • your construct has params _tokenName and _tokenSymbol so why do you then drop the underscores for the ERC721 params? I'm assuming you want those params passed through. as is, they'd be empty strings
    – sola24
    Jul 2 at 16:14

2 Answers 2

0

Try this:

    string tokenName = "TokenName";
    string tokenSymbol = "TKS";
     address contractAddress;

    constructor (address Dispenser,) ERC721 (  tokenName  ,  tokenSymbol) 
   {
          //your other codes here, excluding token name and symbol//
          contractAddress = Dispenser;

     }

Your approach does not work because a constructor function runs on deployment and your ERC721 inherited contract needs the token name and symbol right then and there for it to be set in it's own constructor as well since you imported it ..I'd advice you look up Erc721.sol

another approach would be

    constructor() ERC721("TokenName", "Symbol") {
        //your other codes here
    }
1
  • Hello, Thank you for responding However this doesn't solve my problem, Doing this is still hard-coding the tokenName and tokenSymbol. I guess I should explain further and update my question (sorry new to the platform). The code snippet is from an NFT contract that is going to be controlled by another contract, I need to be able to pass in the tokenName and tokenSymbol from the controlling contract, hence the reason why hard-coding the tokenName and tokenSymbol into this contract is an issue. Jul 3 at 19:55
0

Lol I think I just figured out the solution to this.

for anyone that might have the same issue

The solution to the issues is

constructor (address Dispenser, string memory _tokenName, string memory _tokenSymbol) 
**ERC721 (  _tokenName  ,  _tokenSymbol)**

by simple changing tokenName and tokenSymbol to _tokenName and _tokenSymbol

What I did wrong (reason for the previous question). I was trying to pass in a string variable instead of the string literal into constructor.

the logic of my code was to

. collect user input:

string memory _tokenName, string memory _tokenSymbol

. store input in a variable(tokenName and tokenSymbol):

tokenName = _tokenName, tokenSymbol = _tokenSymbol 

. pass input from variable into the ERC721 constructor:
ERC721 ( tokenName, tokenSymbol) which is wrong

whereas it should have been

. collect user input:

_tokenName, _tokeSymbol 

. pass input into the constructor:

ERC721 ( _tokenName, _tokenSymbol) right

By passing the user input directly into the ERC721 constructor I was able to solve the issue.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.