"use strict";
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
    var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
    if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
    else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
    return c > 3 && r && Object.defineProperty(target, key, r), r;
};
var __metadata = (this && this.__metadata) || function (k, v) {
    if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
};
var __param = (this && this.__param) || function (paramIndex, decorator) {
    return function (target, key) { decorator(target, key, paramIndex); }
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.QueryManager = void 0;
const LogStoreQueryManager_json_1 = require("@logsn/contracts/artifacts/src/QueryManager.sol/LogStoreQueryManager.json");
const shared_1 = require("@logsn/shared");
const utils_1 = require("@streamr/utils");
const tsyringe_1 = require("tsyringe");
const Config_1 = require("../Config");
const Ethereum_1 = require("../Ethereum");
const Authentication_1 = require("../streamr/Authentication");
const Config_2 = require("../streamr/Config");
const ContractFactory_1 = require("../streamr/ContractFactory");
const LoggerFactory_1 = require("../streamr/LoggerFactory");
const contract_1 = require("../streamr/utils/contract");
let QueryManager = class QueryManager {
    constructor(contractFactory, loggerFactory, authentication, streamrClientConfig, logStoreClientConfig) {
        this.contractFactory = contractFactory;
        this.logStoreClientConfig = logStoreClientConfig;
        this.streamrClientConfig = streamrClientConfig;
        this.logger = loggerFactory.createLogger(module);
        this.authentication = authentication;
        this.queryManagerContractsReadonly = (0, Ethereum_1.getStreamRegistryChainProviders)(this.streamrClientConfig).map((provider) => {
            return this.contractFactory.createReadContract((0, utils_1.toEthereumAddress)(this.logStoreClientConfig.contracts.logStoreQueryManagerChainAddress), LogStoreQueryManager_json_1.abi, provider, 'queryManager');
        });
    }
    async connectToContract() {
        if (!this.queryManagerContract) {
            this.logger.debug('Getting stream registry chain signer...');
            const chainSigner = await this.authentication.getStreamRegistryChainSigner();
            this.logger.debug('Successfully obtained stream registry chain signer.');
            this.queryManagerContract =
                this.contractFactory.createWriteContract((0, utils_1.toEthereumAddress)(this.logStoreClientConfig.contracts.logStoreQueryManagerChainAddress), LogStoreQueryManager_json_1.abi, chainSigner, 'queryManager');
            this.logger.debug('Successfully connected to QueryManager contract.');
        }
    }
    async getQueryBalance() {
        this.logger.debug('Getting query balance for same account...');
        const address = await this.authentication.getAddress();
        const balance = await this.getQueryBalanceOf(address);
        return balance;
    }
    async getQueryBalanceOf(account) {
        this.logger.debug(`Getting query balance of account ${account}...`);
        const balance = await (0, contract_1.queryAllReadonlyContracts)((contract) => {
            return contract.balanceOf(account).then((b) => b.toBigInt());
        }, this.queryManagerContractsReadonly);
        this.logger.debug(`Query balance of account ${account} is ${balance}`);
        return balance;
    }
    async queryStake(amount, options = { usd: false }, overrides) {
        this.logger.debug(`Staking ${amount} with options: ${JSON.stringify(options)}...`);
        const chainSigner = await this.authentication.getStreamRegistryChainSigner();
        await this.connectToContract();
        const mergedOverrides = {
            ...(0, Ethereum_1.getStreamRegistryOverrides)(this.streamrClientConfig),
            ...overrides,
        };
        const stakeAmount = (0, shared_1.prepareStakeForQueryManager)(chainSigner, Number(amount), options.usd, undefined, true, mergedOverrides);
        this.logger.debug(`Stake amount prepared: ${stakeAmount}`);
        return (0, contract_1.waitForTx)(this.queryManagerContract.stake(stakeAmount, mergedOverrides));
    }
};
exports.QueryManager = QueryManager;
exports.QueryManager = QueryManager = __decorate([
    (0, tsyringe_1.scoped)(tsyringe_1.Lifecycle.ContainerScoped),
    __param(0, (0, tsyringe_1.inject)(ContractFactory_1.ContractFactoryInjectionToken)),
    __param(1, (0, tsyringe_1.inject)(LoggerFactory_1.LoggerFactoryInjectionToken)),
    __param(2, (0, tsyringe_1.inject)(Authentication_1.AuthenticationInjectionToken)),
    __param(3, (0, tsyringe_1.inject)(Config_2.StreamrClientConfigInjectionToken)),
    __param(4, (0, tsyringe_1.inject)(Config_1.LogStoreClientConfigInjectionToken)),
    __metadata("design:paramtypes", [Object, Object, Object, Object, Object])
], QueryManager);
//# sourceMappingURL=QueryManager.js.map