-
Notifications
You must be signed in to change notification settings - Fork 2
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
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Large diffs are not rendered by default.
This file was deleted.
This file was deleted.
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 GitHub Actions / build (14.x)
Check failure on line 2 in src/event.ts GitHub Actions / build (16.x)
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why do we have |
||
import { CLValueParser } from "casper-js-sdk/dist/types/clvalue/Parser"; | ||
Check failure on line 3 in src/event.ts GitHub Actions / build (14.x)
Check failure on line 3 in src/event.ts GitHub Actions / build (16.x)
|
||
|
||
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)) { | ||
|
@@ -32,8 +29,8 @@ | |
const eventName = eventNameWithPrefix.replace('event_', ''); | ||
|
||
return { | ||
data: eventName, | ||
remainder: eventNameWithRemainder.remainder, | ||
result: eventName, | ||
bytes: eventNameWithRemainder.bytes, | ||
}; | ||
} | ||
|
||
|
@@ -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 GitHub Actions / build (14.x)
Check failure on line 63 in src/event.ts GitHub Actions / build (16.x)
|
||
eventSchema, | ||
eventNameWithRemainder.remainder, | ||
eventNameWithRemainder.bytes, | ||
), | ||
}; | ||
} | ||
|
@@ -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 GitHub Actions / build (14.x)
Check failure on line 79 in src/event.ts GitHub Actions / build (16.x)
|
||
|
||
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; | ||
|
There was a problem hiding this comment.
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.