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 {
investmentCount++;
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')
console.log(oldTX)
})
})
})
The test returns something like this:
Contract: TronStack
deployment
TCW4HMVoRKpQUqDhFjxXmMQkd5Hse3JXh4
✓ deploys successfully
investments
047f8548dae22ea85dcd569b888e0ff2deaca1bd23b43460cb5759a736e5c6c7
[]
[
{
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)