Created
January 25, 2022 22:39
-
-
Save samlaf/ffbcb821adc552e246908a6bc2f2c2bb to your computer and use it in GitHub Desktop.
Listening/Filtering events using ethersjs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const { ethers } = require("hardhat"); | |
const main = async () => { | |
const yourContract = await ethers.getContractAt( | |
"YourContract", | |
"0x5FC8d32690cc91D4c39d9d3abcBD16989F875707" | |
); | |
// Note that we filter for SetPurpose events that have "filterme" as purpose | |
const filter = yourContract.filters.SetPurpose(null, "filterme"); | |
yourContract.on(filter, (sender, purpose, event) => { | |
console.log(event); | |
}); | |
}; | |
main(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// This is the basic scaffold-eth contract. Note the SetPurpose event :) | |
pragma solidity >=0.8.0 <0.9.0; | |
import "hardhat/console.sol"; | |
contract YourContract { | |
// `indexed` is necessary to filter! | |
event SetPurpose(address sender, string indexed purpose); | |
string public purpose = "Building Unstoppable Apps!!!"; | |
constructor() { | |
// what should we do on deploy? | |
} | |
function setPurpose(string memory newPurpose) public { | |
purpose = newPurpose; | |
console.log(msg.sender,"set purpose to",purpose); | |
emit SetPurpose(msg.sender, purpose); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment