Smart Contract Events
Syncing historical smart contract events and getting notification when new events happen in real-time! Moralis puts the event data into your database in real time. You just have to provide the topic.

Why do we need to sync and watch smart contract events?

Smart contracts emit so-called events when something meaningful happens within the smart contract that the smart contract wants to communicate to dapps and other smart contracts. The developers of the smart contract decide when these events are emitted. For example - when somebody sends an ERC20 token - the token contract will emit a Transferevent containing all of the data about the transfer.
When you are building dapps it's very important for you to be able to listen to these events in real time. For example, if you are building an NFT Marketplace, your dapp needs to know when somebody creates a new auction or when somebody bids on an item - the smart contract will communicate these events by emitting events.
Luckily, Moralis Server has this functionality built in.
You can listen to smart contract events in real-time and also get all historic occurences of a specific event.
A tutorial demonstrating how to sync and watch smart contract event with Moralis.

The Graph vs Moralis For Smart Contract Indexing

Indexing Smart Contracts with The Graph vs Moralis

Webhooks, SMS, Emails, Custom Code Based on Smart Contract Events

This video explains how you can setup SMS, Email, Webhooks and run custom code when a smart contract even triggers.

Sync and Watch Contract Events

Defining

You can get all historical events from a specific smart contract topic and listen to new events in real-time. It requires the following information:
  • _description: _a short description to identify this sync job.
  • _topic: _The topic you will listen to, this could either be a definition or sha3:
    • bet(address,uint256,bool)
    • 0x20d932006281d267f137cd3921b707c2097e1f041b1291181cc3d0e86f449ebb
  • abi: If you provide the abi for this event, Moralis will automatically parse all the fields and populate the schema accordingly.
  • address: The address you will listen to for this event.
  • tableName: The name of the subclass that will be created in your Moralis database.
if you do not provide the _**abi **_then the following schema will be used:
1
{
2
"objectId": String,
3
"block_hash": String,
4
"topic0": String,
5
"topic1": String,
6
"block_timestamp": Date,
7
"ACL": ACL,
8
"data": String,
9
"updatedAt": Date,
10
"transaction_hash": String,
11
"transaction_index": Number,
12
"address": String,
13
"log_index": Number,
14
"createdAt": Date,
15
"block_number": Number
16
}
Copied!
This is an example of a valid abi:
1
{
2
"anonymous": false,
3
"inputs": [
4
{
5
"indexed": true,
6
"internalType": "address",
7
"name": "user",
8
"type": "address"
9
},
10
{
11
"indexed": true,
12
"internalType": "uint256",
13
"name": "bet",
14
"type": "uint256"
15
},
16
{
17
"indexed": true,
18
"internalType": "bool",
19
"name": "win",
20
"type": "bool"
21
},
22
{
23
"indexed": false,
24
"internalType": "uint8",
25
"name": "side",
26
"type": "uint8"
27
}
28
],
29
"name": "bet",
30
"type": "event"
31
}
Copied!
If the abi provided contains an "id" input, it will then be parsed to "uid" due to field "id" being reserved in the Moralis.Object

Table Name field limits

Table names are not allowed to contain nuneric characters (0 to 9)

Name field limits

Name fields that start with and _ don't work for now_, _like for example "name": "_side" will not work, and you'll have to change it to "name": "side" in order to make it work.

Historical Event Limit

If a sync job is created that would result in retrieving 500k or more historical events, then the "Sync_historical (Optional)option will be disabled and no historical data will be saved. It is possible to contact support to upgrade your account to enable saving it anyway but think hard about whether it's actually necessary before doing so. It's possible to handle the events in real-time without saving the data to the database.

Editing

To edit a sync job, click on View Details on the server where the plugin is installed and go to the Sync tab. There you will see all your sync jobs. Click the _Edit _button.
Since making any alterations is potentially a breaking change to the event schema, saving the changes requires providing a new tableName. Boo-urns! Yes, this is inconvenient, but how plugins work will be changing soon anyway when the custom plugin platform is released and this will then be reexamined (so at least there's that to look forward to!).
If a new table name is not provided you'll see the following error message:
This error message will also appear if you accidently name it the same as an existing plugin.

Event Filters

Imagine you only want to get events in your database based on some conditions.
Moralis gives you an easy way to define filters based on ABI variables.
For example the filter below will only sync event instances where the variable price is equal to 80000000000000000:
{"eq": ["price", "80000000000000000"]}
Below are some more filter examples. You can set conditions for any variable in the event.
If sender = 0x0 AND receiver = 0x0
1
{
2
"and": [
3
{ "eq": ["sender", "0x0"] },
4
{ "eq": ["receiver", "0x0"] }
5
]
6
}
Copied!
If sender = 0x0 OR receiver = 0x0
1
{
2
"or": [
3
{ "eq": ["sender", "0x0"] },
4
{ "eq": ["receiver", "0x0"] }
5
]
6
}
Copied!
If (sender = 0x0 AND amount = 1000 ) OR (receiver = 0x0 AND amount = 100 )
1
{
2
"or": [
3
{
4
"and": [
5
{ "eq": ["sender", "0x0"] },
6
{ "eq": ["amount", "1000"]}
7
]
8
},
9
{
10
"and": [
11
{ "eq": ["receiver", "0x0"] },
12
{ "eq": ["amount", "1000"]}
13
]
14
}
15
]
16
}
Copied!
If an event has wei denominated values you can use <name>_decimals fields to run comparisons (like greater than/less than):
1
{
2
"and": [
3
{ "eq": ["sender", "0x0"] },
4
{ "gt": ["amount_decimals", "1000"]}
5
]
6
}
Copied!

Watching from Code

Watch new smart contract event

Moralis Server has a special cloud function called watchContractEvent(options). You can call it using the master key.
Note: limit parameter is available only for Nitro servers (those one that have coreservices plugin). If limit parameter is not provided then the default value is 500000.
Note: at the moment the events created via code won't be seen in the admin UI, you can only see them in the database, we are working on connecting the admin UI properly
1
// code example of creating a sync event from cloud code
2
let options = {
3
chainId: "0x1",
4
address: "0x5c69bee701ef814a2b6a3edd4b1652cb9cc5aa6f",
5
topic: "PairCreated(address, address, address, uint256)",
6
abi: {
7
anonymous: false,
8
inputs: [
9
{
10
indexed: true,
11
internalType: "address",
12
name: "token0",
13
type: "address",
14
},
15
{
16
indexed: true,
17
internalType: "address",
18
name: "token1",
19
type: "address",
20
},
21
{
22
indexed: false,
23
internalType: "address",
24
name: "pair",
25
type: "address",
26
},
27
{
28
indexed: false,
29
internalType: "uint256",
30
name: "test",
31
type: "uint256",
32
},
33
],
34
name: "PairCreated",
35
type: "event",
36
},
37
limit: 500000,
38
tableName: "UniPairCreated",
39
sync_historical: false,
40
};
41
โ€‹
42
Moralis.Cloud.run("watchContractEvent", options, { useMasterKey: true });
Copied!

Unwatch existing smart contract event

1
// unwatch event that has TABLE_NAME as table in the database
2
let options = { tableName: "TABLE_NAME" };
3
Moralis.Cloud.run("unwatchContractEvent", options, { useMasterKey: true });
Copied!

Important Note

These features to watch/unwatch from code are still beta.
โ€‹Join our Discord to discuss the development!