Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Create a first pab-based chain client
- Loading branch information
Showing
4 changed files
with
62 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
module Hydra.Chain.ExternalPAB where | ||
|
||
import Cardano.Prelude | ||
import Hydra.Chain (Chain (Chain, postTx)) | ||
import Hydra.HeadLogic (OnChainTx (InitTx)) | ||
import Hydra.Ledger (Tx) | ||
import Hydra.Logging (Tracer) | ||
import Network.HTTP.Req (POST (..), ReqBodyJson (..), defaultHttpConfig, http, jsonResponse, port, req, responseBody, responseStatusCode, runReq, (/:)) | ||
|
||
data ExternalPABLog | ||
|
||
withExternalPAB :: | ||
Tx tx => | ||
Tracer IO ExternalPABLog -> | ||
(OnChainTx tx -> IO ()) -> | ||
(Chain tx IO -> IO a) -> | ||
IO a | ||
withExternalPAB _tracer _callback action = do | ||
action $ Chain{postTx} | ||
where | ||
postTx = \case | ||
InitTx _ -> loadCid >>= postInitTx | ||
tx -> panic $ "should post " <> show tx | ||
|
||
loadCid = readFile "/tmp/W1.cid" | ||
|
||
-- TODO(SN): use MonadHttp, but clashes with MonadThrow | ||
postInitTx :: Text -> IO () | ||
postInitTx cid = do | ||
runReq defaultHttpConfig $ do | ||
res <- | ||
req | ||
POST | ||
(http "127.0.0.1" /: "api" /: "new" /: "contract" /: "instance" /: cid /: "endpoint" /: "init") | ||
(ReqBodyJson ()) | ||
jsonResponse | ||
(port 8080) | ||
when (responseStatusCode res /= 200) $ | ||
panic "failed to postInitTx" | ||
pure $ responseBody res |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
module Hydra.Chain.ExternalPABSpec where | ||
|
||
import Cardano.Prelude | ||
import Hydra.Chain (Chain (..)) | ||
import Hydra.Chain.ExternalPAB (withExternalPAB) | ||
import Hydra.HeadLogic (OnChainTx (..)) | ||
import Hydra.Ledger.Mock (MockTx) | ||
import Hydra.Logging (nullTracer) | ||
import Test.Hspec.Core.Spec (Spec, describe, it) | ||
|
||
spec :: Spec | ||
spec = | ||
describe "ExternalPAB" $ do | ||
it "publishes init tx using wallet 1" $ do | ||
-- TODO(SN): launch hydra-pab as process | ||
withExternalPAB nullTracer (panic "called back") $ \Chain{postTx} -> | ||
postTx $ InitTx @MockTx (panic "unused") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters