Deserialize ContractEvent

Hi there.

If you have the contract event as hex format and the schema for the event type, you should have the following options:

With @concordium/web-sdk version >7 (recommended)

import { ContractEvent } from '@concordium/web-sdk' // version 7 or higher

const EVENT_SCHEMA: string = ... // base64 representation of the schema for your contract event

const contractEventHex: string = ... // your contract event as hex
const contractEvent = ContractEvent.fromHexString(contractEventHex)
const parsedContractEvent = ContractEvent.parseWithSchemaTypeBase64(contractEvent)

With @concordium/node-sdk (the one used in the example linked)

import { deserializeTypeValue } from '@concordium/node-sdk'

const EVENT_SCHEMA: string = ... // base64 representation of the schema for your contract event

const contractEventHex: string = ... // your contract event as hex
const parsedContractEvent = deserializeTypeValue(Buffer.from(contractEventHex, 'hex'), Buffer.from(EVENT_SCHEMA, 'base64')

Both of these, however, require that you pass just the event portion of the contract schema. If you have access to the contract sourcecode, you can get this by building the module with the --schema-json-out argument:

cargo concordium build --schema-json-out ./out-dir

Hope this helps

2 Likes