-
Notifications
You must be signed in to change notification settings - Fork 36
/
Copy pathCurrency.fs
87 lines (77 loc) · 2.83 KB
/
Currency.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
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
namespace GWallet.Backend
open System.Linq
open System.ComponentModel
open GWallet.Backend.FSharpUtil.UwpHacks
// this attribute below is for Json.NET (Newtonsoft.Json) to be able to deserialize this as a dict key
[<TypeConverter(typeof<StringTypeConverter>)>]
type Currency =
// <NOTE if adding a new cryptocurrency below, remember to add it too to GetAll() and ToString()
| BTC
| LTC
| ETH
| ETC
| DAI
| SAI
// </NOTE>
#if STRICTER_COMPILATION_BUT_WITH_REFLECTION_AT_RUNTIME
static member ToStrings() =
Microsoft.FSharp.Reflection.FSharpType.GetUnionCases(typeof<Currency>)
|> Array.map (fun info -> info.Name)
#endif
static member GetAll(): seq<Currency> =
#if STRICTER_COMPILATION_BUT_WITH_REFLECTION_AT_RUNTIME
FSharpUtil.GetAllElementsFromDiscriminatedUnion<Currency>()
#else
seq {
yield BTC
yield LTC
yield ETH
yield ETC
yield DAI
yield SAI
}
#endif
static member Parse(currencyString: string): Currency =
Currency.GetAll().First(fun currency -> currencyString = currency.ToString())
member self.IsEther() =
self = Currency.ETC || self = Currency.ETH
member self.IsEthToken() =
self = Currency.DAI || self = Currency.SAI
member self.IsEtherBased() =
self.IsEther() || self.IsEthToken()
member self.IsUtxo() =
self = Currency.BTC || self = Currency.LTC
member self.DecimalPlaces(): int =
if self.IsUtxo() then
8
elif self.IsEther() then
18
elif self = Currency.SAI || self = Currency.DAI then
18
else
failwith <| SPrintF1 "Unable to determine decimal places for %A" self
override self.ToString() =
#if STRICTER_COMPILATION_BUT_WITH_REFLECTION_AT_RUNTIME
SPrintF1 "%A" self
#else
// when we can depend on newer versions of F#, we might be able to get rid of this (or ToString() altogther) below
// (see FSharpUtil's test names "converts fsharp's print syntax to String-Format (advanced II)" for more info):
match self with
| BTC -> "BTC"
| LTC -> "LTC"
| ETH -> "ETH"
| ETC -> "ETC"
| SAI -> "SAI"
| DAI -> "DAI"
#endif
// the reason we have used "and" is because of the circular reference
// between StringTypeConverter and Currency
and private StringTypeConverter() =
inherit TypeConverter()
override __.CanConvertFrom(context, sourceType) =
sourceType = typeof<string> || base.CanConvertFrom(context, sourceType)
override __.ConvertFrom(context, culture, value) =
match value with
| :? string as stringValue ->
Seq.find (fun cur -> cur.ToString() = stringValue) (Currency.GetAll()) :> obj
| _ -> base.ConvertFrom(context, culture, value)