<p>A little dummy. Can be improved</p>
<pre><code>{-# LANGUAGE TypeOperators              #-}
{-# LANGUAGE DataKinds                  #-}

...

runClientError :: Eff (Error ClientError ': r) a -&gt; Eff r (Either ClientError a)
runClientError = runError

runHandleChainIndexClient :: DatumHash -&gt; IO (Maybe Datum)
runHandleChainIndexClient dh = do
   manager' &lt;- newManager defaultManagerSettings
   let
      clientEnv = mkClientEnv manager' (BaseUrl Http &quot;localhost&quot; 9083 &quot;&quot;)
      reqDatum = DatumFromHash dh
   mbDatum &lt;- runM
              -- $ runError
              $ runClientError
              $ runReader clientEnv
              $ handleChainIndexClient reqDatum
   case mbDatum of
       Left _ -&gt; return Nothing
       Right d -&gt; return d
</code></pre>

CC BY-SA 4.0
2022-04-23T09:12:17.617Z
2022-04-23T09:12:17.617Z
ViktorYasterbov
<p>I'm not guru with Eff system under Haskell.
What i got that method could contains the constraints for monad that should be used with.</p>
<pre><code>module ChainIndexHelper
where


import Plutus.ChainIndex.Client
import Plutus.ChainIndex.Effects (ChainIndexQueryEffect (..))
import Control.Monad.Freer.Reader
import Control.Monad.Freer
import Control.Monad.Freer.Error

-- import Control.Monad.IO.Class
import Control.Monad.Trans

import Plutus.V1.Ledger.Scripts (DatumHash, Datum)
import Data.Function ((&amp;))

import Network.HTTP.Client (newManager, defaultManagerSettings)

import Servant.API
import Servant.Client

testGetDatum :: DatumHash -&gt; IO (Maybe Datum)
testGetDatum dh = do
    manager' &lt;- newManager defaultManagerSettings
    let
        reqDatum = DatumFromHash dh
    mbDatum &lt;- runM
               $ runError
               $ runReader (mkClientEnv manager' (BaseUrl Http &quot;localhost&quot; 9083 &quot;&quot;))
               $ handleChainIndexClient reqDatum
    return mbDatum -- Might be post processing stmts
</code></pre>
<p>For scripts I have to get Datum of previous UTxO so I want to use Chain Index server and call client code. Then I'm constructing pure Cardano Tx. Signing should be on the client side.</p>

<haskell><plutus-pab>
Can someone help to understand how to use handleChainIndexClient?
# Can someone help to understand how to use handleChainIndexClient?

(Asked by: username_0 on 2022-04-22)

I'm not guru with Eff system under Haskell.
What i got that method could contains the constraints for monad that should be used with.
module ChainIndexHelper
where


import Plutus.ChainIndex.Client
import Plutus.ChainIndex.Effects (ChainIndexQueryEffect (..))
import Control.Monad.Freer.Reader
import Control.Monad.Freer
import Control.Monad.Freer.Error

-- import Control.Monad.IO.Class
import Control.Monad.Trans

import Plutus.V1.Ledger.Scripts (DatumHash, Datum)
import Data.Function ((&))

import Network.HTTP.Client (newManager, defaultManagerSettings)

import Servant.API
import Servant.Client

testGetDatum :: DatumHash -> IO (Maybe Datum)
testGetDatum dh = do
    manager' <- newManager defaultManagerSettings
    let
        reqDatum = DatumFromHash dh
    mbDatum <- runM
               $ runError
               $ runReader (mkClientEnv manager' (BaseUrl Http "localhost" 9083 ""))
               $ handleChainIndexClient reqDatum
    return mbDatum -- Might be post processing stmts

For scripts I have to get Datum of previous UTxO so I want to use Chain Index server and call client code. Then I'm constructing pure Cardano Tx. Signing should be on the client side.

**Tags:** haskell, plutus-pab

---

## 1 answers

### Answer by username_0 on 2022-04-23. Score: 0

A little dummy. Can be improved
{-# LANGUAGE TypeOperators              #-}
{-# LANGUAGE DataKinds                  #-}

...

runClientError :: Eff (Error ClientError ': r) a -> Eff r (Either ClientError a)
runClientError = runError

runHandleChainIndexClient :: DatumHash -> IO (Maybe Datum)
runHandleChainIndexClient dh = do
   manager' <- newManager defaultManagerSettings
   let
      clientEnv = mkClientEnv manager' (BaseUrl Http "localhost" 9083 "")
      reqDatum = DatumFromHash dh
   mbDatum <- runM
              -- $ runError
              $ runClientError
              $ runReader clientEnv
              $ handleChainIndexClient reqDatum
   case mbDatum of
       Left _ -> return Nothing
       Right d -> return d


---

