我正在编写Tron智能合同,并在我的测试文件中提供了一个功能,它会发出一个事件。当尝试console.log日志时,我会得到一个空数组。这是因为交易延迟到shasta网络,还是还有什么东西我忽略了?我正在使用tronbox test --network shasta。
下面是我的合同的基本测试版本:
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);
}
}这是我的js测试文件:
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)
})
})
})测试返回如下所示:
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)发布于 2020-09-09 16:32:20
我注意到,如果您在shasta网络上使用tronweb创建一个事务,那么在shasta tronscan上看到相同的事务需要一点时间。当您还创建一个契约时,也会发生同样的情况。
因此,为了解决这个问题,每次与智能契约交互时,我只需等待5s:
let tx = await contract.createToken(tokenType.code, token.reference_identifier).send()
console.log(tx)
await setTimeout(async () => {
await tronWeb.getEventByTransactionID(tx).then((result) => {
console.log(result)
})
}, 5000)我不喜欢在我的代码中出现这些类型的超时,但是它可以工作,我只是希望您在mainnet中没有这些问题。
编辑:我注意到有时候5秒是不够的,所以我还添加了一个do,以确保始终接收事务事件。
// 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)
console.log(result)https://ethereum.stackexchange.com/questions/87272
复制相似问题