@@ -4,6 +4,7 @@ module Cardano.Wallet.Kernel.Pending (
4
4
, newForeign
5
5
, cancelPending
6
6
, NewPendingError
7
+ , PartialTxMeta
7
8
) where
8
9
9
10
import Universum hiding (State )
@@ -14,6 +15,7 @@ import Control.Concurrent.MVar (modifyMVar_)
14
15
15
16
import Data.Acid.Advanced (update' )
16
17
18
+ import Pos.Core (Coin (.. ))
17
19
import Pos.Core.Txp (Tx (.. ), TxAux (.. ), TxOut (.. ))
18
20
import Pos.Crypto (EncryptedSecretKey )
19
21
@@ -30,6 +32,7 @@ import Cardano.Wallet.Kernel.PrefilterTx (filterOurs)
30
32
import Cardano.Wallet.Kernel.Read (getWalletCredentials )
31
33
import Cardano.Wallet.Kernel.Submission (Cancelled , addPending )
32
34
import Cardano.Wallet.Kernel.Types (WalletId (.. ))
35
+ import Cardano.Wallet.Kernel.Util.Core
33
36
34
37
import Pos.Wallet.Web.Tracking.Decrypt (WalletDecrCredentialsKey (.. ),
35
38
keyToWalletDecrCredentials )
@@ -38,6 +41,9 @@ import Pos.Wallet.Web.Tracking.Decrypt (WalletDecrCredentialsKey (..),
38
41
Submit pending transactions
39
42
-------------------------------------------------------------------------------}
40
43
44
+
45
+ type PartialTxMeta = Bool -> Coin -> IO TxMeta
46
+
41
47
-- | Submit a new pending transaction
42
48
--
43
49
-- If the pending transaction is successfully added to the wallet state, the
@@ -47,10 +53,10 @@ import Pos.Wallet.Web.Tracking.Decrypt (WalletDecrCredentialsKey (..),
47
53
newPending :: ActiveWallet
48
54
-> HdAccountId
49
55
-> TxAux
50
- -> Maybe TxMeta
51
- -> IO (Either NewPendingError () )
52
- newPending w accountId tx mbMeta = do
53
- newTx w accountId tx mbMeta $ \ ourAddrs ->
56
+ -> PartialTxMeta
57
+ -> IO (Either NewPendingError TxMeta )
58
+ newPending w accountId tx partialMeta = do
59
+ newTx w accountId tx partialMeta $ \ ourAddrs ->
54
60
update' ((walletPassive w) ^. wallets) $ NewPending accountId (InDb tx) ourAddrs
55
61
56
62
-- | Submit new foreign transaction
@@ -63,7 +69,7 @@ newForeign :: ActiveWallet
63
69
-> TxMeta
64
70
-> IO (Either NewForeignError () )
65
71
newForeign w accountId tx meta = do
66
- newTx w accountId tx (Just meta) $ \ ourAddrs ->
72
+ map void <$> newTx w accountId tx (\ _ _ -> return meta) $ \ ourAddrs ->
67
73
update' ((walletPassive w) ^. wallets) $ NewForeign accountId (InDb tx) ourAddrs
68
74
69
75
-- | Submit a new transaction
@@ -78,40 +84,40 @@ newForeign w accountId tx meta = do
78
84
newTx :: forall e . ActiveWallet
79
85
-> HdAccountId
80
86
-> TxAux
81
- -> Maybe TxMeta
87
+ -> PartialTxMeta
82
88
-> ([HdAddress ] -> IO (Either e () )) -- ^ the update to run, takes ourAddrs as arg
83
- -> IO (Either e () )
84
- newTx ActiveWallet {.. } accountId tx mbMeta upd = do
89
+ -> IO (Either e TxMeta )
90
+ newTx ActiveWallet {.. } accountId tx partialMeta upd = do
85
91
-- run the update
86
92
allOurs' <- allOurs <$> getWalletCredentials walletPassive
87
- res <- upd allOurs'
93
+ let (addrsOurs',coinsOurs) = unzip allOurs'
94
+ outCoins = sumCoinsUnsafe coinsOurs
95
+ allOutsOurs = length allOurs' == length txOut
96
+ res <- upd $ addrsOurs'
88
97
case res of
89
98
Left e -> return (Left e)
90
99
Right () -> do
91
100
-- process transaction on success
92
- putTxMeta' mbMeta
101
+ meta <- partialMeta allOutsOurs outCoins
102
+ putTxMeta (walletPassive ^. walletMeta) meta
93
103
submitTx
94
- return (Right () )
104
+ return (Right meta )
95
105
where
96
- addrs = NE. toList $ map txOutAddress (_txOutputs . taTx $ tx)
106
+ (txOut :: [ TxOut ]) = NE. toList $ (_txOutputs . taTx $ tx)
97
107
wid = WalletIdHdRnd (accountId ^. hdAccountIdParent)
98
108
99
109
-- | NOTE: we recognise addresses in the transaction outputs that belong to _all_ wallets,
100
110
-- not only for the wallet to which this transaction is being submitted
101
- allOurs :: [(WalletId , EncryptedSecretKey )] -> [HdAddress ]
111
+ allOurs :: [(WalletId , EncryptedSecretKey )] -> [( HdAddress , Coin ) ]
102
112
allOurs = concatMap (ourAddrs . snd )
103
113
104
- ourAddrs :: EncryptedSecretKey -> [HdAddress ]
114
+ ourAddrs :: EncryptedSecretKey -> [( HdAddress , Coin ) ]
105
115
ourAddrs esk =
106
- map f $ filterOurs wKey identity addrs
116
+ map f $ filterOurs wKey txOutAddress txOut
107
117
where
108
- f (address ,addressId) = initHdAddress addressId (InDb address )
118
+ f (txOut' ,addressId) = ( initHdAddress addressId (InDb (txOutAddress txOut')), txOutValue txOut' )
109
119
wKey = (wid, keyToWalletDecrCredentials $ KeyForRegular esk)
110
120
111
- putTxMeta' :: Maybe TxMeta -> IO ()
112
- putTxMeta' (Just meta) = putTxMeta (walletPassive ^. walletMeta) meta
113
- putTxMeta' Nothing = pure ()
114
-
115
121
submitTx :: IO ()
116
122
submitTx = modifyMVar_ (walletPassive ^. walletSubmission) $
117
123
return . addPending accountId (Pending. singleton tx)
0 commit comments