1

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?

0

Try tme=einstance.call().getTime().

| improve this answer | |
  • I get the following error!: MismatchedABI: ("The function 'call' was not found in this contract's abi. ", 'Are you sure you provided the correct contract abi?') – DebaSen Feb 1 at 15:03
  • Sorry, the functions part is redundant. I mistakenly copied it from your question (where did you come up with that from anyway?). – goodvibration Feb 1 at 15:10
  • Because when I write this: tme=einstance.getTime().call() I get the following error: 'Contract' object has no attribute 'getTime' – DebaSen Feb 1 at 15:13
  • @DebaSen: How are you initializing the einstance object? – goodvibration Feb 1 at 15:26
0

It depends on how you have deployed your contract. If it was deployed correctly then you can call.

einstance.functions.getTime().call()

where einstance must be instance of web3._utils.datatypes.Contract.

Little brief on how to install your contract

Step1: Compile your contract and get the bytecode and abi. You can install a specific version of sold compiler on your machine .

For abi
solc --abi test.sol

for bytecode
solc --bin test.sol

The instantiate by copying your abi and bytecode

Test = w3.eth.contract(abi=abi, bytecode=bytecode)

Then you have to deploy your contract, I did it on localchain

w3 = Web3("http://localhost:8545")
w3.eth.defaultAccount = w3.eth.accounts[0]  
tx_hash = Test.constructor().transact()
tx_receipt = w3.eth.waitForTransactionReceipt(tx_hash)

Your deployed contract

deployed = w3.eth.contract(
            address=tx_receipt.contractAddress,
            abi=abi

Now you can call the functions on your deployed contract by

deployed.functions.getTime().call()
>>>1585998887

I used this contract

pragma solidity 0.6.4;

contract Test {

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

}

Make sure your solc compiler (Installed on your machine) is same as in pragma statement.

| improve this answer | |
0
// 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}`);
}
| improve this answer | |

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.