Skip to content

Commit

Permalink
feat: Nicer API for instance deployment (AztecProtocol#4493)
Browse files Browse the repository at this point in the history
Adds a `deployInstance` method to aztec.js.
  • Loading branch information
spalladino committed Feb 7, 2024
1 parent 5fc02e7 commit 99c3fba
Show file tree
Hide file tree
Showing 5 changed files with 43 additions and 10 deletions.
1 change: 1 addition & 0 deletions yarn-project/aztec.js/src/api/deployment.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
export { registerContractClass } from '../deployment/register_class.js';
export { broadcastPrivateFunction, broadcastUnconstrainedFunction } from '../deployment/broadcast_function.js';
export { deployInstance } from '../deployment/deploy_instance.js';
28 changes: 28 additions & 0 deletions yarn-project/aztec.js/src/deployment/deploy_instance.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import { ContractInstanceWithAddress } from '@aztec/types/contracts';

import { ContractFunctionInteraction } from '../contract/contract_function_interaction.js';
import { Wallet } from '../wallet/index.js';
import { getDeployerContract } from './protocol_contracts.js';

/**
* Sets up a call to the canonical deployer contract to publicly deploy a contract instance.
* @param wallet - The wallet to use for the deployment.
* @param instance - The instance to deploy.
* @param opts - Additional options.
*/
export function deployInstance(
wallet: Wallet,
instance: ContractInstanceWithAddress,
opts: { /** Set to true to *not* mix in the deployer into the address. */ universalDeploy?: boolean } = {},
): ContractFunctionInteraction {
const deployer = getDeployerContract(wallet);
const { salt, contractClassId, portalContractAddress, publicKeysHash } = instance;
return deployer.methods.deploy(
salt,
contractClassId,
instance.initializationHash,
portalContractAddress,
publicKeysHash,
!!opts.universalDeploy,
);
}
7 changes: 7 additions & 0 deletions yarn-project/aztec.js/src/deployment/protocol_contracts.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { getCanonicalClassRegisterer } from '@aztec/protocol-contracts/class-registerer';
import { getCanonicalInstanceDeployer } from '@aztec/protocol-contracts/instance-deployer';

import { UnsafeContract } from '../contract/unsafe_contract.js';
import { Wallet } from '../wallet/index.js';
Expand All @@ -8,3 +9,9 @@ export function getRegistererContract(wallet: Wallet) {
const { artifact, instance } = getCanonicalClassRegisterer();
return new UnsafeContract(instance, artifact, wallet);
}

/** Returns a Contract wrapper for the instance deployer. */
export function getDeployerContract(wallet: Wallet) {
const { artifact, instance } = getCanonicalInstanceDeployer();
return new UnsafeContract(instance, artifact, wallet);
}
15 changes: 6 additions & 9 deletions yarn-project/end-to-end/src/e2e_deploy_contract.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,10 @@ import {
import {
broadcastPrivateFunction,
broadcastUnconstrainedFunction,
deployInstance,
registerContractClass,
} from '@aztec/aztec.js/deployment';
import { ContractClassIdPreimage, Point, PublicKey, computePublicKeysHash } from '@aztec/circuits.js';
import { ContractClassIdPreimage, Point, PublicKey } from '@aztec/circuits.js';
import { siloNullifier } from '@aztec/circuits.js/abis';
import { FunctionSelector, FunctionType } from '@aztec/foundation/abi';
import { ContractInstanceDeployerContract, StatefulTestContract } from '@aztec/noir-contracts';
Expand Down Expand Up @@ -307,16 +308,12 @@ describe('e2e_deploy_contract', () => {
const salt = Fr.random();
const portalAddress = EthAddress.random();
publicKey = Point.random();
const publicKeysHash = computePublicKeysHash(publicKey);

instance = getContractInstanceFromDeployParams(artifact, initArgs, salt, publicKey, portalAddress);
logger(
`Deploying contract instance at ${instance.address.toString()} with class id ${instance.contractClassId.toString()}`,
);
const tx = await deployer.methods
.deploy(salt, contractClass.id, instance.initializationHash, portalAddress, publicKeysHash, false)
.send()
.wait();
const { address, contractClassId } = instance;
logger(`Deploying contract instance at ${address.toString()} class id ${contractClassId.toString()}`);

const tx = await deployInstance(wallet, instance).send().wait();
deployTxHash = tx.txHash;
});

Expand Down
2 changes: 1 addition & 1 deletion yarn-project/protocol-contracts/README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# Protocol Contracts

Canonical Noir contracts used to power the Aztec Network protocol.
Noir contract artifacts used to power the Aztec Network protocol, along with their canonical deployment information.

Includes:
- Contract class registerer
Expand Down

0 comments on commit 99c3fba

Please sign in to comment.