/
Chain.purs
207 lines (187 loc) · 6.71 KB
/
Chain.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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
module Chain
( mockchainChartOptions
, balancesChartOptions
, evaluationPane
) where
import Bootstrap (empty, nbsp)
import Color (Color, rgb, white)
import Control.Monad.Aff.Class (class MonadAff)
import Data.Array as Array
import Data.Foldable (traverse_)
import Data.Generic (gShow)
import Data.Int as Int
import Data.Maybe (Maybe(Nothing))
import Data.Newtype (unwrap)
import Data.Tuple (Tuple, fst, snd)
import Data.Tuple.Nested ((/\))
import ECharts.Commands (addItem, addLink, axisLine, axisType, backgroundColor, bar, bottom, buildItems, buildLinks, color, colorSource, colors, formatterString, itemStyle, items, label, left, lineStyle, name, nameGap, nameLocationMiddle, nameRotate, normal, right, sankey, series, sourceName, splitLine, targetName, textStyle, tooltip, top, trigger, value, xAxis, yAxis) as E
import ECharts.Extras (focusNodeAdjacencyAllEdges, orientVertical, positionBottom)
import ECharts.Monad (CommandsT, DSL) as E
import ECharts.Types (AxisType(Value, Category), PixelOrPercent(Pixel), TooltipTrigger(ItemTrigger), numItem, strItem) as E
import ECharts.Types.Phantom (I)
import Halogen (HTML)
import Halogen.Component (ParentHTML)
import Halogen.ECharts (EChartsEffects, echarts)
import Halogen.HTML (ClassName(ClassName), br_, div, div_, h2_, h3_, slot', text)
import Halogen.HTML.Events (input)
import Halogen.HTML.Properties (class_)
import Ledger.Types (Slot(..), TxIdOf(..), Value)
import Playground.API (EvaluationResult(EvaluationResult))
import Prelude (class Monad, Unit, discard, map, show, unit, ($), (<$>), (<>), (>>>))
import Types (BalancesChartSlot(BalancesChartSlot), ChildQuery, ChildSlot, MockchainChartSlot(MockchainChartSlot), Query(HandleBalancesChartMessage, HandleMockchainChartMessage), cpBalancesChart, cpMockchainChart)
import Wallet.Emulator.Types (EmulatorEvent(..), Wallet(..))
import Wallet.Graph (FlowGraph(FlowGraph), FlowLink(FlowLink), TxRef(TxRef))
evaluationPane::
forall m aff.
MonadAff (EChartsEffects aff) m
=> EvaluationResult
-> ParentHTML Query ChildQuery ChildSlot m
evaluationPane (EvaluationResult {emulatorLog}) =
div_
[ h2_ [ text "Transactions" ]
, div_
[ h3_ [ text "Chain" ]
, slot' cpMockchainChart MockchainChartSlot
(echarts Nothing)
({width: 930, height: 600} /\ unit)
(input HandleMockchainChartMessage)
]
, br_
, div_
[ h3_ [ text "Logs" ]
, case emulatorLog of
[] -> empty
logs ->
div
[ class_ $ ClassName "logs" ]
(emulatorEventPane <$> Array.reverse logs)
]
, br_
, div_
[ h3_ [ text "Final Balances" ]
, slot' cpBalancesChart BalancesChartSlot
(echarts Nothing)
({width: 930, height: 300} /\ unit)
(input HandleBalancesChartMessage)
]
]
emulatorEventPane :: forall i p. EmulatorEvent -> HTML p i
emulatorEventPane (TxnSubmit (TxIdOf txId)) =
div_
[ text $ "Submitting transaction: " <> txId.getTxId ]
emulatorEventPane (TxnValidate (TxIdOf txId)) =
div_
[ text $ "Validating transaction: " <> txId.getTxId ]
emulatorEventPane (TxnValidationFail (TxIdOf txId) error) =
div [ class_ $ ClassName "error" ]
[ text $ "Validation failed for transaction: " <> txId.getTxId
, br_
, nbsp
, text $ gShow error
]
emulatorEventPane (SlotAdd (Slot slot)) =
div [ class_ $ ClassName "info" ]
[ text $ "Add slot #" <> show slot.getSlot ]
emulatorEventPane (WalletError (Wallet walletId) error) =
div [ class_ $ ClassName "error" ]
[ text $ "Error from wallet #" <> show walletId.getWallet <> ": " <> gShow error ]
emulatorEventPane (WalletInfo (Wallet walletId) info) =
div_
[ text $ "Message from wallet #" <> show walletId.getWallet <> ": " <> info ]
------------------------------------------------------------
offWhite :: Color
offWhite = rgb 188 188 193
lightPurple :: Color
lightPurple = rgb 163 128 188
lightBlue :: Color
lightBlue = rgb 88 119 182
fadedBlue :: Color
fadedBlue = rgb 35 39 64
softPalette :: Array Color
softPalette =
[ rgb 55 68 106
, rgb 54 93 72
, rgb 94 50 62
]
hardPalette :: Array Color
hardPalette =
[ rgb 210 112 240
, rgb 252 255 119
, rgb 255 126 119
, rgb 112 240 130
, rgb 163 128 188
, rgb 112 156 240
]
------------------------------------------------------------
-- | Remember here that the Blockchain is latest-block *first*.
mockchainChartOptions ::
forall m.
Monad m
=> FlowGraph
-> E.CommandsT (series :: I, tooltip :: I, color :: I) m Unit
mockchainChartOptions (FlowGraph {flowGraphLinks, flowGraphNodes}) = do
E.tooltip $ do
E.trigger E.ItemTrigger
E.colors hardPalette
E.series do
E.sankey do
traverse_ (\f -> f (E.Pixel 30)) [ E.top, E.right, E.bottom, E.left ]
focusNodeAdjacencyAllEdges
orientVertical
E.lineStyle $ E.normal do
E.colorSource
E.buildLinks $ traverse_ toEchartLink flowGraphLinks
E.buildItems $ traverse_ toEchartItem flowGraphNodes
E.label $ E.normal do
E.color white
positionBottom
toEchartItem :: forall m i. Monad m => TxRef -> E.CommandsT (item :: I | i) m Unit
toEchartItem (TxRef name) =
E.addItem do
E.name name
E.value 0.0
toEchartLink :: forall m i. Monad m => FlowLink -> E.CommandsT (link :: I | i) m Unit
toEchartLink (FlowLink link) =
E.addLink do
E.sourceName $ unwrap $ link.flowLinkSource
E.targetName $ unwrap $ link.flowLinkTarget
E.value $ Int.toNumber link.flowLinkValue
------------------------------------------------------------
balancesChartOptions ::
forall m.
Monad m
=> Array (Tuple Wallet Value)
-> E.CommandsT ( series :: I
, grid :: I
, xAxis :: I
, yAxis :: I
, backgroundColor :: I
, tooltip :: I
, textStyle :: I
) m Unit
balancesChartOptions wallets = do
E.tooltip $ do
E.trigger E.ItemTrigger
E.formatterString "{c} ADA"
E.textStyle $ E.color lightBlue
E.backgroundColor fadedBlue
E.xAxis do
E.axisType E.Category
E.items $ map (fst >>> unwrap >>> _.getWallet >>> show >>> (<>) "Wallet #" >>> E.strItem) wallets
axisLineStyle
E.yAxis do
E.name "Final Balance"
E.nameRotate 90.0
E.nameLocationMiddle
E.nameGap 30.0
E.axisType E.Value
axisLineStyle
E.series do
E.bar do
E.items $ map (snd >>> unwrap >>> _.getValue >>> Int.toNumber >>> E.numItem) wallets
E.itemStyle $ E.normal $ E.color lightPurple
where
axisLineStyle :: forall i. E.DSL (axisLine :: I, splitLine :: I | i) m
axisLineStyle = do
E.axisLine $ E.lineStyle $ E.color lightBlue
E.splitLine $ E.lineStyle $ E.color lightBlue