Dagger
Dagger is the best way to get realtime updates from Ethereum Blockchain. It provides a way for your DApps and Backend system to get Ethereum blockchain events i.e. transactions, token transfers, receipts and logs in realtime over websocket or socket.
We maintain infrastructure for reliable and scalable realtime events. @maticnetwork/dagger
is consumer library for Dagger project written in NodeJS. It uses Dagger server to get realtime updates from Ethereum Network.
Installation
Network
Ethereum Network
Mainnet
Kovan
Ropsten
Goerli
Matic Network
Mainnet
Mumbai Testnet
Example
- Lets first create a npm project.
- Now we can put following code snippet in
index.js
.
- Run
index.js
& you'll start receiving block number as soon as new block gets created.
API
new Dagger(url)
Create dagger object
url
is dagger server's address. Check network section for all available url values.
Example:
dagger.on(event, fn)
Subscribe to a topic
event
is aString
topic to subscribe to.event
wildcard characters are supported (+
- for single level and#
- for multi level)fn
-function (data, removed)
fn will be executed when event occurred:data
data from eventremoved
flag saying if data is removed from blockchain due to re-organization.
Example:
dagger.once(event, fn)
Same as on but will be fired only once.
Example:
dagger.off(event, fn)
Unsubscribe from a topic
event
is aString
topic to unsubscribe fromfn
-function (data, removed)
Example:
dagger.of(room)
Create room out of dagger. room
has to be one out of two values
latest
confirmed
room
object has following methods:
on
same as daggeron
once
same as daggeronce
off
same as daggeroff
dagger.end([force])
Close the dagger, accepts the following options:
force
: passing it to true will close the dagger right away. This parameter is optional.
dagger.contract(web3Contract)
Creates web3 contract wrapper to support Dagger.
- First create a web3 contract object.
- Now we'll create a dagger contract wrapper on it.
- Time to filter out contract events
- Watching contract events
- Stopping event watching
Events
Every event has a room ∈ {latest
, confirmed
}.
latest
: Events are fired immediately after block included in chain.confirmed
: Events are fired after 12 confirmations.
If you want to show updates on UI in your DApp, use latest
events. It will help to make UI/UX better and user friendly.
Use confirmed
events for irreversible tasks from server or on UI. Like sending email, notifications or allow user to do subsequent task on UI after one transaction gets confirmed.
Network Events
Ethereum event | When? | removed flag |
---|---|---|
block | For every new block created | Yes |
block.number | For every new block number created | |
block.hash | For every new block hash created | Yes |
block/number | When particular block in future included in chain | Yes |
addr/address /tx | On every new transaction for address | Yes |
addr/address /tx/out | On every new outgoing transaction for address | Yes |
addr/address /tx/in | On every new incoming transaction for address | Yes |
tx/txId | When given txId included in block | Yes |
tx/txId /success | When tx status is success (included in block) for txId | Yes |
tx/txId /fail | When tx fails (included in block) for txId | Yes |
tx/txId /receipt | When receipt is generated (included in block) for txId | Yes |
addr/contractAddress /deployed | When new contractAddress included in block | Yes |
log/contractAddress | When new log generated for contractAddress | Yes |
log/contractAddress /filter/topic1 /topic2 | When new log with topic1 and topic2 generated for contractAddress | Yes |
Dagger Events
Dagger Event | When? | args |
---|---|---|
connection.status | When connection status changes | value: Boolean |
Every event has to start with room:
block
For every new block
- latest
- confirmed
block.number
For every new block number
- latest
- confirmed
block.hash
For every new block hash
- latest
- confirmed
block/{number}
When particular block X, in future included in chain
- latest
- confirmed
addr/{address}/tx
On every new transaction for address
- latest
- confirmed
addr/{address}/tx/{dir}
dir
is transaction direction ∈ {in
, out
}. address
can be omitted to receive notification for any address.
- incoming
- outgoing
- wild card
On every new incoming transaction for address
- latest
- confirmed
tx/{txId}/{status}
status
is txId
's status ∈ {success
, fail
, receipt
}. It can be kept empty too i.e. resulting into tx/{txId}
, triggered when txId
gets included in block.
- any
- success
- fail
- receipt
When given txId
included in block
- latest
- confirmed
log/{contractAddress}
When log generated for contractAddress
- latest
- confirmed
log/{contractAddress}/filter/{topic0}/{topic1}/{topic2}
When new log with topic0
, topic1
& topic2
generated for contractAddress
Event names are case-sensitive.
address
,txId
andtopics
must be in lowercase.
Note: You can use wildcard for events too. There are two type of wildcards:
+
(for single) and#
(for multiple). Use with caution as it will fetch more data then you need, and can bombard with data to your DApp.
Test Dagger Server
This library consists woodendagger
executable which is test dagger server on your local machine. So you can test with TestRPC.
Please do not use woodendagger
in production. It's only for development purpose. It doesn't support removed
flag.
Support
If you have any queries, feedback or feature requests, feel free to reach out to us on Telegram
License
MIT