[Ethereum] How to call function from SmartContract

contract-developmentcontract-invocationsolidityweb3.py

I have a smart contract deployed having the following function.

function getTime() public view returns (uint){
       return now;
   }

However, when I try to call the function like this. ("einstance" being the instance of the contract):

tme=einstance.functions.getTime()
tme

I receive:

<Function getTime() bound to ()>

How can I retrieve the time from this?

Best Answer

// Web3 v1.2.6

const Web3 = require('web3');
const web3 = new Web3(new Web3.providers.HttpProvider('http://localhost:8545'));

const contractJSON = require('/path/to/build/contracts/Contract.json');
const contractInstance = new web3.eth.Contract(contractJSON.abi, 'CONTRACT_DEPLOYED_ADDRESS');

async function getTimeFromContract() {
  const time = await contractInstance.methods.getTime().call();
  console.log(`Time returned from the contract: ${time}`);
}
Related Topic