I'm working on a Tron smart contract and am providing a function in my tests file which emits to an event. When trying to console.log the logs I get an empty array. Is this because of lag for the transaction to hit the shasta network or is there something else I'm overlooking? I am using tronbox test --network shasta.

Here is a bare bones test version of my contract:

pragma solidity >=0.5.0 <0.7.0;

contract TronStack {
    uint public investmentCount;
    mapping (uint => Investment) public investments;

    struct Investment {
        uint id;
        uint amount;

    event InvestmentCreated (
        uint id,
        uint amount

    constructor() public {

    function invest(uint _amount) public {
        investments[investmentCount] = Investment(investmentCount, _amount);
        emit InvestmentCreated(investmentCount, _amount);


And here is my js test file:

const TronStack = artifacts.require('./TronStack.sol')
const TronWeb = require('tronweb')

contract('TronStack', (accounts) => {
    let tronstack

    before(async () => {
        tronstack = await TronStack.deployed()

    describe('deployment', async () => {
        it('deploys successfully', async () => {
            const address = await tronstack.address
            assert.notEqual(address, 0x0)
            assert.notEqual(address, '')
            assert.notEqual(address, null)
            assert.notEqual(address, undefined)

            addressTron = tronWeb.address.fromHex(address)
            console.log(addressTron) // returns contract address

    describe('investments', async () => {
        let result, event, investmentCount

        before(async () => {
            result = await tronstack.invest('10000000')
            event = await tronWeb.getEventByTransactionID(result)

            investmentCount = await tronstack.investmentCount()

        it('adds investment', async () => {
            assert.equal(investmentCount, 1)

            console.log(result) // returns tx id
            console.log(event) // returns empty array

            // using a previous tx id prints logs as intended
            oldTX = await tronWeb.getEventByTransactionID('0c960b698ffc889c1c7df81dc2f09eace47d50c73ffc662c2e3fb1671c4515c4')



The test returns something like this:

  Contract: TronStack
      ✓ deploys successfully
    block: 7796397,
    timestamp: 1599495159000,
    contract: 'TAoE543WH1fVerZRHK5ecsKU2VPckE5mLw',
    name: 'InvestmentCreated',
    transaction: '0c960b698ffc889c1c7df81dc2f09eace47d50c73ffc662c2e3fb1671c4515c4',
    result: { '0': '1', '1': '10000000', amount: '10000000', id: '1' },
    resourceNode: 'solidityNode'
      ✓ adds investment (133ms)

  2 passing (956ms)

I noticed that if you create a transaction with tronweb on shasta network it takes a bit to see the same transaction on shasta tronscan. The same happens when you also create a contract.

So to solve this I just wait 5s each time I'm interacting with the smart contract:

 let tx = await contract.createToken(tokenType.code, token.reference_identifier).send()

        await setTimeout(async () => {
            await tronWeb.getEventByTransactionID(tx).then((result) => {
        }, 5000)

I don't like to have those types of timeouts in my code, but it works and I just hope that you don't have those issues in mainnet.

EDIT: I noticed that sometimes 5 seconds wasn't enough, so I also added a do while to be sure to always receive the transaction event

        // Wait 5 seconds for the transaction to propagate
        await this.sleep(5000)
        // Also do a do while, so we are sure that we get the tx event
        let result
        do {
            result = await tronWeb.getEventByTransactionID(tx).then((result) => result)
        } while (result.length === 0)
| improve this answer | |

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.