Error when sending an NFT using cardano-cli

Hello community.

I’ve been trying for a few days now to send a transaction with cardano-cli, but no luck.
The addresses below are not accurate on purpose as I didn’t want to share the real values.

Here are the commands I’m trying.

To get the protocol parameters:

cardano-cli query protocol-parameters
–out-file protocol.json

To find the current slot

currentSlot=$(cardano-cli query tip --mainnet | jq -r ‘.slot’)
echo Current Slot: $currentSlot

To find UTXOs

cardano-cli query utxo
–address $(cat payment.addr)

To insert the destination address:

echo destinationAddress: $destinationAddress

To find my balance:

cardano-cli query utxo
–address $(cat payment.addr)
–mainnet > fullUtxo.out
tail -n +3 fullUtxo.out | sort -k3 -nr > balance.out
cat balance.out
while read -r utxo; do
in_addr=$(awk ‘{ print $1 }’ <<< “${utxo}”)
idx=$(awk ‘{ print $2 }’ <<< “${utxo}”)
utxo_balance=$(awk ‘{ print $3 }’ <<< “${utxo}”)
echo TxHash: ${in_addr}#${idx}
echo ADA: ${utxo_balance}
tx_in="${tx_in} --tx-in ${in_addr}#${idx}"
done < balance.out
txcnt=$(cat balance.out | wc -l)
echo Total ADA balance: ${total_balance}
echo Number of UTXOs: ${txcnt}

Drafting the transaction:

cardano-cli transaction build-raw
–tx-in 08ecf55363fac239cbcaa50cba6cd66bf06a18bc9178190c60d02c5d203b3012#0
–tx-out $(cat payment.addr)+0
–tx-out ${destinationAddress}+0
–invalid-hereafter 0
–fee 0
–out-file tx.draft

Calculating the fee:

cardano-cli transaction calculate-min-fee
–tx-body-file tx.draft
–tx-in-count 1
–tx-out-count 2
–witness-count 1
–byron-witness-count 0
–protocol-params-file protocol.json

expr 116460275 - 2000000 - 176589 = 114283686

echo Change Output: ${txOut}

Building the transaction:

cardano-cli transaction build-raw
–tx-in 08ecf55363fac239cbcaa50cba6cd66bf06a18bc9178190c60d02c5d203b3012#0
–tx-out $(cat payment.addr)+${txOut}
–tx-out ${destinationAddress}+2000000
–invalid-hereafter $(( ${currentSlot} + 10000))
–fee 176589
–out-file tx.raw

Signing the transaction:

cardano-cli transaction sign
–tx-body-file tx.raw
–signing-key-file payment.skey
–out-file tx.signed

Submit the transaction:

cardano-cli transaction submit
–tx-file tx.signed

In the end I’m getting this error:
Command failed: transaction submit Error: Error while submitting tx: ShelleyTxValidationError ShelleyBasedEraAlonzo (ApplyTxError [UtxowFailure (WrappedShelleyEraFailure (UtxoFailure (ValueNotConservedUTxO (Value 1344798 (fromList [(PolicyID {policyID = ScriptHash “5a297aeb79ba4281a153b43f4ff0ed25bc88b7ddc8ba5261b01baec2”},fromList [(“REDACTED”,6)])])) (Value 116460275 (fromList [])))))])

Can you guys tell me if I’m missing something? I’m really going crazy with this.
Any help is appreciated.

Two observations:

  • For getting the ${total_balance} you query all UTxOs on your address, but in the build-raw commands you only use a single UTxO for --tx-in. That might well explain the discrepancy between the 1344798 Lovelace in the ValueNotConservedUTxO error reported to you and the 116460275 Lovelace that you put into --tx-outs and --fee.
  • There seem to be other tokens than ADA in your inputs if I’m not mistaken. You have to put them in outputs also. The guide you seem to follow doesn’t provide for that. You probably have to do it manually.
1 Like

Hey @HeptaSean. Thank you so much for taking the time to reply.

This is what I get when I query the utxos:

TxHash TxIx Amount
08ecf55363fac239cbcaa50cba6cd66bf06a18bc9178190c60d02c5d203b3014 0 1344798 lovelace + 5 5a297aeb79ba4281a153b43f4ff0ed25bc88b7ddc8ba5261b01baec4.5552474f
3ff7497dc2aee28345a3165429d3efc73110decead626cceee9c71c7b31c1448 0 1344798 lovelace + 6 5a297aeb79ba4281a153b43f4ff0ed25bc88b7ddc8ba5261b01baec4.5552474f + TxOutDatumNone
5b96d592fae085a56ef4f201526377560ef18ee2618bfe7f93a83c2b1f0da2de 0 5000000 lovelace + TxOutDatumNone
6aa89bc91a449185496013e32c1cac9d2faf3dd15ed5604336aec9f7428bccef 0 1344798 lovelace + 6 5a297aeb79ba4281a153b43f4ff0ed25bc88b7ddc8ba5261b01baec4.5552474f + TxOutDatumNone
72ae295ed395c407f542991e0d9cf2ef1dcf8fbb573022f74cf80df518c1b948 0 107425881 lovelace + TxOutDatumNone

