Writing Your First Plutus Smart Contract Q/A

Ah, it’s great to see an Android dev. get into Plutus :+1: Those are two solid questions.

#1. watchSmartContract is needed so the offchain wallet keeps track of the smart contract. This, from my understanding, is primarily needed for the MockChain emulator to know which address to keep track state of as these updates would be broadcast on the blockchain and available for all to see on a real live chain. However in more complex dApps (check out my Jellybean example as one) you also program the off-chain wallet to specifically watch the smart contract address and then perform automated calls when certain parameters are met, which in that case may require something like a watchSmartContract call prior in order to ensure it triggers in an appropriate time-frame.

#2. This is indeed something that was interesting for me too at first. Given that we are writing contracts in a UTXO based blockchain this is something that many people will end up hitting as a point of confusion.

The collectFromScript call goes through all coins sent to the SC and evaluates the ValidatorScript with the DataScript from each UTXO and the RedeemerScript you provided. The call expects to withdraw all coins form the contract, so if any coins fail to validate, as in your example, then the withdraw fails.

Currently the Plutus team has also implemented a collectFromScriptTxn function which attempts to collect a specific transaction, which could be used to make your example work. I assume it is in the plans (if not I’ll end up writing the function myself and submitting it as a PR) to create a function called something like collectFromScriptMatching which ends up collecting only whichever locked UTXOs which have DataScripts that end up validating, and ignore the rest that cause errors.

Furthermore I expect a collect function which will be available to withdraw specific ada amounts, however this will have to also use some sort of UTXO selection algorithm (or offer several to choose from) which makes matters more complicated. As Plutus is still in a very early stage I think this will simply take time for them to put it all together but as you can see on the Github page they are committing daily so it shall all come in due time.

Hope that cleared up your questions.

7 Likes