In Hellos.sol
, there is event initContract
after constructor is called. Here is the solidity code:
pragma solidity 0.7.0;
contract Hello {
address owner;
uint256 value;
event initContract(address _owner); //<<==msg.sender in event
constructor() {
owner = msg.sender;
emit initContract(owner);
}
//do something
}
In front end React native dapp with ethers 5.0.x
, here is the code to listen to the event initContract
after deployment:
const contract = await factory.deploy(); //<<==deploy Hello.sol
console.log("contract before mined: xxxxxxxxxxx",contract.address);
contract.on("initContract", async (to, amount, from) => { //<<== listen to event initContract
console.log("event initContract fired");
console.log("to : ", to); //EOA account to submit tx
console.log("amount : ", amount); //json object
console.log("from : ", from); //undefined
....
}
Here is the console output:
[Mon Dec 28 2020 10:52:42.771] LOG contract before mined: xxxxxxxxxxx 0x2b9534670c080E8B0B323e61524699C6efD589eB //<<==contract address
...
[Mon Dec 28 2020 10:52:47.120] LOG event initContract fired
[Mon Dec 28 2020 10:52:47.123] LOG to : 0xF89b29Fb7D2a2Ecf56A4BA57f1680aa9d47BE538 //<<==this is msg.sender
[Mon Dec 28 2020 10:52:47.125] LOG amount : {"address": "0x2b9534670c080E8B0B323e61524699C6efD589eB", "args": ["0xF89b29Fb7D2a2Ecf56A4BA57f1680aa9d47BE538"], "blockHash": "0xef52621b7fe9b8ff9402216c455cc57dc5005b57d1cca9a5368e640d6766bc19", "blockNumber": 255565, "data": "0x000000000000000000000000f89b29fb7d2a2ecf56a4ba57f1680aa9d47be538", "decode": [Function anonymous], "event": "initContract", "eventSignature": "initContract(address)", "getBlock": [Function anonymous], "getTransaction": [Function anonymous], "getTransactionReceipt": [Function anonymous], "logIndex": 0, "removeListener": [Function anonymous], "removed": false, "topics": ["0xcf9d4afa5954e14c65b6abfb613fbb48ee661f50cfb1c9e0b24b914d132dd504"], "transactionHash": "0x7f1a8741dd56544b0183421c2249d9207d1625e01489d0ff0d1efd7d1e2c351c", "transactionIndex": 0}
//<<== what is this? transaction object submitted?
[Mon Dec 28 2020 10:52:47.127] LOG from : undefined //<<==nothing returned on 3rd arg
What is in the return when listening to an event? Is the return the args in event definition, plus the transaction object submitted?