Receiving contract-level events
Last updated
Last updated
Web3 comes with a new storage architecture dApps have to embrace. In traditional Web2 applications, users don't interact with the storage directly. It's typically hidden behind the API layer.
Blockchain is data storage that provides data we can trust, and that trust requires no middleman between the users and the changes in the data. In this setup, dApps have to observe the on-chain activity to display the relevant state to the end users. While this can be done by polling the network state using the CSPR.cloud REST API or CSPR.cloud Casper Node, CSPR.cloud is also prepared to assist dApps developers with observing the on-chain activity with its Streaming API.
To demonstrate it in action, let's create an NFT contract using the Testnet version of CSPR.studio and observe its activity on the network. Navigate to the My NFT collections page and create a new collection. To listen to the contract-level events emitted by our contract, we'll need to know its hash. Copy the hash of the deploy that created your NFT collection on the last step of the collection creation process and query the contract it deployed to the network using the Contract REST API:
Knowing the contract hash, we can subscribe to the contract-level events emitted by our newly created contract using the Contract-level events Streaming API. For this example, you'll need to install the wscat
NPM utility.
Now, let's go back to CSPR.studio and mint an NFT. Once the mint deploy is executed on the network, our WebSocket connection will receive a message containing the Mint
event data, as well as the token data encoded as a JSON string:
Note the extra
property that provides technical information, such as deploy_hash
, event_id
, and the deploy execution transform_id
, which can be used to link the event data to the corresponding deploy properly.