Solidity

如何從 SmartContract 呼叫函式?

  • May 23, 2021

我部署了一個具有以下功能的智能合約。

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

但是,當我嘗試像這樣呼叫函式時。(“einstance”是契約的實例):

tme=einstance.functions.getTime()
tme

我收到:

<Function getTime() bound to ()>

我怎樣才能從中檢索時間?

// 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}`);
}

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

引用自:https://ethereum.stackexchange.com/questions/79510