diff --git a/yarn-project/circuit-types/src/tx/tx_hash.ts b/yarn-project/circuit-types/src/tx/tx_hash.ts index 514097b3d6bc..c92340c1928b 100644 --- a/yarn-project/circuit-types/src/tx/tx_hash.ts +++ b/yarn-project/circuit-types/src/tx/tx_hash.ts @@ -1,3 +1,4 @@ +import { randomBytes } from '@aztec/foundation/crypto'; import { BufferReader, deserializeBigInt, serializeBigInt } from '@aztec/foundation/serialize'; /** @@ -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))); + } } diff --git a/yarn-project/circuit-types/src/tx/tx_receipt.test.ts b/yarn-project/circuit-types/src/tx/tx_receipt.test.ts new file mode 100644 index 000000000000..2eac60ece2e4 --- /dev/null +++ b/yarn-project/circuit-types/src/tx/tx_receipt.test.ts @@ -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); + }); +}); diff --git a/yarn-project/circuit-types/src/tx/tx_receipt.ts b/yarn-project/circuit-types/src/tx/tx_receipt.ts index 39c9f29f540f..fc32f1b077f7 100644 --- a/yarn-project/circuit-types/src/tx/tx_receipt.ts +++ b/yarn-project/circuit-types/src/tx/tx_receipt.ts @@ -66,6 +66,7 @@ export class TxReceipt { error: this.error, blockHash: this.blockHash?.toString('hex'), blockNumber: this.blockNumber, + transactionFee: this.transactionFee?.toString(), }; } @@ -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);