Skip to content

Commit

Permalink
Add function isOurTx to identify transactions relevant to the wallet.
Browse files Browse the repository at this point in the history
  • Loading branch information
jonathanknowles committed Nov 30, 2021
1 parent 8c6c477 commit 7e189ab
Showing 1 changed file with 41 additions and 0 deletions.
41 changes: 41 additions & 0 deletions lib/core/src/Cardano/Wallet/Primitive/Model.hs
Expand Up @@ -45,6 +45,7 @@ module Cardano.Wallet.Primitive.Model
, applyOurTxToUTxO
, utxoFromTx
, spendTx
, isOurTx

-- * Accessors
, currentTip
Expand Down Expand Up @@ -119,6 +120,7 @@ import GHC.Generics
( Generic )

import qualified Cardano.Wallet.Primitive.Types.TokenBundle as TB
import qualified Cardano.Wallet.Primitive.Types.UTxO as UTxO
import qualified Data.Foldable as F
import qualified Data.List.NonEmpty as NE
import qualified Data.Map as Map
Expand Down Expand Up @@ -415,6 +417,12 @@ isOurAddress
-> State s Bool
isOurAddress = fmap isJust . state . isOurs

isOurWithdrawal
:: IsOurs s RewardAccount
=> (RewardAccount, Coin)
-> State s Bool
isOurWithdrawal = fmap isJust . ourWithdrawal

ourDelegation
:: IsOurs s RewardAccount
=> DelegationCertificate
Expand All @@ -440,6 +448,39 @@ ourWithdrawalSumFromTx
ourWithdrawalSumFromTx tx = F.foldMap snd <$>
mapMaybeM ourWithdrawal (Map.toList $ withdrawals tx)

-- | Indicates whether a given transaction is relevant to the wallet.
--
-- Returns 'True' for a given 'Tx' 't' and 'UTxO' set 'u' if (and only if) one
-- or more of the following statements is 'True':
--
-- - 't' has at least one collateral input that uses an entry from 'u'.
-- - 't' has at least one ordinary input that uses an entry from 'u'.
-- - 't' has at least one output with an address owned by the wallet.
-- - 't' has at least one withdrawal from a reward account owned by the wallet.
--
isOurTx
:: forall s. (IsOurs s Address, IsOurs s RewardAccount)
=> Tx
-> UTxO
-> State s Bool
isOurTx tx u = F.or <$> sequence
[ txHasRelevantCollateral
, txHasRelevantInput
, txHasRelevantOutput
, txHasRelevantWithdrawal
]
where
txHasRelevantCollateral =
pure . not . UTxO.null $
u `UTxO.restrictedBy` Set.fromList (fst <$> tx ^. #resolvedCollateral)
txHasRelevantInput =
pure . not . UTxO.null $
u `UTxO.restrictedBy` Set.fromList (fst <$> tx ^. #resolvedInputs)
txHasRelevantOutput =
F.or <$> sequence (isOurAddress . (^. #address) <$> tx ^. #outputs)
txHasRelevantWithdrawal =
F.or <$> sequence (isOurWithdrawal <$> Map.toList (tx ^. #withdrawals))

{-------------------------------------------------------------------------------
Internals
-------------------------------------------------------------------------------}
Expand Down

0 comments on commit 7e189ab

Please sign in to comment.