spacebudz / lucid

Lucid is a library, which allows you to create Cardano transactions and off-chain code for your Plutus contracts in JavaScript, Deno and Node.js.
https://lucid.spacebudz.io
MIT License
336 stars 133 forks source link

Merging change utxos in minting transaction #239

Open khiemn159 opened 7 months ago

khiemn159 commented 7 months ago

Hello, I'm relatively new to Cardano and Lucid. I am trying to mint a token like this, and my goal is to produce only 1 utxo that will also contain the token:

const txn = await params.lucid.newTx()
    .attachMintingPolicy({
      type: "PlutusV2", script: policyScript
    })
    .mintAssets({
      [`${token}`]: 1n,
    }, Data.to(12n))
    .complete();

const signing = await txn.sign().complete();
const hash = await signing.submit();
console.log("hash: ", hash);

And the result get splitted into 2 utxos: image

I have tried other method such as:

const txn = await params.lucid.newTx()
    .attachMintingPolicy({
      type: "PlutusV2", script: policyScript
    })
    .mintAssets({
      [`${token}`]: 1n,
    }, Data.to(12n))
    .payToAddress(await params.lucid.wallet.address(), {
      [`${token}`]: 1n,
    })
    .complete();

const signing = await txn.sign().complete();
const hash = await signing.submit();
console.log("hash: ", hash);

but it still produces 2 utxos. What can I do to make it produce 1 utxo only? Thanks in advance!

cmorgado commented 6 months ago

I don't believe it is possible, if you look carefully, it's not a bug but how the eUTxO and cardano works. You need to send back to the wallet the ada it had minus what it has spent from the input, thus the utxo with the minted value. I've seen this pattern many times.