-
Notifications
You must be signed in to change notification settings - Fork 325
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
11 changed files
with
139 additions
and
78 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,5 @@ | ||
export * from './tx.js'; | ||
export * from './simulated_tx.js'; | ||
export * from './tx_hash.js'; | ||
export * from './tx_receipt.js'; | ||
export * from './processed_tx.js'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
import { mockSimulatedTx } from '../mocks.js'; | ||
import { SimulatedTx } from './simulated_tx.js'; | ||
|
||
describe('simulated_tx', () => { | ||
it('convert to and from json', () => { | ||
const simulatedTx = mockSimulatedTx(); | ||
expect(SimulatedTx.fromJSON(simulatedTx.toJSON())).toEqual(simulatedTx); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
import { AztecAddress } from '@aztec/circuits.js'; | ||
import { type ProcessReturnValues } from '@aztec/foundation/abi'; | ||
|
||
import { Tx } from './tx.js'; | ||
|
||
export class SimulatedTx { | ||
constructor( | ||
public tx: Tx, | ||
public privateReturnValues?: ProcessReturnValues, | ||
public publicReturnValues?: ProcessReturnValues, | ||
) {} | ||
|
||
/** | ||
* Convert a SimulatedTx class object to a plain JSON object. | ||
* @returns A plain object with SimulatedTx properties. | ||
*/ | ||
public toJSON() { | ||
const returnToJson = (data: ProcessReturnValues): string => { | ||
const replacer = (key: string, value: any): any => { | ||
if (typeof value === 'bigint') { | ||
return value.toString() + 'n'; // Indicate bigint with "n" | ||
} else if (value instanceof AztecAddress) { | ||
return value.toString(); | ||
} else { | ||
return value; | ||
} | ||
}; | ||
return JSON.stringify(data, replacer); | ||
}; | ||
|
||
return { | ||
tx: this.tx.toJSON(), | ||
privateReturnValues: returnToJson(this.privateReturnValues), | ||
publicReturnValues: returnToJson(this.publicReturnValues), | ||
}; | ||
} | ||
|
||
/** | ||
* Convert a plain JSON object to a Tx class object. | ||
* @param obj - A plain Tx JSON object. | ||
* @returns A Tx class object. | ||
*/ | ||
public static fromJSON(obj: any) { | ||
const returnFromJson = (json: string): ProcessReturnValues => { | ||
if (json == undefined) { | ||
return json; | ||
} | ||
const reviver = (key: string, value: any): any => { | ||
if (typeof value === 'string') { | ||
if (value.match(/\d+n$/)) { | ||
// Detect bigint serialization | ||
return BigInt(value.slice(0, -1)); | ||
} else if (value.match(/^0x[a-fA-F0-9]{64}$/)) { | ||
return AztecAddress.fromString(value); | ||
} | ||
} | ||
return value; | ||
}; | ||
return JSON.parse(json, reviver); | ||
}; | ||
|
||
const tx = Tx.fromJSON(obj.tx); | ||
const privateReturnValues = returnFromJson(obj.privateReturnValues); | ||
const publicReturnValues = returnFromJson(obj.publicReturnValues); | ||
|
||
return new SimulatedTx(tx, privateReturnValues, publicReturnValues); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters