I am trying to make a Plutus smart contract which takes tokens locked to the contract and then on endpoint call (purchase) sends 1 to the caller and then the rest back to the contract. I cant find a distribution model for this I would assume you would have to create a new utxo out of the existing utxo and reduce it down into a single token on the script but I am very lost with this process.
I need help figuring this out so that I can create this send 1 token out of many script. A specific answer is what I'm looking for.
This is the code relevant to the problem, but it currently does not work.
purchaseUtxo :: ChainIndexTxOut
purchaseUtxo = case fromTxOut (scriptTxOut (Value.singleton (policyID contractInfo) (nameOfToken contractInfo) (1)) validate (Datum $ PlutusTx.toBuiltinData ()) ) of
Nothing -> error ()
Just x -> x
purchase :: AsContractError e => PurchaseParams -> Contract w s e ()
purchase p= do
utxos <- fundsAtAddressGeq valAddress (Ada.lovelaceValueOf 1)
let utxoMap = Map.singleton (TxOutRef (_citxTxId ChainIndexTx) 1) purchaseUtxo
redeemer = ()
pkh = ownPubKey
tx = Constraints.mustPayToTheScript () price <> collectFromScript utxoMap redeemer
void (submitTxConstraintsSpending lootBox utxos tx)
And here is the error:
Compilation Error, Line 148, Column 54 (jump)
error:
• Couldn't match expected type ‘ChainIndexTx’
with actual type ‘TxId
-> Data.Set.Internal.Set TxIn
-> ChainIndexTxOutputs
-> SlotRange
-> Map DatumHash Datum
-> Map RedeemerHash Redeemer
-> Map ScriptHash Script
-> Maybe SomeCardanoApiTx
-> ChainIndexTx’
• Probable cause: ‘ChainIndexTx’ is applied to too few arguments
In the first argument of ‘_citxTxId’, namely ‘ChainIndexTx’
In the first argument of ‘TxOutRef’, namely
‘(_citxTxId ChainIndexTx)’
In the first argument of ‘Map.singleton’, namely
‘(TxOutRef (_citxTxId ChainIndexTx) 1)’
| 148 | let utxoMap = Map.singleton (TxOutRef (_citxTxId ChainIndexTx) 1) purchaseUtxo
| ^^^^^^^^^^^^