I wanted to study the events on hardhat by following this lesson.
npx
version: 9.5.1
node
version: v12.22.9
Here's my contract code:
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.7;
contract SimpleStorage {
uint256 favoriteNumber;
event storedNumber(
uint256 indexed oldNumber,
uint256 indexed newNumber,
uint256 addedNumber,
address sender
);
function store(uint256 _favoriteNumber) public {
emit storedNumber(
favoriteNumber,
_favoriteNumber,
_favoriteNumber + favoriteNumber,
msg.sender
);
favoriteNumber = _favoriteNumber;
}
function retrieve() public view returns (uint256) {
return favoriteNumber;
}
}
Here's my script:
deploy_and_store.js
async function main() {
await hre.run("compile");
// We get the contract to deploy
const SimpleStorage = await hre.ethers.getContractFactory("SimpleStorage");
const simpleStorage = await SimpleStorage.deploy();
await simpleStorage.waitForDeployment();
const transactionResponse = await simpleStorage.store(1);
const transactionReceipt = await transactionResponse.wait();
console.log(transactionReceipt);
console.log(transactionReceipt.events[0]);
}
main().catch((error) => {
console.error(error)
process.exitCode = 1
})
When I execute the command:
npx hardhat run scripts/deploy_and_store.js
The output of console.log(transactionReceipt);
is:
ContractTransactionReceipt {
provider: HardhatEthersProvider {
_hardhatProvider: LazyInitializationProviderAdapter {
_providerFactory: [AsyncFunction (anonymous)],
_emitter: [EventEmitter],
_initializingPromise: [Promise],
provider: [BackwardsCompatibilityProviderAdapter]
},
_networkName: 'hardhat',
_blockListeners: [],
_transactionHashListeners: Map(0) {},
_eventListeners: []
},
to: '0x5FbDB2315678afecb367f032d93F642f64180aa3',
from: '0xf39Fd6e51aad88F6F4ce6aB8827279cffFb92266',
contractAddress: null,
hash: '0x6c7f5c327bda60c98ec15ac8a4911f48e8cac977f9fb94d7e89871f08933a05d',
index: 0,
blockHash: '0xf64f2050788f46fb3064a709a1cfffac792dbabcfcf0388583f048507d868963',
blockNumber: 2,
logsBloom: '0x00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000040000000000000000000000000000000000000000000000040000000000000000000000000040020000000000000000000800000000000000000000000000000000000000000000000000000000000000000000000000000000000000000200000000000000000000000000000000000000000000000000000000010000000000000000000040000000000000000000000000000000040000000000000000000060000000000000000000000000000000000000000000000000000000000001000000',
gasUsed: 46372n,
blobGasUsed: undefined,
cumulativeGasUsed: 46372n,
gasPrice: 1767032241n,
blobGasPrice: undefined,
type: 2,
status: 1,
root: undefined
}
and the output of console.log(transactionReceipt.events[0]);
is:
TypeError: Cannot read properties of undefined (reading '0')
at main (..\scripts\deploy_and_store.js:19:42)
means that I can't see the event.
Can someone tell me where I'm wrong?
Thank you very much. Best regards!