-- | Amount sum type

{-# LANGUAGE NumericUnderscores #-}

module Blockfrost.Types.Shared.Amount
  where

import Blockfrost.Types.Shared.Ada (Lovelaces)
import Data.Aeson
  ( FromJSON (..)
  , ToJSON (..)
  , Value (Object)
  , object
  , withObject
  , (.:)
  , (.=)
  )
import GHC.Generics
import qualified Money
import Servant.Docs (ToSample (..), samples)
import qualified Text.Read

-- | Amount, which is either `AdaAmount Lovelaces` representing
-- amount of lovelaces or `AssetAmount SomeDiscrete` for asset amounts,
-- identified by concatenation of asset policy ID
-- and hex-encoded asset_name
data Amount =
    AdaAmount Lovelaces
  | AssetAmount Money.SomeDiscrete
  deriving (Amount -> Amount -> Bool
(Amount -> Amount -> Bool)
-> (Amount -> Amount -> Bool) -> Eq Amount
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
/= :: Amount -> Amount -> Bool
$c/= :: Amount -> Amount -> Bool
== :: Amount -> Amount -> Bool
$c== :: Amount -> Amount -> Bool
Eq, Int -> Amount -> ShowS
[Amount] -> ShowS
Amount -> String
(Int -> Amount -> ShowS)
-> (Amount -> String) -> ([Amount] -> ShowS) -> Show Amount
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
showList :: [Amount] -> ShowS
$cshowList :: [Amount] -> ShowS
show :: Amount -> String
$cshow :: Amount -> String
showsPrec :: Int -> Amount -> ShowS
$cshowsPrec :: Int -> Amount -> ShowS
Show, Eq Amount
Eq Amount
-> (Amount -> Amount -> Ordering)
-> (Amount -> Amount -> Bool)
-> (Amount -> Amount -> Bool)
-> (Amount -> Amount -> Bool)
-> (Amount -> Amount -> Bool)
-> (Amount -> Amount -> Amount)
-> (Amount -> Amount -> Amount)
-> Ord Amount
Amount -> Amount -> Bool
Amount -> Amount -> Ordering
Amount -> Amount -> Amount
forall a.
Eq a
-> (a -> a -> Ordering)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> a)
-> (a -> a -> a)
-> Ord a
min :: Amount -> Amount -> Amount
$cmin :: Amount -> Amount -> Amount
max :: Amount -> Amount -> Amount
$cmax :: Amount -> Amount -> Amount
>= :: Amount -> Amount -> Bool
$c>= :: Amount -> Amount -> Bool
> :: Amount -> Amount -> Bool
$c> :: Amount -> Amount -> Bool
<= :: Amount -> Amount -> Bool
$c<= :: Amount -> Amount -> Bool
< :: Amount -> Amount -> Bool
$c< :: Amount -> Amount -> Bool
compare :: Amount -> Amount -> Ordering
$ccompare :: Amount -> Amount -> Ordering
$cp1Ord :: Eq Amount
Ord, (forall x. Amount -> Rep Amount x)
-> (forall x. Rep Amount x -> Amount) -> Generic Amount
forall x. Rep Amount x -> Amount
forall x. Amount -> Rep Amount x
forall a.
(forall x. a -> Rep a x) -> (forall x. Rep a x -> a) -> Generic a
$cto :: forall x. Rep Amount x -> Amount
$cfrom :: forall x. Amount -> Rep Amount x
Generic)

-- | SomeDiscrete values always use scale of 1
unitScale :: Money.Scale
unitScale :: Scale
unitScale = let (Just Scale
s) = Rational -> Maybe Scale
Money.scaleFromRational Rational
1 in Scale
s

instance ToJSON Money.SomeDiscrete where
  toJSON :: SomeDiscrete -> Value
toJSON SomeDiscrete
sd =
    [Pair] -> Value
object [ Text
"unit" Text -> Text -> Pair
forall kv v. (KeyValue kv, ToJSON v) => Text -> v -> kv
.= SomeDiscrete -> Text
Money.someDiscreteCurrency SomeDiscrete
sd
           , Text
"quantity" Text -> String -> Pair
forall kv v. (KeyValue kv, ToJSON v) => Text -> v -> kv
.= Integer -> String
forall a. Show a => a -> String
show (SomeDiscrete -> Integer
Money.someDiscreteAmount SomeDiscrete
sd)
           ]

instance FromJSON Money.SomeDiscrete where
  parseJSON :: Value -> Parser SomeDiscrete
parseJSON = String
-> (Object -> Parser SomeDiscrete) -> Value -> Parser SomeDiscrete
forall a. String -> (Object -> Parser a) -> Value -> Parser a
withObject String
"amount" ((Object -> Parser SomeDiscrete) -> Value -> Parser SomeDiscrete)
-> (Object -> Parser SomeDiscrete) -> Value -> Parser SomeDiscrete
forall a b. (a -> b) -> a -> b
$ \Object
o -> do
    Text
u <- Object
o Object -> Text -> Parser Text
forall a. FromJSON a => Object -> Text -> Parser a
.: Text
"unit"
    (String
strQuant :: String) <- Object
o Object -> Text -> Parser String
forall a. FromJSON a => Object -> Text -> Parser a
.: Text
"quantity"
    case String -> Maybe Integer
forall a. Read a => String -> Maybe a
Text.Read.readMaybe String
strQuant of
      Maybe Integer
