{"size": 2063, "ext": "js", "max_stars_repo_path": "source/renderer/app/utils/assets.js", "max_stars_repo_name": "sorki/daedalus", "max_stars_repo_stars_event_min_datetime": "2021-08-30T00:49:12.000Z", "max_stars_repo_stars_event_max_datetime": "2022-01-27T07:07:53.000Z", "max_issues_repo_path": "static/daedalus/static/4.0.5/source/renderer/app/utils/assets.js", "max_issues_repo_name": "c-spider/Cardano-SCK", "max_issues_repo_issues_event_min_datetime": "2021-08-31T23:27:44.000Z", "max_issues_repo_issues_event_max_datetime": "2022-03-25T20:35:16.000Z", "max_forks_repo_path": "static/daedalus/static/4.0.5/source/renderer/app/utils/assets.js", "max_forks_repo_name": "c-spider/Cardano-SCK", "max_forks_repo_forks_event_min_datetime": "2021-05-20T08:26:00.000Z", "max_forks_repo_forks_event_max_datetime": "2022-03-27T22:31:36.000Z"}
{"max_stars_count": 6, "max_issues_count": 17, "max_forks_count": 3, "avg_line_length": 26.4487179487, "max_line_length": 91, "alphanum_fraction": 0.6761997092}
// @flow
import type { Token, Tokens, AssetToken } from '../api/assets/types';
import { TransactionTypes } from '../domains/WalletTransaction';
import type { TransactionType } from '../api/transactions/types';

/**
 *
 * This function removes the `change` assets
 * that are included in the TX API response
 *
 */
export const filterAssets = (
  assets: Array<any>,
  transactionType: TransactionType,
  isInternalAddress: Function
): Array<any> =>
  assets.filter(
    ({ address }) =>
      (transactionType === TransactionTypes.INCOME &&
        isInternalAddress(address)) ||
      (transactionType === TransactionTypes.EXPEND &&
        !isInternalAddress(address))
  );

/**
 *
 * This function receives a Token (the asset included in a wallet or transaction)
 * and combines with the data from the Asset
 *
 * Data from the Token: policyId, assetName, quantity, address
 * Data from the Asset: fingerprint, metadata, decimals, recommendedDecimals, uniqueId
 */
export const getAssetToken = (asset: Token, getAsset: Function): AssetToken => {
  const { policyId, assetName, quantity, address } = asset;
  const { fingerprint, metadata, decimals, recommendedDecimals, uniqueId } =
    getAsset(policyId, assetName) || {};
  const txAsset = {
    policyId,
    assetName,
    quantity,
    address,
    fingerprint,
    metadata,
    decimals,
    recommendedDecimals,
    uniqueId,
  };
  return txAsset;
};

/**
 *
 * This function receives a list of Tokens (the assets included in a wallet or transaction)
 * then retrieves the Assets
 * and sort them accordingly
 *
 */
export const getAssetTokens = (
  tokens: Tokens,
  getAsset: Function
): Array<AssetToken> =>
  tokens
    .map((token) => getAssetToken(token, getAsset))
    .filter((token) => !!token.uniqueId)
    .sort(sortAssets);

export const sortAssets = (asset1: AssetToken, asset2: AssetToken) => {
  if (asset1 && asset2) {
    if (asset1.fingerprint < asset2.fingerprint) {
      return -1;
    }
    if (asset1.fingerprint > asset2.fingerprint) {
      return 1;
    }
  }
  return 0;
};
