<commit_before>// @flow
import { api } from 'neon-js'
import { extend, isEmpty } from 'lodash-es'
import { createActions } from 'spunky'

import { getNode } from './nodeStorageActions'
import { ASSETS } from '../core/constants'
import { COIN_DECIMAL_LENGTH } from '../core/formatters'

type Props = {
  net: string,
  address: string,
  tokens: Array<TokenItemType>
}

export const ID = 'balances'

async function getBalances({ net, address, tokens }: Props) {
  let endpoint = await getNode()
  if (isEmpty(endpoint)) {
    endpoint = await api.getRPCEndpointFrom({ net }, api.neoscan)
  }

  // token balances
  const tokenBalances = await api.nep5.getTokenBalances(
    endpoint,
    tokens
      .filter(token => !token.isUserGenerated)
      .map(token => token.scriptHash),
    address
  )

  const parsedTokenBalances = Object.keys(tokenBalances)
    .map(tokenKey => {
      const foundToken = tokens.find(token => token.symbol === tokenKey)
      if (foundToken && tokenBalances[tokenKey]) {
        return {
          [foundToken.scriptHash]: {
            ...foundToken,
            balance: tokenBalances[tokenKey]
          }
        }
      }
      return {}
    })
    .filter(tokenBalance => !isEmpty(tokenBalance))

  // Handle manually added script hashses here
  const userGeneratedTokenInfo = []
  // eslint-disable-next-line
  for (const token of tokens.filter(token => token.isUserGenerated)) {
    // eslint-disable-next-line
    const info = await api.nep5
      .getToken(endpoint, token.scriptHash, address)
      .catch(error => {
        // eslint-disable-next-line
        console.error(
          'An error occurrred attempting to fetch custom script hash balance info.',
          { error }<commit_msg>updates balancesActions.js to work with 2.9.0 nodes<commit_after>// @flow
import { api } from 'neon-js'
import { extend, isEmpty, unionBy } from 'lodash-es'
import { createActions } from 'spunky'

import { getNode } from './nodeStorageActions'
import { ASSETS } from '../core/constants'
import { COIN_DECIMAL_LENGTH } from '../core/formatters'

type Props = {
  net: string,
  address: string,
  tokens: Array<TokenItemType>
}

export const ID = 'balances'

async function getBalances({ net, address, tokens }: Props) {
  let endpoint = await getNode()
  if (isEmpty(endpoint)) {
    endpoint = await api.getRPCEndpointFrom({ net }, api.neoscan)
  }

  const chunks = tokens
    .filter(token => !token.isUserGenerated)
    .reduce((accum, currVal, index) => {
      if (!accum.length) {
        accum.push([currVal.scriptHash])
        return accum
      }

      if (accum[accum.length - 1].length < 4) {
        accum[accum.length - 1].push(currVal.scriptHash)
      } else {
        accum.push([currVal.scriptHash])
      }
      return accum
    }, [])

  const promiseMap = chunks.map(chunk =>
    api.nep5.getTokenBalances(endpoint, chunk, address)
  )
  const results = await Promise.all(promiseMap)
  const resultObject = results.reduce((result, currentObject) => {
    Object.keys(currentObject).forEach(key => {
      // eslint-disable-next-line
      result[key] = currentObject[key]
    })
    return result
  }, {})

  const parsedTokenBalances = Object.keys(resultObject)
    .map(tokenKey => {
      const foundToken = tokens.find(token => token.symbol === tokenKey)
      if (foundToken && resultObject[tokenKey]) {
        return {
          [foundToken.scriptHash]: {
            ...foundToken,
            balance: resultObject[tokenKey]
          }
        }
      }
      return {}
    })
    .filter(tokenBalance => !isEmpty(tokenBalance))

  // Handle manually added script hashses here
  const userGeneratedTokenInfo = []
  // eslint-disable-next-line
  for (const token of tokens.filter(token => token.isUserGenerated)) {
    // eslint-disable-next-line
    const info = await api.nep5
      .getToken(endpoint, token.scriptHash, address)
      .catch(error => {
        // eslint-disable-next-line
        console.error(
          'An error occurrred attempting to fetch custom script hash balance info.',
          { error }
{'fraction_non_alphanumeric': 0.12386780905752753, 'fraction_numerical': 0.0024479804161566705, 'mean_word_length': 2.748623853211009, 'pattern_counts': {'":': 0, '<': 6, '<?xml version=': 0, '>': 22, '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': '5900037', 'n_tokens_mistral': 1242, 'n_tokens_neox': 1226, 'n_words': 293}