[SOLVED] How i can get logs from stream using self-hosting server

Good day, on my self-hosting server i write a transaction (txs) to the database, but I also want to add to it the logs that came with it. How can I do this?

An example of a received object from the stream

{
    ...,
    "logs": [ ... ],
    "txs": [ ... ],
    "txsInternal": [],
    "erc20Transfers": [ ... ],
    "erc20Approvals": [],
    "nftTokenApprovals": [],
    "nftApprovals": {
        "ERC721": [],
        "ERC1155": []
    },
    "nftTransfers": [],
    "nativeBalances": []
}

@cryptokid, do you know of any way i can do this?

you want all the logs for that transaction?

the webhook request payload already has those logs?

Yes, I need all the logs that webhook sends me

And yes, the stream are already sending it to the server. But so far I’ve only worked with Parse.Cloud.BeforeSave, and it only has txs information, without the logs I need.

You could look in the code that processes the received webhook request. I don’t know in what conditions the logs will be added to the database.

Maybe you know how you can get these logs when they come to the server along with the rest of the data?
Surely there is a filter somewhere which filters out all unnecessary information leaving only txs

@cryptokid can you please point me to the file or code that processes it?

https://v1docs.moralis.io/moralis-dapp/getting-started/self-hosting-moralis-server/optional-features/streams

https://github.com/MoralisWeb3/Moralis-JS-SDK/blob/main/demos/parse-server-migration/src/index.ts

=>

https://github.com/MoralisWeb3/Moralis-JS-SDK/blob/0558226f4b883d856973608331df4702e71cbf91/packages/parseServer/src/streams/webbhook.ts

Is the package from the last link included in the parse-server-migration?

it is in a library that is imported in parse-server-migration

So, if I understand correctly, this code gets the data that comes to the hook and writes it to the database. But then why don’t I have Logs table even on Moralis server?
image

That is the code that add the data in the database, I don’t know why the logs are not there, you can try to add some logging by adding some console.log lines in that function from that imported library

Where i can find that function on parse-server-migration to add some console.log lines?

that will be probably in the node modules folder, try to search for that string with the name of the function

Since parse-server works with express you can use a middleware for your streams route and process the data yourself whenever you receive a request. You can access the request body using a middleware and save the logs in the database using the parse sdk.

@Iulian Could you give me an example please?

const doSomething = (req, res, next) => {
    console.log(req.body);
    // do something with the body 
    next();
};

app.use('/streams', doSomething, streamSync(parseServer, {
    apiKey: 'my-api-key',
    webhookUrl: '/streams',
}));
1 Like

@Iulian

I changed my code and now the post requests don’t work

All file:

import Moralis from 'moralis';
import express from 'express';
import cors from 'cors';
import config from './config';
import { parseServer } from './parseServer';
// @ts-ignore
import ParseServer from 'parse-server';
import http from 'http';
import { streamsSync } from '@moralisweb3/parse-server';

export const app = express();

Moralis.start({
  apiKey: config.MORALIS_API_KEY,
});

app.use(express.urlencoded({ extended: true }));
app.use(express.json());

app.use(cors());

const newLogic = (req: any, res: any, next: any) => {
  console.log(req);
  next();
};

app.use(config.STREAMS_WEBHOOK_URL, newLogic,
  streamsSync(parseServer, {
    apiKey: config.MORALIS_API_KEY,
    webhookUrl: config.STREAMS_WEBHOOK_URL,
  }),
);

app.use(`/server`, parseServer.app);

const httpServer = http.createServer(app);
httpServer.listen(config.PORT, async () => {
  if (config.USE_STREAMS) {
    // eslint-disable-next-line no-console
    console.log(
      `Moralis Server is running on port ${config.PORT} and stream webhook url ${config.SERVER_URL.slice(0, -7)}${config.STREAMS_WEBHOOK_URL}`,
    );
  } else {
    // eslint-disable-next-line no-console
    console.log(`Moralis Server is running on port ${config.PORT}.`);
  }
});
// This will enable the Live Query real-time server
ParseServer.createLiveQueryServer(httpServer);

@Iulian, ok, I found the problem in my code. How can I add this logs to the data being written to MongoDB?

import Moralis from 'moralis';
import express from 'express';
import cors from 'cors';
import config from './config';
import { parseServer } from './parseServer';
// @ts-ignore
import ParseServer from 'parse-server';
import http from 'http';
import { streamsSync } from '@moralisweb3/parse-server';

export const app = express();

Moralis.start({
  apiKey: config.MORALIS_API_KEY,
});

app.use(express.urlencoded({ extended: true }));
app.use(express.json());

app.use(cors());

const custom = (req: any, res: any, next: any) => {
  const { txs: transactions, logs } = req.body
  console.log({ transactions, logs });
  next();
}

app.use(
  config.STREAMS_WEBHOOK_URL,
  custom
)

app.use(streamsSync(parseServer, {
  apiKey: config.MORALIS_API_KEY,
  webhookUrl: config.STREAMS_WEBHOOK_URL,
}),
);

app.use(`/server`, parseServer.app);

const httpServer = http.createServer(app);
httpServer.listen(config.PORT, async () => {
  // eslint-disable-next-line no-console
  console.log(
    `Moralis Server is running on port ${config.PORT} and stream webhook url ${config.SERVER_URL.slice(0, -7)}${config.STREAMS_WEBHOOK_URL}`,
  );
});
// This will enable the Live Query real-time server
ParseServer.createLiveQueryServer(httpServer);

@cryptokid I created my own server to handle requests. It turned out to be easier and more profitable, because it is more flexible, which opens up more possibilities. Close thread please

2 Likes