{-# LANGUAGE DeriveGeneric #-}
{-# LANGUAGE LambdaCase #-}

-- |
-- Copyright: © 2018-2021 IOHK
-- License: Apache-2.0
--
-- Types and functions relating to Plutus script redeemers
--

module Cardano.Wallet.Primitive.Types.Redeemer
    ( Redeemer (..)
    , redeemerData
    ) where

import Prelude

import Cardano.Api
    ( StakeAddress, serialiseToBech32 )
import Cardano.Wallet.Primitive.Types.TokenPolicy
    ( TokenPolicyId )
import Cardano.Wallet.Primitive.Types.Tx
    ( TxIn )
import Data.ByteString
    ( ByteString )
import Fmt
    ( Buildable (..) )
import GHC.Generics
    ( Generic )

data Redeemer
    = RedeemerSpending ByteString TxIn
    | RedeemerMinting ByteString TokenPolicyId
    | RedeemerRewarding ByteString StakeAddress
    deriving (Redeemer -> Redeemer -> Bool
(Redeemer -> Redeemer -> Bool)
-> (Redeemer -> Redeemer -> Bool) -> Eq Redeemer
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
/= :: Redeemer -> Redeemer -> Bool
$c/= :: Redeemer -> Redeemer -> Bool
== :: Redeemer -> Redeemer -> Bool
$c== :: Redeemer -> Redeemer -> Bool
Eq, (forall x. Redeemer -> Rep Redeemer x)
-> (forall x. Rep Redeemer x -> Redeemer) -> Generic Redeemer
forall x. Rep Redeemer x -> Redeemer
forall x. Redeemer -> Rep Redeemer x
forall a.
(forall x. a -> Rep a x) -> (forall x. Rep a x -> a) -> Generic a
$cto :: forall x. Rep Redeemer x -> Redeemer
$cfrom :: forall x. Redeemer -> Rep Redeemer x
Generic, Int -> Redeemer -> ShowS
[Redeemer] -> ShowS
Redeemer -> String
(Int -> Redeemer -> ShowS)
-> (Redeemer -> String) -> ([Redeemer] -> ShowS) -> Show Redeemer
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
showList :: [Redeemer] -> ShowS
$cshowList :: [Redeemer] -> ShowS
show :: Redeemer -> String
$cshow :: Redeemer -> String
showsPrec :: Int -> Redeemer -> ShowS
$cshowsPrec :: Int -> Redeemer -> ShowS
Show)

instance Buildable Redeemer where
    build :: Redeemer -> Builder
build = \case
        RedeemerSpending ByteString
_ TxIn
input ->
            Builder
"spending(" Builder -> Builder -> Builder
forall a. Semigroup a => a -> a -> a
<> TxIn -> Builder
forall p. Buildable p => p -> Builder
build TxIn
input Builder -> Builder -> Builder
forall a. Semigroup a => a -> a -> a
<> Builder
")"
        RedeemerMinting ByteString
_ TokenPolicyId
pid ->
            Builder
"minting(" Builder -> Builder -> Builder
forall a. Semigroup a => a -> a -> a
<> TokenPolicyId -> Builder
forall p. Buildable p => p -> Builder
build TokenPolicyId
pid Builder -> Builder -> Builder
forall a. Semigroup a => a -> a -> a
<> Builder
")"
        RedeemerRewarding ByteString
_ StakeAddress
addr ->
            Builder
"rewarding(" Builder -> Builder -> Builder
forall a. Semigroup a => a -> a -> a
<> Text -> Builder
forall p. Buildable p => p -> Builder
build (StakeAddress -> Text
forall a. SerialiseAsBech32 a => a -> Text
serialiseToBech32 StakeAddress
addr) Builder -> Builder -> Builder
forall a. Semigroup a => a -> a -> a
<> Builder
")"

redeemerData :: Redeemer -> ByteString
redeemerData :: Redeemer -> ByteString
redeemerData = \case
    RedeemerSpending  ByteString
bytes TxIn
_ -> ByteString
bytes
    RedeemerMinting   ByteString
bytes TokenPolicyId
_ -> ByteString
bytes
    RedeemerRewarding ByteString
bytes StakeAddress
_ -> ByteString
bytes