Writing Your First Plutus Smart Contract Q/A


Thank you so much for the tutorial, it is tremendously helpful.

I do have some questions, however. I’m trying to create a SC for a credit-card like payment system in order to learn Plutus. I’m trying my best to make everything rely on off-chain code as little as possible.

In this system there would be 3 actors: the buyer, the merchant and the investor. Investors would pay to the SC with the intent of collecting more ADA than they put in later. Merchants will collect from the SC the amount they sold minus fees and buyers would be able to pay for their purchases some time (say, 30 days) after buying without interest, or later on with interest.

I have a few problems while trying to develop this system, however:

  • When collecting, both collectFromScript and _collectFromScriptTxn_will not do: Transaction matching needs to find utxo with value smaller than the desired collection value for merchants (to collect many of them until reaching the expected value), and the value for investors will depend on other of the system’s attributes such as the default rate and the time since the investment happened. Also, the investors’ utxo may be used to pay a merchant.
  • Even if utxo matching is possible with off-chain code (this seems to be done by collectFromScript at https://github.com/input-output-hk/plutus/blob/master/wallet-api/src/Wallet/API.hs), I’m afraid too many investors and/or merchants will try to compete for collection of the same utxo. It would be superb if there could be a way to allow the smart contract itself to decide which utxo to collect from (maybe your collectFromScriptMatching suggestion running on-chain?).
  • Is it possible to collect some utxo partially?

Sorry for such a long post and so many questions, and sorry if this has been answered in the tutorial or if it’s somehow obvious; it takes some time to digest all this new stuff! Also, I believe many of these problems I have are common to investment funds in general, since fees and compensations are complex.

Thank you for the awesome work,

1 Like

Always a pleasure to hear that someone enjoyed the lesson.

This is quite the complicated system to start learning with, but hey, ambition is often a good thing. I’d like to emphasize that in production smart contracts you’d typically want to make as much functionality off-chain as possible in order to reduce costs, but it also depends on what you are going for.

In regards to your problems:

  • Indeed, neither of them will work particularly for your use case.
  • That is true, there could end up being races to optimize collecting the best UTXO’s. Stopping this by writing logic which only allows withdrawals of UTXO’s in a more egalitarian way theoretically should be possible in the SC, but the logic to do so may be a bit complicated. Either way, you need to define a UTXO collection strategy whether it is off-chain or on-chain.
  • Not currently, but it will need to be supported at some point or else nothing is going to be realistically doable with Plutus in the vast majority of cases.

Frankly Plutus is still in quite an infantile state, and contracts of this complexity are a little out of scope since they are still polishing off even the more basic features like supporting collecting in all of it’s different variants/possible UTXO selection strategies. I’d recommend keeping this idea and waiting until they come out with their first official version numbered release, which at that point will likely have at least a reasonable stable base to work from.

Nonetheless, I thoroughly encourage you to try to implement some simpler ideas and share a smart contract or two here on the forum. It’d be great to get someone else who’s also creating new examples to show off to the community and continue to get the Plutus community rolling :ok_hand:

1 Like

Some clarification needed.

What does it mean to “publish” a contract? If I already know the address of a contract(a hash string), which contract creator emailed to me, can I payToScript on this address?
If no(so that this address should be known to the blockchain, before I can start receive funds), how this is reflrected in the blockchain(it is a transaction, right? but from whom to whom?)?
I confused a bit at this point…

1 Like

Indeed Andrew, if you know the address of a contract you can indeed send a payToScript Tx to it and thereby interact with it. The Plutus Playground/mockchain is just an emulator so you don’t have any addresses to acquire/send to others, but in the future with the testnet/mainnet you will be able to.


Get it! Tnx a lot.
BTW, I don’t see any reason for Playground not to support this. More accurate emulation, could avoid such confusion moments for learners IMHO…


Indeed, I believe they will eventually, but it is in very early stages of development and has a lot of room to improve.


Hello, bobert.
I’m not sure where to ask, so this thread may be the most appropriate place…

State and UTXO question.

Look, Plutus devs issued State Machine smart contract recently, but trying to understand it I can’t see that it actually works as true State Machine(FSM). In FSM new state is a function of some input and old state. But in example new state(new guess) is not based on the old one. The only access to utxo’s DataScript(old state) we have is in the ValisatorScript, so we can compare old one and new one - validate it, but not produce new state(please, correct me if I’m wrong). So, is it possible in theroy, to read from utxo DataScript, to produce new state, based on it?