Skip to content

Commit

Permalink
Add withdrawals to tx planners
Browse files Browse the repository at this point in the history
  • Loading branch information
xdzurman committed Aug 3, 2020
1 parent c76f2aa commit d98670d
Showing 1 changed file with 21 additions and 7 deletions.
28 changes: 21 additions & 7 deletions app/frontend/wallet/shelley/shelley-transaction-planner.ts
Expand Up @@ -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<Input>,
outputs: Array<Output>,
certs: Array<Cert>
certs: Array<Cert>,
withdrawals?: Withdrawals
): Lovelace {
// exact size for inputs
const preparedInputs = inputs.map(ShelleyTxInputFromUtxo)
Expand Down Expand Up @@ -92,7 +95,8 @@ export function estimateTxSize(
export function computeRequiredTxFee(
inputs: Array<Input>,
outputs: Array<Output>,
certs: Array<Cert> = []
certs: Array<Cert> = [],
withdrawals?: Withdrawals
): Lovelace {
const fee = txFeeFunction(estimateTxSize(inputs, outputs, certs))
return fee
Expand Down Expand Up @@ -130,7 +134,8 @@ export function computeTxPlan(
inputs: Array<Input>,
outputs: Array<Output>,
possibleChange: Output,
certs: Array<Cert>
certs: Array<Cert>,
withdrawals: Withdrawals
): TxPlan | null {
const totalInput = inputs.reduce((acc, input) => acc + input.coins, 0)
const deposit = computeRequiredDeposit(certs)
Expand All @@ -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

Expand All @@ -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
Expand Down Expand Up @@ -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 = []
Expand All @@ -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)}
}

0 comments on commit d98670d

Please sign in to comment.