import { getContractInstanceFromInstantiationParams } from '@aztec/aztec.js/contracts';
import { Fr } from '@aztec/aztec.js/fields';
import { createLogger } from '@aztec/aztec.js/log';
import { deployAccounts, setup, teardown } from '../fixtures/setup.js';
export class DeployTest {
    context;
    logger;
    wallet;
    defaultAccountAddress;
    aztecNode;
    aztecNodeAdmin;
    constructor(testName){
        this.logger = createLogger(`e2e:e2e_deploy_contract:${testName}`);
    }
    async setup() {
        this.logger.info('Setting up test environment');
        this.context = await setup(0, {
            fundSponsoredFPC: true,
            skipAccountDeployment: true
        });
        this.aztecNode = this.context.aztecNodeService;
        this.wallet = this.context.wallet;
        this.aztecNodeAdmin = this.context.aztecNodeService;
        await this.applyInitialAccount();
        return this;
    }
    async teardown() {
        await teardown(this.context);
    }
    async applyInitialAccount() {
        this.logger.info('Applying initial account setup');
        const { deployedAccounts } = await deployAccounts(1, this.logger)({
            wallet: this.context.wallet,
            initialFundedAccounts: this.context.initialFundedAccounts
        });
        this.defaultAccountAddress = deployedAccounts[0].address;
    }
    async registerContract(wallet, contractArtifact, opts = {}) {
        const { salt, publicKeys, initArgs, constructorName, deployer } = opts;
        const instance = await getContractInstanceFromInstantiationParams(contractArtifact.artifact, {
            constructorArgs: initArgs ?? [],
            constructorArtifact: constructorName,
            salt: salt ?? Fr.random(),
            publicKeys,
            deployer
        });
        await wallet.registerContract(instance, contractArtifact.artifact);
        return contractArtifact.at(instance.address, wallet);
    }
}
