Skip to content

Commit

Permalink
Fixed Args and CLType / CLValue parsers, serialization of RPC client …
Browse files Browse the repository at this point in the history
…request and response
  • Loading branch information
alexmyshchyshyn committed Nov 17, 2024
1 parent 9519dce commit 70d418f
Show file tree
Hide file tree
Showing 17 changed files with 275 additions and 282 deletions.
22 changes: 9 additions & 13 deletions src/rpc/response.ts
Original file line number Diff line number Diff line change
Expand Up @@ -158,11 +158,12 @@ export class ChainGetBlockResult {
public rawJSON?: any;

public static fromJSON(data: any): ChainGetBlockResult {
const parsedResult = TypedJSON.parse(data, ChainGetBlockResult);
const serializer = new TypedJSON(ChainGetBlockResult);
const parsedResult = serializer.parse(data);

if (!parsedResult) throw new Error('Failed to parse ChainGetBlockResult');

parsedResult.rawJSON = JSON.stringify(data);
parsedResult.rawJSON = data;

return parsedResult;
}
Expand Down Expand Up @@ -273,8 +274,9 @@ export class InfoGetTransactionResult {
this.rawJSON = rawJSON;
}

static fromJSON(json: string): InfoGetTransactionResult | null {
const temp = TypedJSON.parse(json, InfoGetTransactionResultV1Compatible);
static fromJSON(json: any): InfoGetTransactionResult | null {
const serializer = new TypedJSON(InfoGetTransactionResultV1Compatible);
const temp = serializer.parse(json);

if (temp) {
const result = InfoGetTransactionResultV1Compatible.newInfoGetTransactionResultFromV1Compatible(
Expand All @@ -284,7 +286,8 @@ export class InfoGetTransactionResult {
return result;
}

return TypedJSON.parse(json, InfoGetTransactionResult) ?? null;
const transactionResultSerializer = new TypedJSON(InfoGetTransactionResult);
return transactionResultSerializer.parse(json) ?? null;
}
}

Expand Down Expand Up @@ -356,16 +359,9 @@ export class InfoGetTransactionResultV1Compatible {
rawJSON?: any;

public static newInfoGetTransactionResultFromV1Compatible(
result: InfoGetTransactionResultV1Compatible,
parsedResult: InfoGetTransactionResultV1Compatible,
rawJSON: any
): InfoGetTransactionResult {
const parsedResult = TypedJSON.parse(
result,
InfoGetTransactionResultV1Compatible
);
if (!parsedResult)
throw new Error('Failed to parse InfoGetTransactionResultV1Compatible');

if (parsedResult.transaction) {
if (parsedResult.transaction.transactionV1) {
return new InfoGetTransactionResult(
Expand Down
64 changes: 35 additions & 29 deletions src/sse/event.ts
Original file line number Diff line number Diff line change
Expand Up @@ -71,9 +71,12 @@ export class RawEvent {
this.eventID = eventID;
}

private parseEvent<T>(type: new (params: any) => T): T {
private parseEvent<T>(
type: new (params: any) => T,
parser?: (data: any) => T | Error
): T {
const serializer = new TypedJSON(type);
const parsed = serializer.parse(this.data);
const parsed = parser ? parser(this.data) : serializer.parse(this.data);
if (!parsed) throw new Error('Error parsing event data');
return parsed;
}
Expand All @@ -87,27 +90,39 @@ export class RawEvent {
}

parseAsBlockAddedEvent(): BlockAddedEvent {
return this.parseEvent(BlockAddedEvent);
return this.parseEvent(BlockAddedEvent, BlockAddedEvent.fromJSON);
}

parseAsDeployAcceptedEvent(): DeployAcceptedEvent {
return this.parseEvent(DeployAcceptedEvent);
}

parseAsFinalitySignatureEvent(): FinalitySignatureEvent {
return this.parseEvent(FinalitySignatureEvent);
return this.parseEvent(
FinalitySignatureEvent,
FinalitySignatureEvent.deserialize
);
}

parseAsTransactionExpiredEvent(): TransactionExpiredEvent {
return this.parseEvent(TransactionExpiredEvent);
return this.parseEvent(
TransactionExpiredEvent,
TransactionExpiredEvent.deserialize
);
}

parseAsTransactionProcessedEvent(): TransactionProcessedEvent {
return this.parseEvent(TransactionProcessedEvent);
return this.parseEvent(
TransactionProcessedEvent,
TransactionProcessedEvent.fromJson
);
}

parseAsTransactionAcceptedEvent(): TransactionAcceptedEvent {
return this.parseEvent(TransactionAcceptedEvent);
return this.parseEvent(
TransactionAcceptedEvent,
TransactionAcceptedEvent.fromJson
);
}

parseAsFaultEvent(): FaultEvent {
Expand Down Expand Up @@ -152,7 +167,7 @@ export class BlockAddedEvent {
this.BlockAdded = blockAdded;
}

static fromJSON(data: string): BlockAddedEvent {
static fromJSON(data: any): BlockAddedEvent {
const parsedData = JSON.parse(data);

if (!parsedData) {
Expand Down Expand Up @@ -319,13 +334,10 @@ export class TransactionAcceptedEvent {

public static fromJson(data: unknown): TransactionAcceptedEvent | Error {
try {
const transactionEvent = TypedJSON.parse(
data as string,
TransactionAcceptedEvent
);
const transactionEvent = TypedJSON.parse(data, TransactionAcceptedEvent);
if (!transactionEvent) throw new Error('TransactionAcceptedEvent is nil');

const wrapper = TypedJSON.parse(data as string, TransactionWrapper);
const wrapper = TypedJSON.parse(data, TransactionWrapper);

if (wrapper?.deploy) {
transactionEvent.transactionAcceptedPayload = {
Expand All @@ -341,7 +353,7 @@ export class TransactionAcceptedEvent {
return transactionEvent;
}

const deployEvent = TypedJSON.parse(data as string, DeployAcceptedEvent);
const deployEvent = TypedJSON.parse(data, DeployAcceptedEvent);
if (deployEvent?.deployAccepted) {
transactionEvent.transactionAcceptedPayload = {
transaction: Deploy.newTransactionFromDeploy(
Expand Down Expand Up @@ -374,12 +386,9 @@ export class TransactionExpiredEvent {
})
transactionExpiredPayload: TransactionExpiredPayload;

public static deserialize(data: unknown): TransactionExpiredEvent | Error {
public static deserialize(data: any): TransactionExpiredEvent | Error {
try {
const transactionEvent = TypedJSON.parse(
data as string,
TransactionExpiredEvent
);
const transactionEvent = TypedJSON.parse(data, TransactionExpiredEvent);
if (!transactionEvent) throw new Error('TransactionExpiredEvent is nil');

const payload = transactionEvent.transactionExpiredPayload;
Expand All @@ -391,7 +400,7 @@ export class TransactionExpiredEvent {
return transactionEvent;
}

const deployEvent = TypedJSON.parse(data as string, DeployExpiredEvent);
const deployEvent = TypedJSON.parse(data, DeployExpiredEvent);
if (deployEvent?.deployExpired) {
transactionEvent.transactionExpiredPayload = {
transactionHash: new TransactionHash(
Expand Down Expand Up @@ -450,12 +459,9 @@ export class TransactionProcessedEvent {
})
transactionProcessedPayload: TransactionProcessedPayload;

public static fromJson(data: unknown): TransactionProcessedEvent | Error {
public static fromJson(data: any): TransactionProcessedEvent | Error {
try {
const transactionEvent = TypedJSON.parse(
data as string,
TransactionProcessedEvent
);
const transactionEvent = TypedJSON.parse(data, TransactionProcessedEvent);
if (!transactionEvent)
throw new Error('TransactionProcessedEvent is nil');

Expand All @@ -468,7 +474,7 @@ export class TransactionProcessedEvent {
return transactionEvent;
}

const deployEvent = TypedJSON.parse(data as string, DeployProcessedEvent);
const deployEvent = TypedJSON.parse(data, DeployProcessedEvent);
if (deployEvent?.deployProcessed) {
transactionEvent.transactionProcessedPayload = {
blockHash: deployEvent.deployProcessed.blockHash,
Expand Down Expand Up @@ -669,9 +675,9 @@ export class FinalitySignatureEvent {
@jsonMember({ name: 'FinalitySignature', constructor: FinalitySignature })
finalitySignature: FinalitySignature;

public static deserialize(data: unknown): FinalitySignatureEvent | Error {
public static deserialize(data: any): FinalitySignatureEvent | Error {
try {
const wrapped = TypedJSON.parse(data as string, FinalitySignatureWrapper);
const wrapped = TypedJSON.parse(data, FinalitySignatureWrapper);
if (!wrapped) throw new Error('FinalitySignatureWrapper is nil');

let finalitySignature: FinalitySignature;
Expand All @@ -693,7 +699,7 @@ export class FinalitySignatureEvent {
publicKey: wrapped.v2.publicKey
};
} else {
const v1Event = TypedJSON.parse(data as string, FinalitySignatureV1);
const v1Event = TypedJSON.parse(data, FinalitySignatureV1);
if (!v1Event) throw new Error('Failed to parse FinalitySignatureV1');

finalitySignature = {
Expand Down
8 changes: 3 additions & 5 deletions src/types/Args.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import { concat } from '@ethersproject/bytes';

import {
CLTypeString,
CLValue,
CLValueParser,
CLValueString,
CLValueUInt32,
IResultWithBytes,
CLTypeString
IResultWithBytes
} from './clvalue';
import { jsonMapMember, jsonObject } from 'typedjson';
import { toBytesString, toBytesU32 } from './ByteConverters';
Expand Down Expand Up @@ -59,9 +59,7 @@ export class NamedArg {
* @returns An array where each entry is a key-value pair in JSON format.
*/
const serRA = (map: Map<string, CLValue>) => {
return Array.from(map, ([key, value]) => {
return [key, CLValueParser.toJSON(value)];
});
return Array.from(map, ([key, value]) => [key, CLValueParser.toJSON(value)]);
};

/**
Expand Down
2 changes: 1 addition & 1 deletion src/types/BlockProposer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ export class Proposer {
* @returns A JSON string representing the proposer.
*/
toJSON(): string {
return this.isSystem ? '00' : JSON.stringify(this.publicKey);
return this.isSystem ? '00' : this?.publicKey!.toJSON();
}

/**
Expand Down
10 changes: 0 additions & 10 deletions src/types/ByteConverters.ts
Original file line number Diff line number Diff line change
Expand Up @@ -91,16 +91,6 @@ export const toBytesU256 = toBytesNumber(256, false);
*/
export const toBytesU512 = toBytesNumber(512, false);

/**
* Converts a deploy hash to bytes.
* @param deployHash - A `Uint8Array` representing the deploy hash.
* @returns The `Uint8Array` representation of the deploy hash.
* @deprecated This function will be removed in future versions.
*/
export const toBytesDeployHash = (deployHash: Uint8Array): Uint8Array => {
return deployHash;
};

/**
* Serializes a string into a byte array.
* @param str - The string to be converted.
Expand Down
19 changes: 1 addition & 18 deletions src/types/Deploy.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import { jsonArrayMember, jsonMember, jsonObject, TypedJSON } from 'typedjson';
import { concat } from '@ethersproject/bytes';
import { BigNumber } from '@ethersproject/bignumber';

import { Hash } from './key';
import { PrivateKey } from './keypair/PrivateKey';
Expand Down Expand Up @@ -473,7 +472,7 @@ export class Deploy {
}

const serializer = new TypedJSON(Deploy);
deploy = serializer.parse(JSON.stringify(deployJson));
deploy = serializer.parse(deployJson);

if (!deploy) {
throw new Error("The JSON can't be parsed as a Deploy.");
Expand Down Expand Up @@ -524,22 +523,6 @@ export class Deploy {
}
}

/**
* Creates an instance of standard payment logic for use in a `Deploy` object.
* This method is deprecated. It is recommended to use the `ExecutableDeployItem.standardPayment` method instead.
*
* @deprecated Use {ExecutableDeployItem.standardPayment} instead
* @param paymentAmount The amount of motes to be used to pay for gas. This value should be expressed in motes, where 1 mote = 1 * 10^-9 CSPR.
* @returns An `ExecutableDeployItem` representing the standard payment logic, to be attached to a `Deploy`.
*
* @example
* const paymentAmount = BigNumber.from('1000000');
* const paymentItem = standardPayment(paymentAmount);
*/
export const standardPayment = (paymentAmount: BigNumber) => {
return ExecutableDeployItem.standardPayment(paymentAmount);
};

/**
* Builds a `Deploy` object from the given parameters, session logic, and payment logic.
* This method is deprecated. It is recommended to use `Deploy.fromHeaderAndItems` instead.
Expand Down
20 changes: 18 additions & 2 deletions src/types/EntryPoint.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,15 @@ export class EntryPointArg {
*/
@jsonMember({
name: 'cl_type',
constructor: CLTypeRaw
constructor: CLTypeRaw,
deserializer: json => {
if (!json) return;
return CLTypeRaw.parseCLType(json);
},
serializer: (value: CLTypeRaw) => {
if (!value) return;
return value.toJSON();
}
})
clType: CLTypeRaw;

Expand Down Expand Up @@ -117,7 +125,15 @@ export class EntryPointV1 {
*/
@jsonMember({
name: 'ret',
constructor: CLTypeRaw
constructor: CLTypeRaw,
deserializer: json => {
if (!json) return;
return CLTypeRaw.parseCLType(json);
},
serializer: (value: CLTypeRaw) => {
if (!value) return;
return value.toJSON();
}
})
ret: CLTypeRaw;

Expand Down
Loading

0 comments on commit 70d418f

Please sign in to comment.