Skip to content

Commit

Permalink
Split withWallet into withWalletInit and withWalletLoad
Browse files Browse the repository at this point in the history
  • Loading branch information
HeinrichApfelmus committed Apr 16, 2024
1 parent f06018b commit 6c9679c
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 16 deletions.
1 change: 1 addition & 0 deletions lib/customer-deposit-wallet/customer-deposit-wallet.cabal
Expand Up @@ -48,6 +48,7 @@ library
, async
, base
, bytestring
, cardano-crypto
, cardano-wallet:cardano-wallet
, cardano-wallet-network-layer
, cardano-wallet-primitive
Expand Down
47 changes: 34 additions & 13 deletions lib/customer-deposit-wallet/src/Cardano/Wallet/Deposit/IO.hs
Expand Up @@ -4,12 +4,13 @@
module Cardano.Wallet.Deposit.IO
(
-- * Types
WalletEnv
WalletEnv (..)
, WalletInstance

-- * Operations
-- ** Initialization
, withWallet
, withWalletInit
, withWalletLoad

-- ** Mapping between customers and addresses
, listCustomers
Expand All @@ -25,6 +26,9 @@ module Cardano.Wallet.Deposit.IO

import Prelude

import Cardano.Crypto.Wallet
( XPub
)
import Cardano.Wallet.Deposit.Pure
( Customer
, WalletState
Expand Down Expand Up @@ -96,20 +100,37 @@ readWalletState WalletInstance{env,walletState} =
Operations
Initialization
------------------------------------------------------------------------------}
withWallet :: WalletEnv IO -> (WalletInstance -> IO a) -> IO a
withWallet env@WalletEnv{..} action = do
walletState <- loadWalletStateFromDatabase
-- | 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
loadWalletStateFromDatabase = atomically $ do
es <- Store.loadS database
case es of
Left _ ->
DBVar.initDBVar database $ Wallet.fromGenesis genesisData
Right _ ->
DBVar.loadDBVar database

doChainSync = Network.chainSync networkEnv trChainSync . chainFollower
trChainSync = contramap (\_ -> WalletLogDummy) logger
chainFollower w = Network.ChainFollower
Expand Down
10 changes: 7 additions & 3 deletions lib/customer-deposit-wallet/src/Cardano/Wallet/Deposit/Pure.hs
Expand Up @@ -15,7 +15,7 @@ module Cardano.Wallet.Deposit.Pure
, isCustomerAddress

-- ** Reading from the blockchain
, fromGenesis
, fromXPubAndGenesis
, localTip
, availableBalance
, rollForwardMany
Expand All @@ -38,6 +38,9 @@ module Cardano.Wallet.Deposit.Pure

import Prelude

import Cardano.Crypto.Wallet
( XPub
)
import Cardano.Wallet.Deposit.Pure.UTxOHistory
( UTxOHistory
)
Expand Down Expand Up @@ -116,8 +119,9 @@ isCustomerAddress _ _ = Nothing
Reading from the blockchain
------------------------------------------------------------------------------}

fromGenesis :: Read.GenesisData -> WalletState
fromGenesis = undefined
fromXPubAndGenesis :: XPub -> Integer -> Read.GenesisData -> WalletState
fromXPubAndGenesis _xpub _knownCustomerCount _ = fromGenesisUTxO mempty
-- FIXME: This is a mock implementation

fromGenesisUTxO :: Read.UTxO -> WalletState
fromGenesisUTxO utxo =
Expand Down

0 comments on commit 6c9679c

Please sign in to comment.