0

I have a contract that is deployed when I run a hardhat deploy.

pragma solidity >=0.7.0 <0.9.0;
contract SampleCode {
    uint256 public variable;

    constructor() public {
        variable = 5;
    }

    function getVariable() public view returns(uint256){
        return variable;
    }
}

And I deploy it with :

deployments.deploy('SampleCode', {...})

But when I try to call my function, I am unable to:

sampleCode = await deployments.get("SampleCode")
sampleCode.getVariable()

With:

Uncaught TypeError: sampleCode.getVariable is not a function

What gives?

1

await deployments.get("SampleCode") doesn't return a specific instance of a contract by design.

You need to get the actual contract after you get the deployment:

SampleCode = await deployments.get("SampleCode")
sampleCode = await ethers.getContractAt("SampleCode", SampleCode.address)
sampleCode.getVariable()

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.