Nothing    -> String -> Parser SomeDiscrete
forall (m :: * -> *) a. MonadFail m => String -> m a
fail String
"Unable to read quantity as Integer"
      Just Integer
quant -> SomeDiscrete -> Parser SomeDiscrete
forall (f :: * -> *) a. Applicative f => a -> f a
pure (SomeDiscrete -> Parser SomeDiscrete)
-> SomeDiscrete -> Parser SomeDiscrete
forall a b. (a -> b) -> a -> b
$ Text -> Scale -> Integer -> SomeDiscrete
Money.mkSomeDiscrete Text
u Scale
unitScale Integer
quant

instance ToJSON Amount where
  toJSON :: Amount -> Value
toJSON (AdaAmount Lovelaces
lovelaces) =
    [Pair] -> Value
object [ Text
"unit" Text -> String -> Pair
forall kv v. (KeyValue kv, ToJSON v) => Text -> v -> kv
.= (String
"lovelace" :: String)
           , Text
"quantity" Text -> Value -> Pair
forall kv v. (KeyValue kv, ToJSON v) => Text -> v -> kv
.= Discrete' "ADA" '(1000000, 1) -> Value
forall a. ToJSON a => a -> Value
toJSON Discrete' "ADA" '(1000000, 1)
Lovelaces
lovelaces
           ]
  toJSON (AssetAmount SomeDiscrete
av) = SomeDiscrete -> Value
forall a. ToJSON a => a -> Value
toJSON SomeDiscrete
av

instance FromJSON Amount where
  parseJSON :: Value -> Parser Amount
parseJSON x :: Value
x@(Object Object
o) = do
    (String
u :: String) <- Object
o Object -> Text -> Parser String
forall a. FromJSON a => Object -> Text -> Parser a
.: Text
"unit"
    Value
v <- Object
o Object -> Text -> Parser Value
forall a. FromJSON a => Object -> Text -> Parser a
.: Text
"quantity"
    case String
u of
      String
"lovelace" -> Discrete' "ADA" '(1000000, 1) -> Amount
Lovelaces -> Amount
AdaAmount (Discrete' "ADA" '(1000000, 1) -> Amount)
-> Parser (Discrete' "ADA" '(1000000, 1)) -> Parser Amount
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Value -> Parser (Discrete' "ADA" '(1000000, 1))
forall a. FromJSON a => Value -> Parser a
parseJSON Value
v
      String
_          -> SomeDiscrete -> Amount
AssetAmount (SomeDiscrete -> Amount) -> Parser SomeDiscrete -> Parser Amount
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Value -> Parser SomeDiscrete
forall a. FromJSON a => Value -> Parser a
parseJSON Value
x
  parseJSON Value
other = String -> Parser Amount
forall (m :: * -> *) a. MonadFail m => String -> m a
fail (String -> Parser Amount) -> String -> Parser Amount
forall a b. (a -> b) -> a -> b
$ String
"Amount expecting object, got" String -> ShowS
forall a. [a] -> [a] -> [a]
++ Value -> String
forall a. Show a => a -> String
show Value
other

instance ToSample Amount where
  toSamples :: Proxy Amount -> [(Text, Amount)]
toSamples = [(Text, Amount)] -> Proxy Amount -> [(Text, Amount)]
forall (f :: * -> *) a. Applicative f => a -> f a
pure ([(Text, Amount)] -> Proxy Amount -> [(Text, Amount)])
-> [(Text, Amount)] -> Proxy Amount -> [(Text, Amount)]
forall a b. (a -> b) -> a -> b
$ [Amount] -> [(Text, Amount)]
forall a. [a] -> [(Text, a)]
samples
    [ Lovelaces -> Amount
AdaAmount Lovelaces
42000000
    , SomeDiscrete -> Amount
AssetAmount
        (SomeDiscrete -> Amount) -> SomeDiscrete -> Amount
forall a b. (a -> b) -> a -> b
$ Discrete'
  "b0d07d45fe9514f80213f4020e5a61241458be626841cde717cb38a76e7574636f696e"
  '(1, 1)
-> SomeDiscrete
forall (currency :: Symbol) (scale :: (Nat, Nat)).
(KnownSymbol currency, GoodScale scale) =>
Discrete' currency scale -> SomeDiscrete
Money.toSomeDiscrete
          (Discrete'
  "b0d07d45fe9514f80213f4020e5a61241458be626841cde717cb38a76e7574636f696e"
  '(1, 1)
12 :: Money.Discrete'
                    "b0d07d45fe9514f80213f4020e5a61241458be626841cde717cb38a76e7574636f696e"
                    '(1,1))
    , SomeDiscrete -> Amount
AssetAmount
        (SomeDiscrete -> Amount) -> SomeDiscrete -> Amount
forall a b. (a -> b) -> a -> b
$ Discrete'
  "6804edf9712d2b619edb6ac86861fe93a730693183a262b165fcc1ba1bc99cad"
  '(1, 1)
-> SomeDiscrete
forall (currency :: Symbol) (scale :: (Nat, Nat)).
(KnownSymbol currency, GoodScale scale) =>
Discrete' currency scale -> SomeDiscrete
Money.toSomeDiscrete
          (Discrete'
  "6804edf9712d2b619edb6ac86861fe93a730693183a262b165fcc1ba1bc99cad"
  '(1, 1)
18605647 :: Money.Discrete'
                          "6804edf9712d2b619edb6ac86861fe93a730693183a262b165fcc1ba1bc99cad"
                          '(1,1))
    ]