Declare ERC20 in constructor fails

I made 2 versions of my code. One is returning an error and the other one is working.
However the 2 code are supposed to do the asme thing.

I’m working on Remix with Solidity 0.8.0.

Code 1 :

address tokenAddress;

//Setting the creator address on deployment
address creator;

constructor(address _tokenAddress) ERC721("RAMEN CARD", "RMC") {
    tokenAddress = _tokenAddress;
    creator = msg.sender;
}

function retourne() public view returns(address){
    return tokenAddress;
}

TokenInterface RMN = TokenInterface(tokenAddress);

function send() public {
    RMN.transferFrom(0x1002CA2d139962cA9bA0B560C7A703b4A149F6e0, 0x1002CA2d139962cA9bA0B560C7A703b4A149F6e0, 100);
}

Code 2 :

address tokenAddress = 0x031A4e59A6faDD01F0D129fD09E6B8d94268a46D;
TokenInterface RMN = TokenInterface(tokenAddress);

//Setting the creator address on deployment
address creator;

constructor() ERC721("RAMEN CARD", "RMC") {
    creator = msg.sender;
}

function send() public {
    RMN.transferFrom(0x1002CA2d139962cA9bA0B560C7A703b4A149F6e0, 0x1002CA2d139962cA9bA0B560C7A703b4A149F6e0, 100);
}

Code 2 is working and Code 1 is not.

Hey, how about changing the code as the following:

TokenInterface RMN;
constructor(address _tokenAddress) ERC721("RAMEN CARD", "RMC") {
    tokenAddress = _tokenAddress;
    creator = msg.sender;
    RMN = TokenInterface(tokenAddress);
}

1 Like

Thanks it worked :stuck_out_tongue:

1 Like