@@ -110,13 +110,14 @@ import Universum
110
110
import qualified Data.Acid as Acid
111
111
112
112
import Control.Arrow ((***) )
113
- import Control.Lens (at , has , ix , lens , makeClassy , makeLenses , non' ,
114
- to , toListOf , traversed , (%=) , (+=) , (.=) , (<<.=) , (?=) ,
115
- _Empty , _Just , _head )
113
+ import Control.Lens (At , Index , IxValue , at , has , ix , lens ,
114
+ makeClassy , makeLenses , non' , to , toListOf , traversed ,
115
+ (%=) , (.=) , (<<.=) , (?=) , (?~) , _Empty , _Just , _head )
116
116
import Control.Monad.State.Class (get , put )
117
117
import Data.Default (Default , def )
118
118
import qualified Data.HashMap.Strict as HM
119
119
import qualified Data.Map as M
120
+ import Data.Maybe (fromJust )
120
121
import Data.SafeCopy (Migrate (.. ), base , deriveSafeCopySimple ,
121
122
extension )
122
123
import Data.Time.Clock (nominalDay )
@@ -636,21 +637,40 @@ createWallet cWalId cWalMeta isReady curTime = do
636
637
let info = WalletInfo cWalMeta curTime curTime NotSynced noSyncStatistics mempty isReady
637
638
wsWalletInfos . at cWalId %= (<|> Just info)
638
639
639
- -- | Add new address given 'CWAddressMeta ' (which contains information about
640
- -- target wallet and account too).
640
+ -- | Add new address given 'WAddressMeta ' (which contains information about
641
+ -- target wallet and account too). If the input account is /not/ there, creates it.
641
642
addWAddress :: WAddressMeta -> Update ()
642
643
addWAddress addrMeta = do
643
- let accInfo :: Traversal' WalletStorage AccountInfo
644
- accInfo = wsAccountInfos . ix (addrMeta ^. wamAccount)
645
- addr = addrMeta ^. wamAddress
646
- whenJustM (preuse accInfo) $ \ info -> do
647
- let mAddr = info ^. aiAddresses . at addr
648
- when (isNothing mAddr) $ do
649
- -- Here we increment current account's last address index
650
- -- and assign its value to sorting index of newly created address.
651
- accInfo . aiUnusedKey += 1
652
- let key = info ^. aiUnusedKey
653
- accInfo . aiAddresses . at addr ?= AddressInfo addrMeta key
644
+ let accId = addrMeta ^. wamAccount
645
+ accountInfo <- getAccountInfo accId
646
+ let maddr = accountInfo ^. aiAddresses . at (addrMeta ^. wamAddress)
647
+ when (isNothing maddr) $
648
+ modify $ (wsAccountInfos . at accId) ?~ (addAddress accountInfo)
649
+ where
650
+ getAccountInfo :: WebTypes. AccountId -> Update AccountInfo
651
+ getAccountInfo accId = do
652
+ ws <- get
653
+ let infos = createIfMissing defaultAccount accId (ws ^. wsAccountInfos)
654
+ put $ ws & wsAccountInfos .~ infos
655
+ -- NOTE: 'fromJust' is safe since we just added the account
656
+ return $ fromJust $ infos ^. at accId
657
+
658
+ addAddress :: AccountInfo -> AccountInfo
659
+ addAddress accountInfo =
660
+ let
661
+ unusedKey = accountInfo ^. aiUnusedKey
662
+ addrInfo = AddressInfo addrMeta unusedKey
663
+ in accountInfo
664
+ & aiAddresses . at (addrMeta ^. wamAddress) ?~ addrInfo
665
+ & aiUnusedKey .~ unusedKey + 1
666
+
667
+ defaultAccount :: AccountInfo
668
+ defaultAccount =
669
+ AccountInfo (WebTypes. CAccountMeta " New account" ) mempty mempty 0
670
+
671
+ createIfMissing :: At m => IxValue m -> Index m -> m -> m
672
+ createIfMissing val idx =
673
+ at idx %~ (\ x -> x <|> pure val)
654
674
655
675
-- | Update account metadata.
656
676
setAccountMeta :: WebTypes. AccountId -> WebTypes. CAccountMeta -> Update ()
0 commit comments