Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[ADP-3443] Add tx history to wallet state #4817

Merged
merged 6 commits into from
Oct 23, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 10 additions & 3 deletions lib/customer-deposit-wallet/customer-deposit-wallet.cabal
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,8 @@ flag release
manual: True

library
import: language, opts-lib, no-delta-table-on-windows
hs-source-dirs: src
import: language, opts-lib, no-delta-table-on-windows
hs-source-dirs: src
build-depends:
, async
, base
Expand All @@ -68,19 +68,25 @@ library
, delta-types
, io-classes
, microlens
, monoidal-containers
, mtl
, mwc-random
paolino marked this conversation as resolved.
Show resolved Hide resolved
, OddWord
, random
, text
, time

reexported-modules: Cardano.Wallet.Address.BIP32
exposed-modules:
Cardano.Wallet.Deposit.IO
Cardano.Wallet.Deposit.IO.DB
Cardano.Wallet.Deposit.IO.Network.Mock
Cardano.Wallet.Deposit.IO.Network.Type
Cardano.Wallet.Deposit.IO.Resource
Cardano.Wallet.Deposit.IO.Resource.Event
Cardano.Wallet.Deposit.Map
Cardano.Wallet.Deposit.Pure
Cardano.Wallet.Deposit.Pure.API.TxHistory
Cardano.Wallet.Deposit.Pure.API.TxHistory.Mock
Cardano.Wallet.Deposit.Pure.Balance
Cardano.Wallet.Deposit.Pure.Submissions
Cardano.Wallet.Deposit.Pure.UTxO
Expand Down Expand Up @@ -153,6 +159,7 @@ library rest
, contra-tracer
, crypto-primitives
, customer-deposit-wallet
, customer-deposit-wallet-pure
, delta-store
, directory
, filepath
Expand Down
39 changes: 25 additions & 14 deletions lib/customer-deposit-wallet/rest/Cardano/Wallet/Deposit/REST.hs
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ module Cardano.Wallet.Deposit.REST
, getWalletTip
, availableBalance
, getCustomerHistory
, getCustomerHistories
, getValueTransfers

-- ** Writing to the blockchain
, createPayment
Expand All @@ -47,6 +47,7 @@ module Cardano.Wallet.Deposit.REST
, deleteWallet
, deleteTheDepositWalletOnDisk
, customerAddress
, getValueTransfersWithTxIds
) where

