/
Store.hs
142 lines (132 loc) · 4.46 KB
/
Store.hs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
{-# LANGUAGE DeriveAnyClass #-}
{-# LANGUAGE NamedFieldPuns #-}
{-# LANGUAGE ScopedTypeVariables #-}
module Cardano.Wallet.DB.Store.DeltaUTxO.Store
( mkStoreUTxOHistory
)
where
import Prelude
import Cardano.Wallet.DB.Sqlite.Schema
( DeltaUTxOSlots (..), DeltaUTxOValue (..), EntityField (..) )
import Cardano.Wallet.DB.Sqlite.Types
( getTxId )
import Cardano.Wallet.DB.Store.DeltaUTxO.Model
( DeltaUTxOHistory (..), Spent (..) )
import Cardano.Wallet.DB.Store.DeltaUTxO.Model.Internal
( UTxOHistory (..) )
import Cardano.Wallet.DB.Store.DeltaUTxO.TxOutCBOR
( deserializeTxOut )
import Cardano.Wallet.Primitive.Types
( WalletId )
import Cardano.Wallet.Primitive.Types.Tx.TxIn
( TxIn (TxIn) )
import Cardano.Wallet.Primitive.Types.UTxO
( UTxO (..) )
import Control.Lens
( lazy, view, (<&>) )
import Data.DBVar
( Store (..) )
import Data.Either.Extra
( eitherToMaybe, maybeToEither )
import Data.Foldable
( foldl' )
import Database.Persist.Sql
( SqlPersistT, entityVal, selectFirst, selectList, (==.) )
import GHC.Exception
( Exception, SomeException )
import GHC.Exception.Type
( toException )
import qualified Data.Map.Strict as Map
import qualified Data.Set as Set
-- | Create a 'Store' for 'UTxOHistory' using the given 'WalletId' as a key.
mkStoreUTxOHistory
:: WalletId
-> Store (SqlPersistT IO) DeltaUTxOHistory
mkStoreUTxOHistory wid =
Store
{ loadS = load wid
, writeS = write wid
, updateS = update wid
}
-- | Issues with UTxOHistory operations.
data UTxOHistoryError
= UTxOHistoryNotFound WalletId
| UTxOHistoryNotDeserializationError WalletId
deriving (Show, Eq, Exception)
load :: WalletId -> SqlPersistT IO (Either SomeException UTxOHistory)
load wid = do
slots :: Maybe DeltaUTxOSlots <-
fmap entityVal
<$> selectFirst [DeltaUTxOSlotsWallet ==. wid] []
case slots of
Nothing -> pure $ Left $ toException $ UTxOHistoryNotFound wid
Just DeltaUTxOSlots{deltaUTxOSlotsTip, deltaUTxOSlotsFinality} -> do
xs <-
fmap entityVal
<$> selectList [DeltaUTxOValueWalletId ==. wid] []
pure
$ maybeToEither
(toException $ UTxOHistoryNotDeserializationError wid)
$ foldl'
patchByRow
( Just $
UTxOHistory
mempty
mempty
mempty
mempty
deltaUTxOSlotsTip
deltaUTxOSlotsFinality
)
xs
patchByRow :: Maybe UTxOHistory -> DeltaUTxOValue -> Maybe UTxOHistory
patchByRow Nothing _ = Nothing
patchByRow
( Just
UTxOHistory
{ history
, creationSlots
, spentSlots
, spentTxIns
, finality
, tip
}
)
DeltaUTxOValue
{ deltaUTxOValueTxInTx
, deltaUTxOValueTxInIx
, deltaUTxOValueTxOut
, deltaUTxOValueCreation
, deltaUTxOValueSpent
} =
decodeTxOut deltaUTxOValueTxOut <&> \txOut ->
UTxOHistory
{ history = history <> UTxO (Map.singleton txIn txOut)
, creationSlots =
Map.insertWith
(<>)
deltaUTxOValueCreation
txInSingleton
creationSlots
, spentSlots = case deltaUTxOValueSpent of
Unspent -> spentSlots
Spent slot ->
Map.insertWith
(<>)
slot
txInSingleton
spentSlots
, spentTxIns = case deltaUTxOValueSpent of
Unspent -> spentTxIns
Spent slot -> Map.insert txIn slot spentTxIns
, tip
, finality
}
where
txIn = TxIn (getTxId deltaUTxOValueTxInTx) deltaUTxOValueTxInIx
txInSingleton = Set.singleton txIn
decodeTxOut = eitherToMaybe . deserializeTxOut . view lazy
write :: WalletId -> UTxOHistory -> SqlPersistT IO ()
write _wid = undefined
update :: WalletId -> Maybe UTxOHistory -> DeltaUTxOHistory -> SqlPersistT IO ()
update _wid = undefined