Web3.js: methods.myMethod.send does not return contract events

Created on 29 May 2019  路  9Comments  路  Source: ChainSafe/web3.js

Description

The registration of the contract works well, except that after using the send method I don't have the expected result.

I'm trying to capture the event sent by my contract using

await factory.methods.register registrarQueja(this.state.description,this.state.name,this.state.mail,this.state.date).send({
            from: accounts[0]
        }).on('receipt', function(receipt){
            console.log(receipt);
        })

However, the console.log returns almost all the elements described in the documentation.
https://web3js.readthedocs.io/en/1.0/web3-eth-contract.html#id21

Expected behavior

 "events": {
            "MyEvent": {
                returnValues: {
                    myIndexedParam: 20,
                    myOtherIndexedParam: '0x123456789...',
                    myNonIndexParam: 'My String'
                },
                raw: {
                    data: '0x7f9fade1c0d57a7af66ab4ead79fade1c0d57a7af66ab4ead7c2c2eb7b11a91385',
                    topics: ['0xfd43ade1c09fade1c0d57a7af66ab4ead7c2c2eb7b11a91ffdd57a7af66ab4ead7', '0x7f9fade1c0d57a7af66ab4ead79fade1c0d57a7af66ab4ead7c2c2eb7b11a91385']
                },
                event: 'MyEvent',
                signature: '0xfd43ade1c09fade1c0d57a7af66ab4ead7c2c2eb7b11a91ffdd57a7af66ab4ead7',
                logIndex: 0,
                transactionIndex: 0,
                transactionHash: '0x7f9fade1c0d57a7af66ab4ead79fade1c0d57a7af66ab4ead7c2c2eb7b11a91385',
                blockHash: '0xfd43ade1c09fade1c0d57a7af66ab4ead7c2c2eb7b11a91ffdd57a7af66ab4ead7',
                blockNumber: 1234,
                address: '0xde0B295669a9FD93d5F28D9Ec85E40f4cb697BAe'
            },
            "MyOtherEvent": {
                ...
            },

Actual behavior

 {
  "transactionHash": "0xf927e7b6f2909ef6177f3bd929bd3198ad97eb208ba694996291d5b49f9b14a3",
  "transactionIndex": 0,
  "blockHash": "0xe3d9efbaaebdc8744a6d6c807c0fe52cafd4a8760d881c2d1eff4936a289c060",
  "blockNumber": 108,
  "from": "0x3377b79A500B6d4A8e2E391B86eBc7f88E99F2c4",
  "to": "0xE08ae26ea5694026E2bECFc6Af9725eB13bF52CD",
  "gasUsed": 754719,
  "cumulativeGasUsed": 754719,
  "contractAddress": null,
  "status": true,
  "logsBloom": "0x00000000010000000000000200000000000000000000000000000000000000000000000000000100000000000000000000000000000000000000000000001000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001000000000000000400000000000000000000000000000000000003000000000000000000000000000400000001000000000000000000000000000000000000000800000000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000",
  "v": "0x2d46",
  "r": "0x18e6ca87ab797193911eb00978b833bd82d77621a5fcc66dd1d64af764be00d1",
  "s": "0x3c14863b26938f597aa6e58d19f7b0b70b9a2561f69fb237c6974fa59d02f805",
  "events": {
    "0": {
      "raw": {
        "data": "0x0000000000000000000000000000000000000000000000000000000000000020000000000000000000000000000000000000000000000000000000000000000664616e69656c0000000000000000000000000000000000000000000000000000",
        "topics": [
          "0xabf569c29023498f9292dbb4f02d15221cbfeb36d71a45c619158c2f2c8b91f4"
        ]
      }
    },
    "1": {
      "raw": {
        "data": "0x0000000000000000000000000000000000000000000000000000000000000020000000000000000000000000000000000000000000000000000000000000000664616e69656c0000000000000000000000000000000000000000000000000000",
        "topics": [
          "0x4704303a8919b51a4c52e7794ce51a432970e0b441ef60bf5283fe4520dc5308"
        ]
      }
    }
  }
}

Steps to reproduce the behavior

await factory.methods.register registrarQueja(this.state.description,this.state.name,this.state.mail,this.state.date).send({
            from: accounts[0]
        }).on('receipt', function(receipt){
            console.log(receipt);
        })

Additional Info

contract CAPAFactory {
    address[] public contratos;

    event RegistroQueja(string nomre);

    function registrarQueja(string memory _descripcion, string memory _nombre, string memory _correo, string memory _fecha) public {
        address contrato = address(new Queja(_descripcion, _nombre, _correo, _fecha));
        contratos.push(contrato);
        emit RegistroQueja('test');
    }
    function listadoQuejas() public view returns (address[] memory) {
        return contratos;
    }

    function quejasRegistradas() public view returns (address[] memory) {
        return contratos;
    }
}

Versions

*web3.js: 1.0.0-beta.55
*nodejs: v12.2.0
*browser: chrome/firefox/opera
*ethereum node: Infura websocket/ganache

support

Most helpful comment

@DanielZambranoC async / await is syntactic sugar for promises.
The problem here is that something is calling the reject on the promise. This is throwing an error and you aren't trapping errors. As a result your code continues long past where the error actually is.
Finish putting awaits in front of all of those assignments where you are communicating with the blockchain because each of those is returning a promise when they return if they don't reject immediately.

Wrap all that await stuff inside a try / catch block and you'll find your culprit.

All 9 comments

Is it possible to reference a GitHub repository for seeing the complete JS code and the ABI of the contract?

Here is it
https://github.com/DanielZambranoC/Capachain/

Ive tried switching between Rinkeby Testnetwork and my local ganache, same issue

Basically this contract is what they call a Contract Factory, at the moment of the deployment I create a contract with the established scheme of data and at the same time I store in the factory to know all the contracts that have been generated.

What I want is that once a contract is generated, the address is issued for the event so I can show it to the user on screen and tell him... This is your report number. (Or send it by email)

var Web3 = require('web3')

const web3 = new Web3('http://127.0.0.1:8545')

json = require('./../../../build/contracts/permission.json');

const interface = json['abi'];
const bytecode = json['bytecode'];

async function setup(){
    var contract =  new web3.eth.Contract(interface,'0xBcf2D22144C11C20B31A7Aa84875414B252a92DB');
    const accounts = await  web3.eth.getAccounts();
    const admin = accounts[9];
    var receiver = accounts[1];
    var sender = accounts[3];

    const xl =  await contract.deploy({ data: bytecode })

    const bk =  xl.send({from: admin,gas: 4712388,gasPrice: 100000000000});
    //set data
    const bl = xl.methods.newTransact(receiver, 'dob');
    //get data
    const br = xl.methods.getTransactionCount().call();

    console.log(xl,bk);
}
setup();

now a transaction hash is generated for 1st send() then
(node:11340) UnhandledPromiseRejectionWarning: TypeError: Cannot read property 'newTransact' of undefined
at setup (/home/maddy/naisam/backend-server/cloudstorage/backend/routes/filesrec.js:20:27)
at processTicksAndRejections (internal/process/taskqueues.js:86:5)
(node:11340) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). (rejection id: 1)
(node:11340) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code
is the warning then its just waits for me to exit the program . is it some connecticvity issue or program logic issue please help

