Skip to content

Commit 5a9ca18

Browse files
authored
chore: Split up protocol contract artifacts (#10765)
The protocol contracts package was loading all artifacts in a single file, and then re-exporting from there. This means that whenever a protocol contract was loaded, all other protocol contracts were fetched and processed as well. This breaks protocol contracts into granular exports, and adds a bundle export that contains all for the few cases where we do need a single function for loading all artifacts.
1 parent 284b0a4 commit 5a9ca18

File tree

22 files changed

+177
-89
lines changed

22 files changed

+177
-89
lines changed

yarn-project/archiver/src/archiver/archiver.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -47,10 +47,10 @@ import { elapsed } from '@aztec/foundation/timer';
4747
import { InboxAbi, RollupAbi } from '@aztec/l1-artifacts';
4848
import {
4949
ContractClassRegisteredEvent,
50-
ContractInstanceDeployedEvent,
5150
PrivateFunctionBroadcastedEvent,
5251
UnconstrainedFunctionBroadcastedEvent,
53-
} from '@aztec/protocol-contracts';
52+
} from '@aztec/protocol-contracts/class-registerer';
53+
import { ContractInstanceDeployedEvent } from '@aztec/protocol-contracts/instance-deployer';
5454
import { Attributes, type TelemetryClient, type Traceable, type Tracer, trackSpan } from '@aztec/telemetry-client';
5555

5656
import groupBy from 'lodash.groupby';

yarn-project/archiver/src/factory.ts

+2-1
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,8 @@ import { type DataStoreConfig } from '@aztec/kv-store/config';
1010
import { createStore } from '@aztec/kv-store/lmdb';
1111
import { TokenContractArtifact } from '@aztec/noir-contracts.js/Token';
1212
import { TokenBridgeContractArtifact } from '@aztec/noir-contracts.js/TokenBridge';
13-
import { getCanonicalProtocolContract, protocolContractNames } from '@aztec/protocol-contracts';
13+
import { protocolContractNames } from '@aztec/protocol-contracts';
14+
import { getCanonicalProtocolContract } from '@aztec/protocol-contracts/bundle';
1415
import { type TelemetryClient } from '@aztec/telemetry-client';
1516
import { NoopTelemetryClient } from '@aztec/telemetry-client/noop';
1617

yarn-project/aztec.js/src/fee/fee_juice_payment_method_with_claim.ts

+3-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
import { type FunctionCall } from '@aztec/circuit-types';
22
import { type AztecAddress, Fr, FunctionSelector } from '@aztec/circuits.js';
33
import { FunctionType } from '@aztec/foundation/abi';
4-
import { ProtocolContractAddress, ProtocolContractArtifact } from '@aztec/protocol-contracts';
4+
import { ProtocolContractAddress } from '@aztec/protocol-contracts';
5+
import { getCanonicalFeeJuice } from '@aztec/protocol-contracts/fee-juice';
56

