Skip to content

Commit 9ec9ca7

Browse files
fix: Fix lint errors and upgrade casper-js-sdk
1 parent 269be8a commit 9ec9ca7

File tree

5 files changed

+96
-44
lines changed

5 files changed

+96
-44
lines changed

package-lock.json

Lines changed: 8 additions & 8 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 2 additions & 2 deletions
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": "^5.0.11-beta2"
42+
"casper-js-sdk": "^5.0.12-beta2"
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": "^5.0.11-beta2",
50+
"casper-js-sdk": "^5.0.12-beta2",
5151
"copy-webpack-plugin": "^11.0.0",
5252
"crypto-browserify": "^3.12.0",
5353
"eslint": "^8.34.0",

src/event.ts

Lines changed: 18 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,13 @@
1-
import {CLTypeString, CLValue, CLValueParser, Conversions, Hash, IResultWithBytes} from 'casper-js-sdk';
1+
import {
2+
CLTypeString,
3+
CLValue,
4+
CLValueParser,
5+
Conversions,
6+
Hash,
7+
IResultWithBytes,
8+
} from 'casper-js-sdk';
29

3-
import {Schema, Schemas} from './schema';
10+
import { Schema, Schemas } from './schema';
411

512
const EVENT_PREFIX = 'event_';
613

