Skip to content

Commit

Permalink
fixes problem with SEND ALL other assets (#522)
Browse files Browse the repository at this point in the history
- the problem was fees to low
- fee estimation was made with only asset inputs and outputs
- afterwards, when adding inputs and outputs to pay fee the tx would get too large for the fee amount

the solution implemented is to calculate a dummy pset paying fees, get the fee amount for this dummy pset and start counting fee amount for the entire transaction from this value.
  • Loading branch information
bordalix authored Jan 3, 2024
1 parent 54259aa commit b7e73a0
Showing 1 changed file with 16 additions and 3 deletions.
19 changes: 16 additions & 3 deletions src/domain/pset.ts
Original file line number Diff line number Diff line change
Expand Up @@ -195,6 +195,20 @@ export class PsetBuilder {
}
if (!fromAccounts) fromAccounts = getMainAccountsIDs(network);
const feeAssetHash = networks[network].assetHash;

let feeAmount = 0;

// lets make a dummy pset to pay fees, and start with that fee amount
if (asset !== feeAssetHash) {
const recipient: AddressRecipient = {
address: addr,
asset: feeAssetHash,
value: 300,
};
const dummyPset = await this.createRegularPset([recipient], [], fromAccounts);
feeAmount += dummyPset.feeAmount;
}

const unlockedUtxos = (
await this.walletRepository.getUnlockedUtxos(network, ...fromAccounts)
).filter((utxo) => utxo.blindingData?.asset === asset);
Expand Down Expand Up @@ -240,7 +254,7 @@ export class PsetBuilder {
await chainSource.close();
const sats1000Bytes = relayFee * 10 ** 8;
const estimatedSize = estimateVirtualSize(updater.pset, true);
const feeAmount = Math.ceil(estimatedSize * (sats1000Bytes / 1000));
feeAmount += Math.ceil(estimatedSize * (sats1000Bytes / 1000));

if (feeAssetHash === asset) {
updater.addOutputs([
Expand Down Expand Up @@ -365,7 +379,7 @@ export class PsetBuilder {
throw new Error('chain source not found, cannot estimate fee');
}
const updater = new Updater(pset).addInputs(ins).addOutputs(outs);
// we add 100% to the min relay fee in order to be sure that the transaction will be accepted by the network
// we add 10% to the min relay fee in order to be sure that the transaction will be accepted by the network
// some inputs and outputs may be added later to pay the fees
const relayFee = (await chainSource.getRelayFee()) * 1.1;
await chainSource.close();
Expand Down Expand Up @@ -467,7 +481,6 @@ export class PsetBuilder {
]);
}
}

return {
pset: updater.pset,
feeAmount,
Expand Down

0 comments on commit b7e73a0

Please sign in to comment.