Receiving contract-level events
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.
![](https://docs.cspr.cloud/~gitbook/image?url=https%3A%2F%2F2188842003-files.gitbook.io%2F%7E%2Ffiles%2Fv0%2Fb%2Fgitbook-x-prod.appspot.com%2Fo%2Fspaces%252FhFtHeQLio9cHTYRZdS6Y%252Fuploads%252Fgit-blob-8f3029d6535121aae9050901ec57f8618ce1e590%252Fuser-storage-interaction-in-web2.png%3Falt%3Dmedia&width=768&dpr=4&quality=100&sign=e9964c53&sv=1)
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.
![](https://docs.cspr.cloud/~gitbook/image?url=https%3A%2F%2F2188842003-files.gitbook.io%2F%7E%2Ffiles%2Fv0%2Fb%2Fgitbook-x-prod.appspot.com%2Fo%2Fspaces%252FhFtHeQLio9cHTYRZdS6Y%252Fuploads%252Fgit-blob-3ac2c8bc02bc457ccbba23770ba5147fab6244c7%252Fuser-storage-interaction-in-web3.png%3Falt%3Dmedia&width=768&dpr=4&quality=100&sign=e62aba9e&sv=1)
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.
Last updated