-
Notifications
You must be signed in to change notification settings - Fork 0
/
Logging.fs
62 lines (50 loc) · 2.29 KB
/
Logging.fs
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
namespace Web3.fs
[<AutoOpen>]
module Logging =
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Logging
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////
///
/// Binds and starts the transaction monitor if a transaction hash was
/// emitted from `makeEthTxn`. Intended to be placed in a transaction
/// pipeline to provide realtime logging of transaction completion.
///
let internal monitorTransaction (monitor: Monitor) (r: Result<EthTransactionHash, Web3Error>) =
match r with
| Ok o -> monitor o
| Error e -> e |> Error
///
/// Forwards CallResponses, logs errors to the console
let private logCallResponsesOrWeb3Errors (logger: Logger) (pipeResult: Result<CallResponses, Web3Error>) =
match pipeResult with
| Ok o -> logger.Post (Success, o)
| Error e ->
match e with
| PayableFunctionZeroValueWarning w -> logger.Post (Warn, $"{w}" |> Library)
| e -> logger.Post (Failure, $"{e}" |> Library)
///
/// Unwraps Result when Emit or LogAndEmit are signalled.
let private emitter pipeResult =
match pipeResult with
| Ok callResponses -> callResponses
| Error _ -> Empty
///
/// Generic logger for use in all RPC calls.
///
/// * logger: A `Logger`. Typically generated from `createWeb3Environment`.
/// * signal: An indicator to tell the logger how to handle the Result. One
/// of 'Log', 'Emit', 'LogAndEmit', or 'Quiet'
/// * pipeResult: Result flow from an Ethereum call into this function.
///
let public log (logger: Logger) signal (pipeResult: Result<CallResponses, Web3Error>) =
match signal with
| Log ->
logCallResponsesOrWeb3Errors logger pipeResult
Async.Sleep(100) |> Async.RunSynchronously // I don't like this bodge
Empty
| Emit -> emitter pipeResult
| LogAndEmit ->
logCallResponsesOrWeb3Errors logger pipeResult
Async.Sleep(100) |> Async.RunSynchronously
emitter pipeResult
| Quiet -> Empty