"use strict";
/**
 * @file Ethereum
 * @desc These methods facilitate interactions with the Ethereum blockchain.
 */
Object.defineProperty(exports, "__esModule", { value: true });
exports._createProvider = exports.getBalance = exports.getProviderNetwork = exports.trx = exports.read = void 0;
const ethers_1 = require("ethers");
var JsonRpc;
(function (JsonRpc) {
    JsonRpc[JsonRpc["EthSendTransaction"] = 0] = "EthSendTransaction";
    JsonRpc[JsonRpc["EthCall"] = 1] = "EthCall";
    // NetVersion,
})(JsonRpc || (JsonRpc = {}));
/**
 * This is a generic method for invoking JSON RPC's `eth_call` or `eth_send`
 *     with Ethers.js. This function supports the public `read` and `trx`
 *     methods in this module.
 *
 * @param {boolean} isWrite True for `eth_send` and false for `eth_call`.
 * @param {string} address The Ethereum address the transaction is directed to.
 * @param {string} method The smart contract member in which to invoke.
 * @param {any[]} [parameters] Parameters of the method to invoke.
 * @param {CallOptions} [options] Options to set for `eth_call`, optional ABI
 *     (as JSON object), and Ethers.js method overrides. The ABI can be a string
 *     of the single intended method, an array of many methods, or a JSON object
 *     of the ABI generated by a Solidity compiler.
 *
 * @hidden
 *
 * @returns {Promise<any>} Return value of the invoked smart contract member
 *     or an error object if the call failed.
 */
function _ethJsonRpc(jsonRpcMethod, address, method, 
// eslint-disable-next-line @typescript-eslint/no-explicit-any
parameters = [], options = {}) {
    // eslint-disable-next-line @typescript-eslint/no-explicit-any
    return new Promise((resolve, reject) => {
        const provider = options._sumerProvider || _createProvider(options);
        const overrides = {
            gasPrice: options.gasPrice,
            nonce: options.nonce,
            value: options.value,
            chainId: options.chainId,
            from: options.from,
            gasLimit: options.gasLimit,
            blockTag: options.blockTag,
        };
        parameters.push(overrides);
        let contract;
        let abi;
        if (options.abi) {
            // Assumes `method` is a string of the member name
            // Assumes `abi` is a JSON object
            abi = options.abi;
            contract = new ethers_1.ethers.Contract(address, abi, provider);
        }
        else {
            // Assumes `method` is a string of the member definition
            abi = [method];
            contract = new ethers_1.ethers.Contract(address, abi, provider);
            method = Object.keys(contract.functions)[1];
        }
        if (jsonRpcMethod === JsonRpc.EthSendTransaction) {
            contract
                .getFunction(method)(...parameters.slice(0, parameters.length))
                .then((result) => {
                resolve(result);
            })
                .catch((error) => {
                try {
                    delete parameters[parameters.length - 1].privateKey;
                }
                catch (e) { }
                try {
                    delete parameters[parameters.length - 1].mnemonic;
                }
                catch (e) { }
                reject({
                    message: 'Error occurred during [eth_sendTransaction]. See {error}.',
                    error,
                    method,
                    parameters,
                });
            });
        }
        else if (jsonRpcMethod === JsonRpc.EthCall) {
            contract[method].staticCall
                .apply(null, parameters)
                .then((result) => {
                resolve(result);
            })
                .catch((error) => {
                try {
                    delete parameters[parameters.length - 1].privateKey;
                }
                catch (e) { }
                try {
                    delete parameters[parameters.length - 1].mnemonic;
                }
                catch (e) { }
                reject({
                    message: 'Error occurred during [eth_call]. See {error}.',
                    error,
                    method,
                    parameters,
                });
            });
        }
    });
}
/**
 * This is a generic method for invoking JSON RPC's `eth_call` with Ethers.js.
 *     Use this method to execute a smart contract's constant or non-constant
 *     member without using gas. This is a read-only method intended to read a
 *     value or test a transaction for valid parameters. It does not create a
 *     transaction on the block chain.
 *
 * @param {string} address The Ethereum address the transaction is directed to.
 * @param {string} method The smart contract member in which to invoke.
 * @param {any[]} [parameters] Parameters of the method to invoke.
 * @param {CallOptions} [options] Options to set for `eth_call`, optional ABI
 *     (as JSON object), and Ethers.js method overrides. The ABI can be a string
 *     of the single intended method, an array of many methods, or a JSON object
 *     of the ABI generated by a Solidity compiler.
 *
 * @returns {Promise<any>} Return value of the invoked smart contract member or an error
 *     object if the call failed.
 *
 * @example
 * ```
 * const cEthAddress = Sumer.util.getAddress(Sumer.cETH);
 *
 * (async function() {
 *
 *   const srpb = await Sumer.eth.read(
 *     cEthAddress,
 *     'function supplyRatePerBlock() returns (uint256)',
 *     // [], // [optional] parameters
 *     // {}  // [optional] call options, provider, network, plus Ethers.js "overrides"
 *   );
 *
 *   console.log('cETH market supply rate per block:', srpb.toString());
 *
 * })().catch(console.error);
 * ```
 */
