what is the best way to access smart contract public variables using HardHat?
Asked Answered
F

1

5

I want to test my smart contract but don't know the way to access the public variables after the contact was deployed? For example:

contract NFT is ERC721, Ownable {
using SafeMath for uint256;

bool public  isActive = False;

uint256 public startingIndexBlock;

uint256 public startingIndex;
}

I want to access the variable isActive I have the run.js to deploy the contract into the local environment and here is the code:

    async function main() {

    const [owner] = await hre.ethers.getSigners();

    console.log(
    "Deploying contracts with the account:",
    owner.address
    );

    const contractFactory = await hre.ethers.getContractFactory("ApeNFT");

    // Deploy contract with the correct constructor arguments
    const contract = await contractFactory.deploy("Mom", "MM" ,10000, 0);

    let isActive = await contract.isActive.toString();
    }

Here, I will never get the value of isActive. Does anyone know how to solve this issue?

Frum answered 5/2, 2022 at 4:27 Comment(0)
G
9

If your settings are correct, anything that you define as public, the solidity will assign a getter function.

// contract.isActive()
let isActive = await contract.isActive().toString();
console.log(isActive)
Gittens answered 5/2, 2022 at 5:15 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.