Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(cli): Add --impersonate flag to the build command #1499

Open
wants to merge 10 commits into
base: dev
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
58 changes: 12 additions & 46 deletions packages/cli/src/commands/build.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,12 +42,12 @@ interface Params {
upgradeFrom?: string;
pkgInfo: any;
getArtifact?: (name: string) => Promise<ContractArtifact>;
getSigner?: (addr: viem.Address) => Promise<CannonSigner>;
getSigner: (addr: viem.Address) => Promise<CannonSigner>;
getDefaultSigner?: () => Promise<CannonSigner>;
projectDirectory?: string;
overrideResolver?: CannonRegistry;
wipe?: boolean;
persist?: boolean;
dryRun?: boolean;
plugins?: boolean;
privateSourceCode?: boolean;
rpcUrl?: string;
Expand All @@ -70,7 +70,7 @@ export async function build({
getDefaultSigner,
overrideResolver,
wipe = false,
persist = true,
dryRun,
plugins = true,
privateSourceCode = false,
rpcUrl,
Expand All @@ -85,7 +85,7 @@ export async function build({
throw new Error('wipe and upgradeFrom are mutually exclusive. Please specify one or the other');
}

if (!persist && rpcUrl) {
if (dryRun && rpcUrl) {
log(
yellowBright(bold('⚠️ This is a simulation. No changes will be made to the chain. No package data will be saved.\n'))
);
Expand Down Expand Up @@ -119,29 +119,10 @@ export async function build({
provider,
chainId,
getArtifact,
getSigner:
getSigner ||
async function (addr: viem.Address) {
const client = provider as unknown as viem.TestClient;

// on test network any user can be conjured
await client.impersonateAccount({ address: addr });
await client.setBalance({ address: addr, value: viem.parseEther('10000') });

return {
address: addr,
wallet: viem.createWalletClient({
account: addr,
chain: provider.chain,
transport: viem.custom(provider.transport),
}),
};
},

getSigner,
getDefaultSigner,

snapshots: chainId === CANNON_CHAIN_ID,
allowPartialDeploy: chainId !== CANNON_CHAIN_ID && persist,
allowPartialDeploy: chainId !== CANNON_CHAIN_ID,
// ChainBuilderRuntime uses publicSourceCode to determine if source code should be included in the package
publicSourceCode: !privateSourceCode,
gasPrice,
Expand Down Expand Up @@ -233,22 +214,9 @@ export async function build({

log(bold(`Building the chain (ID ${chainId})${rpcUrlMsg ? ' via ' + hideApiKey(rpcUrlMsg) : ''}...`));

let defaultSignerAddress: string;
if (getDefaultSigner) {
const defaultSigner = await getDefaultSigner();
if (defaultSigner) {
defaultSignerAddress = defaultSigner.address;
log(`Using ${defaultSignerAddress}`);
} else {
log();
log(bold(red('Signer not found.')));
log(
red(
'Provide a signer to execute this build. Add the --private-key option or set the env variable CANNON_PRIVATE_KEY.'
)
);
process.exit(1);
}
log(`Using ${defaultSigner.address}`);
}

if (!_.isEmpty(resolvedSettings)) {
Expand Down Expand Up @@ -296,9 +264,7 @@ export async function build({
log(`${' '.repeat(d)} ${green('\u2714')} Successfully performed operation`);
}

if (txn.signer != defaultSignerAddress) {
log(gray(`${' '.repeat(d)} Signer: ${txn.signer}`));
}
log(gray(`${' '.repeat(d)} Signer: ${txn.signer}`));

if (c.target) {
const contractAddress = getContractFromPath(ctx, c.target[0])?.address;
Expand Down Expand Up @@ -374,7 +340,7 @@ export async function build({
}
ctrlcs++;
};
if (persist && chainId != CANNON_CHAIN_ID) {
if (!dryRun && chainId != CANNON_CHAIN_ID) {
process.on('SIGINT', handler);
process.on('SIGTERM', handler);
process.on('SIGQUIT', handler);
Expand Down Expand Up @@ -446,7 +412,7 @@ export async function build({
const metaUrl = await runtime.putBlob(metadata);

// write upgrade-from info on-chain
if (stepsExecuted && persist) {
if (stepsExecuted && !dryRun) {
for (let i = 0; i < 3; i++) {
try {
log(gray('Writing upgrade info...'));
Expand Down Expand Up @@ -498,7 +464,7 @@ export async function build({
' to pin the partial deployment package on IPFS. Then use https://usecannon.com/deploy to collect signatures from a Safe for the skipped operations in the partial deployment package.'
);
} else {
if (!persist) {
if (dryRun) {
log(bold(`💥 ${fullPackageRef} would be successfully built on ${chainName} (Chain ID: ${chainId})`));
log(gray(`Estimated Total Cost: ${viem.formatEther(totalCost)} ${nativeCurrencySymbol}`));
log();
Expand Down Expand Up @@ -541,7 +507,7 @@ export async function build({

const isMainPreset = preset === PackageReference.DEFAULT_PRESET;

if (persist) {
if (!dryRun) {
if (isMainPreset) {
log(
bold(
Expand Down
7 changes: 6 additions & 1 deletion packages/cli/src/commands/config/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,12 @@ export const commandsConfig: CommandsConfig = {
},
{
flags: '--dry-run',
description: 'Simulate building on a local fork rather than deploying on the real network',
description:
'Simulate building on a local fork rather than deploying on the real network. Impersonates all signers.',
},
{
flags: '--impersonate [addresses]',
description: 'Specify a comma separated list of signers to impersonate. Only works with --dry-run',
},
{
flags: '--keep-alive',
Expand Down
4 changes: 3 additions & 1 deletion packages/cli/src/commands/run.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@ export interface RunOptions {
privateKey?: viem.Hash;
upgradeFrom?: string;
getArtifact?: (name: string) => Promise<ContractArtifact>;
getSigner: (addr: viem.Address) => Promise<CannonSigner>;
getDefaultSigner: () => Promise<CannonSigner>;
registryPriority: 'local' | 'onchain' | 'offline';
fundAddresses?: string[];
helpInformation?: string;
Expand Down Expand Up @@ -119,7 +121,7 @@ export async function run(packages: PackageSpecification[], options: RunOptions)
provider,
overrideResolver: resolver,
upgradeFrom: options.upgradeFrom,
persist: false,
dryRun: true,
});

buildOutputs.push({ pkg, outputs });
Expand Down
Loading
Loading