Skip to content

Commit 27c1fa9

Browse files
feat!: Upgrade to casper-js-sdk 5.x
1 parent 8dc0d5c commit 27c1fa9

File tree

8 files changed

+444
-1274
lines changed

8 files changed

+444
-1274
lines changed

Diff for: package-lock.json

+337-858
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Diff for: package.json

+2-2
Original file line numberDiff line numberDiff line change
@@ -39,15 +39,15 @@
3939
"ts-results": "^3.3.0"
4040
},
4141
"peerDependencies": {
42-
"casper-js-sdk": "^2.12.0"
42+
"casper-js-sdk": "^5.0.0-rc8"
4343
},
4444
"devDependencies": {
4545
"@types/jest": "^29.4.0",
4646
"@typescript-eslint/eslint-plugin": "^5.53.0",
4747
"@typescript-eslint/parser": "^5.53.0",
4848
"browserify-zlib": "^0.2.0",
4949
"buffer": "^6.0.3",
50-
"casper-js-sdk": "^2.12.0",
50+
"casper-js-sdk": "^5.0.0-rc8",
5151
"copy-webpack-plugin": "^11.0.0",
5252
"crypto-browserify": "^3.12.0",
5353
"eslint": "^8.34.0",

Diff for: src/casper/types.ts

-35
This file was deleted.

Diff for: src/casper/utils.ts

-227
This file was deleted.

Diff for: src/event.ts

+21-29
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,25 @@
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';
85
import { Schema, Schemas } from './schema';
96

107
const EVENT_PREFIX = 'event_';
118

129
export 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

1916
export 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;

Diff for: src/index.ts

-5
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,3 @@ export {
1111
parseEventDataFromBytes,
1212
parseEventNameWithRemainder,
1313
} from './event';
14-
export { ExecutionResult } from './casper/types';
15-
export {
16-
parseBytesWithRemainder,
17-
parseCLValueFromBytesWithRemainder,
18-
} from './casper/utils';

0 commit comments

Comments
 (0)