Skip to content
Draft
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
4 changes: 2 additions & 2 deletions cabal.project
Original file line number Diff line number Diff line change
Expand Up @@ -158,8 +158,8 @@ source-repository-package
source-repository-package
type: git
location: https://github.com/cardano-foundation/cardano-wallet-agda
tag: 33702851de8b846cc0bb9c48ee24c987e6d02c01
--sha256: 0qiffp4dgz2c8wjjs4qk8g307a8li2lcczbdzinfcyxn0q01pcvy
tag: 5a24dca9b1d884688483ad78a00b8f7485d522a4
--sha256: 1r05aihgwx3if6lxq8vs07scaz4bxkgnrii60wn3zpi6r1gmrpq9
subdir: lib/customer-deposit-wallet-pure

-- With (semi-circular) dependency on cardano-wallet-read:
Expand Down
1 change: 1 addition & 0 deletions lib/customer-deposit-wallet/customer-deposit-wallet.cabal
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ library
exposed-modules:
Cardano.Wallet.Deposit.IO
Cardano.Wallet.Deposit.IO.DB
Cardano.Wallet.Deposit.IO.DB.Store.UTxO.UTxOHistory
Cardano.Wallet.Deposit.IO.Network.Mock
Cardano.Wallet.Deposit.IO.Network.Type
Cardano.Wallet.Deposit.Pure
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
{-# LANGUAGE NamedFieldPuns #-}

-- |
-- Copyright: © 2024 Cardano Foundation
-- License: Apache-2.0
--
module Cardano.Wallet.Deposit.IO.Address.Store
( mkStoreAddressState
)
where

import Prelude

import Cardano.Wallet.Deposit.Pure
( Customer
)
import Cardano.Wallet.Deposit.Pure.Address
( AddressState (..)
)
import Control.Exception
( Exception
, SomeException (SomeException)
)
import Data.Delta
( Delta (..)
)
import Data.Store
( UpdateStore
, mkUpdateStore
, updateLoad
)
import Database.Table
( Col (..)
, Row
, Table
, (:.)
)

import qualified Cardano.Wallet.Deposit.Read as Read
import qualified Data.Map.Strict as Map
import qualified Database.SQLite.Simple as Sqlite
import qualified Database.Table.SQLite.Simple as Sql

{-----------------------------------------------------------------------------
Delta type
TODO: move out
------------------------------------------------------------------------------}

data DeltaAddressState
= InsertAddressCustomer Read.Address Customer

instance Delta DeltaAddressState where
type Base DeltaAddressState = AddressState
apply (DeltaAddressState addr c) s@AddressStateC{addresses}
= s {addresses = Map.insert addr c addresses}

{-----------------------------------------------------------------------------
Database schema
------------------------------------------------------------------------------}

type TableAddressesMap =
Table "deposit_addresses"
:. Col "address" Address
:. Col "customer" Customer

tableAddressesMap :: Proxy TableAddressesMap
tableAddressesMap = Proxy

type TableAddressState =
Table "deposit_xpub_change"
:. Col "xpub" XPub
:. Col "change" Address

tableAddressState :: Proxy TableAddressState
tableAddressState = Proxy

{-----------------------------------------------------------------------------
Store
------------------------------------------------------------------------------}

mkStoreAddressState :: UpdateStore Sql.SqlM DeltaAddressState
mkStoreAddressState = mkUpdateStore loadS' writeS' updateS'

data ErrStoreAddressState
= ErrTableAddressState
deriving Show

instance Exception ErrStoreAddressState

loadS' :: Sql.SqlM (Either SomeException AddressState)
loadS' = do
-- TODO: use `catch` or some form of wrapping the exception
addresses <- Map.fromList $ Sql.selectAll tableAddressesMap
[(stateXPub, change)] <- Sql.selectAll tableAddressState
pure $ AddressStateC{addresses, stateXPub, change}

writeS' :: AddressState -> Sql.SqlM ()
writeS' AddressStateC{addresses,stateXPub,change} = do
Sql.deleteAll tableAddressesMap
Sql.insertMany (Map.toList addresses) tableAddressesMap
Sql.deleteAll tableAddressState
Sql.insertOne (stateXPub, change) tableAddressState

updateS'
:: Maybe AddressState
-> DeltaAddressState
-> Sql.SqlM ()
updateS' _ (InsertAddressCustomer addr customer) =
Sql.insertOne (addr, customer) tableAddressesMap
Loading