Skip to content

Commit

Permalink
feat!: Upgrade to casper-js-sdk 5.x
Browse files Browse the repository at this point in the history
  • Loading branch information
Volodymyr-Kuchinskyi committed Dec 19, 2024
1 parent 8dc0d5c commit 27c1fa9
Show file tree
Hide file tree
Showing 8 changed files with 444 additions and 1,274 deletions.
1,195 changes: 337 additions & 858 deletions package-lock.json

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -39,15 +39,15 @@
"ts-results": "^3.3.0"
},
"peerDependencies": {
"casper-js-sdk": "^2.12.0"
"casper-js-sdk": "^5.0.0-rc8"
},
"devDependencies": {
"@types/jest": "^29.4.0",
"@typescript-eslint/eslint-plugin": "^5.53.0",
"@typescript-eslint/parser": "^5.53.0",
"browserify-zlib": "^0.2.0",
"buffer": "^6.0.3",
"casper-js-sdk": "^2.12.0",
"casper-js-sdk": "^5.0.0-rc8",
"copy-webpack-plugin": "^11.0.0",
"crypto-browserify": "^3.12.0",
"eslint": "^8.34.0",
Expand Down
35 changes: 0 additions & 35 deletions src/casper/types.ts

This file was deleted.

227 changes: 0 additions & 227 deletions src/casper/utils.ts

This file was deleted.

50 changes: 21 additions & 29 deletions src/event.ts
Original file line number Diff line number Diff line change
@@ -1,28 +1,25 @@
import { CLValue, decodeBase16, matchByteParserByCLType } from 'casper-js-sdk';
import { WithRemainder } from './casper/types';
import { CLValue, Hash, IResultWithBytes } from 'casper-js-sdk';
import { toBytesString } from "casper-js-sdk/dist/types/ByteConverters";

Check failure on line 2 in src/event.ts

View workflow job for this annotation

GitHub Actions / build (14.x)

Replace `"casper-js-sdk/dist/types/ByteConverters"` with `'casper-js-sdk/dist/types/ByteConverters'`

Check failure on line 2 in src/event.ts

View workflow job for this annotation

GitHub Actions / build (16.x)

Replace `"casper-js-sdk/dist/types/ByteConverters"` with `'casper-js-sdk/dist/types/ByteConverters'`

Check failure on line 2 in src/event.ts

View workflow job for this annotation

GitHub Actions / build (18.x)

Replace `"casper-js-sdk/dist/types/ByteConverters"` with `'casper-js-sdk/dist/types/ByteConverters'`
import { CLValueParser } from "casper-js-sdk/dist/types/clvalue/Parser";

Check failure on line 3 in src/event.ts

View workflow job for this annotation

GitHub Actions / build (14.x)

Replace `"casper-js-sdk/dist/types/clvalue/Parser"` with `'casper-js-sdk/dist/types/clvalue/Parser'`

Check failure on line 3 in src/event.ts

View workflow job for this annotation

GitHub Actions / build (16.x)

Replace `"casper-js-sdk/dist/types/clvalue/Parser"` with `'casper-js-sdk/dist/types/clvalue/Parser'`

Check failure on line 3 in src/event.ts

View workflow job for this annotation

GitHub Actions / build (18.x)

Replace `"casper-js-sdk/dist/types/clvalue/Parser"` with `'casper-js-sdk/dist/types/clvalue/Parser'`

import {
parseBytesWithRemainder,
parseCLValueFromBytesWithRemainder,
} from './casper/utils';
import { Schema, Schemas } from './schema';

const EVENT_PREFIX = 'event_';

export interface Event {
name: string;
contractHash: Uint8Array | null;
contractPackageHash: Uint8Array | null;
contractHash: Hash | null;
contractPackageHash: Hash | null;
data: Record<string, CLValue>;
}

export function parseEventNameWithRemainder(
rawEvent: Uint8Array,
): WithRemainder<string> {
const eventNameWithRemainder = parseBytesWithRemainder(rawEvent);
): IResultWithBytes<string> {
const eventNameWithRemainder = CLValueParser.fromBytesWithType(rawEvent);

const eventNameWithPrefix = new TextDecoder().decode(
eventNameWithRemainder.data,
eventNameWithRemainder.result.bytes(),
);

if (!eventNameWithPrefix.startsWith(EVENT_PREFIX)) {
Expand All @@ -32,8 +29,8 @@ export function parseEventNameWithRemainder(
const eventName = eventNameWithPrefix.replace('event_', '');

return {
data: eventName,
remainder: eventNameWithRemainder.remainder,
result: eventName,
bytes: eventNameWithRemainder.bytes,
};
}

Expand All @@ -44,28 +41,28 @@ export function parseEventNameAndData(
name: string;
data: Record<string, CLValue>;
} {
const event = decodeBase16(rawEvent);
const event = toBytesString(rawEvent);

const clValueWithRemainder = parseCLValueFromBytesWithRemainder(event);
const clValueWithRemainder = CLValueParser.fromBytesWithType(event);

if (clValueWithRemainder.data.bytes.length < 4) {
if (clValueWithRemainder.result.bytes().length < 4) {
throw new Error('invalid event bytes');
}

const eventNameWithRemainder = parseEventNameWithRemainder(
clValueWithRemainder.data.bytes.subarray(4),
clValueWithRemainder.result.bytes().subarray(4),
);

const eventSchema = schemas[eventNameWithRemainder.data];
const eventSchema = schemas[eventNameWithRemainder.result];
if (!eventSchema) {
throw new Error('event name not in schema');
}

return {
name: eventNameWithRemainder.data,
name: eventNameWithRemainder.result,
data: parseEventDataFromBytes(

Check failure on line 63 in src/event.ts

View workflow job for this annotation

GitHub Actions / build (14.x)

Replace `⏎······eventSchema,⏎······eventNameWithRemainder.bytes,⏎····` with `eventSchema,·eventNameWithRemainder.bytes`

Check failure on line 63 in src/event.ts

View workflow job for this annotation

GitHub Actions / build (16.x)

Replace `⏎······eventSchema,⏎······eventNameWithRemainder.bytes,⏎····` with `eventSchema,·eventNameWithRemainder.bytes`

Check failure on line 63 in src/event.ts

View workflow job for this annotation

GitHub Actions / build (18.x)

Replace `⏎······eventSchema,⏎······eventNameWithRemainder.bytes,⏎····` with `eventSchema,·eventNameWithRemainder.bytes`
eventSchema,
eventNameWithRemainder.remainder,
eventNameWithRemainder.bytes,
),
};
}
Expand All @@ -79,19 +76,14 @@ export function parseEventDataFromBytes(
let remainder = rawBytes;

for (const item of schema) {
const parser = matchByteParserByCLType(item.value).unwrap();
const clValueWithRemainder = CLValueParser.fromBytesByType(remainder, item.value);

Check failure on line 79 in src/event.ts

View workflow job for this annotation

GitHub Actions / build (14.x)

Replace `remainder,·item.value` with `⏎······remainder,⏎······item.value,⏎····`

Check failure on line 79 in src/event.ts

View workflow job for this annotation

GitHub Actions / build (16.x)

Replace `remainder,·item.value` with `⏎······remainder,⏎······item.value,⏎····`

Check failure on line 79 in src/event.ts

View workflow job for this annotation

GitHub Actions / build (18.x)

Replace `remainder,·item.value` with `⏎······remainder,⏎······item.value,⏎····`

const clValueWithRemainder = parser.fromBytesWithRemainder(
remainder,
item.value,
);

if (!clValueWithRemainder.remainder) {
if (!clValueWithRemainder.bytes) {
throw new Error('remainder is empty');
}

result[item.property] = clValueWithRemainder.result.unwrap();
remainder = clValueWithRemainder.remainder;
result[item.property] = clValueWithRemainder.result;
remainder = clValueWithRemainder.bytes;
}

return result;
Expand Down
5 changes: 0 additions & 5 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,3 @@ export {
parseEventDataFromBytes,
parseEventNameWithRemainder,
} from './event';
export { ExecutionResult } from './casper/types';
export {
parseBytesWithRemainder,
parseCLValueFromBytesWithRemainder,
} from './casper/utils';
Loading

0 comments on commit 27c1fa9

Please sign in to comment.