67
import { type L2AmountClaim } from '../utils/portal_manager.js';
78
import { FeeJuicePaymentMethod } from './fee_juice_payment_method.js';
@@ -23,7 +24,7 @@ export class FeeJuicePaymentMethodWithClaim extends FeeJuicePaymentMethod {
2324
*/
2425
override getFunctionCalls(): Promise<FunctionCall[]> {
2526
const selector = FunctionSelector.fromNameAndParameters(
26-
ProtocolContractArtifact.FeeJuice.functions.find(f => f.name === 'claim')!,
27+
getCanonicalFeeJuice().artifact.functions.find(f => f.name === 'claim')!,
2728
);
2829

2930
return Promise.resolve([

yarn-project/aztec/src/bin/index.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
#!/usr/bin/env node
2-
import { fileURLToPath } from '@aztec/aztec.js';
32
import { injectCommands as injectBuilderCommands } from '@aztec/builder';
43
import { injectCommands as injectWalletCommands } from '@aztec/cli-wallet';
54
import { injectCommands as injectContractCommands } from '@aztec/cli/contracts';
@@ -9,6 +8,7 @@ import { injectCommands as injectL1Commands } from '@aztec/cli/l1';
98
import { injectCommands as injectMiscCommands } from '@aztec/cli/misc';
109
import { injectCommands as injectPXECommands } from '@aztec/cli/pxe';
1110
import { createConsoleLogger, createLogger } from '@aztec/foundation/log';
11+
import { fileURLToPath } from '@aztec/foundation/url';
1212

1313
import { Command } from 'commander';
1414
import { readFileSync } from 'fs';

yarn-project/protocol-contracts/package.json

+2-1
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
"type": "module",
77
"exports": {
88
".": "./dest/index.js",
9+
"./bundle": "./dest/bundle/index.js",
910
"./*": "./dest/*/index.js"
1011
},
1112
"typedocOptions": {
@@ -100,4 +101,4 @@
100101
"engines": {
101102
"node": ">=18"
102103
}
103-
}
104+
}
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,18 @@
1-
import { type ProtocolContract, getCanonicalProtocolContract } from '../protocol_contract.js';
1+
import { loadContractArtifact } from '@aztec/types/abi';
2+
import { type NoirCompiledContract } from '@aztec/types/noir';
3+
4+
import AuthRegistryJson from '../../artifacts/AuthRegistry.json' assert { type: 'json' };
5+
import { makeProtocolContract } from '../make_protocol_contract.js';
6+
import { type ProtocolContract } from '../protocol_contract.js';
7+
8+
let protocolContract: ProtocolContract;
9+
10+
export const AuthRegistryArtifact = loadContractArtifact(AuthRegistryJson as NoirCompiledContract);
211

312
/** Returns the canonical deployment of the auth registry. */
413
export function getCanonicalAuthRegistry(): ProtocolContract {
5-
return getCanonicalProtocolContract('AuthRegistry');
14+
if (!protocolContract) {
15+
protocolContract = makeProtocolContract('AuthRegistry', AuthRegistryArtifact);
16+
}
17+
return protocolContract;
618
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
import { getContractClassFromArtifact, getContractInstanceFromDeployParams } from '@aztec/circuits.js';
2+
import { type ContractArtifact } from '@aztec/foundation/abi';
3+
4+
import { AuthRegistryArtifact } from '../auth-registry/index.js';
5+
import { ContractClassRegistererArtifact } from '../class-registerer/index.js';
6+
import { FeeJuiceArtifact } from '../fee-juice/index.js';
7+
import { ContractInstanceDeployerArtifact } from '../instance-deployer/index.js';
8+
import { MultiCallEntrypointArtifact } from '../multi-call-entrypoint/index.js';
9+
import { type ProtocolContract } from '../protocol_contract.js';
10+
import { ProtocolContractAddress, type ProtocolContractName, ProtocolContractSalt } from '../protocol_contract_data.js';
11+
import { RouterArtifact } from '../router/index.js';
12+
13+
/** Returns the canonical deployment a given artifact. */
14+
export function getCanonicalProtocolContract(name: ProtocolContractName): ProtocolContract {
15+
const artifact = ProtocolContractArtifact[name];
16+
const address = ProtocolContractAddress[name];
17+
const salt = ProtocolContractSalt[name];
18+
// TODO(@spalladino): This computes the contract class from the artifact twice.
19+
const contractClass = getContractClassFromArtifact(artifact);
20+
const instance = getContractInstanceFromDeployParams(artifact, { salt });
21+
return {
22+
instance: { ...instance, address },
23+
contractClass,
24+
artifact,
25+
address,
26+
};
27+
}
28+
29+
export const ProtocolContractArtifact: Record<ProtocolContractName, ContractArtifact> = {
30+
AuthRegistry: AuthRegistryArtifact,
31+
ContractInstanceDeployer: ContractInstanceDeployerArtifact,
32+
ContractClassRegisterer: ContractClassRegistererArtifact,
33+
MultiCallEntrypoint: MultiCallEntrypointArtifact,
34+
FeeJuice: FeeJuiceArtifact,
35+
Router: RouterArtifact,
36+
};
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,25 @@
1-
import { type ProtocolContract, getCanonicalProtocolContract } from '../protocol_contract.js';
1+
import { loadContractArtifact } from '@aztec/types/abi';
2+
import { type NoirCompiledContract } from '@aztec/types/noir';
3+
4+
import ContractClassRegistererJson from '../../artifacts/ContractClassRegisterer.json' assert { type: 'json' };
5+
import { makeProtocolContract } from '../make_protocol_contract.js';
6+
import { type ProtocolContract } from '../protocol_contract.js';
27

38
export * from './contract_class_registered_event.js';
49
export * from './private_function_broadcasted_event.js';
510
export * from './unconstrained_function_broadcasted_event.js';
611

7-
/** Returns the canonical deployment of the class registerer contract. */
12+
export const ContractClassRegistererArtifact = loadContractArtifact(
13+
ContractClassRegistererJson as NoirCompiledContract,
14+
);
15+
16+
let protocolContract: ProtocolContract;
17+
18+
/** Returns the canonical deployment of the contract. */
819
export function getCanonicalClassRegisterer(): ProtocolContract {
9-
return getCanonicalProtocolContract('ContractClassRegisterer');
20+
if (!protocolContract) {
21+
const artifact = ContractClassRegistererArtifact;
22+
protocolContract = makeProtocolContract('ContractClassRegisterer', artifact);
23+
}
24+
return protocolContract;
1025
}
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,18 @@
1-
import { type ProtocolContract, getCanonicalProtocolContract } from '../protocol_contract.js';
1+
import { loadContractArtifact } from '@aztec/types/abi';
2+
import { type NoirCompiledContract } from '@aztec/types/noir';
23

3-
/** Returns the canonical deployment of the Fee Juice. */
4+
import FeeJuiceJson from '../../artifacts/FeeJuice.json' assert { type: 'json' };
5+
import { makeProtocolContract } from '../make_protocol_contract.js';
6+
import { type ProtocolContract } from '../protocol_contract.js';
7+
8+
export const FeeJuiceArtifact = loadContractArtifact(FeeJuiceJson as NoirCompiledContract);
9+
10+
let protocolContract: ProtocolContract;
11+
12+
/** Returns the canonical deployment of the contract. */
413
export function getCanonicalFeeJuice(): ProtocolContract {
5-
return getCanonicalProtocolContract('FeeJuice');
14+
if (!protocolContract) {
15+
protocolContract = makeProtocolContract('FeeJuice', FeeJuiceArtifact);
16+
}
17+
return protocolContract;
618
}
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,3 @@
1-
export * from './auth-registry/index.js';
2-
export * from './class-registerer/index.js';
3-
export * from './fee-juice/index.js';
4-
export * from './instance-deployer/index.js';
5-
export * from './multi-call-entrypoint/index.js';
61
export * from './protocol_contract.js';
72
export * from './protocol_contract_data.js';
83
export * from './protocol_contract_tree.js';
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,22 @@
1-
import { type ProtocolContract, getCanonicalProtocolContract } from '../protocol_contract.js';
1+
import { loadContractArtifact } from '@aztec/types/abi';
2+
import { type NoirCompiledContract } from '@aztec/types/noir';
3+
4+
import ContractInstanceDeployerJson from '../../artifacts/ContractInstanceDeployer.json' assert { type: 'json' };
5+
import { makeProtocolContract } from '../make_protocol_contract.js';
6+
import { type ProtocolContract } from '../protocol_contract.js';
27

38
export * from './contract_instance_deployed_event.js';
49

5-
/** Returns the canonical deployment of the instance deployer contract. */
10+
export const ContractInstanceDeployerArtifact = loadContractArtifact(
11+
ContractInstanceDeployerJson as NoirCompiledContract,
12+
);
13+
14+
let protocolContract: ProtocolContract;
15+
16+
/** Returns the canonical deployment of the contract. */
617
export function getCanonicalInstanceDeployer(): ProtocolContract {
7-
return getCanonicalProtocolContract('ContractInstanceDeployer');
18+
if (!protocolContract) {
19+
protocolContract = makeProtocolContract('ContractInstanceDeployer', ContractInstanceDeployerArtifact);
20+
}
21+
return protocolContract;
822
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
import { getContractClassFromArtifact, getContractInstanceFromDeployParams } from '@aztec/circuits.js';
2+
import { type ContractArtifact } from '@aztec/foundation/abi';
3+
4+
import { type ProtocolContract } from './protocol_contract.js';
5+
import { ProtocolContractAddress, type ProtocolContractName, ProtocolContractSalt } from './protocol_contract_data.js';
6+
7+
/**
8+
* Returns the canonical deployment given its name and artifact.
9+
* To be used internally within the protocol-contracts package.
10+
*/
11+
export function makeProtocolContract(name: ProtocolContractName, artifact: ContractArtifact): ProtocolContract {
12+
const address = ProtocolContractAddress[name];
13+
const salt = ProtocolContractSalt[name];
14+
// TODO(@spalladino): This computes the contract class from the artifact twice.
15+
const contractClass = getContractClassFromArtifact(artifact);
16+
const instance = getContractInstanceFromDeployParams(artifact, { salt });
17+
return {
18+
instance: { ...instance, address },
19+
contractClass,
20+
artifact,
21+
address,
22+
};
23+
}
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,18 @@
1-
import { type ProtocolContract, getCanonicalProtocolContract } from '../protocol_contract.js';
1+
import { loadContractArtifact } from '@aztec/types/abi';
2+
import { type NoirCompiledContract } from '@aztec/types/noir';
23

3-
export function getCanonicalMultiCallEntrypointContract(): ProtocolContract {
4-
return getCanonicalProtocolContract('MultiCallEntrypoint');
4+
import MultiCallEntrypointJson from '../../artifacts/MultiCallEntrypoint.json' assert { type: 'json' };
5+
import { makeProtocolContract } from '../make_protocol_contract.js';
6+
import { type ProtocolContract } from '../protocol_contract.js';
7+
8+
export const MultiCallEntrypointArtifact = loadContractArtifact(MultiCallEntrypointJson as NoirCompiledContract);
9+
10+
let protocolContract: ProtocolContract;
11+
12+
/** Returns the canonical deployment of the contract. */
13+
export function getCanonicalMultiCallEntrypoint(): ProtocolContract {
14+
if (!protocolContract) {
15+
protocolContract = makeProtocolContract('MultiCallEntrypoint', MultiCallEntrypointArtifact);
16+
}
17+
return protocolContract;
518
}

yarn-project/protocol-contracts/src/protocol_contract.ts

+1-24
Original file line numberDiff line numberDiff line change
@@ -3,17 +3,10 @@ import {
33
type ContractClassIdPreimage,
44
type ContractClassWithId,
55
type ContractInstanceWithAddress,
6-
getContractClassFromArtifact,
7-
getContractInstanceFromDeployParams,
86
} from '@aztec/circuits.js';
97
import { type ContractArtifact } from '@aztec/foundation/abi';
108

11-
import {
12-
ProtocolContractAddress,
13-
ProtocolContractArtifact,
14-
type ProtocolContractName,
15-
ProtocolContractSalt,
16-
} from './protocol_contract_data.js';
9+
import { ProtocolContractAddress } from './protocol_contract_data.js';
1710

1811
/** Represents a canonical contract in the protocol. */
1912
export interface ProtocolContract {
@@ -27,22 +20,6 @@ export interface ProtocolContract {
2720
address: AztecAddress;
2821
}
2922

30-
/** Returns the canonical deployment a given artifact. */
31-
export function getCanonicalProtocolContract(name: ProtocolContractName): ProtocolContract {
32-
const artifact = ProtocolContractArtifact[name];
33-
const address = ProtocolContractAddress[name];
34-
const salt = ProtocolContractSalt[name];
35-
// TODO(@spalladino): This computes the contract class from the artifact twice.
36-
const contractClass = getContractClassFromArtifact(artifact);
37-
const instance = getContractInstanceFromDeployParams(artifact, { salt });
38-
return {
39-
instance: { ...instance, address },
40-
contractClass,
41-
artifact,
42-
address,
43-
};
44-
}
45-
4623
export function isProtocolContract(address: AztecAddress) {
4724
return Object.values(ProtocolContractAddress).some(a => a.equals(address));
4825
}
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,18 @@
1-
import { type ProtocolContract, getCanonicalProtocolContract } from '../protocol_contract.js';
1+
import { loadContractArtifact } from '@aztec/types/abi';
2+
import { type NoirCompiledContract } from '@aztec/types/noir';
23

3-
/** Returns the canonical deployment of the router. */
4+
import RouterJson from '../../artifacts/Router.json' assert { type: 'json' };
5+
import { makeProtocolContract } from '../make_protocol_contract.js';
6+
import { type ProtocolContract } from '../protocol_contract.js';
7+
8+
export const RouterArtifact = loadContractArtifact(RouterJson as NoirCompiledContract);
9+
10+
let protocolContract: ProtocolContract;
11+
12+
/** Returns the canonical deployment of the contract. */
413
export function getCanonicalRouter(): ProtocolContract {
5-
return getCanonicalProtocolContract('Router');
14+
if (!protocolContract) {
15+
protocolContract = makeProtocolContract('Router', RouterArtifact);
16+
}
17+
return protocolContract;
618
}

yarn-project/protocol-contracts/src/scripts/generate_data.ts

-25
Original file line numberDiff line numberDiff line change
@@ -97,26 +97,6 @@ function generateNames(names: string[]) {
9797
`;
9898
}
9999

100-
function generateArtifacts(names: string[]) {
101-
const imports = names
102-
.map(name => {
103-
return `
104-
import ${name}Json from '../artifacts/${name}.json' assert { type: 'json' };
105-
`;
106-
})
107-
.join('\n');
108-
109-
const exports = names.map(name => `${name}: loadContractArtifact(${name}Json as NoirCompiledContract)`).join(',\n');
110-
111-
return `
112-
${imports}
113-
114-
export const ProtocolContractArtifact: Record<ProtocolContractName, ContractArtifact> = {
115-
${exports}
116-
};
117-
`;
118-
}
119-
120100
function generateSalts(names: string[]) {
121101
return `
122102
export const ProtocolContractSalt: Record<ProtocolContractName, Fr> = {
@@ -165,14 +145,9 @@ async function generateOutputFile(names: string[], leaves: Fr[]) {
165145
const content = `
166146
// GENERATED FILE - DO NOT EDIT. RUN \`yarn generate\` or \`yarn generate:data\`
167147
import { AztecAddress, Fr } from '@aztec/circuits.js';
168-
import { type ContractArtifact } from '@aztec/foundation/abi';
169-
import { loadContractArtifact } from '@aztec/types/abi';
170-
import { type NoirCompiledContract } from '@aztec/types/noir';
171148
172149
${generateNames(names)}
173150
174-
${generateArtifacts(names)}
175-
176151
${generateSalts(names)}
177152
178153
${generateContractAddresses(names)}

yarn-project/pxe/src/pxe_service/pxe_service.ts

+2-5
Original file line numberDiff line numberDiff line change
@@ -59,11 +59,8 @@ import { type Logger, createLogger } from '@aztec/foundation/log';
5959
import { Timer } from '@aztec/foundation/timer';
6060
import { type KeyStore } from '@aztec/key-store';
6161
import { type L2TipsStore } from '@aztec/kv-store/stores';
62-
import {
63-
ProtocolContractAddress,
64-
getCanonicalProtocolContract,
65-
protocolContractNames,
66-
} from '@aztec/protocol-contracts';
62+
import { ProtocolContractAddress, protocolContractNames } from '@aztec/protocol-contracts';
63+
import { getCanonicalProtocolContract } from '@aztec/protocol-contracts/bundle';
6764
import { type AcirSimulator } from '@aztec/simulator/client';
6865

6966
import { inspect } from 'util';

yarn-project/simulator/src/public/fee_payment.ts

+3-2
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,14 @@
11
import { computePublicDataTreeLeafSlot, deriveStorageSlotInMap } from '@aztec/circuits.js/hash';
22
import { type AztecAddress } from '@aztec/foundation/aztec-address';
33
import { Fr } from '@aztec/foundation/fields';
4-
import { ProtocolContractAddress, ProtocolContractArtifact } from '@aztec/protocol-contracts';
4+
import { ProtocolContractAddress } from '@aztec/protocol-contracts';
5+
import { FeeJuiceArtifact } from '@aztec/protocol-contracts/fee-juice';
56

67
/**
78
* Computes the storage slot within the Fee Juice contract for the balance of the fee payer.
89
*/
910
export function computeFeePayerBalanceStorageSlot(feePayer: AztecAddress) {
10-
return deriveStorageSlotInMap(ProtocolContractArtifact.FeeJuice.storageLayout.balances.slot, feePayer);
11+
return deriveStorageSlotInMap(FeeJuiceArtifact.storageLayout.balances.slot, feePayer);
1112
}
1213

1314
/**

0 commit comments

Comments
 (0)