diff --git a/packages/extension-base/src/koni/background/handlers/Extension.ts b/packages/extension-base/src/koni/background/handlers/Extension.ts index 6f402cfb4b..fdeebdd043 100644 --- a/packages/extension-base/src/koni/background/handlers/Extension.ts +++ b/packages/extension-base/src/koni/background/handlers/Extension.ts @@ -2359,7 +2359,7 @@ export default class KoniExtension { const substrateApi = this.#koniState.chainService.getSubstrateApi(chain); let estimatedFee = '0'; let estimatedFeeMax = '0'; - let feeOptions: FeeDetail; + let feeOptions: FeeDetail | null = null; let tip = '0'; let maxTransferable = new BigN(freeBalance.value); @@ -2481,7 +2481,28 @@ export default class KoniExtension { utxos }; - const recipients = to ? [to, address] : [address]; + const recipients = transferAll ? [address] : [address, to || address]; + + const fallbackCalculate = (recipients: string[]) => { + const utxos = filterUneconomicalUtxos({ + utxos: determineUtxosArgs.utxos, + feeRate: determineUtxosArgs.feeRate, + recipients, + sender: determineUtxosArgs.sender + }); + + const { txVBytes: vSize } = getSizeInfo({ + inputLength: utxos.length, + sender: address, + recipients + }); + + return { + vSize, + maxTransferable: utxos.reduce((previous, input) => previous.plus(input.value), new BigN(0)), + estimatedFee: Math.ceil(determineUtxosArgs.feeRate * vSize).toString() + }; + }; try { const { fee: _estimatedFee, inputs } = transferAll ? determineUtxosForSpendAll(determineUtxosArgs) : determineUtxosForSpend(determineUtxosArgs); @@ -2494,6 +2515,13 @@ export default class KoniExtension { recipients }); + estimatedFee = new BigN(_estimatedFee).toFixed(0); + feeOptions = { + ..._fee, + estimatedFee, + vSize + }; + if (transferAll) { estimatedFeeMax = new BigN(_estimatedFee).toFixed(0); } else { @@ -2503,57 +2531,29 @@ export default class KoniExtension { maxTransferable = inputs.reduce((previous, input) => previous.plus(input.value), new BigN(0)); estimatedFeeMax = new BigN(estimateFeeMax).toFixed(0); } catch (_e) { - const utxos = filterUneconomicalUtxos({ - utxos: determineUtxosArgs.utxos, - feeRate: determineUtxosArgs.feeRate, - recipients, - sender: determineUtxosArgs.sender - }); - - const { txVBytes: vSize } = getSizeInfo({ - inputLength: utxos.length, - sender: address, - recipients - }); - - maxTransferable = utxos.reduce((previous, input) => previous.plus(input.value), new BigN(0)); - estimatedFeeMax = Math.ceil(determineUtxosArgs.feeRate * vSize).toString(); + const fb = fallbackCalculate([to || address]); + + maxTransferable = fb.maxTransferable; + estimatedFeeMax = fb.estimatedFee; } } - - estimatedFee = new BigN(_estimatedFee).toFixed(0); - feeOptions = { - ..._fee, - estimatedFee, - vSize - }; } catch (_e) { - const utxos = filterUneconomicalUtxos({ - utxos: determineUtxosArgs.utxos, - feeRate: determineUtxosArgs.feeRate, - recipients, - sender: determineUtxosArgs.sender - }); + const fb = fallbackCalculate([to || address]); - const { txVBytes: vSize } = getSizeInfo({ - inputLength: utxos.length, - sender: address, - recipients - }); - - maxTransferable = utxos.reduce((previous, input) => previous.plus(input.value), new BigN(0)); - const _estimatedFee = Math.ceil(determineUtxosArgs.feeRate * vSize).toString(); + maxTransferable = fb.maxTransferable; + estimatedFeeMax = fb.estimatedFee; - estimatedFee = _estimatedFee; - estimatedFeeMax = _estimatedFee; + if (!feeOptions) { + const fb = fallbackCalculate([address, to || address]); - console.log('catch', maxTransferable.toString(), estimatedFeeMax.toString()); + estimatedFee = fb.estimatedFee; - feeOptions = { - ..._fee, - estimatedFee, - vSize - }; + feeOptions = { + ..._fee, + estimatedFee, + vSize: fb.vSize + }; + } } } else { const [mockTx] = await createTransferExtrinsic({ diff --git a/packages/extension-base/src/utils/bitcoin/utxo-management.ts b/packages/extension-base/src/utils/bitcoin/utxo-management.ts index 44b957322f..ce4790c33b 100644 --- a/packages/extension-base/src/utils/bitcoin/utxo-management.ts +++ b/packages/extension-base/src/utils/bitcoin/utxo-management.ts @@ -142,7 +142,7 @@ export function determineUtxosForSpend ({ amount, sizeInfo = getSizeInfo({ inputLength: neededUtxos.length, sender, - recipients: [recipient, sender] + recipients }); }