1

I have a function in solidity

function calculate() external returns (bool) {
    return true;
}

But when I call it with hardhat I get this:

{
  hash: '0xa22571900e096215e636e1ef104e7606bab71e112a4ffa5922ad70904ef11e36',
  type: 2,
  accessList: [],
  blockHash: null,
  blockNumber: null,
  transactionIndex: null,
  confirmations: 0,
  from: '0xf39Fd6e51aad88F6F4ce6aB8827279cffFb92266',
  gasPrice: BigNumber { _hex: '0x08', _isBigNumber: true },
  maxPriorityFeePerGas: BigNumber { _hex: '0x00', _isBigNumber: true },
  maxFeePerGas: BigNumber { _hex: '0x08', _isBigNumber: true },
  gasLimit: BigNumber { _hex: '0x6f72', _isBigNumber: true },
  to: '0x99dBE4AEa58E518C50a1c04aE9b48C9F6354612f',
  value: BigNumber { _hex: '0x00', _isBigNumber: true },
  nonce: 137,
  data: '0xca77ab65',
  r: '0x762dadb2acf9580c24fd95f94a907902c908164ba669ac41faecbdc095fae70e',
  s: '0x02c3b28c03c8b5f48df177b91bb7b7ad95eef6b8e3d9c80c641511eb1650175b',
  v: 1,
  creates: null,
  chainId: 1337,
  wait: [Function (anonymous)]
}

I want to receive a simple true value, what is this? Is my value here? How can I get it?

1 Answer 1

2

You need to let the Ethereum know that you're just returning a value and you're not modifying the state, which does not require a transaction. To do this, mark your function as a pure function:

function calculate() external pure returns (bool) {
  return true;
}

in case you need to change the state you can emit an event and read the value from that event

here you have an example

  let contractWithSigner = contract.connect(wallet);

  let tx = await contractWithSigner.setGreeting("whats up!");
    
  await tx.wait();

  contract.on("GreeterEvent", (setter, NewGreeting, event) => {
    console.log("New Greeting is", NewGreeting);
  })
3
  • 1
    yeah, but I need to modify the state, that's the deal
    – Paul
    Jun 30 at 13:15
  • 1
    I heard people solving this by emiting events
    – Paul
    Jun 30 at 13:16
  • 1
    I suggest you to edit your question in order to show exactly what you are trying, but if you need a value from a transaction you can emit an event I have edited my answer Jun 30 at 13:28

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.