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

fix: Tx receipt serialization to JSON #6711

Merged
merged 1 commit into from
May 28, 2024
Merged
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
9 changes: 9 additions & 0 deletions yarn-project/circuit-types/src/tx/tx_hash.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { randomBytes } from '@aztec/foundation/crypto';
import { BufferReader, deserializeBigInt, serializeBigInt } from '@aztec/foundation/serialize';

/**
Expand Down Expand Up @@ -105,4 +106,12 @@ export class TxHash {
public static fromString(str: string): TxHash {
return new TxHash(Buffer.from(str, 'hex'));
}

/**
* Generates a random TxHash.
* @returns A new TxHash object.
*/
public static random(): TxHash {
return new TxHash(Buffer.from(randomBytes(TxHash.SIZE)));
}
}
23 changes: 23 additions & 0 deletions yarn-project/circuit-types/src/tx/tx_receipt.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import { TxHash } from './tx_hash.js';
import { TxReceipt, TxStatus } from './tx_receipt.js';

describe('TxReceipt', () => {
it('serializes and deserializes from json', () => {
const receipt = new TxReceipt(
TxHash.random(),
TxStatus.SUCCESS,
'error',
BigInt(1),
Buffer.from('blockHash'),
undefined,
);

expect(TxReceipt.fromJSON(receipt.toJSON())).toEqual(receipt);
});

it('serializes and deserializes from json with undefined fields', () => {
const receipt = new TxReceipt(TxHash.random(), TxStatus.DROPPED, 'error', undefined, undefined, undefined);

expect(TxReceipt.fromJSON(receipt.toJSON())).toEqual(receipt);
});
});
3 changes: 2 additions & 1 deletion yarn-project/circuit-types/src/tx/tx_receipt.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ export class TxReceipt {
error: this.error,
blockHash: this.blockHash?.toString('hex'),
blockNumber: this.blockNumber,
transactionFee: this.transactionFee?.toString(),
};
}

Expand All @@ -78,7 +79,7 @@ export class TxReceipt {
const txHash = TxHash.fromString(obj.txHash);
const status = obj.status as TxStatus;
const error = obj.error;
const transactionFee = obj.transactionFee;
const transactionFee = obj.transactionFee ? BigInt(obj.transactionFee) : undefined;
const blockHash = obj.blockHash ? Buffer.from(obj.blockHash, 'hex') : undefined;
const blockNumber = obj.blockNumber ? Number(obj.blockNumber) : undefined;
return new TxReceipt(txHash, status, error, transactionFee, blockHash, blockNumber);
Expand Down
Loading