Create a transaction to send only a small amount of available lovelace - cardano-cli


I’m trying to use cardano-cli to send transactions and it’s working pretty well, but I’m facing a trouble.

If I send like 3 ADA to the cardano-cli receipient address (to send the amount to other addresses later) using this command: cardano-cli query utxo --address $(cat payment1.addr) --mainnet the output is:

                       TxHash                                 TxIx        Amount

e25caa3b5634b42073cb2392460ee9911286492e103abc3b6b305585e6bc056f 0 3000000 lovelace + TxOutDatumHashNone

(Let’s say that the transaction fee is: 170869
If I try to create a transaction to send all of the available amount it works, like this:
cardano-cli transaction build-raw --tx-in e25caa3b5634b42073cb2392460ee9911286492e103abc3b6b305585e6bc056f#0 --tx-out addr1q98gt90xur6v6r75p9tuu6rdgyqfxhyz2ge5380ylqmxfke2c5ywt37a674gltrah4etwzq07xj4eh85fv3f098g7vkq9p6vz8+2829131 --fee 170869 --out-file tx.raw

And in this case my cardano-cli address would be “empty” cause all the available lovelaces have been sent. But I would like to send a amount that is lower than the available amount in the address/TxHash

Like, instead of sending 2829131 I’d like to send exactly 2500000 (and must remain 329131 lovelaces), so the --tx-out address would receive EXACTLY 2500000 lovelaces (or 2.5A) instead of 2829131 (or 2.82A).

If I try to do something like this:
cardano-cli transaction build-raw --tx-in e25caa3b5634b42073cb2392460ee9911286492e103abc3b6b305585e6bc056f#0 --tx-out addr1q98gt90xur6v6r75p9tuu6rdgyqfxhyz2ge5380ylqmxfke2c5ywt37a674gltrah4etwzq07xj4eh85fv3f098g7vkq9p6vz8+2500000 --fee 170869 --out-file tx.raw
The console output is

Command failed: transaction submit Error: Error while submitting tx: ShelleyTxValidationError ShelleyBasedEraAlonzo (ApplyTxError [UtxowFailure (WrappedShelleyEraFailure (UtxoFailure (ValueNotConservedUTxO (Value 3000000 (fromList )) (Value 2500000 (fromList )))))])

Can someone guide me please? Resuming it, I want to send a ADA/lovelace amount that is lower than the available in the cardano-cli address/TxHash

With cardano-cli transaction build-raw the sum of all lovelace must be 0.

cardano-cli transaction build-raw 
  --tx-in txid#idx (you can have N of those which make up the sum of all input lovelace)
  --tx-out addr+amount (you can have N of those which make up the sum of all output lovelace - fees)
  --tx-out refundAddr+refundAmount (you cannot partially spend an input, this is the refund to yourself)
  --fee feeAmount (your calculated fee)

This approach is low level and quite inconvenient. Instead have a look at cardano-cli transaction build - it balances the Tx automatically (i.e. calculates the fee and the refund)

cardano-cli transaction build
  --tx-in txid#idx
  --tx-out addr+amount
  --change-address refundAddr

Hi there and welcome!

Good to hear you managed to create your other transaction! About your problem, in a UTXO model we want to preserve the total amount of value. That is, the total supply can not increase nor decrease! So in every transaction all funds must be accounted for. In your last construction of a transaction the error raised is “ValueNotConservedUTxO”, hinting that the total amount of input (the utxo of 3 ada that ends with …6bc056f) is not the same as the output that you are trying to make.

How to overcome this and still construct such a output of 2.5 ada at the desired address. There are 2 ways I can think of. The first is to also create a change UTXO that will end up at the address of the initial UTXO. But there is a problem. In your situation the total amount of change (0.329131 ada) is smaller than the minimum amount of value an UTXO must contain. In the current ledger rules there are some parameters that say how dividable UTXO’s are (in the abstract sense of “what are the atoms” that we can make). Currently that is around 1 ada. So it is currently not allowed to create an UTXO with less value than 1 ada. This can also be fixed, what you can do is consume an additional UTXO that will raise the total amount of flowing ada in the transaction to circumvent this minimum UTXO value problem.

The second more straight forward way of solving this inbalance of value between input and output is to raise the transaction fee! The calculated fee is a minimum fee, higher is allowed and will tip all people that are staking ada. You could thus send 2.5 ada and pay a transaction fee of 0.5 ada.

Hope this helps!