import Debug from 'debug'; import _ from 'lodash'; import * as viem from 'viem'; import { z } from 'zod'; import { mergeTemplateAccesses } from '../access-recorder'; import { ARACHNID_DEFAULT_DEPLOY_ADDR, ensureArachnidCreate2Exists, makeArachnidCreate2Txn } from '../create2'; import { CannonError, handleTxnError } from '../error'; import { deploySchema } from '../schemas'; import { ChainArtifacts, ChainBuilderContext, ContractArtifact } from '../types'; import { encodeDeployData, getContractDefinitionFromPath, getMergedAbiFromContractPaths } from '../util'; import { template } from '../utils/template'; import { CannonAction } from '../actions'; import { getBlockRetried } from '../helpers'; const debug = Debug('cannon:builder:deploy'); /** * Available properties for contract operation * @public * @group Contract */ export type Config = z.infer; export interface ContractOutputs { abi: string; address: string; deployTxnHash: string; } function resolveBytecode( artifactData: ContractArtifact, config: Config ): [viem.Hex, { [sourceName: string]: { [libName: string]: string } }] { let injectedBytecode = artifactData.bytecode; const linkedLibraries: { [sourceName: string]: { [libName: string]: string } } = {}; for (const file in artifactData.linkReferences) { for (const lib in artifactData.linkReferences[file]) { // get the lib from the config const libraryAddress = _.get(config, `libraries.${lib}`); if (!libraryAddress) { throw new Error(`library not defined: ${lib}`); } debug('lib ref', lib, libraryAddress); // afterwards, inject link references const linkReferences = artifactData.linkReferences[file][lib]; for (const ref of linkReferences) { injectedBytecode = (injectedBytecode.substring(0, 2 + ref.start * 2) + libraryAddress.substring(2) + injectedBytecode.substring(2 + (ref.start + ref.length) * 2)) as viem.Hex; _.set(linkedLibraries, [file, lib], libraryAddress); } } } return [injectedBytecode, linkedLibraries]; } function checkConstructorArgs(abi: viem.Abi, args: any[] | undefined) { const abiConstructor = (abi.find((v) => v.type === 'constructor') as unknown as viem.AbiFunction) || { type: 'constructor', inputs: [], }; const neededArgs = abiConstructor.inputs || []; const suppliedArgs = args || []; if (suppliedArgs.length !== neededArgs.length) { throw new Error( `incorrect number of constructor arguments to deploy contract. supplied: ${suppliedArgs.length}, expected: ${neededArgs.length}` ); } } function generateOutputs( config: Config, ctx: ChainBuilderContext, artifactData: ContractArtifact, deployTxn: viem.TransactionReceipt | null, deployTxnBlock: viem.Block | null, deployAddress: viem.Address, currentLabel: string ): ChainArtifacts { const [, linkedLibraries] = resolveBytecode(artifactData, config); let abi = artifactData.abi; // override abi? if (config.abi) { if (config.abi.trimStart().startsWith('[')) { // Allow to pass in a literal abi string abi = JSON.parse(config.abi); } else { // Load the abi from another contract const implContract = getContractDefinitionFromPath(ctx, config.abi); if (!implContract) { throw new Error(`previously deployed contract with name ${config.abi} for abi not found`); } abi = implContract.abi; } } else if (config.abiOf) { abi = getMergedAbiFromContractPaths(ctx, config.abiOf); } return { contracts: { [currentLabel.split('.')[1] || '']: { address: viem.getAddress(deployAddress), abi, constructorArgs: config.args || [], linkedLibraries, deployTxnHash: deployTxn?.transactionHash || '', deployTxnBlockNumber: deployTxn?.blockNumber.toString() || '', deployTimestamp: deployTxnBlock?.timestamp.toString() || '', sourceName: artifactData.sourceName, contractName: artifactData.contractName, deployedOn: currentLabel!, highlight: config.highlight, gasUsed: Number(deployTxn?.gasUsed) || 0, gasCost: deployTxn?.effectiveGasPrice?.toString() || '0', }, }, }; } // ensure the specified contract is already deployed // if not deployed, deploy the specified hardhat contract with specfied options, export address, abi, etc. // if already deployed, reexport deployment options for usage downstream and exit with no changes const deploySpec = { label: 'deploy', validate: _.cloneDeep(deploySchema), async getState(runtime, ctx, config, packageState) { const parsedConfig = this.configInject(ctx, config, packageState); return [ [ resolveBytecode(await runtime.getArtifact!(parsedConfig.artifact), parsedConfig)[0], // bytecode parsedConfig.args?.map((v) => (typeof v === 'string' ? v : JSON.stringify(v))) || [], // args parsedConfig.salt, // salt parsedConfig.value || [], // value ], { bytecode: resolveBytecode(await runtime.getArtifact!(parsedConfig.artifact), parsedConfig)[0], args: parsedConfig.args?.map((v) => (typeof v === 'string' ? v : JSON.stringify(v))) || [], salt: parsedConfig.salt, value: parsedConfig.value || [], }, ]; }, configInject(ctx, config) { config = _.cloneDeep(config); config.from = template(config.from || '', ctx); config.nonce = template(config.nonce || '', ctx); config.artifact = template(config.artifact, ctx); config.value = template(config.value || '', ctx); config.abi = template(config.abi || '', ctx); if (config.abiOf) { config.abiOf = _.map(config.abiOf, (v) => template(v, ctx)); } if (config.args) { config.args = _.map(config.args, (a) => { // just convert it to a JSON string when. This will allow parsing of complicated nested structures return JSON.parse(template(JSON.stringify(a), ctx)); }); } if (config.libraries) { config.libraries = _.mapValues(config.libraries, (a) => { return template(a, ctx); }); } if (config.salt) { config.salt = template(config.salt, ctx); } if (config?.overrides?.gasLimit) { config.overrides.gasLimit = template(config.overrides.gasLimit, ctx); } return config; }, getInputs(config, engine) { let accesses = engine.computeTemplateAccesses(config.from); accesses = mergeTemplateAccesses(accesses, engine.computeTemplateAccesses(config.nonce)); accesses = mergeTemplateAccesses(accesses, engine.computeTemplateAccesses(config.artifact)); accesses = mergeTemplateAccesses(accesses, engine.computeTemplateAccesses(config.value)); accesses = mergeTemplateAccesses(accesses, engine.computeTemplateAccesses(config.abi)); accesses = mergeTemplateAccesses(accesses, engine.computeTemplateAccesses(config.salt)); if (config.abiOf) { _.forEach(config.abiOf, (v) => (accesses = mergeTemplateAccesses(accesses, engine.computeTemplateAccesses(v)))); } if (config.args) { _.forEach( config.args, (v) => (accesses = mergeTemplateAccesses(accesses, engine.computeTemplateAccesses(JSON.stringify(v)))) ); } if (config.libraries) { _.forEach(config.libraries, (v) => (accesses = mergeTemplateAccesses(accesses, engine.computeTemplateAccesses(v)))); } if (config?.overrides?.gasLimit) { accesses = mergeTemplateAccesses(accesses, engine.computeTemplateAccesses(config.overrides.gasLimit)); } return accesses; }, getOutputs(_, packageState) { const stepName = packageState.currentLabel.split('.')[1]; return [`contracts.${stepName}`, stepName]; }, async exec(runtime, ctx, config, packageState) { debug('exec', config); // sanity check that any connected libraries are bytecoded for (const lib in config.libraries || {}) { if ((await runtime.provider.getCode({ address: config.libraries![lib] as viem.Address })) === '0x') { throw new Error(`library ${lib} has no bytecode. This is most likely a missing dependency or bad state.`); } } const artifactData = await runtime.getArtifact!(config.artifact); if (!artifactData) { throw new Error( `bytecode/abi for artifact ${config.artifact} not found. please double check the contract name and your build configuration` ); } const [injectedBytecode] = resolveBytecode(artifactData, config); // finally, deploy // check that the correct number of deploy args are given checkConstructorArgs(artifactData.abi, config.args); const txn = { data: encodeDeployData({ abi: artifactData.abi, bytecode: injectedBytecode, args: config.args || [], }), }; const overrides: any = {}; if (config.overrides?.gasLimit) { overrides.gasLimit = config.overrides.gasLimit; } if (runtime.gasPrice) { overrides.gasPrice = runtime.gasPrice; } if (runtime.gasFee) { overrides.maxFeePerGas = runtime.gasFee; } if (runtime.priorityGasFee) { overrides.maxPriorityFeePerGas = runtime.priorityGasFee; } let receipt: viem.TransactionReceipt | null = null; let deployAddress: viem.Address; try { if (config.create2) { const arachnidDeployerAddress = await ensureArachnidCreate2Exists( runtime, typeof config.create2 === 'string' ? (config.create2 as viem.Address) : ARACHNID_DEFAULT_DEPLOY_ADDR ); debug('performing arachnid create2'); const [create2Txn, addr] = makeArachnidCreate2Txn(config.salt || '', txn.data!, arachnidDeployerAddress); debug(`create2: deploy ${addr} by ${arachnidDeployerAddress}`); const bytecode = await runtime.provider.getCode({ address: addr }); if (bytecode && bytecode !== '0x') { debug('create2 contract already completed'); // the cannon state does not think a contract should be deployed, but the on-chain state says a contract // is deployed. this could be a mistake. alert the user and explain how to override if (config.ifExists !== 'continue') { throw new CannonError( `The contract at the create2 destination ${addr} is already deployed, but the Cannon state does not recognize that this contract has already been deployed. This typically indicates incorrect upgrade configuration. Please confirm if this contract should already be deployed or not, and if you want to continue the build as-is, add 'ifExists = "continue"' to the step definition`, 'CREATE2_COLLISION' ); } } else { const signer = config.from ? await runtime.getSigner(config.from as viem.Address) : await runtime.getDefaultSigner!(txn, config.salt); const fullCreate2Txn = _.assign(create2Txn, overrides, { account: signer.wallet.account || signer.address }); debug('final create2 txn', fullCreate2Txn); const preparedTxn = await runtime.provider.prepareTransactionRequest(fullCreate2Txn); const hash = await signer.wallet.sendTransaction(preparedTxn as any); receipt = await runtime.provider.waitForTransactionReceipt({ hash }); debug('arachnid create2 complete', receipt); } deployAddress = addr; } else { const curAccountNonce = config.from ? await runtime.provider.getTransactionCount({ address: config.from as viem.Address }) : 0; if (config.from && config.nonce?.length && parseInt(config.nonce) < curAccountNonce) { const contractAddress = viem.getContractAddress({ from: config.from as viem.Address, nonce: BigInt(config.nonce), }); debug(`contract appears already deployed to address ${contractAddress} (nonce too high)`); // check that the contract bytecode that was deployed matches the requested const actualBytecode = await runtime.provider.getCode({ address: contractAddress }); // we only check the length because solidity puts non-substantial changes (ex. comments) in bytecode and that // shouldn't trigger any significant change. And also this is just kind of a sanity check so just verifying the // length should be sufficient if (!actualBytecode || artifactData.deployedBytecode.length !== actualBytecode.length) { debug('bytecode does not match up', artifactData.deployedBytecode, actualBytecode); // this can happen normally. for now lets just disable it for now /*throw new Error( `the address at ${config.from!} should have deployed a contract at nonce ${config.nonce!} at address ${contractAddress}, but the bytecode does not match up. actual bytecode length: ${ (actualBytecode || '').length }` );*/ } deployAddress = contractAddress; } else { const signer = config.from ? await runtime.getSigner(config.from as viem.Address) : await runtime.getDefaultSigner!(txn, config.salt); if (config.overrides?.simulate) { // if the code goes here, it means that the Create2 deployment failed // and prepareTransactionRequest will throw an error with the underlying revert message await runtime.provider.prepareTransactionRequest( _.assign(txn, overrides, { account: signer.wallet.account || signer.address }) ); throw new Error( 'The CREATE2 contract seems to be failing in the constructor. However, we were not able to get a stack trace.' ); } else { const preparedTxn = await runtime.provider.prepareTransactionRequest( _.assign(txn, overrides, { account: signer.wallet.account || signer.address }) ); const hash = await signer.wallet.sendTransaction(preparedTxn as any); receipt = await runtime.provider.waitForTransactionReceipt({ hash }); deployAddress = receipt.contractAddress!; } } } } catch (error: any) { // catch an error when it comes from create2 deployer if (!(error instanceof CannonError && error.code === 'CREATE2_COLLISION') && config.create2) { // arachnid create2 does not return the underlying revert message. // ref: https://github.com/Arachnid/deterministic-deployment-proxy/blob/master/source/deterministic-deployment-proxy.yul#L13 // simulate a non-create2 deployment to get the underlying revert message const simulateConfig = { ...config, create2: false, overrides: { ...config.overrides, simulate: true, }, }; return await this.exec(runtime, ctx, simulateConfig, packageState); } else { // catch an error when it comes from normal deployment const contractArtifact = generateOutputs( config, ctx, artifactData, receipt, null, // note: send zero address since there is no contract address viem.zeroAddress, packageState.currentLabel ); return await handleTxnError(contractArtifact, runtime.provider, error); } } const block = receipt ? await getBlockRetried(runtime.provider, receipt.blockHash) : null; return generateOutputs(config, ctx, artifactData, receipt, block, deployAddress, packageState.currentLabel); }, async importExisting(runtime, ctx, config, packageState, existingKeys) { if (existingKeys.length != 1) { throw new Error( 'a contract can only be deployed on one transaction, so you can only supply one hash transaction to import' ); } const artifactData = await runtime.getArtifact!(config.artifact); const txn = await runtime.provider.getTransaction({ hash: existingKeys[0] as viem.Hash }); const receipt = await runtime.provider.getTransactionReceipt({ hash: existingKeys[0] as viem.Hash }); // We need to compute from the import data let contractAddress; // In the case that the contract address is given in the transaction receipt: It was a create (1) transaction if (receipt.contractAddress) { contractAddress = receipt.contractAddress; // in the case that the contract address is *NOT* given, its CREATE2 through arachnid (or, not a contract creation transaction at all). } else if (txn.to && txn.input && txn.input.length > 66) { // Note: the `from` address is actually the `to` address here because this function wants the address of the contract executing the deployment of the contract contractAddress = viem.getContractAddress({ opcode: 'CREATE2', from: txn.to, salt: viem.sliceHex(txn.input, 0, 32), bytecode: viem.sliceHex(txn.input, 32), }); } else { throw new Error('The transaction hash you provided does not appear to deploy a contract'); } const block = await runtime.provider.getBlock({ blockNumber: txn?.blockNumber }); return generateOutputs(config, ctx, artifactData, receipt, block, contractAddress!, packageState.currentLabel); }, } satisfies CannonAction; export default deploySpec;