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" ;
3
4
4
- import {
5
- parseBytesWithRemainder ,
6
- parseCLValueFromBytesWithRemainder ,
7
- } from './casper/utils' ;
8
5
import { Schema , Schemas } from './schema' ;
9
6
10
7
const EVENT_PREFIX = 'event_' ;
11
8
12
9
export interface Event {
13
10
name : string ;
14
- contractHash : Uint8Array | null ;
15
- contractPackageHash : Uint8Array | null ;
11
+ contractHash : Hash | null ;
12
+ contractPackageHash : Hash | null ;
16
13
data : Record < string , CLValue > ;
17
14
}
18
15
19
16
export function parseEventNameWithRemainder (
20
17
rawEvent : Uint8Array ,
21
- ) : WithRemainder < string > {
22
- const eventNameWithRemainder = parseBytesWithRemainder ( rawEvent ) ;
18
+ ) : IResultWithBytes < string > {
19
+ const eventNameWithRemainder = CLValueParser . fromBytesWithType ( rawEvent ) ;
23
20
24
21
const eventNameWithPrefix = new TextDecoder ( ) . decode (
25
- eventNameWithRemainder . data ,
22
+ eventNameWithRemainder . result . bytes ( ) ,
26
23
) ;
27
24
28
25
if ( ! eventNameWithPrefix . startsWith ( EVENT_PREFIX ) ) {
@@ -32,8 +29,8 @@ export function parseEventNameWithRemainder(
32
29
const eventName = eventNameWithPrefix . replace ( 'event_' , '' ) ;
33
30
34
31
return {
35
- data : eventName ,
36
- remainder : eventNameWithRemainder . remainder ,
32
+ result : eventName ,
33
+ bytes : eventNameWithRemainder . bytes ,
37
34
} ;
38
35
}
39
36
@@ -44,28 +41,28 @@ export function parseEventNameAndData(
44
41
name : string ;
45
42
data : Record < string , CLValue > ;
46
43
} {
47
- const event = decodeBase16 ( rawEvent ) ;
44
+ const event = toBytesString ( rawEvent ) ;
48
45
49
- const clValueWithRemainder = parseCLValueFromBytesWithRemainder ( event ) ;
46
+ const clValueWithRemainder = CLValueParser . fromBytesWithType ( event ) ;
50
47
51
- if ( clValueWithRemainder . data . bytes . length < 4 ) {
48
+ if ( clValueWithRemainder . result . bytes ( ) . length < 4 ) {
52
49
throw new Error ( 'invalid event bytes' ) ;
53
50
}
54
51
55
52
const eventNameWithRemainder = parseEventNameWithRemainder (
56
- clValueWithRemainder . data . bytes . subarray ( 4 ) ,
53
+ clValueWithRemainder . result . bytes ( ) . subarray ( 4 ) ,
57
54
) ;
58
55
59
- const eventSchema = schemas [ eventNameWithRemainder . data ] ;
56
+ const eventSchema = schemas [ eventNameWithRemainder . result ] ;
60
57
if ( ! eventSchema ) {
61
58
throw new Error ( 'event name not in schema' ) ;
62
59
}
63
60
64
61
return {
65
- name : eventNameWithRemainder . data ,
62
+ name : eventNameWithRemainder . result ,
66
63
data : parseEventDataFromBytes (
67
64
eventSchema ,
68
- eventNameWithRemainder . remainder ,
65
+ eventNameWithRemainder . bytes ,
69
66
) ,
70
67
} ;
71
68
}
@@ -79,19 +76,14 @@ export function parseEventDataFromBytes(
79
76
let remainder = rawBytes ;
80
77
81
78
for ( const item of schema ) {
82
- const parser = matchByteParserByCLType ( item . value ) . unwrap ( ) ;
79
+ const clValueWithRemainder = CLValueParser . fromBytesByType ( remainder , item . value ) ;
83
80
84
- const clValueWithRemainder = parser . fromBytesWithRemainder (
85
- remainder ,
86
- item . value ,
87
- ) ;
88
-
89
- if ( ! clValueWithRemainder . remainder ) {
81
+ if ( ! clValueWithRemainder . bytes ) {
90
82
throw new Error ( 'remainder is empty' ) ;
91
83
}
92
84
93
- result [ item . property ] = clValueWithRemainder . result . unwrap ( ) ;
94
- remainder = clValueWithRemainder . remainder ;
85
+ result [ item . property ] = clValueWithRemainder . result ;
86
+ remainder = clValueWithRemainder . bytes ;
95
87
}
96
88
97
89
return result ;
0 commit comments