Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
10 changed files
with
385 additions
and
12 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
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,132 @@ | ||
module Marlowe.Runtime.Web where | ||
|
||
import Prelude | ||
|
||
import Contrib.Data.Argonaut (JsonParser, JsonParserResult, decodeFromString) | ||
import Contrib.Data.Argonaut.Generic.Record (decodeNewtypedRecord) | ||
import Data.Argonaut (class DecodeJson, Json, JsonDecodeError(..), decodeJson) | ||
import Data.Argonaut.Decode.Generic (genericDecodeJson) | ||
import Data.Either (note) | ||
import Data.Generic.Rep (class Generic) | ||
import Data.Int as Int | ||
import Data.Map (Map) | ||
import Data.Map as Map | ||
import Data.Maybe (Maybe(..)) | ||
import Data.Newtype (class Newtype) | ||
import Data.String as String | ||
import Data.Traversable (for) | ||
import Data.Tuple.Nested (type (/\), (/\)) | ||
import Foreign.Object (Object) | ||
import Foreign.Object as Object | ||
|
||
newtype TxId = TxId String | ||
derive instance Generic TxId _ | ||
derive instance Newtype TxId _ | ||
derive instance Eq TxId | ||
derive instance Ord TxId | ||
instance DecodeJson TxId where | ||
decodeJson = genericDecodeJson | ||
|
||
newtype TxOutRef = TxOutRef | ||
{ txId :: TxId | ||
, txIx :: Int | ||
} | ||
derive instance Generic TxOutRef _ | ||
derive instance Eq TxOutRef | ||
derive instance Newtype TxOutRef _ | ||
instance DecodeJson TxOutRef where | ||
decodeJson = decodeFromString $ String.split (String.Pattern "#") >>> case _ of | ||
[txId, txIxStr] -> do | ||
txIx <- Int.fromString txIxStr | ||
pure $ TxOutRef { txId: TxId txId, txIx } | ||
_ -> Nothing | ||
|
||
txOutRefFromString :: String -> Maybe TxOutRef | ||
txOutRefFromString = String.split (String.Pattern "#") >>> case _ of | ||
[txId, txIxStr] -> do | ||
txIx <- Int.fromString txIxStr | ||
pure $ TxOutRef { txId: TxId txId, txIx } | ||
_ -> Nothing | ||
|
||
txOutRefToString :: TxOutRef -> String | ||
txOutRefToString (TxOutRef { txId: TxId txId, txIx }) = txId <> "#" <> show txIx | ||
|
||
newtype PolicyId = PolicyId String | ||
derive instance Generic PolicyId _ | ||
derive instance Newtype PolicyId _ | ||
derive instance Eq PolicyId | ||
derive instance Ord PolicyId | ||
instance DecodeJson PolicyId where | ||
decodeJson = map PolicyId <$> decodeJson | ||
|
||
data MarloweVersion = V1 | ||
derive instance Generic MarloweVersion _ | ||
derive instance Eq MarloweVersion | ||
derive instance Ord MarloweVersion | ||
instance DecodeJson MarloweVersion where | ||
decodeJson = decodeFromString case _ of | ||
"v1" -> Just V1 | ||
_ -> Nothing | ||
|
||
data TxStatus | ||
= Unsigned | ||
| Submitted | ||
| Confirmed | ||
derive instance Eq TxStatus | ||
derive instance Ord TxStatus | ||
|
||
-- deriving (Show, Eq, Ord) | ||
|
||
instance DecodeJson TxStatus where | ||
decodeJson = decodeFromString case _ of | ||
"unsigned" -> Just Unsigned | ||
"submitted" -> Just Submitted | ||
"confirmed" -> Just Confirmed | ||
_ -> Nothing | ||
|
||
|
||
newtype BlockHeader = BlockHeader | ||
{ slotNo :: Int | ||
, blockNo :: Int | ||
, blockHeaderHash :: String | ||
} | ||
derive instance Generic BlockHeader _ | ||
derive instance Newtype BlockHeader _ | ||
derive instance Eq BlockHeader | ||
derive instance Ord BlockHeader | ||
instance DecodeJson BlockHeader where | ||
decodeJson json = BlockHeader <$> decodeJson json | ||
|
||
-- FIXME: We want to make it more concrete soon ;-) | ||
type Metadata = Map Int (Object Json) | ||
|
||
newtype ContractHeader = ContractHeader | ||
{ contractId :: TxOutRef | ||
, roleTokenMintingPolicyId :: PolicyId | ||
, version :: MarloweVersion | ||
, metadata :: Metadata | ||
, status :: TxStatus | ||
, block :: Maybe BlockHeader | ||
} | ||
derive instance Generic ContractHeader _ | ||
derive instance Newtype ContractHeader _ | ||
derive instance Eq ContractHeader | ||
|
||
instance DecodeJson ContractHeader where | ||
decodeJson = do | ||
let | ||
decodeMetadata :: JsonParser Metadata | ||
decodeMetadata json = do | ||
(obj :: Object (Object Json)) <- decodeJson json | ||
|
||
(arr :: Array (Int /\ Object Json)) <- for (Object.toUnfoldable obj) \(idx /\ value) -> do | ||
idx' <- do | ||
let | ||
err = TypeMismatch $ "Expecting an integer metadata label but got: " <> show idx | ||
note err $ Int.fromString idx | ||
pure (idx' /\ value) | ||
|
||
pure $ Map.fromFoldable arr | ||
decodeNewtypedRecord { metadata: map decodeMetadata :: Maybe Json -> Maybe (JsonParserResult Metadata) } | ||
|
||
|
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,35 @@ | ||
module Marlowe.Runtime.Web.Types where | ||
|
||
import Prelude | ||
|
||
import Contrib.Data.Argonaut.Generic.Record (class DecodeRecord, DecodeJsonFieldFn, decodeRecord) | ||
import Data.Argonaut (class DecodeJson, Json, JsonDecodeError, decodeJson) | ||
import Data.Argonaut.Decode.Generic (genericDecodeJson) | ||
import Data.Either (Either) | ||
import Data.Generic.Rep (class Generic) | ||
import Data.Newtype (class Newtype) | ||
import Type.Row.Homogeneous (class Homogeneous) | ||
|
||
newtype ResourceLink = ResourceLink String | ||
derive instance Generic ResourceLink _ | ||
derive instance Newtype ResourceLink _ | ||
derive instance Eq ResourceLink | ||
derive instance Ord ResourceLink | ||
instance DecodeJson ResourceLink where | ||
decodeJson json= ResourceLink <$> decodeJson json | ||
|
||
type ResourceWithLinksRow resource linksRow = | ||
( links :: { | linksRow } | ||
, resource :: resource | ||
) | ||
|
||
type ResourceWithLinks resource linksRow = { | ResourceWithLinksRow resource linksRow } | ||
|
||
decodeResourceWithLink | ||
:: forall a linksRow | ||
. Homogeneous linksRow ResourceLink | ||
=> DecodeRecord (resource :: DecodeJsonFieldFn a) (ResourceWithLinksRow a linksRow) | ||
=> DecodeJsonFieldFn a | ||
-> Json | ||
-> Either JsonDecodeError (ResourceWithLinks a linksRow) | ||
decodeResourceWithLink decodeResource = decodeRecord { resource: decodeResource } |
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
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,12 @@ | ||
module Test.Marlowe.Runtime.Web where | ||
|
||
import Prelude | ||
|
||
import Test.Marlowe.Web.ContractHeader as ContractHeader | ||
import Test.Spec (Spec, describe) | ||
import Test.Spec as Spec | ||
|
||
spec :: Spec Unit | ||
spec = do | ||
describe "Marlowe.Web" $ Spec.parallel do | ||
ContractHeader.spec |
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,43 @@ | ||
module Test.Marlowe.Web.ContractHeader where | ||
|
||
import Prelude | ||
|
||
import Control.Monad.Error.Class (throwError) | ||
import Data.Argonaut (Json, JsonDecodeError, decodeJson, fromObject, fromString, jsonParser, stringify) | ||
import Data.Argonaut.Decode ((.:)) | ||
import Data.Either (Either(..), either) | ||
import Data.Foldable (for_) | ||
import Effect.Exception (error) | ||
import Foreign.Object as Object | ||
import Marlowe.Runtime.Web (ContractHeader) | ||
import Marlowe.Runtime.Web.Types (ResourceLink, ResourceWithLinks, decodeResourceWithLink) | ||
import Node.Encoding (Encoding(..)) | ||
import Node.FS.Aff (readTextFile) | ||
import Test.Spec (Spec, describe, it, pending) | ||
import Test.Spec.Assertions (fail) | ||
|
||
spec :: Spec Unit | ||
spec = do | ||
describe "ContractHeader" do | ||
describe "decodeJson" do | ||
it "contracts.json" do | ||
jsonStr <- readTextFile UTF8 "./test/Marlowe/Runtime/Web/contracts.json" | ||
json <- either (throwError <<< error) pure $ jsonParser jsonStr | ||
|
||
(contractsWithLinksJson :: Array Json) <- either (throwError <<< error <<< show) pure do | ||
obj <- decodeJson json | ||
obj .: "results" | ||
|
||
for_ contractsWithLinksJson \contractWithLinksJson -> do | ||
let | ||
contracts :: Either JsonDecodeError (ResourceWithLinks ContractHeader (contract :: ResourceLink)) | ||
contracts = decodeResourceWithLink (map decodeJson) contractWithLinksJson | ||
case contracts of | ||
Left err -> do | ||
let | ||
errJson = fromObject $ Object.fromHomogeneous { json: contractWithLinksJson, err: fromString $ show err } | ||
fail $ stringify errJson | ||
Right result -> do | ||
pure unit | ||
|
||
pending "feature complete" |
Oops, something went wrong.