1- import { CLValue , decodeBase16 , matchByteParserByCLType } from 'casper-js-sdk' ;
2- import { WithRemainder } from './casper/types' ;
1+ import { CLValue , Hash , IResultWithBytes } from 'casper-js-sdk' ;
2+ import { toBytesString } from "casper-js-sdk/dist/types/ByteConverters" ;
3+ import { CLValueParser } from "casper-js-sdk/dist/types/clvalue/Parser" ;
34
4- import {
5- parseBytesWithRemainder ,
6- parseCLValueFromBytesWithRemainder ,
7- } from './casper/utils' ;
85import { Schema , Schemas } from './schema' ;
96
107const EVENT_PREFIX = 'event_' ;
118
129export interface Event {
1310 name : string ;
14- contractHash : Uint8Array | null ;
15- contractPackageHash : Uint8Array | null ;
11+ contractHash : Hash | null ;
12+ contractPackageHash : Hash | null ;
1613 data : Record < string , CLValue > ;
1714}
1815
1916export function parseEventNameWithRemainder (
2017 rawEvent : Uint8Array ,
21- ) : WithRemainder < string > {
22- const eventNameWithRemainder = parseBytesWithRemainder ( rawEvent ) ;
18+ ) : IResultWithBytes < string > {
19+ const eventNameWithRemainder = CLValueParser . fromBytesWithType ( rawEvent ) ;
2320
2421 const eventNameWithPrefix = new TextDecoder ( ) . decode (
25- eventNameWithRemainder . data ,
22+ eventNameWithRemainder . result . bytes ( ) ,
2623 ) ;
2724
2825 if ( ! eventNameWithPrefix . startsWith ( EVENT_PREFIX ) ) {
@@ -32,8 +29,8 @@ export function parseEventNameWithRemainder(
3229 const eventName = eventNameWithPrefix . replace ( 'event_' , '' ) ;
3330
3431 return {
35- data : eventName ,
36- remainder : eventNameWithRemainder . remainder ,
32+ result : eventName ,
33+ bytes : eventNameWithRemainder . bytes ,
3734 } ;
3835}
3936
@@ -44,28 +41,28 @@ export function parseEventNameAndData(
4441 name : string ;
4542 data : Record < string , CLValue > ;
4643} {
47- const event = decodeBase16 ( rawEvent ) ;
44+ const event = toBytesString ( rawEvent ) ;
4845
49- const clValueWithRemainder = parseCLValueFromBytesWithRemainder ( event ) ;
46+ const clValueWithRemainder = CLValueParser . fromBytesWithType ( event ) ;
5047
51- if ( clValueWithRemainder . data . bytes . length < 4 ) {
48+ if ( clValueWithRemainder . result . bytes ( ) . length < 4 ) {
5249 throw new Error ( 'invalid event bytes' ) ;
5350 }
5451
5552 const eventNameWithRemainder = parseEventNameWithRemainder (
56- clValueWithRemainder . data . bytes . subarray ( 4 ) ,
53+ clValueWithRemainder . result . bytes ( ) . subarray ( 4 ) ,
5754 ) ;
5855
59- const eventSchema = schemas [ eventNameWithRemainder . data ] ;
56+ const eventSchema = schemas [ eventNameWithRemainder . result ] ;
6057 if ( ! eventSchema ) {
6158 throw new Error ( 'event name not in schema' ) ;
6259 }
6360
6461 return {
65- name : eventNameWithRemainder . data ,
62+ name : eventNameWithRemainder . result ,
6663 data : parseEventDataFromBytes (
6764 eventSchema ,
68- eventNameWithRemainder . remainder ,
65+ eventNameWithRemainder . bytes ,
6966 ) ,
7067 } ;
7168}
@@ -79,19 +76,14 @@ export function parseEventDataFromBytes(
7976 let remainder = rawBytes ;
8077
8178 for ( const item of schema ) {
82- const parser = matchByteParserByCLType ( item . value ) . unwrap ( ) ;
79+ const clValueWithRemainder = CLValueParser . fromBytesByType ( remainder , item . value ) ;
8380
84- const clValueWithRemainder = parser . fromBytesWithRemainder (
85- remainder ,
86- item . value ,
87- ) ;
88-
89- if ( ! clValueWithRemainder . remainder ) {
81+ if ( ! clValueWithRemainder . bytes ) {
9082 throw new Error ( 'remainder is empty' ) ;
9183 }
9284
93- result [ item . property ] = clValueWithRemainder . result . unwrap ( ) ;
94- remainder = clValueWithRemainder . remainder ;
85+ result [ item . property ] = clValueWithRemainder . result ;
86+ remainder = clValueWithRemainder . bytes ;
9587 }
9688
9789 return result ;
0 commit comments