Skip to content

Commit

Permalink
refactor: moved computeContractFunctionTreeRoot function to aztec.js
Browse files Browse the repository at this point in the history
  • Loading branch information
benesjan committed Dec 14, 2023
1 parent 1517700 commit 8cbfb17
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 9 deletions.
1 change: 1 addition & 0 deletions yarn-project/aztec.js/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ export {
isContractDeployed,
EthCheatCodes,
computeAuthWitMessageHash,
computeContractFunctionTreeRoot,
} from './utils/index.js';

export { createPXEClient } from './pxe_client.js';
Expand Down
17 changes: 17 additions & 0 deletions yarn-project/aztec.js/src/utils/l2_contracts.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
import { Fr, generateFunctionLeaves } from '@aztec/circuits.js';
import { computeFunctionTreeRoot } from '@aztec/circuits.js/abis';
import { ContractArtifact, FunctionSelector } from '@aztec/foundation/abi';
import { AztecAddress } from '@aztec/foundation/aztec-address';
import { PXE } from '@aztec/types';

Expand All @@ -10,3 +13,17 @@ import { PXE } from '@aztec/types';
export async function isContractDeployed(pxe: PXE, contractAddress: AztecAddress): Promise<boolean> {
return !!(await pxe.getContractData(contractAddress));
}

/**
* Computes the root of a function tree for a given smart contract artifact.
* @param artifact - The smart contract artifact.
* @returns The computed function tree root based on the functions in the given contract artifact.
*/
export function computeContractFunctionTreeRoot(artifact: ContractArtifact): Fr {
const functions = artifact.functions.map(f => ({
...f,
selector: FunctionSelector.fromNameAndParameters(f.name, f.parameters),
}));
const functionLeaves = generateFunctionLeaves(functions);
return computeFunctionTreeRoot(functionLeaves);
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,10 @@ import {
AztecAddress,
CompleteAddress,
Fr,
FunctionSelector,
INITIAL_L2_BLOCK_NUM,
PXE,
computeContractFunctionTreeRoot,
} from '@aztec/aztec.js';
import { generateFunctionLeaves } from '@aztec/circuits.js';
import { computeFunctionTreeRoot } from '@aztec/circuits.js/abis';
import { InclusionProofsContract } from '@aztec/noir-contracts/types';

import { jest } from '@jest/globals';
Expand Down Expand Up @@ -212,12 +210,7 @@ describe('e2e_inclusion_proofs_contract', () => {

const getContractFunctionTreeRoot = () => {
if (!contractFunctionTreeRoot) {
const functions = contract.artifact.functions.map(f => ({
...f,
selector: FunctionSelector.fromNameAndParameters(f.name, f.parameters),
}));
const functionLeaves = generateFunctionLeaves(functions);
contractFunctionTreeRoot = computeFunctionTreeRoot(functionLeaves);
contractFunctionTreeRoot = computeContractFunctionTreeRoot(contract.artifact);
}
return contractFunctionTreeRoot;
};
Expand Down

0 comments on commit 8cbfb17

Please sign in to comment.