cardano-serialization-lib icon indicating copy to clipboard operation
cardano-serialization-lib copied to clipboard

Updating outputs after adding them to the transaction

Open lamadeni opened this issue 3 years ago • 0 comments

For example, if I have this structure:

Inputs

  • Address 1 - 100 ADA
  • Address 2 - 50 ADA

Expected outputs

  • Address 3 - 120 ADA
  • Address 4 - 30 ADA

And say I want the fee to be charged from the Address 4 output (So instead of it getting 30 ADA it would get 30 - fee ADA).

Is there a way to construct the transaction with the expected outputs and then subtract the fee from them?

The way I'm handling this currently is:

  1. add all inputs/outputs except Address 4
  2. get transaction fee with min_fee
  3. get additional fees for the Address 4 output with fee_for_output
  4. calculate total fee (2 + 3) with checked_add
  5. subtract the total fee from Address 4 output
  6. add Address 4 output

This process works but it's a bit dirty and it complicates dynamically constructing the transaction (every time I add any input or output I'd have to redo all the steps since I'm constructing the transaction from a dynamic JSON object and I would have to ensure that the address that would pay the fee is always added last). I would prefer to just add all inputs and outputs from the start and then just subtract the fee at the end if it's possible.

I know that the add_change_if_needed would maybe simplify this if I just add all the utxos that don't have to pay the fee and just send the change to the one that does, but still, if I were to add a utxo after that I would have to go through the whole tx building process again to get the fee.

lamadeni avatar Apr 27 '22 12:04 lamadeni