Filter Streams
In some cases you might want to filter the data you receive from the webhook. You can do this by adding a filter to the stream.
Filter | Function | Note | Example | Demo |
---|---|---|---|---|
or | either ... or ... | Need at least 2 filters | { "or" : [ {..filter1}, {...filter2} ]} | Monitor for Burn/Mint Tokens |
and | all filters must satisfy | Need at least 2 filters | { "and" : [ {..filter1}, {...filter2} ]} | Monitor ENS Name Registrations |
eq | checks for equality | { "eq": ["value", "1000"] } | ||
ne | checks for inequality | { "ne": ["address", "0x...325"] } | ||
lt | value is less than | Value must be a number | { "lt": ["amount", "50"] } | |
gt | value is greater than | Value must be a number | { "gt": ["price", "500000"] } | |
lte | value is less than or equal to | Value must be a number | { "lte": ["value", "100"] } | |
gte | value is greater than or equal to | Value must be a number | { "gte": ["value", "100"] } | Monitor for Burn/Mint Tokens |
in | value is in array | Must provide an array | { "in": ["city": ["berlin", "paris"]]} | Monitor specific NFTs |
nin | value is not in array | Must provide an array | { "nin": ["name": ["bob", "alice"]]} |
In some cases you might want to filter the data you receive from the webhook. You can do this by adding a filter to the stream. Important: You must add a (valid!) ABI of the event you want to filter! Otherwise the stream will not work.
Special variables that can be used in a filter
moralis_streams_contract_address
- for identifying current contract address that emits an event, example{eq: ['moralis_streams_contract_address', '0x0000000000000000000000000000000000000000']}
(note: the address has to be in lowercase)moralis_streams_chain_id
- for identifying current chain for current event, example{eq: ['moralis_streams_chain_id', '0x1']}
moralis_streams_possibleSpam
- for filtering onpossibleSpam
(read more about possibleSpam here) events, example{eq: [moralis_streams_possibleSpam: "false"]}
to filter out events that are possible spam
Special Variable | Info | Example | |
---|---|---|---|
moralis_streams_contract_address | For identifying current contract address that emits an event (note: the address has to be in lowercase) | {eq: ['moralis_streams_contract_address', '0x0000000000000000000000000000000000000000']} | |
moralis_streams_chain_id | For identifying current chain for current event | {eq: ['moralis_streams_chain_id', '0x1']} | |
moralis_streams_possibleSpam | For filtering on possibleSpam (read more about possibleSpam here) events | {eq: [moralis_streams_possibleSpam: "false"]} |
Here is an example of applying different filters for different contract addresses
moralis_streams_contract_address
variable name can be used to identify current contract address that emits current event
Note: the address is in lowercase
Example:
const filter = {
or: [
{
and: [
{
eq: ["moralis_streams_contract_address", "0x1"],
},
{
gte: ["value", 1000000000], // Example of USDT (6 Decimals)
},
],
},
{
and: [
{
eq: ["moralis_streams_contract_address", "0x2"],
},
{
gte: ["value", 1000000000000000000000], // Example of BUSD (18 Decimals)
},
],
},
],
};
Steps to add filter via Admin panel
- Go to streams page, add contract address(eg:USDC -
0xA0b86991c6218b36c1d19D4a2e9Eb0cE3606eB48
) to create a stream, add the following ABI and selectTransfer(address,address,uint256)
topic.
[
{
"anonymous": false,
"inputs": [
{
"indexed": true,
"internalType": "address",
"name": "from",
"type": "address"
},
{
"indexed": true,
"internalType": "address",
"name": "to",
"type": "address"
},
{
"indexed": false,
"internalType": "uint256",
"name": "value",
"type": "uint256"
}
],
"name": "Transfer",
"type": "event"
}
]
Your screen will look like below::
- Let's create the following filter example with UI: This will filter all transfers with value greater than 5000 USDC and less than 50000 USDC.
{
"and": [{ "gt": ["value", "5000000000"] }, { "lt": ["value", "50000000000"] }]
}
decimals on USDC contract is 6
5000 USDC
value will be5000000000
50000 USDC
value will be50000000000
Click on
Select topic to filter
and add your topic.Click on
Choose variable
and selectvalue
from the listReplace
Equal
withGreater than
and Enter5000000000
in the input fieldClick on
Add filter
and selectLess than
filter from the list.Click on
Choose variable
and selectvalue
from the list and Enter50000000000
in the input fieldClick on
Or
and replace it withAnd
Now click on Update and you have successfully added your filter.
Steps to add complex filters using Admin Panel
- Go to streams page, add contract address(eg:USDC -
0xA0b86991c6218b36c1d19D4a2e9Eb0cE3606eB48
) to create a stream, add the following ABI and selectTransfer(address,address,uint256)
topic.
[
{
"anonymous": false,
"inputs": [
{
"indexed": true,
"internalType": "address",
"name": "from",
"type": "address"
},
{
"indexed": true,
"internalType": "address",
"name": "to",
"type": "address"
},
{
"indexed": false,
"internalType": "uint256",
"name": "value",
"type": "uint256"
}
],
"name": "Transfer",
"type": "event"
}
]
Your screen will look like below::
- Let's create the following filter example with UI: This will filter all transfers where the
from
or theto
is the zero address and the amount is greater or equal to 10000 USDC.
{
"or": [
{
"and": [
{ "eq": ["from", "0x0000000000000000000000000000000000000000"] },
{ "gte": ["value", "10000000000"] }
]
},
{
"and": [
{ "eq": ["to", "0x0000000000000000000000000000000000000000"] },
{ "gte": ["value", "10000000000"] }
]
}
]
}
A zero address(
0x0000000000000000000000000000000000000000
) infrom
equals to Mint and into
equals to burn.decimals in USDC contract is 6
10000 USDC
value will be10000000000
Click on
Select topic to filter
and add your topic.Click on the delete icon.
Click on
Add filter
and selectAnd
filter from the list.Click on
Choose variable
and selectfrom
from the list and enter0x0000000000000000000000000000000000000000
in the input field.Click on the inner
Add filter
and selectGreater than and equal to
filter from the list.Click on
Choose variable
and selectvalue
from the list and enter10000000000
in the input field.Now click on the outer
Add filter
and selectAnd
filter from the list.Click on
Choose variable
and selectto
from the list and enter0x0000000000000000000000000000000000000000
in the input field.Click on the inner
Add filter
and selectGreater than and equal to
filter from the list.Click on
Choose variable
and selectvalue
from the list and enter10000000000
in the input field.Now click on Update and you have successfully added your filter.