import { getSchnorrWallet } from '@aztec/accounts/schnorr';
import { Fr, createLogger, getContractInstanceFromInstantiationParams } from '@aztec/aztec.js';
import { createSnapshotManager, deployAccounts } from '../fixtures/snapshot_manager.js';
const { E2E_DATA_PATH: dataPath } = process.env;
export class DeployTest {
    snapshotManager;
    logger;
    pxe;
    wallet;
    defaultAccountAddress;
    aztecNode;
    aztecNodeAdmin;
    constructor(testName){
        this.logger = createLogger(`e2e:e2e_deploy_contract:${testName}`);
        this.snapshotManager = createSnapshotManager(`e2e_deploy_contract/${testName}`, dataPath);
    }
    async setup() {
        await this.applyInitialAccountSnapshot();
        const context = await this.snapshotManager.setup();
        ({ pxe: this.pxe, aztecNode: this.aztecNode } = context);
        this.aztecNodeAdmin = context.aztecNode;
        return this;
    }
    async teardown() {
        await this.snapshotManager.teardown();
    }
    async applyInitialAccountSnapshot() {
        await this.snapshotManager.snapshot('initial_account', deployAccounts(1, this.logger), async ({ deployedAccounts }, { pxe })=>{
            const wallets = await Promise.all(deployedAccounts.map((a)=>getSchnorrWallet(pxe, a.address, a.signingKey)));
            wallets.forEach((w, i)=>this.logger.verbose(`Wallet ${i} address: ${w.getAddress()}`));
            this.wallet = wallets[0];
            this.defaultAccountAddress = this.wallet.getAddress();
        });
    }
    async registerContract(wallet, contractArtifact, opts = {}) {
        const { salt, publicKeys, initArgs, constructorName, deployer } = opts;
        const instance = await getContractInstanceFromInstantiationParams(contractArtifact.artifact, {
            constructorArgs: initArgs ?? [],
            constructorArtifact: constructorName,
            salt,
            publicKeys,
            deployer
        });
        await wallet.registerContract({
            artifact: contractArtifact.artifact,
            instance
        });
        return contractArtifact.at(instance.address, wallet);
    }
    async registerRandomAccount() {
        const completeAddress = await this.pxe.registerAccount(Fr.random(), Fr.random());
        return completeAddress.address;
    }
}
