I can't work out what i'm doing wrong here. Why can't i get a simple uint to return from my contract when running using hardhat tests?


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

import "@openzeppelin/contracts/utils/math/SafeMath.sol";
import 'hardhat/console.sol';
import "@openzeppelin/contracts/access/AccessControl.sol";

contract Contract is AccessControl {
    using SafeMath for uint256;

    constructor() {
        _grantRole(DEFAULT_ADMIN_ROLE, msg.sender);

    function doIt() public returns (uint256) {
        console.log("Returning 1 from contract");
        return 1;



const { expect, assert } = require("chai");
const { ethers, upgrades } = require("hardhat");

describe("Contract", function () {  
  let acontract;

  beforeEach(async function () {
    // Deploying contract
    const aContract = await ethers.getContractFactory("Contract");
    acontract = await aContract.deploy();
    await acontract.deployed();
    console.log(`Contract deployed to ${acontract.address}`);

  describe("test contract", function () {
    it("doIt", async function () {
        console.log(JSON.stringify(await acontract.doIt()));


npx hardhat test test-test/test.js

    test contract
Contract deployed to 0x5FbDB2315678afecb367f032d93F642f64180aa3
Returning 1 from contract
      ✓ doIt

Any help appreciated. Thanks

1 Answer 1


When you call a non view/pure function, ethers creates and signs a transaction. Ethers then returns the transaction data and not the value returned by your solidity function (the value is actually encoded in the transaction hash). You can get the returned value by:

  • emitting an event,
  • changing the type of your function to view if it does not change the state of the contract, or to pure if it does not access state variables

A more detailed explanation is available here

  • 1
    Thank you so much! So, just so i'm understanding..... If the function isn't view/pure there is no way to get that return value unless i emit it as an event as part of the function?
    – stupid dev
    Aug 29, 2022 at 17:55
  • I don't think you can Sep 1, 2022 at 17:31

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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