|
| 1 | +module Hydra.Chain.Blockfrost.Client where |
| 2 | + |
| 3 | +import Hydra.Prelude |
| 4 | + |
| 5 | +import Blockfrost.Client ( |
| 6 | + BlockfrostClientT, |
| 7 | + runBlockfrost, |
| 8 | + ) |
| 9 | +import Blockfrost.Client qualified as Blockfrost |
| 10 | +import Cardano.Api.UTxO qualified as UTxO |
| 11 | +import Control.Concurrent.Class.MonadSTM ( |
| 12 | + MonadSTM (readTVarIO), |
| 13 | + newTVarIO, |
| 14 | + writeTVar, |
| 15 | + ) |
| 16 | +import Hydra.Cardano.Api ( |
| 17 | + ChainPoint (..), |
| 18 | + HasTypeProxy (..), |
| 19 | + Hash, |
| 20 | + Key (..), |
| 21 | + NetworkId (..), |
| 22 | + NetworkMagic (..), |
| 23 | + PaymentKey, |
| 24 | + SerialiseAsCBOR (..), |
| 25 | + ShelleyWitnessSigningKey (WitnessPaymentKey), |
| 26 | + SigningKey, |
| 27 | + SlotNo (..), |
| 28 | + SocketPath, |
| 29 | + Tx, |
| 30 | + TxId, |
| 31 | + TxIn (..), |
| 32 | + TxIx (..), |
| 33 | + UTxO, |
| 34 | + WitCtx (..), |
| 35 | + examplePlutusScriptAlwaysFails, |
| 36 | + getTxBody, |
| 37 | + getTxId, |
| 38 | + makeShelleyKeyWitness, |
| 39 | + makeSignedTransaction, |
| 40 | + mkScriptAddress, |
| 41 | + mkScriptRef, |
| 42 | + mkTxOutAutoBalance, |
| 43 | + mkVkAddress, |
| 44 | + selectLovelace, |
| 45 | + serialiseToRawBytes, |
| 46 | + throwErrorAsException, |
| 47 | + txOutValue, |
| 48 | + pattern TxOutDatumNone, |
| 49 | + ) |
| 50 | +import Hydra.Cardano.Api.Prelude ( |
| 51 | + BlockHeader (..), |
| 52 | + ) |
| 53 | +import Hydra.Chain.CardanoClient ( |
| 54 | + QueryPoint (..), |
| 55 | + awaitTransaction, |
| 56 | + buildTransaction, |
| 57 | + queryProtocolParameters, |
| 58 | + queryUTxOByTxIn, |
| 59 | + queryUTxOFor, |
| 60 | + submitTransaction, |
| 61 | + ) |
| 62 | +import Hydra.Contract.Head qualified as Head |
| 63 | +import Hydra.Plutus (commitValidatorScript, initialValidatorScript) |
| 64 | +import Hydra.Tx.ScriptRegistry (ScriptRegistry (..), newScriptRegistry) |
| 65 | + |
| 66 | +publishHydraScripts' :: |
| 67 | + -- | Expected network discriminant. |
| 68 | + NetworkId -> |
| 69 | + -- | Path to the cardano-node's domain socket |
| 70 | + SocketPath -> |
| 71 | + -- | Keys assumed to hold funds to pay for the publishing transaction. |
| 72 | + SigningKey PaymentKey -> |
| 73 | + IO [TxId] |
| 74 | +publishHydraScripts' networkId socketPath sk = do |
| 75 | + pparams <- queryProtocolParameters networkId socketPath QueryTip |
| 76 | + forM scripts $ \script -> do |
| 77 | + utxo <- queryUTxOFor networkId socketPath QueryTip vk |
| 78 | + let output = mkScriptTxOut pparams <$> [mkScriptRef script] |
| 79 | + totalDeposit = sum (selectLovelace . txOutValue <$> output) |
| 80 | + someUTxO = |
| 81 | + maybe mempty UTxO.singleton $ |
| 82 | + UTxO.find (\o -> selectLovelace (txOutValue o) > totalDeposit) utxo |
| 83 | + buildTransaction |
| 84 | + networkId |
| 85 | + socketPath |
| 86 | + changeAddress |
| 87 | + someUTxO |
| 88 | + [] |
| 89 | + output |
| 90 | + >>= \case |
| 91 | + Left e -> |
| 92 | + throwErrorAsException e |
| 93 | + Right x -> do |
| 94 | + let body = getTxBody x |
| 95 | + let tx = makeSignedTransaction [makeShelleyKeyWitness body (WitnessPaymentKey sk)] body |
| 96 | + submitTransaction networkId socketPath tx |
| 97 | + void $ awaitTransaction networkId socketPath tx |
| 98 | + return $ getTxId body |
| 99 | + where |
| 100 | + scripts = [initialValidatorScript, commitValidatorScript, Head.validatorScript] |
| 101 | + vk = getVerificationKey sk |
| 102 | + |
| 103 | + changeAddress = mkVkAddress networkId vk |
| 104 | + |
| 105 | + mkScriptTxOut pparams = |
| 106 | + mkTxOutAutoBalance |
| 107 | + pparams |
| 108 | + unspendableScriptAddress |
| 109 | + mempty |
| 110 | + TxOutDatumNone |
| 111 | + |
| 112 | + unspendableScriptAddress = |
| 113 | + mkScriptAddress networkId $ examplePlutusScriptAlwaysFails WitCtxTxIn |
0 commit comments