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.

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:

curl -X 'GET' \
  'https://api.testnet.cspr.cloud/contracts?deploy_hash=149710648b6295b426fa31bb4b79b65374edd3e9ccd242b7d1bf6a7abb308f47' \
  -H 'accept: application/json' \
  -H 'authorization: 55f79117-fc4d-4d60-9956-65423f39a06a'
{
  "data": [
    {
      "contract_hash": "98f4b9ad7891d982e1b5f39e51dd332cae6f83f9615f3025af32796587bea527",
      "contract_package_hash": "19e6954b07d1b4d0341a6f73689c7fbc96959c33f4465c0e11c49b6bc850884a",
      "contract_type_id": 7,
      "contract_version": 1,
      "deploy_hash": "149710648b6295b426fa31bb4b79b65374edd3e9ccd242b7d1bf6a7abb308f47",
      "is_disabled": false,
      "timestamp": "2024-01-09T14:29:30Z"
    }
  ],
  "item_count": 1,
  "page_count": 1
}

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.

wscat -c 'wss://streaming.testnet.cspr.cloud/contract-events?contract_hash=98f4b9ad7891d982e1b5f39e51dd332cae6f83f9615f3025af32796587bea527' \
  -H 'authorization: 55f79117-fc4d-4d60-9956-65423f39a06a'

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:

{
  "data": {
    "contract_package_hash": "19e6954b07d1b4d0341a6f73689c7fbc96959c33f4465c0e11c49b6bc850884a",
    "contract_hash": "98f4b9ad7891d982e1b5f39e51dd332cae6f83f9615f3025af32796587bea527",
    "data": {
      "data": "{\"name\":\"User storage interaction in Web2\",\"description\":\"Users typically don\\u0027t interact directly with the data storage in Web2\",\"asset\":\"https://maritime.sealstorage.io/ipfs/bafybeicfojnysuueowj4mcparaxzkl5ckytq7ma5mqhhzju35r734ercra\"}",
      "recipient": "account-hash-1856e4a0b23c70b64e4509987680de0d99145fa0cdc71ad9b78760e18ff0deec",
      "token_id": "0"
    },
    "name": "Mint"
  },
  "action": "emitted",
  "extra": {
    "deploy_hash": "7d23a8b0cd927a7e54b6e55b267c8000016c3d0b3a9e97a2c281239c8a8f9120",
    "event_id": 0,
    "transform_id": 31
  },
  "timestamp": "2024-01-09T17:06:51.43910404Z"
}

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