2

This is my contract

pragma solidity ^'0.5.2';

contract MyContract {

    address[] public contracts;    
    event ContractCreated(address newAddress);

    constructor() public { }

    function createOtherContract() public {
        OtherContract contract = new OtherContract();
        address contractAddress=address(contract);
        contracts.push(contractAddress);
        emit ContractCreated(contractAddress);
    }
    function getDeployedContracts() public view returns (address[] memory) {
        return contracts;
    }
}

if I watch the events using this code

MyContract.methods
        .createOtherContract()
        .send({from: accounts[0]})
        .on('transactionHash', function (hash) {

            MyContract.events.allEvents({
                fromBlock: 4477390,
                toBlock: 'latest'
            }, (error, events) => { 
                if (error) { console.log("ERR", error); }
                console.log("Events", JSON.stringify(events)); 
            });


        });

I get this result enter image description here

If I use the contract method getDeployedContracts enter image description here

From the events data, I get a lot more zeros. I think this happens because the first result is a 32-byte HEX and the function results are 20-byte address but the type of the parameter is the same in both functions: address.

What's going on?

1

Ethereum event values are always stored using HEX encoding, therefore you need to know the type of the source value in order to decode it correctly.

Web3 provides some functionalities to facilitate the work, for instance, you could have done:

MyContract.methods
        .createOtherContract()
        .send({from: accounts[0]})
        .on('transactionHash', function (hash) {

            MyContract.events.allEvents({
                fromBlock: 4477390,
                toBlock: 'latest'
            }, (error, events) => { 
                if (error) { console.log("ERR", error); }
                eventDataArray = events.map((event) => {
                   web3.eth.abi.decodeParameters(['uint256', 'uint256', 
                   'uint256', 'address', 'address', 'string'], event.raw.data)
                });
                console.log("Events": eventDataArray);
            });
        });

See official docs here

In addition to that alternative, you could have fetch the event values directly from the tx receipt as follow (using latest web3js v1.0):

MyContract.methods
        .createOtherContract()
        .send({from: accounts[0]})
        .on('receipt', (receipt) => {
        // receipt example
    console.log(receipt);
    > {
        "transactionHash": "0x9fc76417374aa880d4449a1f7f31ec597f00b1f6f3dd2d66f4c9c6c445836d8b",
        "transactionIndex": 0,
        "blockHash": "0xef95f2f1ed3ca60b048b4bf67cde2195961e0bba6f70bcbea9a2c4e133e34b46",
        "blockNumber": 3,
        "contractAddress": "0x11f4d0A3c12e86B4b5F39B213F7E19D048276DAe",
        "cumulativeGasUsed": 314159,
        "gasUsed": 30234,
        "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": {
                ...
            },
            "MyMultipleEvent":[{...}, {...}] // If there are multiple of the same event, they will be in an array
        }
        })

Extracted from this example in web3js docs

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.