Solidity

如何使用之前部署的合約實例返回函式值?

  • May 15, 2018

我無法檢索為先前部署的合約實例儲存的變數內容,我只能獲取生成的目前合約實例的內容(在我的情況下,我試圖發送雜湊並檢索它,我只能檢索現值)。

myContract.methods.sendHash(files[0].hash).send({ from: '0xDA799A5F1c519feEE6C4915E9BFA2b86ff428FA7' }, function (error, transactionHash) {
   console.log("The data has been sent to the fuction ")
   console.log("data sent and the transaction hash is", transactionHash);

                    });

 myContract.deploy({
                data: '0x608060405234801561001057600080fd5b506102d7806100206000396000f30060806040526004361061004c576000357c0100000000000000000000000000000000000000000000000000000000900463ffffffff168063d13319c414610051578063dfb29935146100e1575b600080fd5b34801561005d57600080fd5b5061006661014a565b6040518080602001828103825283818151815260200191508051906020019080838360005b838110156100a657808201518184015260208101905061008b565b50505050905090810190601f1680156100d35780820380516001836020036101000a031916815260200191505b509250505060405180910390f35b3480156100ed57600080fd5b50610148600480360381019080803590602001908201803590602001908080601f01602080910402602001604051908101604052809392919081815260200183838082843782019150505050505091929192905050506101ec565b005b606060008054600181600116156101000203166002900480601f0160208091040260200160405190810160405280929190818152602001828054600181600116156101000203166002900480156101e25780601f106101b7576101008083540402835291602001916101e2565b820191906000526020600020905b8154815290600101906020018083116101c557829003601f168201915b5050505050905090565b8060009080519060200190610202929190610206565b5050565b828054600181600116156101000203166002900490600052602060002090601f016020900481019282601f1061024757805160ff1916838001178555610275565b82800160010185558215610275579182015b82811115610274578251825591602001919060010190610259565b5b5090506102829190610286565b5090565b6102a891905b808211156102a457600081600090555060010161028c565b5090565b905600a165627a7a723058206f2d78b72f2a66110648246dfeb8be76501828815b1edadf3354d47db771053e0029',
                arguments: [files[0].hash]                                              

                   })
                .send({
                           from: '0xDA799A5F1c519feEE6C4915E9BFA2b86ff428FA7',
                           gas: 1500000,
                           gasPrice: '30000000000000'

                    }, function (error, transactionHash) { "Transaction hash is this", console.log(transactionHash) })
                       .on('error', function (error) { console.log(error) })
                       .on('transactionHash', function (transactionHash) { console.log('3') })
                       .on('receipt', function (receipt) {
                           console.log("contract address is this", receipt.contractAddress) // contains the new contract address


                   })
                       .on('confirmation', function (confirmationNumber, receipt) { })
                       .then(function (newContractInstance) {
                           console.log("New contract instance is this", newContractInstance.options.address) // instance with the new contract address



                           // using the callback
                         myContract.methods.getHash().call({ from: newContractInstance.options.address }, function (error, result) {

                         console.log("The HASH RETRIVED FROM THE CONTACT IS",result);

                           });


                       });


               });

堅固性程式碼

pragma solidity ^0.4.17;

contract Contract {
string ipfsHash;

function sendHash(string x) public {
  ipfsHash = x;
}

function getHash() public view returns (string x) {

返回ipfsHash;

}

}

使用 async/await 您可以部署新實例並進行事務

async function deployAndQuery() {
   const MyContract = new web3.eth.Contract(ContractABI);

   // Assign the new instance to a separate variable
   const myContract = await MyContract.deploy({
       data: '0x60....'           // <- bytecode prefixed by 
   })
   .send({
       from: MyAccount,
       gas: 2000000,              // <- adjust the amount of gas used
       gasPrice: '30000000000'    // <- adjust gas price
   });

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

deployAndQuery();

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