import { GasSettings } from '@aztec/circuits.js';
import { createDebugLogger } from '@aztec/foundation/log';
import { getGasLimits } from './get_gas_limits.js';
import { ProvenTx } from './proven_tx.js';
import { SentTx } from './sent_tx.js';
/**
 * Base class for an interaction with a contract, be it a deployment, a function call, or a batch.
 * Implements the sequence create/simulate/send.
 */
export class BaseContractInteraction {
    constructor(wallet) {
        this.wallet = wallet;
        this.log = createDebugLogger('aztec:js:contract_interaction');
    }
    /**
     * Creates a transaction execution request, simulates and proves it. Differs from .prove in
     * that its result does not include the wallet nor the composed tx object, but only the proving result.
     * This object can then be used to either create a ProvenTx ready to be sent, or directly send the transaction.
     * @param options - optional arguments to be used in the creation of the transaction
     * @returns The proving result.
     */
    async proveInternal(options = {}) {
        const txRequest = await this.create(options);
        const txSimulationResult = await this.wallet.simulateTx(txRequest, !options.skipPublicSimulation, undefined, true);
        return await this.wallet.proveTx(txRequest, txSimulationResult.privateExecutionResult);
    }
    /**
     * Proves a transaction execution request and returns a tx object ready to be sent.
     * @param options - optional arguments to be used in the creation of the transaction
     * @returns The resulting transaction
     */
    async prove(options = {}) {
        const txProvingResult = await this.proveInternal(options);
        return new ProvenTx(this.wallet, txProvingResult.toTx());
    }
    /**
     * Sends a transaction to the contract function with the specified options.
     * This function throws an error if called on an unconstrained function.
     * It creates and signs the transaction if necessary, and returns a SentTx instance,
     * which can be used to track the transaction status, receipt, and events.
     * @param options - An optional object containing 'from' property representing
     * the AztecAddress of the sender. If not provided, the default address is used.
     * @returns A SentTx instance for tracking the transaction status and information.
     */
    send(options = {}) {
        const promise = (async () => {
            const txProvingResult = await this.proveInternal(options);
            return this.wallet.sendTx(txProvingResult.toTx());
        })();
        return new SentTx(this.wallet, promise);
    }
    /**
     * Estimates gas for a given tx request and returns gas limits for it.
     * @param opts - Options.
     * @returns Gas limits.
     */
    async estimateGas(opts) {
        const txRequest = await this.create({ ...opts, estimateGas: false });
        const simulationResult = await this.wallet.simulateTx(txRequest, true);
        const { totalGas: gasLimits, teardownGas: teardownGasLimits } = getGasLimits(simulationResult);
        return { gasLimits, teardownGasLimits };
    }
    /**
     * Helper method to return fee options based on the user opts, estimating tx gas if needed.
     * @param request - Request to execute for this interaction.
     * @returns Fee options for the actual transaction.
     */
    async getFeeOptionsFromEstimatedGas(request) {
        const fee = request.fee;
        if (fee) {
            const txRequest = await this.wallet.createTxExecutionRequest(request);
            const simulationResult = await this.wallet.simulateTx(txRequest, true);
            const { totalGas: gasLimits, teardownGas: teardownGasLimits } = getGasLimits(simulationResult);
            this.log.debug(`Estimated gas limits for tx: DA=${gasLimits.daGas} L2=${gasLimits.l2Gas} teardownDA=${teardownGasLimits.daGas} teardownL2=${teardownGasLimits.l2Gas}`);
            const gasSettings = GasSettings.default({ ...fee.gasSettings, gasLimits, teardownGasLimits });
            return { ...fee, gasSettings };
        }
        return fee;
    }
}
//# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoiYmFzZV9jb250cmFjdF9pbnRlcmFjdGlvbi5qcyIsInNvdXJjZVJvb3QiOiIiLCJzb3VyY2VzIjpbIi4uLy4uL3NyYy9jb250cmFjdC9iYXNlX2NvbnRyYWN0X2ludGVyYWN0aW9uLnRzIl0sIm5hbWVzIjpbXSwibWFwcGluZ3MiOiJBQUNBLE9BQU8sRUFBVyxXQUFXLEVBQUUsTUFBTSxvQkFBb0IsQ0FBQztBQUMxRCxPQUFPLEVBQUUsaUJBQWlCLEVBQUUsTUFBTSx1QkFBdUIsQ0FBQztBQUkxRCxPQUFPLEVBQUUsWUFBWSxFQUFFLE1BQU0scUJBQXFCLENBQUM7QUFDbkQsT0FBTyxFQUFFLFFBQVEsRUFBRSxNQUFNLGdCQUFnQixDQUFDO0FBQzFDLE9BQU8sRUFBRSxNQUFNLEVBQUUsTUFBTSxjQUFjLENBQUM7QUFtQnRDOzs7R0FHRztBQUNILE1BQU0sT0FBZ0IsdUJBQXVCO0lBRzNDLFlBQXNCLE1BQWM7UUFBZCxXQUFNLEdBQU4sTUFB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