You need to wrap this in a try catch block.
You'll see that

const xl =  await contract.deploy({ data: bytecode })

Is throwing an error, you aren't trapping it, it's bubbling up somewhere else leaving xl undefined and then your code is going on it's merry way smashing through the rest of your assumptions about what should happen next.

Trap that error and see what's going on.

It has always been in a Try Catch block as you can see the code

Line 30
https://github.com/DanielZambranoC/Capachain/blob/master/pages/queja/nueva.js 

If I change the structure of the contract to no factory it works properly.

You need to wrap this in a try catch block.
You'll see that

const xl =  await contract.deploy({ data: bytecode })

Is throwing an error, you aren't trapping it, it's bubbling up somewhere else leaving xl undefined and then your code is going on it's merry way smashing through the rest of your assumptions about what should happen next.

Trap that error and see what's going on.

var Web3 = require('web3')

const web3 = new Web3('http://127.0.0.1:8545')

json = require('./../../../build/contracts/permission.json');

const interface = json['abi'];
const bytecode = json['bytecode'];

async function setup(){
    var contract =  new web3.eth.Contract(interface,'0xBcf2D22144C11C20B31A7Aa84875414B252a92DB');
    const accounts = await  web3.eth.getAccounts();
    const admin = accounts[9];
    var receiver = accounts[1];
    var sender = accounts[3];

    const xl =  await contract.deploy({ data: bytecode })

    const bk =  xl.send({from: admin,gas: 4712388,gasPrice: 100000000000});
    //set data
    const bl = xl.methods.newTransact(receiver, 'dob');
    //get data
    const br = xl.methods.getTransactionCount().call();

    console.log(xl,bk);
}
setup();

now a transaction hash is generated for 1st send() then
(node:11340) UnhandledPromiseRejectionWarning: TypeError: Cannot read property 'newTransact' of undefined
at setup (/home/maddy/naisam/backend-server/cloudstorage/backend/routes/filesrec.js:20:27)
at processTicksAndRejections (internal/process/taskqueues.js:86:5)
(node:11340) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). (rejection id: 1)
(node:11340) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code
is the warning then its just waits for me to exit the program . is it some connecticvity issue or program logic issue please help

This doesn't look like the example I shared.

@DanielZambranoC async / await is syntactic sugar for promises.
The problem here is that something is calling the reject on the promise. This is throwing an error and you aren't trapping errors. As a result your code continues long past where the error actually is.
Finish putting awaits in front of all of those assignments where you are communicating with the blockchain because each of those is returning a promise when they return if they don't reject immediately.

Wrap all that await stuff inside a try / catch block and you'll find your culprit.

Was this page helpful?
0 / 5 - 0 ratings