Moralis Streams vs ChainSyncer
Tags: moralis, architecture, blockchain, web3
Published: 2023-02-26 19:18:15


Hey guys, welcome to my article on Moralis Streams and ChainSyncer - two tools that can make your lives easier when working with blockchain networks.

## What is Moralis Streams?

[Moralis Streams](https://moralis.io/streams) is a popular API for setting up Web3 streams. It's user-friendly, and it makes it easier to stream blockchain data into your application or Web3 project's backend. It's a great option for those who don't want to spend too much time building complex infrastructure to handle blockchain data.

## ChainSyncer - What is it?

[ChainSyncer](https://github.com/bytesbay/chain-syncer) is an open-source solution that allows you to easily set up blockchain listeners and catch on-chain events to write them immediately to your database. This is more flexible than storing all data on-chain or off-chain because it allows for a hybrid approach that simplifies development of decentralized applications.

## Comparison of ChainSyncer vs Moralis Streams

| Feature | ChainSyncer | Moralis Streams |
| --- | --- | --- |
| Custom smart contracts listener | ✅ | ✅ |
| Multiple contracts | ✅ | ✅ |
| 100% event delivery guarantee | ✅ | ✅ (more 💰) |
| Listening to Wallet Addresses | ❌ | ✅ |
| All possible EVM chains | ✅ | ❌ |
| Unlimited retries | ✅ | ❌ |
| Internal transactions listener | ❌ | ✅ |


## Comparison overview

Moralis Streams is great for those who want an easy-to-use solution, but it's not ideal for those who don't want to pay for the Business Plan. ChainSyncer is open-source and free, and it provides a custom smart contract listener, multiple contracts, 100% event delivery guarantee, and the ability to connect to any EVM-compatible blockchain. Additionally, ChainSyncer allows for unlimited retries, which is particularly useful in cases where there is a high volume of events.

Another key difference between ChainSyncer and Moralis Streams is the ability to listen to wallet addresses. While Moralis Streams allows you to listen to wallet addresses, ChainSyncer does not have this feature. However, ChainSyncer is compatible with all possible EVM chains, which means that you can use it with any EVM-compatible blockchain.

## Moralis Streams usage example

Here's a quick example of how to use Moralis Streams to set up a new stream and monitor wallet events on Ethereum and Polygon networks:

```typescript
import Moralis from "moralis";
import { EvmChain } from "@moralisweb3/evm-utils";

Moralis.start({
  apiKey: "YOUR_API_KEY",
});

const stream = {
  chains: [EvmChain.ETHEREUM, EvmChain.POLYGON], // list of blockchains to monitor
  description: "monitor Bobs wallet", // your description
  tag: "bob", // give it a tag
  webhookUrl: "https://YOUR_WEBHOOK_URL", // webhook url to receive events,
};

const newStream = await Moralis.Streams.add(stream);
const { id } = newStream.toJSON(); // { id: 'YOUR_STREAM_ID', ...newStream }

// Now we attach bobs address to the stream
const address = "0x68b3f12d6e8d85a8d3dbbc15bba9dc5103b888a4";

await Moralis.Streams.addAddress({ address, id });

// watch the webhook (assuming you are using express)
app.post("/webhook", async (req, res) => {
  const { payload } = req.body;
  console.log(payload);
  res.sendStatus(200);
});
```

In this example, we're monitoring Bob's wallet by setting up a new stream with the Moralis Streams API. We specify that we want to monitor events on both the Ethereum and Polygon networks. We also give the stream a tag and a webhook URL where we can receive the events. Finally, we add Bob's address to the stream and set up a webhook to receive events.

## ChainSyncer usage example

Now let's take a look at how you can use ChainSyncer to monitor on-chain events. Here's a basic example to get you started:
  
```typescript
import { ChainSyncer, InMemoryAdapter } from 'chain-syncer';
import abi from './abi.json';

const default_adapter = new InMemoryAdapter(); // change it to any other adapter

const contracts = {
  'NFTContract': {
    abi,
    address: '0xe9e7CEA3DedcA5984780Bafc599bD69ADd087D56',
    start_block: 27118825 // scanner will start from this block
  }
}

const syncer = new ChainSyncer(default_adapter, {
  
  block_time: 3500,

  network_id: 97,
  
  rpc_url: [
    'https://data-seed-prebsc-1-s1.binance.org:8545',
    'https://data-seed-prebsc-2-s1.binance.org:8545' // will be used as a fallback
  ],
  
  contractsResolver: contract_name => contracts[contract_name],
});

syncer.on('NFTContract.Transfer', async (
  { block_timestamp },
  from, 
  to, 
  token_id,
) => {
  const nft = await Nft.findOne({ id: token_id });

  if(!nft) { // postpone until nft created in our DB
    return false;
  }

  nft.owner = to;
  nft.updated_at = new Date(block_timestamp * 1000);
  await nft.save();
}));

await syncer.start();
```

With ChainSyncer, you can monitor and interact with on-chain events in real-time, allowing you to streamline your development process and make your application more efficient.

## Conclusion

To sum it up, if you're looking for an open-source and free tool to monitor and interact with on-chain events, ChainSyncer is the way to go. With ChainSyncer, you can even use a set of free RPCs to index blockchain data for absolutely free without trading reliability. This is especially useful for small/micro projects.

While Moralis Streams has some great features, it can be costly and is a centralized service. On the other hand, ChainSyncer is open-source, free, and offers more flexibility, reliability, and decentralization. By choosing ChainSyncer, you can build innovative, collaborative applications that align with the core philosophy of web3. It's important to choose tools and solutions that support decentralization and openness as we continue to build the web3 ecosystem. With ChainSyncer, you can do just that while building the next generation of decentralized applications.

---

There is still a war going on in Ukraine and at this moment the support of each and everyone is really important. Every dollar strongly brings Ukraine closer to victory 🇺🇦. [How can you help](https://aid.prytulafoundation.org/en/).