import Prelude
Expand Down Expand Up @@ -110,6 +111,9 @@ import Data.ByteArray.Encoding
import Data.List
( isPrefixOf
)
import Data.Map.Strict
( Map
)
import Data.Store
( Store (..)
, newStore
Expand All @@ -129,7 +133,6 @@ import qualified Cardano.Wallet.Deposit.Read as Read
import qualified Cardano.Wallet.Deposit.Write as Write
import qualified Data.ByteString.Char8 as B8
import qualified Data.ByteString.Lazy as BL
import qualified Data.Map as Map

{-----------------------------------------------------------------------------
Types
Expand Down Expand Up @@ -290,10 +293,10 @@ loadWallet bootEnv dir = do
let action :: (WalletIO.WalletInstance -> IO b) -> IO (Either ErrDatabase b)
action f = findTheDepositWalletOnDisk dir $ \case
Right wallet ->
Right <$>
WalletIO.withWalletLoad
Right
<$> WalletIO.withWalletLoad
(WalletIO.WalletEnv bootEnv wallet)
f
f
Left e -> pure $ Left $ ErrLoadingDatabase e
resource <- ask
lift
Expand Down Expand Up @@ -323,9 +326,9 @@ initXPubWallet tr bootEnv dir xpub users = do
(WalletIO.WalletEnv bootEnv wallet)
xpub
users
$ \i -> do
ls <- WalletIO.listCustomers i
last ls `seq` f i
$ \i -> do
ls <- WalletIO.listCustomers i
last ls `seq` f i
Nothing ->
pure
$ Left
Expand Down Expand Up @@ -357,12 +360,12 @@ walletPublicIdentity = onWalletInstance WalletIO.walletPublicIdentity
{-----------------------------------------------------------------------------
Operations
------------------------------------------------------------------------------}

-- | List all tracked customers addresses.
listCustomers :: WalletResourceM [(Customer, Address)]
listCustomers = onWalletInstance WalletIO.listCustomers

-- | Retrieve the address for a customer if it's tracked by the wallet.

customerAddress :: Customer -> WalletResourceM (Maybe Address)
customerAddress = onWalletInstance . WalletIO.customerAddress

Expand All @@ -378,13 +381,21 @@ availableBalance = onWalletInstance WalletIO.availableBalance

getCustomerHistory
:: Customer
-> WalletResourceM [Wallet.TxSummary]
-> WalletResourceM (Map Read.TxId Wallet.TxSummary)
getCustomerHistory = onWalletInstance . WalletIO.getCustomerHistory

getCustomerHistories
:: (Read.ChainPoint, Read.ChainPoint)
-> WalletResourceM (Map.Map Customer Wallet.ValueTransfer)
getCustomerHistories = onWalletInstance . WalletIO.getCustomerHistories
getValueTransfers
:: WalletResourceM (Map Read.Slot (Map Address Wallet.ValueTransfer))
getValueTransfers = onWalletInstance WalletIO.getValueTransfers

getValueTransfersWithTxIds
:: WalletResourceM
( Map
Read.Slot
(Map Address (Map Read.TxId Wallet.ValueTransfer))
)
getValueTransfersWithTxIds =
onWalletInstance WalletIO.getValueTransfersWithTxIds

{-----------------------------------------------------------------------------
Operations
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,14 @@ import Control.Monad.IO.Class
)
import Control.Tracer
( Tracer
, stdoutTracer
, traceWith
)
import Data.Functor.Contravariant
( (>$<)
)

import qualified Cardano.Wallet.Deposit.Read as Read

lg :: (MonadIO m, Show a) => Tracer IO String -> String -> a -> m ()
lg tr p x = liftIO $ traceWith tr $ p <> ": " <> show x
Expand All @@ -46,10 +52,10 @@ loadDepositWalletFromDisk tr dir env resource = do
Left e -> error $ show e
Right _ -> pure ()

fakeBootEnv :: WalletBootEnv m
fakeBootEnv :: MonadIO m => WalletBootEnv m
fakeBootEnv =
( WalletBootEnv
(error "Not defined")
(error "Not defined")
(error "Not defined")
(show >$< stdoutTracer)
Read.mockGenesisDataMainnet
(error "network env not defined")
)
84 changes: 50 additions & 34 deletions lib/customer-deposit-wallet/src/Cardano/Wallet/Deposit/IO.hs
Original file line number Diff line number Diff line change
@@ -1,36 +1,38 @@
{-# LANGUAGE NamedFieldPuns #-}
{-# LANGUAGE Rank2Types #-}
{-# LANGUAGE RecordWildCards #-}

module Cardano.Wallet.Deposit.IO
(
-- * Types
( -- * Types
WalletEnv (..)
, WalletBootEnv (..)
, WalletPublicIdentity (..)
, WalletInstance

-- * Operations
-- ** Initialization
-- * Operations

-- ** Initialization
, withWalletInit
, Word31
, withWalletLoad

-- ** Mapping between customers and addresses
-- ** Mapping between customers and addresses
, listCustomers
, customerAddress

-- ** Reading from the blockchain
-- ** Reading from the blockchain
, getWalletTip
, availableBalance
, getCustomerHistory
, getCustomerHistories
, getValueTransfers

-- ** Writing to the blockchain
-- ** Writing to the blockchain
, createPayment
, getBIP32PathsForOwnedInputs
, signTxBody
, WalletStore
, walletPublicIdentity
, getValueTransfersWithTxIds
) where

import Prelude
Expand All @@ -43,12 +45,15 @@ import Cardano.Wallet.Address.BIP32
)
import Cardano.Wallet.Deposit.Pure
( Customer
, TxSummary
, ValueTransfer
, WalletPublicIdentity (..)
, WalletState
, Word31
)
import Cardano.Wallet.Deposit.Read
( Address
, Slot
)
import Cardano.Wallet.Network.Checkpoints.Policy
( defaultPolicy
Expand All @@ -63,6 +68,9 @@ import Data.Bifunctor
import Data.List.NonEmpty
( NonEmpty
)
import Data.Map.Strict
( Map
)

import qualified Cardano.Wallet.Deposit.IO.Network.Type as Network
import qualified Cardano.Wallet.Deposit.Pure as Wallet
Expand All @@ -74,7 +82,6 @@ import qualified Data.Delta as Delta
( Replace (..)
)
import qualified Data.Delta.Update as Delta
import qualified Data.Map.Strict as Map
import qualified Data.Store as Store

{-----------------------------------------------------------------------------
Expand All @@ -84,25 +91,25 @@ import qualified Data.Store as Store
-- | The environment needed to initialize a wallet, before a database is
-- connected.
data WalletBootEnv m = WalletBootEnv
{ logger :: Tracer m WalletLog
-- ^ Logger for the wallet.
, genesisData :: Read.GenesisData
-- ^ Genesis data for the wallet.
, networkEnv :: Network.NetworkEnv m (Read.EraValue Read.Block)
-- ^ Network environment for the wallet.
{ logger :: Tracer m WalletLog
-- ^ Logger for the wallet.
, genesisData :: Read.GenesisData
-- ^ Genesis data for the wallet.
, networkEnv :: Network.NetworkEnv m (Read.EraValue Read.Block)
-- ^ Network environment for the wallet.
}

-- | The wallet store type.
type WalletStore = Store.UpdateStore IO Wallet.DeltaWalletState

-- | The full environment needed to run a wallet.
data WalletEnv m =
WalletEnv
{ bootEnv :: WalletBootEnv m
-- ^ The boot environment.
, store :: WalletStore
-- ^ The store for the wallet.
}
data WalletEnv m
= WalletEnv
{ bootEnv :: WalletBootEnv m
-- ^ The boot environment.
, store :: WalletStore
-- ^ The store for the wallet.
}

data WalletInstance = WalletInstance
{ env :: WalletEnv IO
Expand All @@ -112,14 +119,16 @@ data WalletInstance = WalletInstance
{-----------------------------------------------------------------------------
Helpers
------------------------------------------------------------------------------}

-- | Convenience to apply an 'Update' to the 'WalletState' via the 'DBLayer'.
onWalletState
:: WalletInstance
-> Delta.Update Wallet.DeltaWalletState r
-> IO r
onWalletState WalletInstance{walletState} =
Delta.onDBVar walletState
-- FIXME: Propagation of exceptions from Pure to IO.

-- FIXME: Propagation of exceptions from Pure to IO.

-- | Convenience to read the 'WalletState'.
--
Expand Down Expand Up @@ -202,10 +211,12 @@ customerAddress c w = Wallet.customerAddress c <$> readWalletState w
walletPublicIdentity :: WalletInstance -> IO WalletPublicIdentity
walletPublicIdentity w = do
state <- readWalletState w
pure $ WalletPublicIdentity
{ pubXpub = Wallet.walletXPub state
, pubNextUser = Wallet.trackedCustomers state
}
pure
$ WalletPublicIdentity
{ pubXpub = Wallet.walletXPub state
, pubNextUser = Wallet.trackedCustomers state
}

{-----------------------------------------------------------------------------
Operations
Reading from the blockchain
Expand All @@ -218,16 +229,20 @@ availableBalance :: WalletInstance -> IO Read.Value
availableBalance w =
Wallet.availableBalance <$> readWalletState w

getCustomerHistory :: Customer -> WalletInstance -> IO [Wallet.TxSummary]
getCustomerHistory :: Customer -> WalletInstance -> IO (Map Read.TxId TxSummary)
getCustomerHistory c w =
Wallet.getCustomerHistory c <$> readWalletState w

getCustomerHistories
:: (Read.ChainPoint, Read.ChainPoint)
-> WalletInstance
-> IO (Map.Map Customer Wallet.ValueTransfer)
getCustomerHistories a w =
Wallet.getCustomerHistories a <$> readWalletState w
getValueTransfers
:: WalletInstance
-> IO (Map Slot (Map Address ValueTransfer))
getValueTransfers w = Wallet.getValueTransfers <$> readWalletState w

getValueTransfersWithTxIds
:: WalletInstance
-> IO (Map Slot (Map Address (Map Read.TxId ValueTransfer)))
getValueTransfersWithTxIds w =
Wallet.getValueTransfersWithTxIds <$> readWalletState w

rollForward
:: WalletInstance -> NonEmpty (Read.EraValue Read.Block) -> tip -> IO ()
Expand Down Expand Up @@ -265,3 +280,4 @@ signTxBody txbody w = Wallet.signTxBody txbody <$> readWalletState w
------------------------------------------------------------------------------}
data WalletLog
= WalletLogDummy
deriving Show
Loading
Loading