Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat!: Upgrade to casper-js-sdk 5.x #27

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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"
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We'll release 5.0.0-beta1 tomorrow. Let's use it.

},
"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'`
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why do we have dist in the path? This doesn't seem correct

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 @@
const eventName = eventNameWithPrefix.replace('event_', '');

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

Expand All @@ -44,28 +41,28 @@
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 @@
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
Loading