{-# LANGUAGE OverloadedStrings #-}
module Cometa.Transaction.Deferred
( Query,
draft,
selectedInputs,
fromEither,
run,
inputIndex,
referenceInputIndex,
mintPolicyIndex,
withdrawalIndex,
voteIndex,
proposalIndex,
redeemerIndex,
outputIndex,
outputIndexWhere,
redeemer,
encodedRedeemer,
)
where
import Cometa.Address.Reward (RewardAddress)
import Cometa.Error (Error (InvalidBuildingRequest))
import Cometa.Evaluation (RedeemerId)
import Cometa.Governance.Proposal (Proposal)
import Cometa.Governance.Voting (Voter)
import qualified Cometa.PlutusData as Data
import Cometa.PolicyId (PolicyId)
import Cometa.Transaction (Transaction)
import qualified Cometa.Transaction as Tx
import qualified Cometa.Transaction.Body as Body
import qualified Cometa.Transaction.Fields as Fields
import qualified Cometa.Transaction.Indices as Indices
import Cometa.Transaction.Input (Input)
import Cometa.Transaction.Output (Output)
import Cometa.Transaction.Plan (DeferredRedeemer)
import Cometa.Transaction.Utxo (Utxo)
import Data.Text (Text)
import Data.Word (Word64)
newtype Query a = Query (Transaction -> [Utxo] -> Either Error a)
instance Functor Query where
fmap :: forall a b. (a -> b) -> Query a -> Query b
fmap a -> b
f (Query Transaction -> [Utxo] -> Either Error a
query) = (Transaction -> [Utxo] -> Either Error b) -> Query b
forall a. (Transaction -> [Utxo] -> Either Error a) -> Query a
Query (\Transaction
transaction [Utxo]
inputs -> (a -> b) -> Either Error a -> Either Error b
forall a b. (a -> b) -> Either Error a -> Either Error b
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap a -> b
f (Transaction -> [Utxo] -> Either Error a
query Transaction
transaction [Utxo]
inputs))
instance Applicative Query where
pure :: forall a. a -> Query a
pure a
value = (Transaction -> [Utxo] -> Either Error a) -> Query a
forall a. (Transaction -> [Utxo] -> Either Error a) -> Query a
Query (\Transaction
_ [Utxo]
_ -> a -> Either Error a
forall a b. b -> Either a b
Right a
value)
Query Transaction -> [Utxo] -> Either Error (a -> b)
function <*> :: forall a b. Query (a -> b) -> Query a -> Query b
<*> Query Transaction -> [Utxo] -> Either Error a
argument = (Transaction -> [Utxo] -> Either Error b) -> Query b
forall a. (Transaction -> [Utxo] -> Either Error a) -> Query a
Query ((Transaction -> [Utxo] -> Either Error b) -> Query b)
-> (Transaction -> [Utxo] -> Either Error b) -> Query b
forall a b. (a -> b) -> a -> b
$ \Transaction
transaction [Utxo]
inputs ->
Transaction -> [Utxo] -> Either Error (a -> b)
function Transaction
transaction [Utxo]
inputs Either Error (a -> b) -> Either Error a -> Either Error b
forall a b.
Either Error (a -> b) -> Either Error a -> Either Error b
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> Transaction -> [Utxo] -> Either Error a
argument Transaction
transaction [Utxo]
inputs
instance Monad Query where
Query Transaction -> [Utxo] -> Either Error a
query >>= :: forall a b. Query a -> (a -> Query b) -> Query b
>>= a -> Query b
next = (Transaction -> [Utxo] -> Either Error b) -> Query b
forall a. (Transaction -> [Utxo] -> Either Error a) -> Query a
Query ((Transaction -> [Utxo] -> Either Error b) -> Query b)
-> (Transaction -> [Utxo] -> Either Error b) -> Query b
forall a b. (a -> b) -> a -> b
$ \Transaction
transaction [Utxo]
inputs -> do
a
value <- Transaction -> [Utxo] -> Either Error a
query Transaction
transaction [Utxo]
inputs
Query b -> Transaction -> [Utxo] -> Either Error b
forall a. Query a -> Transaction -> [Utxo] -> Either Error a
run (a -> Query b
next a
value) Transaction
transaction [Utxo]
inputs
draft :: Query Transaction
draft :: Query Transaction
draft = (Transaction -> [Utxo] -> Either Error Transaction)
-> Query Transaction
forall a. (Transaction -> [Utxo] -> Either Error a) -> Query a
Query (\Transaction
transaction [Utxo]
_ -> Transaction -> Either Error Transaction
forall a b. b -> Either a b
Right Transaction
transaction)
selectedInputs :: Query [Utxo]
selectedInputs :: Query [Utxo]
selectedInputs = (Transaction -> [Utxo] -> Either Error [Utxo]) -> Query [Utxo]
forall a. (Transaction -> [Utxo] -> Either Error a) -> Query a
Query (\Transaction
_ [Utxo]
inputs -> [Utxo] -> Either Error [Utxo]
forall a b. b -> Either a b
Right [Utxo]
inputs)
fromEither :: Either Error a -> Query a
fromEither :: forall a. Either Error a -> Query a
fromEither Either Error a
result = (Transaction -> [Utxo] -> Either Error a) -> Query a
forall a. (Transaction -> [Utxo] -> Either Error a) -> Query a
Query (\Transaction
_ [Utxo]
_ -> Either Error a
result)
run :: Query a -> Transaction -> [Utxo] -> Either Error a
run :: forall a. Query a -> Transaction -> [Utxo] -> Either Error a
run (Query Transaction -> [Utxo] -> Either Error a
query) = Transaction -> [Utxo] -> Either Error a
query
inputIndex :: Input -> Query Word64
inputIndex :: Input -> Query Word64
inputIndex Input
reference = Text -> (Transaction -> Maybe Word64) -> Query Word64
required Text
"Deferred spending input is missing" (Input -> Transaction -> Maybe Word64
Indices.findInput Input
reference)
referenceInputIndex :: Input -> Query Word64
referenceInputIndex :: Input -> Query Word64
referenceInputIndex Input
reference = Text -> (Transaction -> Maybe Word64) -> Query Word64
required Text
"Deferred reference input is missing" (Input -> Transaction -> Maybe Word64
Indices.findReferenceInput Input
reference)
mintPolicyIndex :: PolicyId -> Query Word64
mintPolicyIndex :: PolicyId -> Query Word64
mintPolicyIndex PolicyId
policy = Text -> (Transaction -> Maybe Word64) -> Query Word64
required Text
"Deferred mint policy is missing" (PolicyId -> Transaction -> Maybe Word64
Indices.findMintPolicy PolicyId
policy)
withdrawalIndex :: RewardAddress -> Query Word64
withdrawalIndex :: RewardAddress -> Query Word64
withdrawalIndex RewardAddress
account = Text -> (Transaction -> Maybe Word64) -> Query Word64
required Text
"Deferred withdrawal is missing" (RewardAddress -> Transaction -> Maybe Word64
Indices.findWithdrawal RewardAddress
account)
voteIndex :: Voter -> Query Word64
voteIndex :: Voter -> Query Word64
voteIndex Voter
voter = Text -> (Transaction -> Maybe Word64) -> Query Word64
required Text
"Deferred voter is missing" (Voter -> Transaction -> Maybe Word64
Indices.findVote Voter
voter)
proposalIndex :: Proposal -> Query Word64
proposalIndex :: Proposal -> Query Word64
proposalIndex Proposal
proposal = Text -> (Transaction -> Maybe Word64) -> Query Word64
required Text
"Deferred proposal is missing" (Proposal -> Transaction -> Maybe Word64
Indices.findProposal Proposal
proposal)
redeemerIndex :: RedeemerId -> Query Word64
redeemerIndex :: RedeemerId -> Query Word64
redeemerIndex RedeemerId
identifier = Text -> (Transaction -> Maybe Word64) -> Query Word64
required Text
"Deferred redeemer is missing" (RedeemerId -> Transaction -> Maybe Word64
Indices.findRedeemer RedeemerId
identifier)
required :: Text -> (Transaction -> Maybe Word64) -> Query Word64
required :: Text -> (Transaction -> Maybe Word64) -> Query Word64
required Text
message Transaction -> Maybe Word64
lookupIndex = do
Transaction
transaction <- Query Transaction
draft
Either Error Word64 -> Query Word64
forall a. Either Error a -> Query a
fromEither (Either Error Word64
-> (Word64 -> Either Error Word64)
-> Maybe Word64
-> Either Error Word64
forall b a. b -> (a -> b) -> Maybe a -> b
maybe (Error -> Either Error Word64
forall a b. a -> Either a b
Left (Text -> Error
InvalidBuildingRequest Text
message)) Word64 -> Either Error Word64
forall a b. b -> Either a b
Right (Transaction -> Maybe Word64
lookupIndex Transaction
transaction))
outputIndex :: Output -> Query Word64
outputIndex :: Output -> Query Word64
outputIndex Output
output = (Output -> Bool) -> Query Word64
outputIndexWhere (Output -> Output -> Bool
forall a. Eq a => a -> a -> Bool
== Output
output)
outputIndexWhere :: (Output -> Bool) -> Query Word64
outputIndexWhere :: (Output -> Bool) -> Query Word64
outputIndexWhere Output -> Bool
matches = do
Transaction
transaction <- Query Transaction
draft
let outputs :: [Output]
outputs = Fields -> [Output]
Fields.fieldOutputs (Body -> Fields
Body.bodyFields (EncodedBody -> Body
Body.toBody (Transaction -> EncodedBody
Tx.body Transaction
transaction)))
positions :: [Word64]
positions = [Word64
index | (Word64
index, Output
output) <- [Word64] -> [Output] -> [(Word64, Output)]
forall a b. [a] -> [b] -> [(a, b)]
zip [Word64
0 ..] [Output]
outputs, Output -> Bool
matches Output
output]
Either Error Word64 -> Query Word64
forall a. Either Error a -> Query a
fromEither (Either Error Word64 -> Query Word64)
-> Either Error Word64 -> Query Word64
forall a b. (a -> b) -> a -> b
$ case [Word64]
positions of
[] -> Error -> Either Error Word64
forall a b. a -> Either a b
Left (Text -> Error
InvalidBuildingRequest Text
"Deferred output is missing")
[Word64
index] -> Word64 -> Either Error Word64
forall a b. b -> Either a b
Right Word64
index
[Word64]
_ -> Error -> Either Error Word64
forall a b. a -> Either a b
Left (Text -> Error
InvalidBuildingRequest Text
"Deferred output is ambiguous")
redeemer :: (a -> Data.PlutusData) -> Query a -> DeferredRedeemer
redeemer :: forall a. (a -> PlutusData) -> Query a -> DeferredRedeemer
redeemer a -> PlutusData
encode Query a
query = Query EncodedPlutusData -> DeferredRedeemer
encodedRedeemer (PlutusData -> EncodedPlutusData
Data.fromData (PlutusData -> EncodedPlutusData)
-> (a -> PlutusData) -> a -> EncodedPlutusData
forall b c a. (b -> c) -> (a -> b) -> a -> c
. a -> PlutusData
encode (a -> EncodedPlutusData) -> Query a -> Query EncodedPlutusData
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Query a
query)
encodedRedeemer :: Query Data.EncodedPlutusData -> DeferredRedeemer
encodedRedeemer :: Query EncodedPlutusData -> DeferredRedeemer
encodedRedeemer = Query EncodedPlutusData -> DeferredRedeemer
forall a. Query a -> Transaction -> [Utxo] -> Either Error a
run