Skip to content

Commit 90cd24b

Browse files
committed
Construct a TimeHandle using blockfrost
Signed-off-by: Sasha Bogicevic <[email protected]>
1 parent 1605cdd commit 90cd24b

File tree

6 files changed

+52
-19
lines changed

6 files changed

+52
-19
lines changed

hydra-node/hydra-node.cabal

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@ library
5757
Hydra.Chain
5858
Hydra.Chain.Blockfrost
5959
Hydra.Chain.Blockfrost.Client
60+
Hydra.Chain.Blockfrost.TimeHandle
6061
Hydra.Chain.CardanoClient
6162
Hydra.Chain.Direct
6263
Hydra.Chain.Direct.Handlers

hydra-node/src/Hydra/Chain/Blockfrost.hs

Lines changed: 15 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -43,19 +43,21 @@ mkTinyWallet ::
4343
mkTinyWallet tracer config = do
4444
keyPair@(_, sk) <- readKeyPair cardanoSigningKey
4545
prj <- Blockfrost.projectFromFile projectPath
46-
genesis@Blockfrost.Genesis{_genesisSystemStart, _genesisNetworkMagic} <- runBlockfrostM prj queryGenesis
47-
let networkId = toCardanoNetworkId _genesisNetworkMagic
48-
let queryEpochInfo = pure $ toEpochInfo $ mkEraHistory genesis
49-
-- NOTE: we don't need to provide address here since it is derived from the
50-
-- keypair but we still want to keep the same wallet api.
51-
let queryWalletInfo queryPoint _address = runBlockfrostM prj $ do
52-
point <- queryTip queryPoint
53-
utxo <- queryUTxO sk networkId
54-
let walletUTxO = Ledger.unUTxO $ toLedgerUTxO utxo
55-
let systemStart = SystemStart $ posixSecondsToUTCTime _genesisSystemStart
56-
pure $ WalletInfoOnChain{walletUTxO, systemStart, tip = point}
57-
let querySomePParams = runBlockfrostM prj toCardanoPParams
58-
newTinyWallet (contramap Wallet tracer) networkId keyPair queryWalletInfo queryEpochInfo querySomePParams
46+
runBlockfrostM prj $ do
47+
Blockfrost.Genesis{_genesisSystemStart, _genesisNetworkMagic} <- queryGenesis
48+
let networkId = toCardanoNetworkId _genesisNetworkMagic
49+
eraHistory <- mkEraHistory
50+
let queryEpochInfo = pure $ toEpochInfo eraHistory
51+
-- NOTE: we don't need to provide address here since it is derived from the
52+
-- keypair but we still want to keep the same wallet api.
53+
let queryWalletInfo queryPoint _address = runBlockfrostM prj $ do
54+
point <- queryTip queryPoint
55+
utxo <- queryUTxO sk networkId
56+
let walletUTxO = Ledger.unUTxO $ toLedgerUTxO utxo
57+
let systemStart = SystemStart $ posixSecondsToUTCTime _genesisSystemStart
58+
pure $ WalletInfoOnChain{walletUTxO, systemStart, tip = point}
59+
let querySomePParams = runBlockfrostM prj toCardanoPParams
60+
liftIO $ newTinyWallet (contramap Wallet tracer) networkId keyPair queryWalletInfo queryEpochInfo querySomePParams
5961
where
6062
BlockfrostChainConfig{projectPath, cardanoSigningKey} = config
6163

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
-- | Module to deal with time in Blockfrost cardano chain layer. Defines the
2+
-- means to 'queryTimeHandle'.
3+
module Hydra.Chain.Blockfrost.TimeHandle where
4+
5+
import Hydra.Prelude
6+
7+
import Blockfrost.Client qualified as Blockfrost
8+
import Cardano.Slotting.Slot (SlotNo (SlotNo))
9+
import Data.Time.Clock.POSIX (posixSecondsToUTCTime)
10+
import Hydra.Cardano.Api (
11+
SystemStart (..),
12+
)
13+
import Hydra.Cardano.Api.Prelude (ChainPoint (ChainPoint, ChainPointAtGenesis))
14+
import Hydra.Chain.Blockfrost.Client (mkEraHistory, queryGenesis, queryTip, runBlockfrostM)
15+
import Hydra.Chain.CardanoClient (
16+
QueryPoint (QueryTip),
17+
)
18+
import Hydra.Chain.Direct.TimeHandle (TimeHandle, mkTimeHandle)
19+
20+
-- | Query node for system start and era history before constructing a
21+
-- 'TimeHandle' using the slot at the tip of the network.
22+
queryTimeHandle :: Blockfrost.Project -> IO TimeHandle
23+
queryTimeHandle prj = runBlockfrostM prj $ do
24+
tip <- queryTip QueryTip
25+
26+
Blockfrost.Genesis{_genesisSystemStart} <- queryGenesis
27+
let systemStart = SystemStart $ posixSecondsToUTCTime _genesisSystemStart
28+
eraHistory <- mkEraHistory
29+
currentTipSlot <-
30+
case tip of
31+
ChainPointAtGenesis -> pure $ SlotNo 0
32+
ChainPoint slotNo _ -> pure slotNo
33+
34+
pure $ mkTimeHandle currentTipSlot systemStart eraHistory

hydra-node/src/Hydra/Chain/Blockfrost/Wallet.hs

Lines changed: 0 additions & 2 deletions
This file was deleted.

hydra-node/src/Hydra/Chain/Direct.hs

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -80,9 +80,6 @@ import Hydra.Chain.Direct.State (
8080
ChainStateAt (..),
8181
)
8282
import Hydra.Chain.Direct.TimeHandle (queryTimeHandle)
83-
import Hydra.Chain.Direct.Util (
84-
readKeyPair,
85-
)
8683
import Hydra.Chain.Direct.Wallet (
8784
TinyWallet (..),
8885
WalletInfoOnChain (..),

weeder.toml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,10 @@ roots = [
1111
, "spy$"
1212
, "spy'$"
1313
, "redeemer$"
14-
# toCardanoGenesisParameters and mkTinyWallet will be needed for full blockfrost integration
14+
# toCardanoGenesisParameters, mkTinyWallet, queryTimeHandle will be needed for full blockfrost integration
1515
, "toCardanoGenesisParameters"
1616
, "mkTinyWallet"
17+
, "queryTimeHandle"
1718
]
1819
root-instances = [
1920
# Stock instances are treated as roots.

0 commit comments

Comments
 (0)