<commit_before>import {
  countDecimalPlaces,
  formatCurrencyAmount,
  handleSignificantDecimals,
} from '@cardstack/cardpay-sdk';
import Helper from '@ember/component/helper';
import BN from 'bn.js';
import { fromWei } from 'web3-utils';

type FormatWeiAmountHelperParams = [BN, boolean?];

export function formatWeiAmount(
  amountInSmallestUnit: BN,
  round: boolean = true
): string {
  if (amountInSmallestUnit == null) {
    return '';
  }

  const minDecimals = 2;

  let result: string | number = Number(fromWei(amountInSmallestUnit));
  if (!round) {
    // return the exact same amount of decimal places if truncation should not occur
    return formatCurrencyAmount(
      result,
      Math.max(countDecimalPlaces(result), minDecimals)
    );
  } else if (Math.abs(result) > 0.0001 && Math.abs(result) < 1) {
    result = handleSignificantDecimals(result, 2, 2);
    return formatCurrencyAmount(
      result,
      Math.max(countDecimalPlaces(result), minDecimals)
    );
  } else {
    // by default, just truncate to 2 decimal places
    return formatCurrencyAmount(result, minDecimals);
  }
}

class FormatWeiAmountHelper extends Helper {
  compute(
    [amountInSmallestUnit, round]: FormatWeiAmountHelperParams /*, hash*/
  ) {
    return formatWeiAmount(amountInSmallestUnit, round);
  }
}<commit_msg>fix loss of precision in format-wei-amount<commit_after>import {
  countDecimalPlaces,
  formatCurrencyAmount,
  handleSignificantDecimals,
} from '@cardstack/cardpay-sdk';
import Helper from '@ember/component/helper';
import BN from 'bn.js';
import { fromWei } from 'web3-utils';

type FormatWeiAmountHelperParams = [BN, boolean?];

export function formatWeiAmount(
  amountInSmallestUnit: BN,
  round: boolean = true
): string {
  if (amountInSmallestUnit == null) {
    return '';
  }

  const minDecimals = 2;

  let result: string = fromWei(amountInSmallestUnit);
  if (!round) {
    // return the exact same amount of decimal places if rounding should not occur
    return formatCurrencyAmount(
      result,
      Math.max(countDecimalPlaces(result), minDecimals)
    );
  } else if (
    Math.abs(Number(result)) > 0.0001 &&
    Math.abs(Number(result)) < 1
  ) {
    result = handleSignificantDecimals(result, 2, 2);
    return formatCurrencyAmount(
      result,
      Math.max(countDecimalPlaces(result), minDecimals)
    );
  } else {
    // by default, just truncate to 2 decimal places
    return formatCurrencyAmount(result, minDecimals);
  }
}

class FormatWeiAmountHelper extends Helper {
  compute(
    [amountInSmallestUnit, round]: FormatWeiAmountHelperParams /*, hash*/
  ) {
    return formatWeiAmount(amountInSmallestUnit, round);
  }
}
{'fraction_non_alphanumeric': 0.10559701492537313, 'fraction_numerical': 0.008208955223880597, 'mean_word_length': 4.001865671641791, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5039399', 'n_tokens_mistral': 860, 'n_tokens_neox': 822, 'n_words': 208}