Smart Contract Event Listener Server

Hi.

I want to build a simple server that monitors the events of one smart contract and logs them into the terminal.

I have used web3 npm module and programmed the logic of listening to smart contract events and logging them to the console.

When I do ā€œnpm startā€ which does ā€œnode server.jsā€, it immediately returns to the terminal. I was expecting it to hang there and keep logging received events.

How can I make the server keep running and monitoring for emitted events of the smart contract?

Hi,
Can you share the code from server.js?
How is that you donā€™t run node server.js directly?

server.js

import startRelay from "./relay";

startRelay();

relay.js

require("dotenv").config();

import {
    newTransfer
} from "./blockchain";

const start = () => {
    newTransfer((error, result) => {
        console.log(result.args);
    });
};

export default start;

blockchain.js

require("dotenv").config();

import Web3 from "web3";

const web3 = new Web3(new Web3.providers.HttpProvider(process.env.WEB3_PROVIDER_ADDRESS));
const bep20Abi = JSON.parse(process.env.BEP20_ABI);
const tokenContract = web3.eth.contract(bep20Abi).at(process.env.TOKEN_CONTRACT_ADDRESS);

export const newTransfer = (callback) => {
    tokenContract.Transfer((error, result) => callback(error, result));
};

How is that there is no startRelay string in relay.js?

In relay.js ā€œstartā€ function is default. I assume that will be called with startRelay() call. Please correct me if I am wrong.

What does this code do?

export const newTransfer = (callback) => {
    tokenContract.Transfer((error, result) => callback(error, result));
};

I assume it is web3 functionality. It is registering listener to ā€œTranferā€ events emitted from the token contract.

Iā€™m not familiar with this syntax, you can write one single script file and run it and see what errors you get on execution.

How would you do this? A simple server that monitors for the Tranfer events of the token smart contract and logs them.