Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
SCP-5029 create common file for web e2e tests
- Loading branch information
Showing
3 changed files
with
72 additions
and
59 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
68 changes: 68 additions & 0 deletions
68
marlowe-integration-tests/test/Language/Marlowe/Runtime/Web/Common.hs
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,68 @@ | ||
module Language.Marlowe.Runtime.Web.Common | ||
where | ||
|
||
import Cardano.Api | ||
( AsType(..) | ||
, ShelleyWitnessSigningKey(..) | ||
, TextEnvelope(..) | ||
, TextEnvelopeType(..) | ||
, deserialiseFromTextEnvelope | ||
, serialiseToTextEnvelope | ||
, signShelleyTransaction | ||
) | ||
import Cardano.Api.SerialiseTextEnvelope (TextEnvelopeDescr(..)) | ||
import Control.Concurrent (threadDelay) | ||
import Control.Monad.IO.Class (MonadIO(liftIO)) | ||
|
||
import qualified Data.Set as Set | ||
import qualified Data.Text as T | ||
import qualified Language.Marlowe as V1 | ||
import Language.Marlowe.Runtime.Integration.Common | ||
import Language.Marlowe.Runtime.Transaction.Api (WalletAddresses(..)) | ||
import qualified Language.Marlowe.Runtime.Web as Web | ||
import Language.Marlowe.Runtime.Web.Client (getContract, postContract, putContract) | ||
import Language.Marlowe.Runtime.Web.Server.DTO (ToDTO(toDTO)) | ||
import Servant.Client (ClientM) | ||
|
||
createCloseContract :: Wallet -> ClientM Web.TxOutRef | ||
createCloseContract Wallet{..}= do | ||
let WalletAddresses{..} = addresses | ||
let webChangeAddress = toDTO changeAddress | ||
let webExtraAddresses = Set.map toDTO extraAddresses | ||
let webCollataralUtxos = Set.map toDTO collateralUtxos | ||
|
||
Web.CreateTxBody{txBody = createTxBody, ..} <- postContract | ||
webChangeAddress | ||
(Just webExtraAddresses) | ||
(Just webCollataralUtxos) | ||
Web.PostContractsRequest | ||
{ metadata = mempty | ||
, version = Web.V1 | ||
, roles = Nothing | ||
, contract = V1.Close | ||
, minUTxODeposit = 2_000_000 | ||
} | ||
|
||
createTx <- liftIO $ signShelleyTransaction' createTxBody signingKeys | ||
putContract contractId createTx | ||
_ <- waitUntilConfirmed (\Web.ContractState{status} -> status) $ getContract contractId | ||
pure contractId | ||
|
||
signShelleyTransaction' :: Web.TextEnvelope -> [ShelleyWitnessSigningKey] -> IO Web.TextEnvelope | ||
signShelleyTransaction' Web.TextEnvelope{..} wits = do | ||
let te = TextEnvelope { teType = TextEnvelopeType (T.unpack teType), teDescription = TextEnvelopeDescr (T.unpack teDescription), teRawCBOR = Web.unBase16 teCborHex } | ||
txBody <- case deserialiseFromTextEnvelope (AsTxBody AsBabbage) te of | ||
Left err -> fail $ show err | ||
Right a -> pure a | ||
pure case serialiseToTextEnvelope Nothing $ signShelleyTransaction txBody wits of | ||
TextEnvelope (TextEnvelopeType ty) _ bytes -> Web.TextEnvelope (T.pack ty) "" $ Web.Base16 bytes | ||
|
||
waitUntilConfirmed :: MonadIO m => (a -> Web.TxStatus) -> m a -> m a | ||
waitUntilConfirmed getStatus getResource = do | ||
resource <- getResource | ||
case getStatus resource of | ||
Web.Confirmed -> pure resource | ||
_ -> do | ||
liftIO $ threadDelay 1000 | ||
waitUntilConfirmed getStatus getResource | ||
|
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