0

I have used infura in my App.js to connect to the kovan network. Then I am creating an instance of the smart contract deployed on that network. Then I am calling a function on that smart contract which emits an event.

This returns a transaction hash but I want to access the event details emitted by that smart contract function. I thought I might use that transaction hash on kovan etherscan site and get the event details emitted but it didnot work. How do I access them?

I have sent a signed transaction through infura:

    const Tx = require('ethereumjs-tx').Transaction

    // connect to Infura node
    const web3 = new Web3(new Web3.providers.HttpProvider("//kovan infura key"));

    // the address that will send the test transaction
    const addressFrom = '//myaccountaddress'
    const privKey = '//my key'

    // the destination address
    const addressTo = '//contractaddress'

    function sendSigned(txData,cb) {
      const privateKey = new Buffer.from("privateKey", 'hex')
      const transaction = new Tx(txData)
      transaction.sign(privateKey)
      const serializedTx = transaction.serialize().toString('hex')
      web3.eth.sendSignedTransaction('0x' + serializedTx,cb)
    }

    const contract = new web3.eth.Contract(Contract.abi,Contract.address, {from: addressFrom,gasLimit: 3000000});
    const contractFunction = contract.methods.getDetails(0);

    const functionAbi = contractFunction.encodeABI();

    // get the number of transactions sent so far so we can create a fresh nonce
    web3.eth.getTransactionCount(addressFrom).then(txCount => {

      // construct the transaction data
      const txData = {
        nonce: web3.utils.toHex(txCount),
        gasLimit: web3.utils.toHex(25000),
        gasPrice: web3.utils.toHex(10e12), // 10 Gwei
        to: addressTo,
        from: addressFrom,
        data: functionAbi
      }

      // fire away!
       sendSigned(txData, function(err, result) {
      if (err) return console.log('error', err)
      console.log('sent', result)
    })

0

Logs are included in the transaction receipt.

Unless you use some wrapper around the contract to get the logs they will be encoded. Then you will have to use something like web3.api.abi.decodeLog.

| improve this answer | |
  • can you please explain in little bit more detail that what is happening over here and what exactly I need to do? – Shivam Raina Mar 29 at 20:09
  • @ShivamRaina From web3 sendSignedTransaction documentation the callback returns the transaction hash, it doesn't wait for the transaction to be mined. You have to use the on('receipt', ..). Etherscan without a contract being verified it will not process events, also in kovan txs processing might get delayed because it is a testnet. – Ismael Mar 29 at 22:44

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.