module Network.Ethereum.Unit (
Unit(..)
, Wei
, Babbage
, Lovelace
, Shannon
, Szabo
, Finney
, Ether
, KEther
) where
import Text.ParserCombinators.ReadPrec
import Data.Text.Lazy (Text, unpack)
import qualified Text.Read.Lex as L
import Data.Monoid ((<>))
import Text.Printf
import GHC.Read
class (Read a, Show a, UnitSpec a, Fractional a) => Unit a where
fromWei :: Integer -> a
toWei :: a -> Integer
convert :: Unit b => a -> b
convert = fromWei . toWei
class UnitSpec a where
divider :: RealFrac b => Value a -> b
name :: Value a -> Text
data Value a = MkValue { unValue :: Integer }
deriving (Eq, Ord)
mkValue :: (UnitSpec a, RealFrac b) => b -> Value a
mkValue = modify res . round . (divider res *)
where res = undefined :: UnitSpec a => Value a
modify :: Value a -> Integer -> Value a
modify _ = MkValue
instance UnitSpec a => Unit (Value a) where
fromWei = MkValue
toWei = unValue
instance UnitSpec a => UnitSpec (Value a) where
divider = divider . (undefined :: Value (Value a) -> Value a)
name = name . (undefined :: Value (Value a) -> Value a)
instance UnitSpec a => Num (Value a) where
a + b = MkValue (unValue a + unValue b)
a b = MkValue (unValue a unValue b)
a * b = MkValue (unValue a * unValue b)
signum (MkValue a) = MkValue (abs a)
abs (MkValue a) = MkValue (abs a)
fromInteger = mkValue . fromIntegral
instance UnitSpec a => Fractional (Value a) where
a / b = MkValue (unValue a `div` unValue b)
fromRational = mkValue
instance UnitSpec a => Show (Value a) where
show val = printf "%F %s" (x / d :: Double) (name val)
where
x = fromIntegral (unValue val)
d = divider val
instance UnitSpec a => Read (Value a) where
readPrec = parens $ do
x <- readPrec
let res = mkValue x
resName = unpack (name res)
step $ expectP (L.Ident resName)
return res
data U0
data U1
data U2
data U3
data U4
data U5
data U6
data U7
type Wei = Value U0
instance UnitSpec U0 where
divider = const 1
name = const "wei"
type Babbage = Value U1
instance UnitSpec U1 where
divider = const 1e3
name = const "babbage"
type Lovelace = Value U2
instance UnitSpec U2 where
divider = const 1e6
name = const "lovelace"
type Shannon = Value U3
instance UnitSpec U3 where
divider = const 1e9
name = const "shannon"
type Szabo = Value U4
instance UnitSpec U4 where
divider = const 1e12
name = const "szabo"
type Finney = Value U5
instance UnitSpec U5 where
divider = const 1e15
name = const "finney"
type Ether = Value U6
instance UnitSpec U6 where
divider = const 1e18
name = const "ether"
type KEther = Value U7
instance UnitSpec U7 where
divider = const 1e21
name = const "kether"