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

unexpected behavior of `fee_for_output`

Open RaaCT0R opened this issue 8 months ago • 4 comments

How can we estimate the fee changes on adding an output to a transaction?

Previously, I used the TransactionBuilder.fee_for_output function, but seems it's not working after v13?

You can find full script here: https://github.com/rosen-bridge/ui/blob/6eadd8a38b70a1e2204b7050c5084f2a6750cce0/networks/cardano/src/generateUnsignedTx.ts#L123

I tested the exact script with CSL v12, v13 and v14. Only in v12, the function worked correctly and in other versions, it returned 0.

RaaCT0R avatar Apr 05 '25 17:04 RaaCT0R

Hi @RaaCT0R! We will investigate it, but if you want to save your time I recommend to use function TransactionBuilder.add_change_if_needed(), it will help to avoid some corner cases that is not covered in your function. Here is example how these code lines can be replaced:

    // set temp fee and auxiliary data
    //txBuilder.set_fee(txBuilder.min_fee()); - remove manual fee setup.
    txBuilder.set_auxiliary_data(auxiliaryData);

    const cslChangeAddress = wasm.Address.from_bech32(changeAddress) 

    // set tx fee and change 
    txBuilder.add_change_if_needed(cslChangeAddress);

    // build transaction
    const tx = txBuilder.build_tx();  // return tx, and do additional checks to catch issues on early stage before tx submission 

    return tx.to_hex();

lisicky avatar Apr 05 '25 23:04 lisicky

@lisicky Thanks for the response.

I had issues with this function before. That's why I used the fee_for_output and generated the change box manually, though I don't recall the exact reason.

Currently, I face this error while using add_change_if_needed:

{
  "name": "NonError",
  "message": "\"Not enough ADA leftover to include non-ADA assets in a change address\"",
  "stack": "NonError: \"Not enough ADA leftover to include non-ADA assets in a change address\"\n    at deserializeError (webpack-internal:///(app-pages-browser)/./node_modules/serialize-error/index.js:200:9)\n    at Object.deserializeError (webpack-internal:///(app-pages-browser)/./app/_safeServerAction/safeServerAction.ts:13:81)\n    at Object.eval [as generateUnsignedTx] (webpack-internal:///(app-pages-browser)/./app/_safeServerAction/safeServerAction.ts:83:38)\n    at async EtrnlWallet.transfer (webpack-internal:///(app-pages-browser)/../../wallets/eternl/dist/wallet.js:59:31)\n    at async startTransaction (webpack-internal:///(app-pages-browser)/./app/_hooks/useTransaction.ts:47:26)"
}

I'm sure there is enough ADA in the inputs. For this scenario, I have 65_116_387n LOVELACE in the added inputs (2 utxos) and added only 1 output with 1_176_630n LOVELACE. There are only two assets leftover.

RaaCT0R avatar Apr 07 '25 01:04 RaaCT0R

@RaaCT0R hm it very strange, could you provide a code snippet to reproduce issue on our side ?

lisicky avatar Apr 07 '25 11:04 lisicky

@lisicky Seems add_change_if_needed is okay. There was a bug on my side while adding the inputs to the builder.

I fixed it temporary, but Please let me know the progress on fee_for_output. I can provide the script to reproduce it's behavior, if that helps.

RaaCT0R avatar Apr 08 '25 14:04 RaaCT0R