2

I have defined two endpoints. One for creating a UTxO at the script address with a Datum that contains the wallet's own PaymentPubKeyHash. And a second endpoint that spends such UTxOs from the script address. The validator only checks for equality between the two PaymentPubKeyHashes.

First endpoint:

one :: AsContractError e => Params -> Contract w s e ()
one Params{..} = do
    pkh <- ownPaymentPubKeyHash
    let dat = Stake PPDatum { owner = pkh }
        tx  = Constraints.mustPayToTheScript dat $ Ada.lovelaceValueOf amount
    ledgerTx <- submitTxConstraints validatorInstance tx
    void $ awaitTxConfirmed $ getCardanoTxId ledgerTx

Second endpoint:

second :: forall w s e. AsContractError e => Contract w s e ()
second = do 
    pkh   <- ownPaymentPubKeyHash
    utxos <- Map.filter (isSuitable pkh) <$> utxosAt validatorAddress
    ...
    where
        isSuitable :: PaymentPubKeyHash -> ChainIndexTxOut -> Bool
        isSuitable pkh o = case _ciTxOutDatum o of
            Left _          -> False
            Right (Datum e) -> case PlutusTx.fromBuiltinData e of
                Nothing -> False
                Just d  -> owner d == pkh

But the equality fails between the PaymentPubKeyHashes.

After testing around I recognized that the type of ownPaymentPubKeyHash does not just return a PaymentPubKeyHash but a Contract w s e PaymentPubKeyHash.

So how can I create the datum so that the PaymentPubKeyHashes match?

UPDATE

I was able to resolve my issue. It has nothing to do with the usage of ownPaymentPubKeyHash. As already correctly stated by others in this thread binding with the help of <- ownPaymentPubKeyHash returns the type PaymentPubKeyHash.

The problem originated from unwrapping the datum incorrectly. And this was caused due to allowing two different types of datum at the script address using the pipe operator. Therefore, I needed to unwrap the datum twice.

My solution looks something like this now:

data ParentDatum = One ADatum | Two BDatum

-- Helper function
getParentDatum :: ChainIndexTxOut -> ParentDatum
getLotteryDatum o = case _ciTxOutDatum o of
    Left _ -> traceError "unexpected datum"
    Right (Datum d) -> do
        let parsedDatum = PlutusTx.fromBuiltinData d :: Maybe ParentDatum
        case parsedDatum of
            Just s -> s
            _ -> traceError "Unknown datum type"



-- inside the off-chain endpoint code
-- {...} 
    where
        isSuitable :: PaymentPubKeyHash -> ChainIndexTxOut -> Bool
        isSuitable pkh o = case getParentDatum o of
            One ADatum{..} -> owner == pkh
            Two BDatum{..} -> owner == pkh
            _              -> traceError "Unhandled datum type"

1 Answer 1

3

You are using the operator <- to bind the result to pkh. That means that pkh is of type PaymentPubKeyHash. If the equality fails then because of another reason. If pkh wasn’t of the right type you would get a compilation error.

Maybe the way you are retrieving the datum is not correct. I would have written the following function assuming that your datum is of type Stake:

 stake :: ChainIndexTxOut -> Maybe Stake
 stake (ScriptChainIndexTxOut _ _ (Right dd) _) = do
     Datum d <- Just dd
     PlutusTx.fromBuiltinData d
 stake _ = Nothing
5
  • Thanks, that makes sense, but what else makes it fail to retrieve the funds? I've have tested multiple things, like just returning True for the isSuitable function, which works as expected for retrieving any funds from the contract address. So the validator works and it must be part of the isSuitable function Jan 30 at 21:07
  • 1
    Maybe it is the way you retrieve the datum. I have edited my answer to show how I would have done it.
    – Jey
    Jan 30 at 21:57
  • 1
    Thanks @Jey I resolved the issue - you were right, the problem did originate from unwrapping the datum incorrectly. Jan 31 at 15:22
  • Cool, I'm happy that my answer was useful.
    – Jey
    Jan 31 at 17:55
  • If my answer is correct you can accept it as the solution ; )
    – Jey
    Feb 4 at 16:12

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.