@@ -15,7 +22,10 @@ export interface Event {
1522
export function parseEventNameWithRemainder(
1623
rawEvent: Uint8Array,
1724
): IResultWithBytes<string> {
18-
const eventNameWithRemainder = CLValueParser.fromBytesByType(rawEvent, CLTypeString);
25+
const eventNameWithRemainder = CLValueParser.fromBytesByType(
26+
rawEvent,
27+
CLTypeString,
28+
);
1929

2030
const eventNameWithPrefix = eventNameWithRemainder.result.toString();
2131

@@ -57,10 +67,7 @@ export function parseEventNameAndData(
5767

5868
return {
5969
name: eventNameWithRemainder.result,
60-
data: parseEventDataFromBytes(
61-
eventSchema,
62-
eventNameWithRemainder.bytes,
63-
),
70+
data: parseEventDataFromBytes(eventSchema, eventNameWithRemainder.bytes),
6471
};
6572
}
6673

@@ -73,7 +80,10 @@ export function parseEventDataFromBytes(
7380
let remainder = rawBytes;
7481

7582
for (const item of schema) {
76-
const clValueWithRemainder = CLValueParser.fromBytesByType(remainder, item.value);
83+
const clValueWithRemainder = CLValueParser.fromBytesByType(
84+
remainder,
85+
item.value,
86+
);
7787

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

src/parser.ts

Lines changed: 53 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,12 @@ import {
1111
RpcClient,
1212
TypeID,
1313
} from 'casper-js-sdk';
14-
import {Event, parseEventDataFromBytes, parseEventNameWithRemainder,} from './event';
15-
import {parseSchemasFromBytes, Schemas} from './schema';
14+
import {
15+
Event,
16+
parseEventDataFromBytes,
17+
parseEventNameWithRemainder,
18+
} from './event';
19+
import { parseSchemasFromBytes, Schemas } from './schema';
1620

1721
export interface ContractMetadata {
1822
schemas: Schemas;
@@ -23,9 +27,9 @@ export interface ContractMetadata {
2327
}
2428

2529
interface Dictionary {
26-
uref: string,
27-
key: string,
28-
value: Uint8Array,
30+
uref: string;
31+
key: string;
32+
value: Uint8Array;
2933
}
3034

3135
export const EVENTS_SCHEMA_NAMED_KEY = '__events_schema';
@@ -64,7 +68,11 @@ export class Parser {
6468

6569
for (const contractHash of contractHashes) {
6670
const contractData = (
67-
await rpcClient.getStateItem(stateRootHash.stateRootHash.toHex(), `hash-${contractHash}`, [])
71+
await rpcClient.getStateItem(
72+
stateRootHash.stateRootHash.toHex(),
73+
`hash-${contractHash}`,
74+
[],
75+
)
6876
).storedValue.contract;
6977

7078
const namedKeys = Object.values(contractData!.namedKeys);
@@ -73,8 +81,8 @@ export class Parser {
7381
throw new Error('contract data not found');
7482
}
7583

76-
let eventsSchemaUref = "";
77-
let eventsUref = "";
84+
let eventsSchemaUref = '';
85+
let eventsUref = '';
7886

7987
for (const namedKey of namedKeys) {
8088
if (namedKey.name === EVENTS_SCHEMA_NAMED_KEY) {
@@ -83,7 +91,7 @@ export class Parser {
8391
eventsUref = namedKey.key;
8492
}
8593

86-
if (eventsSchemaUref !== "" && eventsUref !== "") {
94+
if (eventsSchemaUref !== '' && eventsUref !== '') {
8795
break;
8896
}
8997
}
@@ -96,12 +104,20 @@ export class Parser {
96104
throw new Error(`no '${EVENTS_NAMED_KEY}' uref found`);
97105
}
98106

99-
const schemaResponse = await rpcClient.getStateItem(stateRootHash.stateRootHash.toHex(), eventsSchemaUref.toString(), []);
107+
const schemaResponse = await rpcClient.getStateItem(
108+
stateRootHash.stateRootHash.toHex(),
109+
eventsSchemaUref.toString(),
110+
[],
111+
);
100112
if (!schemaResponse.storedValue.clValue) {
101113
throw new Error(`no schema uref for ${eventsSchemaUref}`);
102114
}
103115

104-
const schemas = parseSchemasFromBytes(Conversions.decodeBase16(schemaResponse.rawJSON.stored_value.CLValue.bytes));
116+
const schemas = parseSchemasFromBytes(
117+
Conversions.decodeBase16(
118+
schemaResponse.rawJSON.stored_value.CLValue.bytes,
119+
),
120+
);
105121

106122
contractsSchemas[eventsUref.toString()] = {
107123
schemas,
@@ -142,9 +158,7 @@ export class Parser {
142158
try {
143159
dictionary = this.newDictionaryFromBytes(clValue.any.bytes());
144160

145-
eventNameWithRemainder = parseEventNameWithRemainder(
146-
dictionary.value,
147-
);
161+
eventNameWithRemainder = parseEventNameWithRemainder(dictionary.value);
148162
} catch (err) {
149163
continue;
150164
}
@@ -170,7 +184,8 @@ export class Parser {
170184
parsedEvent.contractHash = contractMetadata.contractHash;
171185
parsedEvent.contractPackageHash = contractMetadata.contractPackageHash;
172186

173-
const eventSchema = contractMetadata.schemas[eventNameWithRemainder.result];
187+
const eventSchema =
188+
contractMetadata.schemas[eventNameWithRemainder.result];
174189
if (!eventSchema) {
175190
results.push({
176191
event: parsedEvent,
@@ -213,28 +228,41 @@ export class Parser {
213228

214229
const clValue = CLValueParser.fromBytesWithType(u32.bytes);
215230

216-
if (!(clValue.result.type instanceof CLTypeList) || clValue.result.type.elementsType.getTypeID() !== TypeID.U8) {
231+
if (
232+
!(clValue.result.type instanceof CLTypeList) ||
233+
clValue.result.type.elementsType.getTypeID() !== TypeID.U8
234+
) {
217235
throw new Error('failed to parse CLList(CLU8) from bytes');
218236
}
219237

220238
const clValueByteSize = CLValueUInt32.fromBytes(clValue.bytes);
221239

222-
const clByteArrayAsUref = CLValueParser.fromBytesByType(clValueByteSize.bytes, new CLTypeByteArray(clValueByteSize.result.toNumber()));
240+
const clByteArrayAsUref = CLValueParser.fromBytesByType(
241+
clValueByteSize.bytes,
242+
new CLTypeByteArray(clValueByteSize.result.toNumber()),
243+
);
223244
if (!clByteArrayAsUref.result.byteArray) {
224245
throw new Error('failed to parse CLByteArray from bytes');
225246
}
226247

227-
const uref = `uref-${Conversions.encodeBase16(clByteArrayAsUref.result.byteArray.bytes())}-007`;
248+
const uref = `uref-${Conversions.encodeBase16(
249+
clByteArrayAsUref.result.byteArray.bytes(),
250+
)}-007`;
228251

229-
const clStringAsDictKey = CLValueParser.fromBytesByType(clByteArrayAsUref.bytes, CLTypeString);
252+
const clStringAsDictKey = CLValueParser.fromBytesByType(
253+
clByteArrayAsUref.bytes,
254+
CLTypeString,
255+
);
230256
if (!clStringAsDictKey.result.stringVal) {
231257
throw new Error('failed to parse CLString from bytes');
232258
}
233259

234260
return {
235261
uref: uref,
236262
key: clStringAsDictKey.result.stringVal.toString(),
237-
value: new Uint8Array(clValue.result.list!.elements.map(el => el.ui8!.toNumber())),
263+
value: new Uint8Array(
264+
clValue.result.list!.elements.map(el => el.ui8!.toNumber()),
265+
),
238266
};
239267
}
240268
}
@@ -244,7 +272,11 @@ export async function fetchContractSchemasBytes(
244272
contractHash: string,
245273
stateRootHash: string,
246274
): Promise<Uint8Array> {
247-
const schemaResponse = await rpcClient.getStateItem(stateRootHash, `hash-${contractHash}`, [EVENTS_SCHEMA_NAMED_KEY]);
275+
const schemaResponse = await rpcClient.getStateItem(
276+
stateRootHash,
277+
`hash-${contractHash}`,
278+
[EVENTS_SCHEMA_NAMED_KEY],
279+
);
248280

249281
if (!schemaResponse.storedValue.clValue) {
250282
throw new Error('no clvalue for contract schema');

src/schema.ts

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,9 @@ import {
66
CLValueParser,
77
CLValueUInt32,
88
IResultWithBytes,
9-
RpcClient
9+
RpcClient,
1010
} from 'casper-js-sdk';
11-
import {EVENTS_SCHEMA_NAMED_KEY} from './parser';
11+
import { EVENTS_SCHEMA_NAMED_KEY } from './parser';
1212

1313
export type Schemas = Record<string, Schema>;
1414

@@ -67,7 +67,9 @@ export function parseSchemaFromBytesWithRemainder(
6767
for (let i = 0; i < fieldsNumber; i++) {
6868
const fieldName = CLValueParser.fromBytesByType(remainder, CLTypeString);
6969

70-
const clTypeWithRemainder = CLTypeParser.matchBytesToCLType(fieldName.bytes);
70+
const clTypeWithRemainder = CLTypeParser.matchBytesToCLType(
71+
fieldName.bytes,
72+
);
7173

7274
if (!clTypeWithRemainder.bytes) {
7375
throw new Error('remainder is empty');
@@ -92,7 +94,11 @@ export async function fetchContractSchemasBytes(
9294
contractHash: string,
9395
stateRootHash: string,
9496
): Promise<Uint8Array> {
95-
const contractData = await rpcClient.getStateItem(stateRootHash, `hash-${contractHash}`, []);
97+
const contractData = await rpcClient.getStateItem(
98+
stateRootHash,
99+
`hash-${contractHash}`,
100+
[],
101+
);
96102

97103
if (!contractData || !contractData.storedValue.contract) {
98104
throw new Error('contract data not found');
@@ -107,7 +113,11 @@ export async function fetchContractSchemasBytes(
107113
);
108114
}
109115

110-
const schemaResponse = await rpcClient.getStateItem(stateRootHash, eventsSchema.key.toString(), []);
116+
const schemaResponse = await rpcClient.getStateItem(
117+
stateRootHash,
118+
eventsSchema.key.toString(),
119+
[],
120+
);
111121

112122
if (!schemaResponse.storedValue.clValue) {
113123
throw new Error('no CLValue for schema');

0 commit comments

Comments
 (0)