f-o-a-m / purescript-web3

a purescript library for the web3 api
Apache License 2.0
127 stars 24 forks source link

Proposal for more general TokenUnit #108

Closed safareli closed 6 years ago

safareli commented 6 years ago

Module EtherUnit can be used with other token if we change name of module from Network.Ethereum.Web3.Types.EtherUnit to Network.Ethereum.Web3.Types.TokenUnit and:

foreign import kind Token

class TokenUnit a where
  fromMinorUnit  :: BigNumber -> a
  toMinorUnit :: a -> BigNumber

class TokenUnitSpec a where
  divider :: Proxy a -> BigNumber
  -- note `name` was removed (it's not used at all)

Found the term "minor unit" here

This way the module and it's content would be applicable to other tokens too, we can then have:

foreign import data NoPay :: Token -> Type
instance unitSpecNoPay :: TokenUnitSpec (NoPay t) where divider = const $ unsafeConvert "1"

foreign import data MinorUnit :: Token -> Type
instance unitSpecMinorUnit :: TokenUnitSpec (MinorUnit t) where divider = const $ unsafeConvert "1"

foreign import data MinorUnitE3 :: Token -> Type
instance unitSpecMinorUnitE3 :: TokenUnitSpec (MinorUnitE3 t) where divider = const $ unsafeConvert "1000"

foreign import data MinorUnitE6 :: Token -> Type
instance unitSpecMinorUnitE6 :: TokenUnitSpec (MinorUnitE6 t) where divider = const $ unsafeConvert "1000000"

foreign import data MinorUnitE9 :: Token -> Type
instance unitSpecMinorUnitE9 :: TokenUnitSpec (MinorUnitE9 t) where divider = const $ unsafeConvert "1000000000"

foreign import data MinorUnitE12 :: Token -> Type
instance unitSpecMinorUnitE12 :: TokenUnitSpec (MinorUnitE12 t) where divider = const $ unsafeConvert "1000000000000"

foreign import data MinorUnitE15 :: Token -> Type
instance unitSpecMinorUnitE15 :: TokenUnitSpec (MinorUnitE15 t) where divider = const $ unsafeConvert "1000000000000000"

foreign import data MinorUnitE18 :: Token -> Type
instance unitSpecMinorUnitE18 :: TokenUnitSpec (MinorUnitE18 t) where divider = const $ unsafeConvert "1000000000000000000"

foreign import data ETHER :: Token -> Type

type Wei = MinorUnit ETHER
type Babbage = MinorUnitE3 ETHER
type Lovelace = MinorUnitE6 ETHER
type Shannon = MinorUnitE9 ETHER
type Szabo = MinorUnitE12 ETHER
type Finney = MinorUnitE15 ETHER
type Ether = MinorUnitE18 ETHER

-- Number of decimals for Ether is 18 while Arbitrary ERC20 token can have different number of [decimals](https://github.com/ethereum/EIPs/blob/master/EIPS/eip-20.md#decimals). 
-- let's say MyCoolApp (MCA for short) token has 12 decimals
foreign import data MY_COOL_APP_TOKEN :: Token -> Type
type MCA = MinorUnitE12 MY_COOL_APP_TOKEN