Based on my understanding, my balance should be 116,460,275, which is my txout. Do I need to put all my utxos in the tx-in or just the one that refers to the NFT I’m trying to send?
Regarding your last point, I really don’t know how I can put the NFT as outputs as you’re suggesting. Any help is appreciated. Thank you so much!!

1 Like

First, I have to tell you that from the UTxOs, we can navigate to the address and the rest of your wallet.
(Because you wrote


You need to put everything that should go out also into the transaction.

I suppose you got that code collecting all UTxOs from: Submitting a Simple Transaction - CoinCashew

They do a very simplified thing there, just always using all UTxOs as inputs. That’s often not what you want. For example, that 5 ADA output you have in there looks like it’s set aside for collateral. If you consume it by a transaction, it won’t be available for collateral, anymore.

However, the UTxOs with your tokens do not have enough ADA to have the minimum UTxO for sending them away and also paying the transaction fee. So, you need to put in at least a second UTxO with enough ADA.

Also, there is cardano-cli transaction build, which is easier to use than build-raw, doesn’t need the drafting step, and calculates the fee for you.

What I would do:

$ cardano-cli transaction build --mainnet \
--tx-in 72ae295ed395c407f542991e0d9cf2ef1dcf8fbb573022f74cf80df518c1b948#0 \
--tx-in 6aa89bc91a449185496013e32c1cac9d2faf3dd15ed5604336aec9f7428bccef#0 \
--tx-out "${destinationAddress}+2000000+1 5a297aeb79ba4281a153b43f4ff0ed25bc88b7ddc8ba5261b01baec4.5552474f" \
--tx-out "$(cat payment.addr)+1344798+5 5a297aeb79ba4281a153b43f4ff0ed25bc88b7ddc8ba5261b01baec4.5552474f" \
--change-address $(cat payment.addr) \
--out-file tx.raw
$ cardano-cli transaction sign --mainnet \
--tx-body-file tx.raw \
--signing-key-file payment.skey \
--out-file tx.signed
$ cardano-cli transaction submit --mainnet \
--tx-file tx.signed

The first --tx-in is to have enough ADA available, the second is to have 6 of your tokens available. (It doesn’t seem to be an NFT. NFTs only have 1 token minted, not more.)

The first --tx-out sends one of the tokens away together with 2 ADA, the second --tx-out sends the remaining 5 tokens back to your own address together with the minimal ADA accompanying them.

The --change-address will create a third output automatically with all the remaining ADA after the automatically calculated fee.

1 Like

Right!!! They are tokens!

Perfect @HeptaSean. You’re helping me A LOT. I was now able to send one token away, leaving me with more 16 in my wallet. If I wanted to send the remaining 16 tokens, can you give me an example transaction build command?

I would like to give something back to you for your help. Please send me your ADA address

The example is more or less the transaction you just did. It is always the same pattern:

  1. You do cardano-cli query utxo to see the UTxOs currently at your payment address.
  2. You choose several of them as --tx-ins for cardano-cli transaction build. You will want to have one with a larger amount of ADA in there (it is now a different one, because you moved that ADA with the previous transaction), because the ADA that you sent out to the destination address(es) and the fee has to come from somewhere. And you will want to have one or several with the tokens that you want to send out.
  3. You can send to several destination addresses in one transaction and you can, of course, also send more than one token to a destination address. Only important thing is that there are as many tokens in all the --tx-outs together as there are in all the --tx-ins together. If some are left over send them back to your payment address as you did in the transaction above. (You don’t have to put the destination addresses in shell variables like ${destinationAddress}. You can just give the addresses directly to cardano-cli like --tx-out "addr1…+2000000+1 51297…".)
  4. cardano-cli transaction sign and cardano-cli transaction submit stay exactly the same.
1 Like

Hi @joaoamaral,
I’m not sure I have correctly separated the data in the table? My apologies, if I have made any mistakes, the intention was to make the table more readable ; )

1 Like

Thank you @HeptaSean. With your help I was finally able to send all tokens away

1 Like