1+ {-# LANGUAGE LambdaCase #-}
12module Money.Theory.TokenModel where
3+ -- containers
4+ import qualified Data.IntMap as IntMap
25--
36import Money.Theory.SemanticMoney
47
@@ -7,8 +10,46 @@ data TokenEvent mt acc where
710 TransferEvent :: forall mt acc {t } {v }.
811 MonetaryTypes'tv mt t v =>
912 t -> acc -> acc -> v -> TokenEvent mt acc
10- -- UpdateFlowEvent :: forall mt acc {t} {v} {u }.
11- -- MonetaryTypes'tvu mt t v u =>
12- -- t -> acc -> acc -> v -> TokenEvent mt acc
13+ UpdateFlowEvent :: forall mt acc {t } {fr }.
14+ MonetaryTypes'tr mt t fr =>
15+ t -> acc -> acc -> fr -> TokenEvent mt acc
1316
14- data TokenModel mt acc
17+ type Account = Int
18+
19+ ------------------------------------------------------------------------------------------------------------------------
20+ -- NaiveTokenModel
21+ ------------------------------------------------------------------------------------------------------------------------
22+
23+ data NaiveTokenModel mt acc = MkNaiveTokenModel
24+ { accounts :: IntMap. IntMap (BasicParticle mt ) -- ^ accounts indexed by Int
25+ , pools :: IntMap. IntMap (PDP_Index mt (BasicParticle mt )) -- ^ pools indexed by Int
26+ }
27+
28+ naiveProcessOneEvent ::
29+ MonetaryTypes mt =>
30+ NaiveTokenModel mt Account ->
31+ TokenEvent mt Account ->
32+ NaiveTokenModel mt Account
33+ naiveProcessOneEvent (MkNaiveTokenModel accs pools) = \ case
34+ TransferEvent t from to amount -> go2 from to (shift2a amount t)
35+ UpdateFlowEvent t from to rate -> go2 from to (flow2a rate t)
36+ where go2 from to op =
37+ let sender = IntMap. findWithDefault mempty from accs
38+ receiver = IntMap. findWithDefault mempty from accs
39+ (sender', receiver') = op (sender, receiver)
40+ accs' = IntMap. insert from sender'
41+ $ IntMap. insert to receiver'
42+ $ accs
43+ in MkNaiveTokenModel accs' pools
44+
45+ naiveProcessEvents ::
46+ MonetaryTypes mt =>
47+ [TokenEvent mt Account ] ->
48+ NaiveTokenModel mt Account
49+ naiveProcessEvents = foldl' naiveProcessOneEvent (MkNaiveTokenModel IntMap. empty IntMap. empty)
50+
51+ naiveSystemSnapshot ::
52+ MonetaryTypes'tv mt t v =>
53+ NaiveTokenModel mt Account ->
54+ IntMap. IntMap (t -> v )
55+ naiveSystemSnapshot = (rtb <$> ) . accounts
0 commit comments