Skip to content

Commit d10a16f

Browse files
authored
Merge pull request #74 from cmditch/event-sentry-fix
All kinds of tricky considerations arose as the re-implementation moved along. Basically we mark all requests as pending until a block number is returned from the EventSentry.init RPC call. Once we get a blockNumber, the whole machinery starts. This accounts for the edge case "What if two or more blocks are mined between the 2-second polling period?". This also implements the EventySentry.watch API a bit more honestly, as the old one ignored the LogFilter's fromBlock and toBlock fields, and mutated them to watch the latest block off the bat. This implementation will allow you to grab arbitrary block ranges, as well as continuously poll the latest blocks.
2 parents 767f94f + 471de92 commit d10a16f

File tree

3 files changed

+240
-111
lines changed

3 files changed

+240
-111
lines changed

elm.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
"name": "cmditch/elm-ethereum",
44
"summary": "feed the tree some ether.",
55
"license": "MIT",
6-
"version": "3.0.0",
6+
"version": "3.0.1",
77
"exposed-modules": [
88
"Eth",
99
"Eth.Decode",

integration-tests/src/Main.elm

Lines changed: 87 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -95,8 +95,9 @@ view model =
9595

9696
type Msg
9797
= InitTest
98-
| WatchOnce
99-
| ContinousWatching
98+
| WatchLatest
99+
| WatchRanged
100+
| WatchOnceRangeToLatest
100101
| NewResponse String
101102
| EventSentryMsg EventSentry.Msg
102103

@@ -112,26 +113,36 @@ update msg model =
112113
, transactionCmd
113114
, blockCmd
114115
, contractCmds
115-
, watchOnceEvent
116-
, continuousWatchingEvent
116+
, watchLatest
117+
, watchRanged
118+
, watchOnceRangeToLatest
117119
]
118120
)
119121

120-
WatchOnce ->
122+
WatchLatest ->
121123
let
122-
( subModel, subCmd ) =
123-
EventSentry.watchOnce (logToString >> (++) "WatchOnce Cmd: " >> NewResponse)
124+
( subModel, subCmd, _ ) =
125+
EventSentry.watch watchLatestHelper
124126
model.eventSentry
125-
daiTransferFilter
127+
filterLatest
126128
in
127129
( { model | eventSentry = subModel }, subCmd )
128130

129-
ContinousWatching ->
131+
WatchRanged ->
130132
let
131133
( subModel, subCmd, _ ) =
132-
EventSentry.watch (logToString >> (++) "ContinousWatching Cmd: " >> NewResponse)
134+
EventSentry.watch watchRangedHelper
135+
model.eventSentry
136+
filterRanged
137+
in
138+
( { model | eventSentry = subModel }, subCmd )
139+
140+
WatchOnceRangeToLatest ->
141+
let
142+
( subModel, subCmd ) =
143+
EventSentry.watchOnce watchOnceRangeToLatestHelper
133144
model.eventSentry
134-
daiTransferFilter
145+
filterRangeToLatest
135146
in
136147
( { model | eventSentry = subModel }, subCmd )
137148

@@ -150,16 +161,6 @@ update msg model =
150161
-- Test Cmds
151162

152163

153-
watchOnceEvent : Cmd Msg
154-
watchOnceEvent =
155-
Task.perform (\_ -> WatchOnce) (Task.succeed ())
156-
157-
158-
continuousWatchingEvent : Cmd Msg
159-
continuousWatchingEvent =
160-
Task.perform (\_ -> ContinousWatching) (Task.succeed ())
161-
162-
163164
logCmd : Cmd Msg
164165
logCmd =
165166
Eth.getLogs ethNode erc20TransferFilter
@@ -268,15 +269,6 @@ erc20TransferFilter =
268269
}
269270

270271

271-
daiTransferFilter : LogFilter
272-
daiTransferFilter =
273-
{ fromBlock = LatestBlock
274-
, toBlock = LatestBlock
275-
, address = Utils.unsafeToAddress "0x89d24A6b4CcB1B6fAA2625fE562bDD9a23260359"
276-
, topics = [ Just <| Utils.unsafeToHex "0xddf252ad1be2c89b69c2b068fc378daa952ba7f163c4a11628f55a4df523b3ef" ]
277-
}
278-
279-
280272
erc20Contract : Address
281273
erc20Contract =
282274
Utils.unsafeToAddress "0x9f8f72aa9304c8b593d555f12ef6589cc3a579a2"
@@ -310,3 +302,68 @@ responseToString okToString result =
310302

311303
Err err ->
312304
String.Conversions.fromHttpError err
305+
306+
307+
308+
-- EventSentry Helpers
309+
-- ( Using DAI transfer event )
310+
311+
312+
watchLatest : Cmd Msg
313+
watchLatest =
314+
Task.perform (\_ -> WatchLatest) (Task.succeed ())
315+
316+
317+
watchLatestHelper =
318+
logToString >> (++) "WatchLatest Cmd: " >> NewResponse
319+
320+
321+
filterLatest : LogFilter
322+
filterLatest =
323+
{ fromBlock = LatestBlock
324+
, toBlock = LatestBlock
325+
, address = Utils.unsafeToAddress "0x89d24A6b4CcB1B6fAA2625fE562bDD9a23260359"
326+
, topics = [ Just <| Utils.unsafeToHex "0xddf252ad1be2c89b69c2b068fc378daa952ba7f163c4a11628f55a4df523b3ef" ]
327+
}
328+
329+
330+
331+
--
332+
333+
334+
watchRanged : Cmd Msg
335+
watchRanged =
336+
Task.perform (\_ -> WatchRanged) (Task.succeed ())
337+
338+
339+
watchRangedHelper =
340+
logToString >> (++) "WatchRanged Cmd: " >> NewResponse
341+
342+
343+
filterRanged : LogFilter
344+
filterRanged =
345+
{ filterLatest
346+
| fromBlock = BlockNum 7396400
347+
, toBlock = BlockNum 7396404
348+
}
349+
350+
351+
352+
--
353+
354+
355+
watchOnceRangeToLatest : Cmd Msg
356+
watchOnceRangeToLatest =
357+
Task.perform (\_ -> WatchOnceRangeToLatest) (Task.succeed ())
358+
359+
360+
watchOnceRangeToLatestHelper =
361+
logToString >> (++) "WatchOnceRangeToLatest Cmd: " >> NewResponse
362+
363+
364+
filterRangeToLatest : LogFilter
365+
filterRangeToLatest =
366+
{ filterLatest
367+
| fromBlock = BlockNum 7396400
368+
, toBlock = LatestBlock
369+
}

0 commit comments

Comments
 (0)