"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.getEthersOverrides = exports.getStreamRegistryChainProviders = exports.generateEthereumAccount = void 0;
/**
 * Config and utilities for interating with identity & Ethereum chain.
 */
const wallet_1 = require("@ethersproject/wallet");
const providers_1 = require("@ethersproject/providers");
const generateEthereumAccount = () => {
    const wallet = wallet_1.Wallet.createRandom();
    return {
        address: wallet.address,
        privateKey: wallet.privateKey,
    };
};
exports.generateEthereumAccount = generateEthereumAccount;
const getStreamRegistryChainProviders = (config) => {
    return getRpcProviders(config.contracts.streamRegistryChainRPCs, config.contracts.pollInterval);
};
exports.getStreamRegistryChainProviders = getStreamRegistryChainProviders;
const getRpcProviders = (connectionInfo, pollInterval) => {
    return connectionInfo.rpcs.map((c) => {
        const provider = new providers_1.JsonRpcProvider(c);
        if (pollInterval !== undefined) {
            provider.pollingInterval = pollInterval;
        }
        return provider;
    });
};
/**
 * Apply the gasPriceStrategy to the estimated gas price, if given
 * Ethers.js will resolve the gas price promise before sending the tx
 */
const getEthersOverrides = (config) => {
    const chainConfig = config.contracts.ethereumNetwork;
    const overrides = chainConfig.overrides ?? {};
    if ((chainConfig.highGasPriceStrategy) && (chainConfig.overrides?.gasPrice === undefined)) {
        const primaryProvider = (0, exports.getStreamRegistryChainProviders)(config)[0];
        const gasPriceStrategy = (estimatedGasPrice) => {
            const INCREASE_PERCENTAGE = 30;
            return estimatedGasPrice.mul(100 + INCREASE_PERCENTAGE).div(100);
        };
        return {
            ...overrides,
            gasPrice: primaryProvider.getGasPrice().then(gasPriceStrategy)
        };
    }
    return overrides;
};
exports.getEthersOverrides = getEthersOverrides;
//# sourceMappingURL=Ethereum.js.map