"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.fees = fees;

var _rxjs = require("rxjs");

var _operators = require("rxjs/operators");

var _drr = require("../util/drr");

// Copyright 2017-2019 @polkadot/api-derive authors & contributors
// This software may be modified and distributed under the terms
// of the Apache-2.0 license. See the LICENSE file for details.
function queryV2(api) {
  const paymentBase = api.consts.transactionPayment || api.consts.balances;
  return (0, _rxjs.of)([// get values from api.const for substrate versions post spec_version: 101
  // https://github.com/paritytech/substrate/pull/2883/files#diff-5e5e1c3aec9ddfde0a9054d062ab3db9R131
  api.consts.balances.creationFee, api.consts.balances.existentialDeposit, api.consts.balances.transferFee, paymentBase.transactionBaseFee, paymentBase.transactionByteFee]);
}

function queryV1(api) {
  return api.queryMulti([// Support older versions and get values from storage
  api.query.balances.creationFee, api.query.balances.existentialDeposit, api.query.balances.transferFee, api.query.balances.transactionBaseFee, api.query.balances.transactionByteFee]);
}
/**
 * @name fees
 * @returns An object containing the combined results of the storage queries for
 * all relevant fees as declared in the substrate chain spec.
 * @example
 * <BR>
 *
 * ```javascript
 * api.derive.balances.fees(({ creationFee, transferFee }) => {
 *   console.log(`The fee for creating a new account on this chain is ${creationFee} units. The fee required for making a transfer is ${transferFee} units.`);
 * });
 * ```
 */


function fees(api) {
  return () => {
    return (api.consts.balances ? queryV2(api) : queryV1(api)).pipe((0, _operators.map)((_ref) => {
      let [creationFee, existentialDeposit, transferFee, transactionBaseFee, transactionByteFee] = _ref;
      return {
        creationFee,
        existentialDeposit,
        transactionBaseFee,
        transactionByteFee,
        transferFee
      };
    }), (0, _drr.drr)());
  };
}