-
Notifications
You must be signed in to change notification settings - Fork 463
/
MarloweStorage.purs
136 lines (124 loc) · 6.12 KB
/
MarloweStorage.purs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
module Capability.MarloweStorage
( class ManageMarloweStorage
, clearAllLocalStorage
, getWalletLibrary
, insertIntoWalletLibrary
, getContractNicknames
, insertIntoContractNicknames
, addAssets
, getContracts
, insertContract
, getAllWalletRoleContracts
, getWalletRoleContracts
, insertWalletRoleContracts
) where
import Prologue
import AppM (AppM)
import Control.Monad.Except (lift, runExcept)
import Data.Array (find)
import Data.Either (hush)
import Data.Foldable (for_)
import Data.Lens (set, view)
import Data.Map (Map, insert, lookup)
import Data.Maybe (fromMaybe)
import Effect.Class (liftEffect)
import Foreign.Generic (decodeJSON, encodeJSON)
import Halogen (HalogenM)
import LocalStorage (Key(..), getItem, removeItem, setItem)
import Marlowe.PAB (PlutusAppId)
import Marlowe.Semantics (Assets, MarloweData, MarloweParams, TransactionInput, PubKeyHash)
import Contacts.Lenses (_assets, _pubKeyHash, _walletInfo, _walletNickname)
import Contacts.Types (WalletDetails, WalletLibrary)
walletLibraryLocalStorageKey :: Key
walletLibraryLocalStorageKey = Key "walletLibrary"
contractNicknamesLocalStorageKey :: Key
contractNicknamesLocalStorageKey = Key "contractNicknames"
contractsLocalStorageKey :: Key
contractsLocalStorageKey = Key "walletContracts"
walletRoleContractsLocalStorageKey :: Key
walletRoleContractsLocalStorageKey = Key "walletRoleContracts"
class
Monad m <= ManageMarloweStorage m where
clearAllLocalStorage :: m Unit
-- wallet library
getWalletLibrary :: m WalletLibrary
insertIntoWalletLibrary :: WalletDetails -> m Unit
-- contract nicknames
getContractNicknames :: m (Map PlutusAppId String)
insertIntoContractNicknames :: PlutusAppId -> String -> m Unit
-- temporary data that we persist until everything is working with the PAB
addAssets :: PubKeyHash -> Assets -> m Unit
getContracts :: m (Map MarloweParams (Tuple MarloweData (Array TransactionInput)))
insertContract :: MarloweParams -> (Tuple MarloweData (Array TransactionInput)) -> m Unit
getAllWalletRoleContracts :: m (Map String (Map MarloweParams MarloweData))
getWalletRoleContracts :: String -> m (Map MarloweParams MarloweData)
insertWalletRoleContracts :: String -> MarloweParams -> MarloweData -> m Unit
instance manageMarloweStorageAppM :: ManageMarloweStorage AppM where
clearAllLocalStorage =
liftEffect do
removeItem walletLibraryLocalStorageKey
removeItem contractNicknamesLocalStorageKey
removeItem contractsLocalStorageKey
removeItem walletRoleContractsLocalStorageKey
-- wallet library
getWalletLibrary = do
mWalletLibraryJson <- liftEffect $ getItem walletLibraryLocalStorageKey
pure $ fromMaybe mempty $ hush <<< runExcept <<< decodeJSON =<< mWalletLibraryJson
insertIntoWalletLibrary walletDetails = do
walletLibrary <- getWalletLibrary
let
walletNickname = view _walletNickname walletDetails
updatedWalletLibrary = insert walletNickname walletDetails walletLibrary
liftEffect $ setItem walletLibraryLocalStorageKey $ encodeJSON updatedWalletLibrary
-- contract nicknames
getContractNicknames = do
mContractNicknamesJson <- liftEffect $ getItem contractNicknamesLocalStorageKey
pure $ fromMaybe mempty $ hush <<< runExcept <<< decodeJSON =<< mContractNicknamesJson
insertIntoContractNicknames plutusAppId nickname = do
contractNicknames <- getContractNicknames
let
updatedContractNicknames = insert plutusAppId nickname contractNicknames
liftEffect $ setItem contractNicknamesLocalStorageKey $ encodeJSON updatedContractNicknames
-- temporary data that we persist until everything is working with the PAB
addAssets pubKeyHash assets = do
walletLibrary <- getWalletLibrary
for_ (find (\details -> view (_walletInfo <<< _pubKeyHash) details == pubKeyHash) walletLibrary) \details ->
let
existingAssets = view _assets details
updatedAssets = existingAssets <> assets
updatedDetails = set _assets updatedAssets details
in
insertIntoWalletLibrary updatedDetails
getContracts = do
mContractsJson <- liftEffect $ getItem contractsLocalStorageKey
pure $ fromMaybe mempty $ hush <<< runExcept <<< decodeJSON =<< mContractsJson
insertContract marloweParams contractData = do
existingContracts <- getContracts
let
newContracts = insert marloweParams contractData existingContracts
void $ liftEffect $ setItem contractsLocalStorageKey $ encodeJSON newContracts
getAllWalletRoleContracts = do
mAllWalletRoleContracts <- liftEffect $ getItem walletRoleContractsLocalStorageKey
pure $ fromMaybe mempty $ hush <<< runExcept <<< decodeJSON =<< mAllWalletRoleContracts
getWalletRoleContracts walletId = do
allWalletRoleContracts <- getAllWalletRoleContracts
pure $ fromMaybe mempty $ lookup walletId allWalletRoleContracts
insertWalletRoleContracts walletId marloweParams marloweData = do
allWalletRoleContracts <- getAllWalletRoleContracts
walletRoleContracts <- getWalletRoleContracts walletId
let
newWalletRoleContracts = insert marloweParams marloweData walletRoleContracts
newAllWalletRoleContracts = insert walletId newWalletRoleContracts allWalletRoleContracts
void $ liftEffect $ setItem walletRoleContractsLocalStorageKey $ encodeJSON newAllWalletRoleContracts
instance manageMarloweStorageHalogenM :: ManageMarloweStorage m => ManageMarloweStorage (HalogenM state action slots msg m) where
clearAllLocalStorage = lift clearAllLocalStorage
getWalletLibrary = lift getWalletLibrary
insertIntoWalletLibrary = lift <<< insertIntoWalletLibrary
getContractNicknames = lift getContractNicknames
insertIntoContractNicknames plutusAppId nickname = lift $ insertIntoContractNicknames plutusAppId nickname
addAssets walletDetails assets = lift $ addAssets walletDetails assets
getContracts = lift getContracts
insertContract marloweParams contractData = lift $ insertContract marloweParams contractData
getAllWalletRoleContracts = lift getAllWalletRoleContracts
getWalletRoleContracts = lift <<< getWalletRoleContracts
insertWalletRoleContracts walletId marloweParams marloweData = lift $ insertWalletRoleContracts walletId marloweParams marloweData