-
Notifications
You must be signed in to change notification settings - Fork 211
/
IO.hs
196 lines (173 loc) · 6.16 KB
/
IO.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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
{-# LANGUAGE NamedFieldPuns #-}
{-# LANGUAGE Rank2Types #-}
{-# LANGUAGE RecordWildCards #-}
module Cardano.Wallet.Deposit.IO
(
-- * Types
WalletEnv (..)
, WalletInstance
-- * Operations
-- ** Initialization
, withWalletInit
, withWalletLoad
-- ** Mapping between customers and addresses
, listCustomers
, createAddress
-- ** Reading from the blockchain
, availableBalance
, getCustomerHistory
-- ** Writing to the blockchain
, createPayment
) where
import Prelude
import Cardano.Crypto.Wallet
( XPub
)
import Cardano.Wallet.Deposit.Pure
( Customer
, WalletState
)
import Cardano.Wallet.Deposit.Read
( Address
)
import Control.Tracer
( Tracer
, contramap
)
import Data.Bifunctor
( first
)
import Data.List.NonEmpty
( NonEmpty
)
import qualified Cardano.Wallet.Deposit.IO.DB as DB
import qualified Cardano.Wallet.Deposit.IO.Network.Type as Network
import qualified Cardano.Wallet.Deposit.Pure as Wallet
import qualified Cardano.Wallet.Deposit.Read as Read
import qualified Cardano.Wallet.Deposit.Write as Write
import qualified Control.Concurrent.Async as Async
import qualified Data.DBVar as DBVar
import qualified Data.Delta as Delta
( Replace (..)
)
import qualified Data.Delta.Update as Delta
import qualified Data.Store as Store
{-----------------------------------------------------------------------------
Types
------------------------------------------------------------------------------}
data WalletEnv m =
WalletEnv
{ logger :: Tracer m WalletLog
, genesisData :: Read.GenesisData
, networkEnv :: Network.NetworkEnv m Read.Block
, database :: Store.UpdateStore DB.SqlM Wallet.DeltaWalletState
, atomically :: forall a. DB.SqlM a -> m a
}
data WalletInstance = WalletInstance
{ env :: WalletEnv IO
, walletState :: DBVar.DBVar DB.SqlM Wallet.DeltaWalletState
}
{-----------------------------------------------------------------------------
Helpers
------------------------------------------------------------------------------}
-- | Convenience to apply an 'Update' to the 'WalletState' via the 'DBLayer'.
onWalletState
:: WalletInstance
-> Delta.Update Wallet.DeltaWalletState r
-> IO r
onWalletState WalletInstance{env,walletState} update' =
atomically env $ Delta.onDBVar walletState update'
-- FIXME: Propagation of exceptions from Pure to IO.
-- | Convenience to read the 'WalletState'.
--
-- Use 'onWalletState' if you want to use the result in an atomic update.
readWalletState :: WalletInstance -> IO WalletState
readWalletState WalletInstance{env,walletState} =
atomically env $ DBVar.readDBVar walletState
{-----------------------------------------------------------------------------
Operations
Initialization
------------------------------------------------------------------------------}
-- | Initialize a new wallet in the given environment.
withWalletInit
:: XPub
-> Integer
-> WalletEnv IO
-> (WalletInstance -> IO a)
-> IO a
withWalletInit xpub knownCustomerCount env@WalletEnv{..} action = do
walletState <- atomically
$ DBVar.initDBVar database
$ Wallet.fromXPubAndGenesis xpub knownCustomerCount genesisData
withWalletDBVar env walletState action
-- | Load an existing wallet from the given environment.
withWalletLoad
:: WalletEnv IO
-> (WalletInstance -> IO a)
-> IO a
withWalletLoad env@WalletEnv{..} action = do
walletState <- atomically $ DBVar.loadDBVar database
withWalletDBVar env walletState action
withWalletDBVar
:: WalletEnv IO
-> DBVar.DBVar DB.SqlM Wallet.DeltaWalletState
-> (WalletInstance -> IO a)
-> IO a
withWalletDBVar env@WalletEnv{..} walletState action = do
let w = WalletInstance{env,walletState}
Async.withAsync (doChainSync w) $ \_ -> action w
where
doChainSync = Network.chainSync networkEnv trChainSync . chainFollower
trChainSync = contramap (\_ -> WalletLogDummy) logger
chainFollower w = Network.ChainFollower
{ checkpointPolicy = undefined
, readChainPoints = undefined
, rollForward = rollForward w
, rollBackward = rollBackward w
}
{-----------------------------------------------------------------------------
Operations
------------------------------------------------------------------------------}
listCustomers :: WalletInstance -> IO [(Customer, Address)]
listCustomers w =
Wallet.listCustomers <$> readWalletState w
createAddress :: Customer -> WalletInstance -> IO Address
createAddress c w =
onWalletState w
$ Delta.updateWithResult
$ \s0 ->
let (r,s1) = Wallet.createAddress c s0
in (Delta.Replace s1, r)
{-----------------------------------------------------------------------------
Operations
Reading from the blockchain
------------------------------------------------------------------------------}
availableBalance :: WalletInstance -> IO Read.Value
availableBalance w =
Wallet.availableBalance <$> readWalletState w
getCustomerHistory :: WalletInstance -> Customer -> IO [Wallet.TxSummary]
getCustomerHistory w c =
Wallet.getCustomerHistory c <$> readWalletState w
rollForward :: WalletInstance -> NonEmpty Read.Block -> tip -> IO ()
rollForward w blocks _nodeTip =
onWalletState w
$ Delta.update
$ Delta.Replace . Wallet.rollForwardMany blocks
rollBackward :: WalletInstance -> Read.ChainPoint -> IO Read.ChainPoint
rollBackward w point =
onWalletState w
$ Delta.updateWithResult
$ first Delta.Replace . Wallet.rollBackward point
{-----------------------------------------------------------------------------
Operations
Writing to blockchain
------------------------------------------------------------------------------}
createPayment
:: WalletInstance -> [(Address, Read.Value)] -> IO (Maybe Write.Tx)
createPayment w destinations =
Wallet.createPayment destinations <$> readWalletState w
{-----------------------------------------------------------------------------
Logging
------------------------------------------------------------------------------}
data WalletLog
= WalletLogDummy