Solidity

如何檢索發送到先前部署的契約的數據?

  • May 15, 2018

我想訪問我在上一個合約實例期間通過函式發送的數據。是否可以通過合約實例或交易雜湊檢索數據?

您不需要重新部署合約,如果已經部署,您需要創建一個指向合約地址的實例

async function queryContract() {
   const ContractABI = [...];  // <-- Contract ABI
   const ContractAddress = "0x1234....";           // <-- Deployed contract address

   // Assign deployed address of a contract 
   const myContract = new web3.eth.Contract(ContractABI, ContractAddress);

   // Send a transaction
   await myContract.methods.sendHash("0x12341234")
   .send({
       from: MyAccount,
       gas: 2000000,
       gasPrice: '30000000000'
   });

   // Query modified data    
   const hash = await myContract.methods.getHash().call();

   console.log(hash)
}

queryContract();

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