1.3.3 • Published 2 years ago

@make-software/ces-js-parser v1.3.3

Weekly downloads
-
License
ISC
Repository
github
Last release
2 years ago

CES JS Parser

@make-software/ces-js-parser parses contract-level events that follow the Casper Event Standard.

The library is built on top of the casper-js-sdk and operates on types defined by the SDK.

Prerequisites

  • Node.js Version: >12.22.10
  • OS: Linux, MacOS

Install

npm install --save @make-software/ces-js-parser

Usage

Here is an example of parsing CES events using ces-js-parser from a real Testnet deploy loaded with casper-js-sdk:

import { CasperServiceByJsonRPC } from 'casper-js-sdk';
import { Parser } from '@make-software/ces-js-parser';

const rpcClient = new CasperServiceByJsonRPC(
  `http://${process.env.NODE_ADDRESS}:7777/rpc`
);

const parser = await Parser.create(rpcClient, [
  '0640eb43bd95d5c88b799862bc9fb42d7a241f1a8aae5deaa03170a27ee8eeaa'
]);

const deploy = await rpcClient.getDeployInfo(
  'c1bb9ae27877f5ecf4ef71307e7ee3c403bcace065565c3645b81ec0a9bc8978'
);

const events = await parser.parseExecutionResult(
  deploy.execution_results[0].result as ExecutionResult
);

events.forEach(console.log);

API

CES JS Parser provides several public types and functions:

Parser

Parser that accepts a list of observed contracts and provides possibility to parse CES events out of deploy execution results

create

create is a async factory function that accepts CasperServiceByJsonRPC and contractHashes array and created a Parser instance:

ArgumentTypeDescription
rpcClientCasperServiceByJsonRPCInstance of the CasperServiceByJsonRPC client
contractHashesstring[]List of the observed contract hashes

Example

import { CasperServiceByJsonRPC } from 'casper-js-sdk';
import { Parser } from '@make-software/ces-js-parser';

const rpcClient = new CasperServiceByJsonRPC(
  `http://${process.env.NODE_ADDRESS}:7777/rpc`
);

const parser = await Parser.create(rpcClient, [
  '214a0e730e14501d1e3e03504d3a2f940ef32830b13fa47f9d85a40f73b78161'
]);

parseExecutionResults

parseExecutionResults method that accepts deploy execution results and returns ParseResult[]:

ArgumentTypeDescription
executionResultsExecutionResultsDeploy execution results

fetchContractSchemasBytes

fetchContractSchemasBytes method that accepts contract hash and return bytes representation of stored schema:

ArgumentTypeDescription
contractHashstringContract hash schema want to be fetched
stateRootHashstringState root hash of the data (takes latest if not provided)

parseSchemasFromBytes

parseSchemasFromBytes function that accepts raw CES schema bytes stored under the contract __events_schema URef and returns Schemas:

ArgumentTypeDescription
rawBytesUint8ArrayRaw contract schemas bytes

parseEventNameAndData

Function that accepts raw event bytes and contract event schemas and returns Event, that contains name and data:

ArgumentTypeDescription
rawEventstringRaw event bytes in hex
schemasSchemasThe list of contract schemas

Example

import { decodeBase16 } from 'casper-js-sdk';
import {
  parseSchemasFromBytes,
  parseEventNameAndData
} from '@make-software/ces-js-parser';

const schemas = parseSchemasFromBytes(rawBytes);
const rawEvent = decodeBase16('some real example here');

const event = parseEventNameAndData(rawEvent, schemas);

Event

Type that represents an event:

PropertyTypeDescription
contractHashUint8ArrayEvent ContractHash
contractPackageHashUint8ArrayEvent ContractHash
namestringEvent name
dataRecord<string,CLValue>Event Data

ParseResult

Value-object that represents a parse result. Contains error representing weather parsing was successful or not.

PropertyTypeDescription
errorstringParse result error
eventEventces Event

Schema

Schema is slice of PropertyDefinition - value-object that represents an schema item.

PropertyTypeDescription
PropertystringName of the schema property
ValueCLTypecasper CLType

Schemas

Schemas represent a map of event name and its Schema.

Tests

To run unit tests for the library, make sure you are in the root of the library:

npm run test

License

This project is licensed under the terms of the Apache-2.0 license.

Contributing

We welcome contributions from anyone interested in improving this project. Before getting started, please take a moment to read our contributing guidelines to learn more about how to contribute to this project, including how to report bugs, suggest enhancements, and submit pull requests.

We look forward to collaborating with you!

1.3.3

2 years ago

1.3.2

2 years ago

1.3.1

2 years ago

1.3.0

2 years ago

1.2.0

2 years ago

1.0.2

2 years ago

1.1.0

2 years ago

1.2.1

2 years ago

1.0.1

2 years ago

1.0.0

2 years ago