diff --git a/app/frontend/wallet/shelley/shelley-transaction-planner.ts b/app/frontend/wallet/shelley/shelley-transaction-planner.ts index a595034f46..ad8b2f879b 100644 --- a/app/frontend/wallet/shelley/shelley-transaction-planner.ts +++ b/app/frontend/wallet/shelley/shelley-transaction-planner.ts @@ -39,12 +39,15 @@ type Cert = { poolHash: string | null } +type Withdrawals = [string, Lovelace] + // Estimates size of final transaction in bytes. // Note(ppershing): can overshoot a bit export function estimateTxSize( inputs: Array, outputs: Array, - certs: Array + certs: Array, + withdrawals?: Withdrawals ): Lovelace { // exact size for inputs const preparedInputs = inputs.map(ShelleyTxInputFromUtxo) @@ -92,7 +95,8 @@ export function estimateTxSize( export function computeRequiredTxFee( inputs: Array, outputs: Array, - certs: Array = [] + certs: Array = [], + withdrawals?: Withdrawals ): Lovelace { const fee = txFeeFunction(estimateTxSize(inputs, outputs, certs)) return fee @@ -130,7 +134,8 @@ export function computeTxPlan( inputs: Array, outputs: Array, possibleChange: Output, - certs: Array + certs: Array, + withdrawals: Withdrawals ): TxPlan | null { const totalInput = inputs.reduce((acc, input) => acc + input.coins, 0) const deposit = computeRequiredDeposit(certs) @@ -140,7 +145,7 @@ export function computeTxPlan( throw NamedError('CoinAmountError') } - const feeWithoutChange = computeRequiredTxFee(inputs, outputs, certs) + const feeWithoutChange = computeRequiredTxFee(inputs, outputs, certs, withdrawals) // Cannot construct transaction plan if (totalOutput + feeWithoutChange > totalInput) return null @@ -151,7 +156,12 @@ export function computeTxPlan( } } - const feeWithChange = computeRequiredTxFee(inputs, [...outputs, possibleChange], certs) + const feeWithChange = computeRequiredTxFee( + inputs, + [...outputs, possibleChange], + certs, + withdrawals + ) if (totalOutput + feeWithChange > totalInput) { // We cannot fit the change output into the transaction @@ -220,12 +230,16 @@ export function selectMinimalTxPlan( isRedeem = false ): TxPlan | NoTxPlan { const certs = [] + const withdrawals = [] if (poolHash && registerStakingKey) { certs.push(createCert('staking_key_registration', accountAddress, null)) } if (poolHash) { certs.push(createCert('delegation', accountAddress, poolHash)) } + if (isRedeem) { + withdrawals.push(accountAddress, coins) + } const profitableUtxos = utxos.filter(isUtxoProfitable) const inputs = [] @@ -243,9 +257,9 @@ export function selectMinimalTxPlan( for (let i = 0; i < profitableUtxos.length; i++) { inputs.push(profitableUtxos[i]) - const plan = computeTxPlan(inputs, outputs, change, certs) + const plan = computeTxPlan(inputs, outputs, change, certs, withdrawals) if (plan) return plan } - return {estimatedFee: computeRequiredTxFee(inputs, outputs, certs)} + return {estimatedFee: computeRequiredTxFee(inputs, outputs, certs, withdrawals)} }