-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add singular utxo indexer one to many
- Loading branch information
Showing
4 changed files
with
74 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
module Plutarch.SingularUTxOIndexerOneToMany ( | ||
spend, | ||
SpendRedeemer (..), | ||
PSpendRedeemer (..), | ||
) where | ||
|
||
import Plutarch.Api.V2 ( | ||
PScriptPurpose (..), | ||
PTxOut, | ||
PValidator, | ||
) | ||
import Plutarch.Builtin (pasInt) | ||
import Plutarch.DataRepr (PDataFields) | ||
import Plutarch.Prelude | ||
import Plutarch.Unsafe (punsafeCoerce) | ||
import PlutusTx (BuiltinData) | ||
import "liqwid-plutarch-extra" Plutarch.Extra.TermCont ( | ||
pletC, | ||
pletFieldsC, | ||
pmatchC, | ||
) | ||
|
||
data SpendRedeemer = SpendRedeemer | ||
{ inIx :: BuiltinData | ||
, outIxs :: [BuiltinData] | ||
} | ||
deriving stock (Generic, Eq, Show) | ||
|
||
newtype PSpendRedeemer (s :: S) | ||
= PSpendRedeemer (Term s (PDataRecord '["inIx" ':= PData, "outIxs" ':= PBuiltinList PData])) | ||
deriving stock (Generic) | ||
deriving anyclass (PlutusType, PIsData, PDataFields, PShow) | ||
|
||
instance DerivePlutusType PSpendRedeemer where type DPTStrat _ = PlutusTypeData | ||
instance PTryFrom PData PSpendRedeemer | ||
|
||
spend :: Term s (PTxOut :--> PBool) | ||
-> Term s (PTxOut :--> PTxOut :--> PBool) | ||
-> Term s (PBuiltinList PTxOut :--> PInteger :--> PBool) | ||
-> Term s PValidator | ||
spend inputValidator inputOutputValidator collectiveOutputValidator = | ||
plam $ \_datum redeemer ctx -> unTermCont $ do | ||
let red = punsafeCoerce @_ @_ @PSpendRedeemer redeemer | ||
redF <- pletFieldsC @'["indx", "outIxs"] red | ||
ctxF <- pletFieldsC @'["txInfo", "purpose"] ctx | ||
PSpending ownRef' <- pmatchC ctxF.purpose | ||
ownRef <- pletC $ pfield @"_0" # ownRef' | ||
txInfoF <- pletFieldsC @'["inputs", "outputs"] ctxF.txInfo | ||
let inIx = pasInt # redF.inIx | ||
outIxs = pmap # pasInt # redF.outIxs | ||
(_, outTxOuts, outputCount) = | ||
pfoldr # | ||
plan (\(curIdx (prevIdx, acc, count) -> pif | ||
(curIdx #< prevIdx) | ||
(curIdx, pconcat # acc # psingleton (pelemAt @PBuiltinList # curIdx # txInfoF.outputs), count + 1)) | ||
perror | ||
) # | ||
(P.length # outputs, [], 0) # | ||
outIxs | ||
|
||
outOutput = pelemAt @PBuiltinList # outIdx # txInfoF.outputs | ||
inInputF <- pletFieldsC @'["outRef", "resolved"] (pelemAt @PBuiltinList # inIx # txInfoF.inputs) | ||
return $ | ||
popaque $ | ||
pif | ||
(ptraceIfFalse "Indicated input must match the spending one" (ownRef #== inInputF.outRef)) | ||
(inputOutputValidator # inInputF.resolved # outOutput) | ||
perror |