function read(address, method, 
// eslint-disable-next-line @typescript-eslint/no-explicit-any
parameters = [], options = {}) {
    return _ethJsonRpc(JsonRpc.EthCall, address, method, parameters, options);
}
exports.read = read;
/**
 * This is a generic method for invoking JSON RPC's `eth_sendTransaction` with
 *     Ethers.js. Use this method to create a transaction that invokes a smart
 *     contract method. Returns an Ethers.js `TransactionResponse` object.
 *
 * @param {string} address The Ethereum address the transaction is directed to.
 * @param {string} method The smart contract member in which to invoke.
 * @param {any[]} [parameters] Parameters of the method to invoke.
 * @param {CallOptions} [options] Options to set for `eth_sendTransaction`,
 *     (as JSON object), and Ethers.js method overrides. The ABI can be a string
 *     optional ABI of the single intended method, an array of many methods, or
 *     a JSON object of the ABI generated by a Solidity compiler.
 *
 * @returns {Promise<any>} Returns an Ethers.js `TransactionResponse` object or an error
 *     object if the transaction failed.
 *
 * @example
 * ```
 * const oneEthInWei = '1000000000000000000';
 * const cEthAddress = '0x4ddc2d193948926d02f9b1fe9e1daa0718270ed5';
 * const provider = window.ethereum;
 *
 * (async function() {
 *   console.log('Supplying ETH to the Sumer Protocol...');
 *
 *   // Mint some cETH by supplying ETH to the Sumer Protocol
 *   const trx = await Sumer.eth.trx(
 *     cEthAddress,
 *     'function mint() payable',
 *     [],
 *     {
 *       provider,
 *       value: oneEthInWei
 *     }
 *   );
 *
 *   // const result = await trx.wait(1); // JSON object of trx info, once mined
 *
 *   console.log('Ethers.js transaction object', trx);
 * })().catch(console.error);
 * ```
 */
function trx(address, method, 
// eslint-disable-next-line @typescript-eslint/no-explicit-any
parameters = [], options = {}) {
    return _ethJsonRpc(JsonRpc.EthSendTransaction, address, method, parameters, options);
}
exports.trx = trx;
/**
 * This helps the Sumer.js constructor discover which Ethereum network the
 *     developer wants to use.
 *
 * @param {Provider | string} [provider] Optional Ethereum network provider.
 *     Defaults to Ethers.js fallback mainnet provider.
 *
 * @hidden
 *
 * @returns {object} Returns a metadata object containing the Ethereum network
 *     name and ID.
 */
async function getProviderNetwork(provider) {
    let _provider = provider;
    let networkId;
    const networkObj = await _provider.getNetwork();
    networkId = networkObj.chainId;
    networkId = isNaN(networkId) ? 0 : +networkId;
    const network = ethers_1.ethers.Network.from(networkId) || { name: 'unknown' };
    return {
        id: networkId,
        name: network.name === 'homestead' ? 'mainnet' : network.name,
    };
}
exports.getProviderNetwork = getProviderNetwork;
/**
 * Fetches the current Ether balance of a provided Ethereum address.
 *
 * @param {string} address The Ethereum address in which to get the ETH balance.
 * @param {Provider | string} [provider] Optional Ethereum network provider.
 *     Defaults to Ethers.js fallback mainnet provider.
 *
 * @returns {BigNumber} Returns a BigNumber hexadecimal value of the ETH balance
 *     of the address.
 *
 * @example
 * ```
 * (async function () {
 *
 *   balance = await Sumer.eth.getBalance(myAddress, provider);
 *   console.log('My ETH Balance', +balance);
 *
 * })().catch(console.error);
 * ```
 */
async function getBalance(address, provider) {
    let _provider = provider;
    // eslint-disable-next-line @typescript-eslint/no-explicit-any
    let providerInstance = _createProvider({ provider: _provider });
    if (!providerInstance.send && providerInstance.providerConfigs) {
        const url = providerInstance.providerConfigs[0].provider.connection.url;
        providerInstance = new ethers_1.ethers.JsonRpcProvider(url);
    }
    else if (!providerInstance.send && providerInstance.provider) {
        providerInstance = providerInstance.provider;
    }
    const balance = await providerInstance.send('eth_getBalance', [address, 'latest']);
    return balance;
}
exports.getBalance = getBalance;
/**
 * Creates an Ethereum network provider object.
 *
 * @param {CallOptions} options The call options of a pending Ethereum
 *     transaction.
 *
 * @hidden
 *
 * @returns {object} Returns a valid Ethereum network provider object.
 */
function _createProvider(options = {}) {
    // eslint-disable-next-line @typescript-eslint/no-explicit-any
    let provider = options.provider || options.network || 'mainnet';
    // const isObject = typeof provider === 'object'
    // // User passed an ethers.js provider/signer/wallet object
    // if (isObject && (provider._isSigner || provider._isProvider)) {
    //   return provider
    // }
    // if (isObject) {
    //   provider = new ethers.BrowserProvider(provider).getSigner()
    // }
    // // Add an explicit signer
    // if (options.privateKey) {
    //   provider = new ethers.Wallet(options.privateKey, provider)
    // } else if (options.mnemonic) {
    //   provider = ethers.Wallet.fromPhrase(options.mnemonic, provider)
    // }
    return provider;
}
exports._createProvider = _createProvider;
//# sourceMappingURL=eth.js.map