'Can I subscribe event fire from smart contract's events with NodeJS?
I just wrote smart contract function with fire event like this.
And create my node.js application with following code.
const express = require('express')
const Web3 = require('web3');
const app = express()
const PORT = process.env.PORT || 8080
const privateKey = "-- secret"
const { CONTRACT_ABI, CONTRACT_ADDRESS } = require('./config');
const web3 = new Web3('https://data-seed-prebsc-1-s1.binance.org:8545');
const contract = new web3.eth.Contract(CONTRACT_ABI, CONTRACT_ADDRESS);
app.get('/send_transaction', async (req, res) => {
const reciept = await send(web3, privateKey, contract)
res.send(reciept);
});
contract.events.EndedRound({
fromBlock: 0
}, function(error, event) { console.log(event); })
.on('data', function(event) {
console.log(event) // no event coming here.
})
async function send(web3, privateKey, contract) {
const account = web3.eth.accounts.privateKeyToAccount(privateKey).address;
const transaction = contract.methods.endRound(1651799819, [1], Web3.utils.toWei("100"));
const options = {
to: transaction._parent._address,
data: transaction.encodeABI(),
gas: await transaction.estimateGas({ from: account })
};
const signed = await web3.eth.accounts.signTransaction(options, privateKey);
const receipt = await web3.eth.sendSignedTransaction(signed.rawTransaction);
return receipt;
}
app.listen(PORT, () => {
console.log(`Server is running on port : ${PORT}`)
})
module.exports = app
But after I call function in smart contract which fire event EndedRound, My nodejs app not receive any event at all.
Is it possible to do that?
Solution 1:[1]
The event in the smart contract is 'EndRound' and the one in your express server is 'EndedRound'.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | NikolaiSch |