{"size": 1266, "ext": "js", "max_stars_repo_path": "src/transaction.js", "max_stars_repo_name": "hatchpay/ledger-live-common", "max_stars_repo_stars_event_min_datetime": null, "max_stars_repo_stars_event_max_datetime": null, "max_issues_repo_path": "src/transaction.js", "max_issues_repo_name": "hatchpay/ledger-live-common", "max_issues_repo_issues_event_min_datetime": null, "max_issues_repo_issues_event_max_datetime": null, "max_forks_repo_path": "src/transaction.js", "max_forks_repo_name": "hatchpay/ledger-live-common", "max_forks_repo_forks_event_min_datetime": null, "max_forks_repo_forks_event_max_datetime": null}
{"max_stars_count": null, "max_issues_count": null, "max_forks_count": null, "avg_line_length": 23.0181818182, "max_line_length": 80, "alphanum_fraction": 0.6690363349}
// @flow
import type { BigNumber } from "bignumber.js";
import type { Account, Transaction } from "./types";
import type { AccountBridge } from "./bridge/types";

export async function getTransactionStatus(
  bridge: AccountBridge<any>,
  account: Account,
  transaction: Transaction
): Promise<{
  showFeeWarning: boolean,
  estimatedFees: BigNumber,
  amount: BigNumber,
  totalSpent: BigNumber,
  useAllAmount: boolean,
  maxAmount: ?BigNumber
}> {
  const totalSpent = await bridge.getTotalSpent(account, transaction);
  const amount = bridge.getTransactionAmount(account, transaction);
  const useAllAmount = bridge.getTransactionExtra(
    account,
    transaction,
    "useAllAmount"
  );

  let maxAmount;
  if (useAllAmount) maxAmount = await bridge.getMaxAmount(account, transaction);

  const estimatedFees = totalSpent.minus(maxAmount || amount);

  const showFeeWarning =
    (amount &&
      amount.gt(0) &&
      totalSpent
        .minus(amount)
        .times(10)
        .gt(amount)) ||
    (maxAmount &&
      maxAmount.gt(0) &&
      totalSpent
        .minus(maxAmount)
        .times(10)
        .gt(maxAmount)) ||
    false;

  return {
    showFeeWarning,
    totalSpent,
    amount,
    estimatedFees,
    useAllAmount,
    maxAmount
  };
}
