cashscript icon indicating copy to clipboard operation
cashscript copied to clipboard

Investigate better utilities for fee management/estimation

Open mr-zwets opened this issue 1 year ago • 1 comments

@mainnet-pat mentioned an issue on telegram (https://t.me/bch_compilers/8024)

hm it is not trivial in context of multicontract spend to figure out the tx size it is like for each contract spend in context of builder I need to build simple contract spend to figure out the input size

currently for fee management you'd have to get the current transaction size this way:

const rawTxHex = transactionBuilder.build()
const txLengthBytes = rawTxHex.length / 2

we might want to create fee estimation utilities but we'd need to think what they would look like

bch-js has this

let byteCount = bchjs.BitcoinCash.getByteCount({ P2PKH: 1 }, { P2PKH: 1 });

this would obviously be too simple for cashscript but maybe the idea to make it easy to check bytecount for different tx shapes makes sense.

maybe something like

const currentTxSize = transactionBuilder.getCurrentByteSize()
const changeOutputSize = calculateOutputSizes([
  { type: "P2pkhWithTokens", tokenType: "fungible" }
])
const txSizeWithChange = transactionBuilder.getCurrentByteSize() + changeOutputSize

mr-zwets avatar Jan 15 '25 09:01 mr-zwets

we could create something like

transaction.addChangeOutput(address, minerFee)

or even better, without making changes to the TransactionBuilder API:

const changeAmount= calculateBchChange(transaction, minerFee)
if(changeAmount > 1000) transaction.addOutput(to: address, amount: changeAmount)

mr-zwets avatar Feb 18 '25 16:02 mr-zwets