In our fronts, until version v5 of ethers.js we did it this way:
contract.on("TokenPurchase", (from, buyer, value, idToken) => {
console.log(`The account ${buyer} has purchased the nft with id #${idToken}`);
});
As I have researched this does not work in version 6. As discussed here:
it is recommended to downgrade to version 5.
The use of async
is indifferent.
Do you know what is the new methodology in v6 to capture events in our fronts?
Hey, I did a quick test and this works:
const ethers = require("ethers");
const provider = ethers.getDefaultProvider();
const address = "0xa0b86991c6218b36c1d19d4a2e9eb0ce3606eb48"; // USDC
const abi = [
{
anonymous: false,
inputs: [
{
indexed: true,
internalType: "address",
name: "owner",
type: "address",
},
{
indexed: true,
internalType: "address",
name: "spender",
type: "address",
},
{
indexed: false,
internalType: "uint256",
name: "value",
type: "uint256",
},
],
name: "Approval",
type: "event",
},
];
const contract = new ethers.Contract(address, abi, provider);
contract.on("Approval", console.log);
I'm using ethers@6.7.1
It is very similar to how I do it but it does not work for me. Tomorrow I will see if there is any difference in the code.
my version is:": "^6.6.3"
hi @ernestognw .
The only essential difference with the code you have shared with me is the provider, do you think it can affect?
const ethersProvider = new ethers.BrowserProvider(window.ethereum);
const signer = await ethersProvider.getSigner();
const contract = new ethers.Contract(contractAddress, ABI, signer);
static calls I make like this:
const customHttpProvider = new ethers.JsonRpcProvider(url);
const provider = customHttpProvider;
const contract = new ethers.Contract(contractAddress, ABI, provider);
I get back the address of the called contract but not the event info…
I have capture with static provider.