Skip to content

Commit e9ad52d

Browse files
committed
Add roll forward support in TxHistory
1 parent 2dc09f2 commit e9ad52d

File tree

1 file changed

+71
-4
lines changed
  • lib/customer-deposit-wallet/src/Cardano/Wallet/Deposit/Pure/API

1 file changed

+71
-4
lines changed

lib/customer-deposit-wallet/src/Cardano/Wallet/Deposit/Pure/API/TxHistory.hs

Lines changed: 71 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
{-# LANGUAGE DataKinds #-}
22
{-# LANGUAGE FlexibleContexts #-}
3+
{-# LANGUAGE NamedFieldPuns #-}
4+
{-# LANGUAGE RankNTypes #-}
35

46
module Cardano.Wallet.Deposit.Pure.API.TxHistory
57
( ByCustomer
@@ -10,6 +12,9 @@ module Cardano.Wallet.Deposit.Pure.API.TxHistory
1012
, TxHistory (..)
1113
, firstJust
1214
, transfers
15+
, ResolveValueTransfer
16+
, rollForward
17+
, getEraSlotOfBlock
1318
)
1419
where
1520

@@ -19,8 +24,9 @@ import Cardano.Wallet.Deposit.Map
1924
( F
2025
, Map (..)
2126
, W
27+
, singletonFinger
28+
, singletonMap
2229
)
23-
2430
import Cardano.Wallet.Deposit.Pure.Address
2531
( Customer
2632
)
@@ -29,15 +35,27 @@ import Cardano.Wallet.Deposit.Pure.UTxO.ValueTransfer
2935
)
3036
import Cardano.Wallet.Deposit.Read
3137
( Address
38+
, WithOrigin (..)
39+
, getEraBHeader
40+
, getEraSlotNo
41+
, getEraTransactions
3242
)
3343
import Cardano.Wallet.Read
34-
( Slot
44+
( Block
45+
, EraValue (..)
46+
, IsEra
47+
, Slot
48+
, Tx
3549
, TxId
36-
, WithOrigin
50+
, applyEraFun
51+
, getTxId
3752
)
3853
import Data.Foldable
3954
( Foldable (..)
4055
)
56+
import Data.Maybe
57+
( maybeToList
58+
)
4159
import Data.Monoid
4260
( First (..)
4361
)
@@ -48,10 +66,13 @@ import Data.Time
4866
( UTCTime
4967
)
5068

69+
import qualified Data.Map.Strict as Map
70+
5171
firstJust :: a -> First a
5272
firstJust = First . Just
5373

54-
transfers :: Foldable (Map xs) => Map xs ValueTransfer -> ValueTransfer
74+
transfers
75+
:: Foldable (Map xs) => Map xs ValueTransfer -> ValueTransfer
5576
transfers = fold
5677

5778
type DownTime = Down (WithOrigin UTCTime)
@@ -85,3 +106,49 @@ instance Monoid TxHistory where
85106

86107
type ResolveAddress = Address -> Maybe Customer
87108
type ResolveSlot = Slot -> Maybe DownTime
109+
type ResolveValueTransfer =
110+
forall era. IsEra era => Tx era -> Map.Map Address ValueTransfer
111+
112+
getEraSlotOfBlock :: IsEra era => Block era -> Slot
113+
getEraSlotOfBlock = At . getEraSlotNo . getEraBHeader
114+
115+
rollForward
116+
:: ResolveValueTransfer
117+
-> ResolveAddress
118+
-> ResolveSlot
119+
-> EraValue Block
120+
-> TxHistory
121+
-> TxHistory
122+
rollForward resolveValueTransfer resolveAddress resolveSlot block =
123+
(<> txHistory')
124+
where
125+
txHistory' =
126+
applyEraFun
127+
(blockToTxHistory resolveValueTransfer resolveAddress resolveSlot)
128+
block
129+
130+
blockToTxHistory
131+
:: IsEra era
132+
=> ResolveValueTransfer
133+
-> ResolveAddress
134+
-> ResolveSlot
135+
-> Block era
136+
-> TxHistory
137+
blockToTxHistory resolveValueTransfer resolveAddress resolveSlot block =
138+
fold $ do
139+
tx <- getEraTransactions block
140+
let slot = getEraSlotOfBlock block
141+
time <- maybeToList $ resolveSlot slot
142+
(address, valueTransfer) <- Map.toAscList $ resolveValueTransfer tx
143+
customer <- maybeToList $ resolveAddress address
144+
let byTime =
145+
singletonFinger () time
146+
$ singletonMap (First $ Just slot) customer
147+
$ singletonMap (First $ Just address) (getTxId tx)
148+
$ Value valueTransfer
149+
let byCustomer =
150+
singletonMap () customer
151+
$ singletonFinger (First $ Just address) time
152+
$ singletonMap (First $ Just slot) (getTxId tx)
153+
$ Value valueTransfer
154+
pure $ TxHistory{byCustomer, byTime}

0 commit comments

Comments
 (0)