2

I have a Smart Contract rps.

I want my express app to be able to listen to its events.

My express app:

require("dotenv").config();

const express = require("express");
const app = express();
const mongoose = require("mongoose");

mongoose.connect(process.env.DATABASE_URL, {useNewUrlParser: true});
const db = mongoose.connection;
db.on("error", error => console.error(error));
db.once("open", () => console.log("Connected to Database"));

app.use(express.json());

const gamesRouter = require("./routes/games");
app.use("/games", gamesRouter);

app.listen(3000, () => console.log("Server Started"));

// ------------- LISTEN TO EVENTS -------------------

const rps = require("./rps");

rps.events.allEvents()
.on('data', event => {
    console.log("NEW EVENT:", event);
})

The app works well. When I start it, I see this: enter image description here

However, when I make transactions on my Smart Contract, nothing new gets logged.

How can I listen to the events of the rps contract while my express app is running?

I have a React app running in parallel with the same code, dependencies, inheritances and ABIs. All events show up there. So I'm sure my contract is emitting events, and I'm pretty sure there's nothing wrong with my rps variable. The problem should be related to the overall implementation in Express.

1 Answer 1

2

I do not see you contract initialization, but take into account you need to use a web socket URL (wss://xxx) as Web3 provider.

const web3 = new Web3(WSS_ENDPOINT);

const contract = new web3.eth.Contract(
    CONTRACT_ABI,
    CONTRACT_ADDRESS
  );

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.