<commit_before>module Explorer.Util.Factory where

import Prelude
import Data.Array (foldl)
import Data.Time.NominalDiffTime (mkTime)
import Data.Tuple (Tuple(..))
import Data.Lens ((^.))
import Pos.Core.Lenses.Types (_Coin, getCoin)
import Pos.Core.Types (Coin(..))
import Pos.Explorer.Web.ClientTypes (CAddress(..), CAddressSummary(..), CHash(..), CTxEntry(..), CTxId(..))


mkCHash :: String -> CHash
mkCHash = CHash

mkCTxId :: String -> CTxId
mkCTxId =
    CTxId <<< mkCHash

mkCoin :: Int -> Coin
mkCoin coin =
  Coin {getCoin: coin}

mkCAddress :: String -> CAddress
mkCAddress = CAddress

-- | Helper to summarize coins by a list of Tx inputs or outputs
sumCoinOfInputsOutputs :: Array (Tuple CAddress Coin) -> Coin
sumCoinOfInputsOutputs list =
    mkCoin $ foldl (\acc (Tuple _ coin) -> (+) acc $ coin ^. (_Coin <<< getCoin)) 0 list

-- All the following helper function `mkEmpty**` are for debugging only
-- We do need these to mock live data
-- It can be removed if all endpoints are ready

<commit_msg>[CSE-42] Fix sumCoinOfInputsOutputs<commit_after>module Explorer.Util.Factory where

import Prelude
import Data.Foldable (sum)
import Data.Lens ((^.))
import Data.Time.NominalDiffTime (mkTime)
import Data.Tuple (Tuple(..))
import Pos.Core.Lenses.Types (_Coin, getCoin)
import Pos.Core.Types (Coin(..))
import Pos.Explorer.Web.ClientTypes (CAddress(..), CAddressSummary(..), CHash(..), CTxEntry(..), CTxId(..))


mkCHash :: String -> CHash
mkCHash = CHash

mkCTxId :: String -> CTxId
mkCTxId =
    CTxId <<< mkCHash

mkCoin :: Int -> Coin
mkCoin coin =
  Coin {getCoin: coin}

mkCAddress :: String -> CAddress
mkCAddress = CAddress

-- | Helper to summarize coins by a list of Tx inputs or outputs
sumCoinOfInputsOutputs :: Array (Tuple CAddress Coin) -> Coin
sumCoinOfInputsOutputs addressList =
    mkCoin <<< sum $ addressCoins <$> addressList
      where
        -- | Get total number of coins from an address
        addressCoins :: (Tuple CAddress Coin) -> Int
        addressCoins (Tuple _ coin) = coin ^. (_Coin <<< getCoin)

-- All the following helper function `mkEmpty**` are for debugging only
-- We do need these to mock live data
-- It can be removed if all endpoints are ready


{'fraction_non_alphanumeric': 0.12596284549161757, 'fraction_numerical': 0.001359311282283643, 'mean_word_length': 3.7586206896551726, 'pattern_counts': {'":': 0, '<': 19, '<?xml version=': 0, '>': 16, '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': '658423', 'n_tokens_mistral': 758, 'n_tokens_neox': 742, 'n_words': 239}