import { ContractDeploymentData, FunctionData, TxContext, getContractDeploymentInfo, } from '@aztec/circuits.js';
import { encodeArguments } from '@aztec/foundation/abi';
import { EthAddress } from '@aztec/foundation/eth-address';
import { Fr } from '@aztec/foundation/fields';
import { PackedArguments, TxExecutionRequest } from '@aztec/types';
import { BaseContractInteraction } from '../contract/base_contract_interaction.js';
import { DeploySentTx } from './deploy_sent_tx.js';
/**
 * Creates a TxRequest from a contract ABI, for contract deployment.
 * Extends the ContractFunctionInteraction class.
 */
export class DeployMethod extends BaseContractInteraction {
    constructor(publicKey, pxe, artifact, args = []) {
        super(pxe);
        this.publicKey = publicKey;
        this.pxe = pxe;
        this.artifact = artifact;
        this.args = args;
        /** The complete address of the contract. */
        this.completeAddress = undefined;
        const constructorArtifact = artifact.functions.find(f => f.name === 'constructor');
        if (!constructorArtifact)
            throw new Error('Cannot find constructor in the artifact.');
        this.constructorArtifact = constructorArtifact;
    }
    /**
     * Create a contract deployment transaction, given the deployment options.
     * This function internally calls `request()` and `sign()` methods to prepare
     * the transaction for deployment. The resulting signed transaction can be
     * later sent using the `send()` method.
     *
     * @param options - An object containing optional deployment settings, including portalContract, contractAddressSalt, and from.
     * @returns A Promise resolving to an object containing the signed transaction data and other relevant information.
     */
    async create(options = {}) {
        const portalContract = options.portalContract ?? EthAddress.ZERO;
        const contractAddressSalt = options.contractAddressSalt ?? Fr.random();
        const { chainId, protocolVersion } = await this.pxe.getNodeInfo();
        const { completeAddress, constructorHash, functionTreeRoot } = await getContractDeploymentInfo(this.artifact, this.args, contractAddressSalt, this.publicKey);
        const contractDeploymentData = new ContractDeploymentData(this.publicKey, constructorHash, functionTreeRoot, contractAddressSalt, portalContract);
        const txContext = new TxContext(false, false, true, contractDeploymentData, new Fr(chainId), new Fr(protocolVersion));
        const args = encodeArguments(this.constructorArtifact, this.args);
        const functionData = FunctionData.fromAbi(this.constructorArtifact);
        const execution = { args, functionData, to: completeAddress.address };
        const packedArguments = PackedArguments.fromArgs(execution.args);
        const txRequest = TxExecutionRequest.from({
            origin: execution.to,
            functionData: execution.functionData,
            argsHash: packedArguments.hash,
            txContext,
            packedArguments: [packedArguments],
            authWitnesses: [],
        });
        this.txRequest = txRequest;
        this.completeAddress = completeAddress;
        // TODO: Should we add the contracts to the DB here, or once the tx has been sent or mined?
        await this.pxe.addContracts([{ artifact: this.artifact, completeAddress, portalContract }]);
        return this.txRequest;
    }
    /**
     * Send the contract deployment transaction using the provided options.
     * This function extends the 'send' method from the ContractFunctionInteraction class,
     * allowing us to send a transaction specifically for contract deployment.
     *
     * @param options - An object containing various deployment options such as portalContract, contractAddressSalt, and from.
     * @returns A SentTx object that returns the receipt and the deployed contract instance.
     */
    send(options = {}) {
        const txHashPromise = super.send(options).getTxHash();
        return new DeploySentTx(this.artifact, this.pxe, txHashPromise, this.completeAddress);
    }
    /**
     * Simulate the request.
     * @param options - Deployment options.
     * @returns The simulated tx.
     */
    simulate(options) {
        return super.simulate(options);
    }
}
//# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoiZGVwbG95X21ldGhvZC5qcyIsInNvdXJjZVJvb3QiOiIiLCJzb3VyY2VzIjpbIi4uLy4uL3NyYy9jb250cmFjdF9kZXBsb3llci9kZXBsb3lfbWV0aG9kLnRzIl0sIm5hbWVzIjpbXSwibWFwcGluZ3MiOiJBQUFBLE9BQU8sRUFFTCxzQkFBc0IsRUFDdEIsWUFBWSxFQUNaLFNBQVMsRUFDVCx5QkFBeUIsR0FDMUIsTUFBTSxvQkFBb0IsQ0FBQztBQUM1QixPQUFPLEVBQXNDLGVBQWUsRUFBRSxNQUFNLHVCQUF1QixDQUFDO0FBQzVGLE9BQU8sRUFBRSxVQUFVLEVBQUUsTUFBTSwrQkFBK0IsQ0FBQztBQUMzRCxPQUFPLEVBQUUsRUFBRSxFQUFFLE1BQU0sMEJBQTBCLENBQUM7QUFDOUMsT0FBTyxFQUFPLGVBQWUsRUFBaUIsa0JBQWtCLEVBQUUsTUFBTSxjQUFj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