Skip to content

Commit

Permalink
Remove explicit DTO layer
Browse files Browse the repository at this point in the history
  • Loading branch information
jhbertra committed Jan 17, 2022
1 parent 576ad2e commit 84e92d3
Show file tree
Hide file tree
Showing 13 changed files with 57 additions and 310 deletions.
6 changes: 3 additions & 3 deletions marlowe-dashboard-client/generated/Marlowe.purs
Expand Up @@ -8,6 +8,7 @@ import Affjax.RequestBody (json) as Request
import Affjax.RequestHeader (RequestHeader(..))
import Affjax.ResponseFormat (json) as Response
import Cardano.Wallet.Mock.Types (WalletInfo)
import Component.Contacts.Types (WalletId)
import Control.Monad.Error.Class (class MonadError, throwError)
import Control.Monad.Reader.Class (class MonadAsk, asks)
import Data.Argonaut.Decode (decodeJson)
Expand All @@ -23,8 +24,7 @@ import Data.Maybe (Maybe(..))
import Data.Newtype (unwrap)
import Data.String (joinWith)
import Effect.Aff.Class (class MonadAff, liftAff)
import Marlowe.Run.Dto (WalletIdDto)
import Marlowe.Run.Wallet.V1.API (GetTotalFundsResponse)
import Marlowe.Run.Wallet.V1 (GetTotalFundsResponse)
import Marlowe.Run.Wallet.V1.CentralizedTestnet.Types
( CheckPostData
, RestoreError
Expand Down Expand Up @@ -107,7 +107,7 @@ getApiWalletV1ByWalletidTotalfunds
=> MonadAsk env m
=> MonadError AjaxError m
=> MonadAff m
=> WalletIdDto
=> WalletId
-> m GetTotalFundsResponse
getApiWalletV1ByWalletidTotalfunds wallet_id = do
spSettings <- asks spSettings
Expand Down
127 changes: 0 additions & 127 deletions marlowe-dashboard-client/generated/Marlowe/Run/Dto.purs

This file was deleted.

@@ -1,5 +1,5 @@
-- File auto generated by purescript-bridge! --
module Marlowe.Run.Wallet.V1.API where
module Marlowe.Run.Wallet.V1 where

import Prelude

Expand All @@ -20,11 +20,11 @@ import Data.Maybe (Maybe(..))
import Data.Newtype (class Newtype, unwrap)
import Data.Show.Generic (genericShow)
import Data.Tuple.Nested ((/\))
import Marlowe.Run.Dto (AssetsDto)
import Plutus.V1.Ledger.Value (Value)
import Type.Proxy (Proxy(Proxy))

newtype GetTotalFundsResponse = GetTotalFundsResponse
{ assets :: AssetsDto
{ assets :: Value
, sync :: Number
}

Expand All @@ -36,15 +36,15 @@ instance showGetTotalFundsResponse :: Show GetTotalFundsResponse where
instance encodeJsonGetTotalFundsResponse :: EncodeJson GetTotalFundsResponse where
encodeJson = defer \_ -> E.encode $ unwrap >$<
( E.record
{ assets: E.value :: _ AssetsDto
{ assets: E.value :: _ Value
, sync: E.value :: _ Number
}
)

instance decodeJsonGetTotalFundsResponse :: DecodeJson GetTotalFundsResponse where
decodeJson = defer \_ -> D.decode $
( GetTotalFundsResponse <$> D.record "GetTotalFundsResponse"
{ assets: D.value :: _ AssetsDto
{ assets: D.value :: _ Value
, sync: D.value :: _ Number
}
)
Expand All @@ -56,5 +56,5 @@ derive instance newtypeGetTotalFundsResponse :: Newtype GetTotalFundsResponse _
--------------------------------------------------------------------------------

_GetTotalFundsResponse :: Iso' GetTotalFundsResponse
{ assets :: AssetsDto, sync :: Number }
{ assets :: Value, sync :: Number }
_GetTotalFundsResponse = _Newtype
23 changes: 0 additions & 23 deletions marlowe-dashboard-client/src/API/Marlowe/Run/Wallet.purs

This file was deleted.

4 changes: 2 additions & 2 deletions marlowe-dashboard-client/src/AppM.purs
Expand Up @@ -26,7 +26,7 @@ import Halogen.Store.Monad
, runStoreT
, updateStore
)
import Marlowe (postApiWalletCentralizedtestnetCheckmnemonic)
import Marlowe (postApiWalletV1CentralizedtestnetCheckmnemonic)
import Marlowe.Run.Wallet.V1.CentralizedTestnet.Types (CheckPostData(..))
import Store as Store

Expand Down Expand Up @@ -71,7 +71,7 @@ instance checkMnemonicAppM :: CheckMnemonic AppM where
checkMnemonic =
map (either (const false) identity)
<<< runExceptT
<<< postApiWalletCentralizedtestnetCheckmnemonic
<<< postApiWalletV1CentralizedtestnetCheckmnemonic
<<< CheckPostData
<<< map wordToString
<<< toWords
21 changes: 3 additions & 18 deletions marlowe-dashboard-client/src/Capability/Wallet.purs
@@ -1,6 +1,5 @@
module Capability.Wallet
( class ManageWallet
, GetTotalFundsResponse
, createWallet
, restoreWallet
, submitWalletTransaction
Expand All @@ -11,7 +10,6 @@ module Capability.Wallet

import Prologue

import API.Marlowe.Run.Wallet as WBE
import API.Marlowe.Run.Wallet.CentralizedTestnet
( RestoreError
, RestoreWalletOptions
Expand All @@ -23,23 +21,13 @@ import Bridge (toBack, toFront)
import Component.Contacts.Types (WalletId, WalletInfo)
import Control.Monad.Except (lift, runExceptT)
import Halogen (HalogenM)
import Marlowe.Run.Wallet.V1.API as BE
import Marlowe as WBE
import Marlowe.Run.Wallet.V1 (GetTotalFundsResponse(..))
import Marlowe.Semantics (Assets)
import Plutus.V1.Ledger.Tx (Tx)
import Types (AjaxResponse)
import Unsafe.Coerce (unsafeCoerce)

type GetTotalFundsResponse =
{ assets :: Assets
, sync :: Number
}

-- TODO create a Dto module to replace Bridge (but where decoding can fail).
-- This will mirror backend architecture.
getTotalFundsResponseFromDto
:: BE.GetTotalFundsResponse -> GetTotalFundsResponse
getTotalFundsResponseFromDto = unsafeCoerce

-- FIXME: Abstract away AjaxResponse (just return an `m ResponseType` and
-- handle API failures in the concrete Monad instance).
class Monad m <= ManageWallet m where
Expand All @@ -57,10 +45,7 @@ instance monadWalletAppM :: ManageWallet AppM where
MockAPI.submitWalletTransaction (toBack wallet) tx
getWalletInfo wallet = map (map toFront) $ runExceptT $ MockAPI.getWalletInfo
(toBack wallet)
getWalletTotalFunds walletId = runExceptT
$ map getTotalFundsResponseFromDto
$ WBE.getTotalFunds
$ unsafeCoerce walletId -- TODO create DTO module like backend
getWalletTotalFunds = runExceptT <<< WBE.getApiWalletV1ByWalletidTotalfunds
signTransaction wallet tx = runExceptT $ MockAPI.signTransaction
(toBack wallet)
tx
Expand Down
7 changes: 5 additions & 2 deletions marlowe-dashboard-client/src/Page/Dashboard/State.purs
Expand Up @@ -5,6 +5,7 @@ module Page.Dashboard.State

import Prologue

import Bridge (toFront)
import Capability.Contract (class ManageContract)
import Capability.MainFrameLoop (class MainFrameLoop, callMainFrameAction)
import Capability.Marlowe
Expand Down Expand Up @@ -97,6 +98,7 @@ import Marlowe.Deinstantiate (findTemplate)
import Marlowe.Execution.State (getAllPayments)
import Marlowe.Extended.Metadata (_metaData)
import Marlowe.PAB (PlutusAppId, transactionFee)
import Marlowe.Run.Wallet.V1 (GetTotalFundsResponse(..))
import Marlowe.Semantics
( MarloweData
, MarloweParams
Expand Down Expand Up @@ -565,9 +567,10 @@ updateTotalFunds
updateTotalFunds = do
walletId <- use (_walletDetails <<< _walletInfo <<< _walletId)
response <- getWalletTotalFunds walletId
for_ response \({ assets }) ->
for_ response \(GetTotalFundsResponse { assets }) ->
modify_
$ set (_walletDetails <<< _assets) assets
$ set (_walletDetails <<< _assets)
$ toFront assets

toContacts
:: forall m msg slots
Expand Down
20 changes: 9 additions & 11 deletions marlowe-dashboard-server/app/PSGenerator.hs
Expand Up @@ -19,12 +19,11 @@ import Data.Monoid ()
import Data.Proxy (Proxy (Proxy))
import qualified Data.Text.Encoding as T ()
import qualified Data.Text.IO as T ()
import Language.PureScript.Bridge (BridgePart, Language (Haskell), SumType, argonaut, buildBridge, typeName,
writePSTypes, (^==))
import Language.PureScript.Bridge (BridgePart, Language (Haskell, PureScript), SumType, TypeInfo (..), argonaut,
buildBridge, typeName, writePSTypes, (^==))
import Language.PureScript.Bridge.PSTypes (psNumber, psString)
import Language.PureScript.Bridge.SumType (equal, genericShow, mkSumType, order)
import Marlowe.Run.API (HTTPAPI)
import Marlowe.Run.Dto
import Marlowe.Run.Wallet.V1.API (GetTotalFundsResponse)
import Marlowe.Run.Wallet.V1.CentralizedTestnet.Types (CheckPostData, RestoreError, RestorePostData)
import Marlowe.Run.WebSocket (StreamToClient, StreamToServer)
Expand All @@ -37,6 +36,12 @@ doubleBridge = typeName ^== "Double" >> return psNumber
dayBridge :: BridgePart
dayBridge = typeName ^== "Day" >> return psString

psWalletId :: TypeInfo 'PureScript
psWalletId = TypeInfo "marlowe-dashboard-client" "Component.Contacts.Types" "WalletId" []

walletIdBridge :: BridgePart
walletIdBridge = typeName ^== "HttpWalletId" >> return psWalletId

myBridge :: BridgePart
myBridge =
PSGenerator.Common.aesonBridge <|> PSGenerator.Common.containersBridge
Expand All @@ -47,6 +52,7 @@ myBridge =
<|> doubleBridge
<|> dayBridge
<|> defaultBridge
<|> walletIdBridge

data MyBridge

Expand All @@ -59,10 +65,6 @@ instance HasBridge MyBridge where
dto :: SumType 'Haskell -> SumType 'Haskell
dto = equal . genericShow . argonaut

-- FIXME: remove all of this shared stuff from plutus-apps. We should only be
-- exporting API types to PureScript, and those should all be defined
-- internally in this project. With a multi-repo setup, there is far too much
-- potential for breakage with updates.
myTypes :: [SumType 'Haskell]
myTypes =
PSGenerator.Common.ledgerTypes <>
Expand All @@ -74,10 +76,6 @@ myTypes =
mkSumType @RestorePostData,
mkSumType @CheckPostData,
mkSumType @GetTotalFundsResponse,
mkSumType @AssetsDto,
order $ mkSumType @CurrencySymbolDto,
order $ mkSumType @TokenNameDto,
order $ mkSumType @WalletIdDto,
order $ mkSumType @RestoreError
]
)
Expand Down
1 change: 0 additions & 1 deletion marlowe-dashboard-server/marlowe-dashboard-server.cabal
Expand Up @@ -19,7 +19,6 @@ library
default-extensions: NoImplicitPrelude
exposed-modules:
Marlowe.Run.API
Marlowe.Run.Dto
Marlowe.Run.Server
Marlowe.Run.Wallet.V1.API
Marlowe.Run.Wallet.V1.CentralizedTestnet.Types
Expand Down

0 comments on commit 84e92d3

Please sign in to comment.