1

Suppose we want a smart contract (validator script) that locks a user' fund (e.g. ADA) that she deposits and releases it back if she withdraws it. Suppose when she deposits some fund into the validator script, we want in the same transaction the validator script sends her wallet some native tokens as a proof of deposit.

In this scenario, how can the validator script mints some native tokens in the same transaction as a user sends some fund to the validator script?

1 Answer 1

1

You need a separate minting policy to mint the tokens.

we want a smart contract (validator script)

I tend to think of smart contracts as including a set of validators and policies, not just one script. It's typical to use multiple in concert.

Essentially the validator holds an NFT with a datum tracking the contract's state. The validator constrains any transaction spending the datum, and checks that the transaction

  1. deposits/withdraws the correct amount,
  2. mints/burns the correct amount, and
  3. replaces the NFT + datum with updated state

The the minting policy will only mint if the NFT is spent--all the logic is on the validator side.

Check out the discussion on "dummy nfts"/"access tokens" here to get a better idea of how that would work.

3
  • Great explanation, I am a little confused why you'd put all the logic in the validator. In the case of depositing money at the contract address, I'd expect the minting policy to run as well + make sure a corresponding output with ADA is created at the script address or did I misunderstand? I don't see how else the validator script would run if there is no output spent in the case of a depositing action.
    – Will
    Commented Apr 6, 2022 at 15:31
  • Hence, I'd almost say the minting policy could do most of the logic except for validating withdraw ownership based on simple pubkeyHash comparison. But it could certainly ensure deposits to be correct (output at script address with equal amount to minted native token). And also burns which check if a corresponding withdraw is done too... Please let me know if I misunderstand the context.
    – Will
    Commented Apr 6, 2022 at 15:34
  • "make sure a corresponding output with ADA is created at the script address"--that's what I'm saying with "replaces the NFT + datum." Datums must exist on an output. If the validator is already checking that, it would be redundant to have the policy check it again. You could definitely have the minting policy check other things, but remember policies don't have any state, just tx context. There is no datum on the policy. So, if you ever want minting to rely on some state, you need need some validator to manage that state for you. I think it's best to keep all the logic in one spot. Commented Apr 6, 2022 at 17:07

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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