Skip to content

Commit

Permalink
feat: add deployed contract to PXE from CLI (#2850)
Browse files Browse the repository at this point in the history
Please provide a paragraph or two giving a summary of the change,
including relevant motivation and context.

# Checklist:
Remove the checklist to signal you've completed it. Enable auto-merge if
the PR is ready to merge.
- [ ] If the pull request requires a cryptography review (e.g.
cryptographic algorithm implementations) I have added the 'crypto' tag.
- [ ] I have reviewed my diff in github, line by line and removed
unexpected formatting changes, testing logs, or commented-out code.
- [ ] Every change is related to the PR description.
- [ ] I have
[linked](https://docs.github.com/en/issues/tracking-your-work-with-issues/linking-a-pull-request-to-an-issue)
this pull request to relevant issues (if any exist).
  • Loading branch information
alexghr authored Oct 13, 2023
1 parent dbef55f commit 5bad3e3
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 4 deletions.
42 changes: 39 additions & 3 deletions yarn-project/cli/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import {
AztecAddress,
Contract,
ContractDeployer,
EthAddress,
Fr,
GrumpkinScalar,
NotePreimage,
Expand Down Expand Up @@ -33,6 +35,7 @@ import {
getFunctionArtifact,
getTxSender,
parseAztecAddress,
parseEthereumAddress,
parseField,
parseFields,
parseOptionalAztecAddress,
Expand Down Expand Up @@ -218,14 +221,19 @@ export function getProgram(log: LogFn, debugLogger: DebugLogger): Command {
const args = encodeArgs(rawArgs, constructorArtifact!.parameters);
debugLogger(`Encoded arguments: ${args.join(', ')}`);

const tx = deployer.deploy(...args).send({ contractAddressSalt: salt });
const deploy = deployer.deploy(...args);

await deploy.create({ contractAddressSalt: salt });
const tx = deploy.send({ contractAddressSalt: salt });
const txHash = await tx.getTxHash();
debugLogger(`Deploy tx sent with hash ${txHash}`);
if (wait) {
const deployed = await tx.wait();
log(`\nContract deployed at ${deployed.contractAddress!.toString()}\n`);
log(`\nContract deployed at ${deployed.contract.completeAddress.address.toString()}\n`);
log(`Contract partial address ${deployed.contract.completeAddress.partialAddress.toString()}\n`);
} else {
log(`\nContract Address: ${tx.completeContractAddress?.address.toString() ?? 'N/A'}`);
log(`\nContract Address: ${deploy.completeAddress?.address.toString() ?? 'N/A'}`);
log(`Contract Partial Address: ${deploy.completeAddress?.partialAddress.toString() ?? 'N/A'}`);
log(`Deployment transaction hash: ${txHash}\n`);
}
});
Expand All @@ -247,6 +255,34 @@ export function getProgram(log: LogFn, debugLogger: DebugLogger): Command {
else log(`\nNo contract found at ${address.toString()}\n`);
});

program
.command('add-contract')
.description(
'Adds an existing contract to the PXE. This is useful if you have deployed a contract outside of the PXE and want to use it with the PXE.',
)
.requiredOption(
'-c, --contract-artifact <fileLocation>',
"A compiled Aztec.nr contract's ABI in JSON format or name of a contract ABI exported by @aztec/noir-contracts",
)
.requiredOption('-ca, --contract-address <address>', 'Aztec address of the contract.', parseAztecAddress)
.requiredOption('-pa, --partial-address <address>', 'Partial address of the contract', parsePartialAddress)
.option('-p, --public-key <public key>', 'Optional public key for this contract', parsePublicKey)
.option('--portal-address <address>', 'Optional address to a portal contract on L1', parseEthereumAddress)
.addOption(pxeOption)
.action(async options => {
const artifact = await getContractArtifact(options.contractArtifact, log);
const contractAddress: AztecAddress = options.contractAddress;
const completeAddress = new CompleteAddress(
contractAddress,
options.publicKey ?? Fr.ZERO,
options.partialAddress,
);
const portalContract: EthAddress = options.portalContract ?? EthAddress.ZERO;
const client = await createCompatibleClient(options.rpcUrl, debugLogger);

await client.addContracts([{ artifact, completeAddress, portalContract }]);
log(`\nContract added to PXE at ${contractAddress.toString()}\n`);
});
program
.command('get-tx-receipt')
.description('Gets the receipt for the specified transaction hash.')
Expand Down
16 changes: 15 additions & 1 deletion yarn-project/cli/src/utils.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { AztecAddress, Fr, FunctionSelector, GrumpkinScalar, PXE, Point, TxHash } from '@aztec/aztec.js';
import { AztecAddress, EthAddress, Fr, FunctionSelector, GrumpkinScalar, PXE, Point, TxHash } from '@aztec/aztec.js';
import { L1ContractArtifactsForDeployment, createEthereumChain, deployL1Contracts } from '@aztec/ethereum';
import { ContractArtifact } from '@aztec/foundation/abi';
import { DebugLogger, LogFn } from '@aztec/foundation/log';
Expand Down Expand Up @@ -211,6 +211,20 @@ export function parseAztecAddress(address: string): AztecAddress {
}
}

/**
* Parses an Ethereum address from a string.
* @param address - A serialized Ethereum address
* @returns An Ethereum address
* @throws InvalidArgumentError if the input string is not valid.
*/
export function parseEthereumAddress(address: string): EthAddress {
try {
return EthAddress.fromString(address);
} catch {
throw new InvalidArgumentError(`Invalid address: ${address}`);
}
}

/**
* Parses an AztecAddress from a string.
* @param address - A serialized Aztec address
Expand Down

0 comments on commit 5bad3e3

Please sign in to comment.