singular-registry
LicenseApache-2.0
Safe HaskellNone
LanguageGHC2021

Singular.Registry.TxBuilder.Prepare

Description

When no output of the payer's wallet can fund a command (every output carries tokens or a reference script beside the ada, or none holds enough), the command spends one transaction first that puts the ada the command needs into an output of its own. This module is the pure builder of that transaction: selection and body from explicit inputs, priced by the ledger's own balancing, never a typed constant.

A preparation spends the fewest spendable outputs it needs, largest first; an output carrying a reference script is never spent, while any other output, token-carrying or not, may be. It makes the funding output as its first output — ada only, holding exactly the requirement, so the journal reads it back as the transaction's first output — and returns everything else the spent outputs held, tokens included, in one remainder output at the payer's address at or above the ledger minimum for its assets. The fee is the ledger's price of the built body, found by the same balancing the other builders use (balanceTx, which iterates the fee to its fixpoint and appends the change), not a typed number. The transaction carries no scripts, so it needs no collateral, and the one signing key it assumes is priced into the fee.

planPreparation judges the whole thing before anything is signed: it returns the balanced transaction with its funding reference and fee, or a PrepareError naming the requirement, the best fee balancing measured, the remainder floor, what the wallet may spend and its largest output. A wallet whose spendable ada cannot cover preparation plus command is refused from that error, before anything is submitted. An exact cover — spent equals funding plus fee to the lovelace, leaving no remainder — is refused the same way: the balancer always appends a change output, and a zero-ada one is not a valid output. Real wallets never land on it; the boundary spec pins the behaviour around it.

Synopsis

Documentation

data PrepareError Source #

Why no preparation can be planned: what shortfall the wallet leaves.

Constructors

PrepareShortfall 

Fields

  • prepareRequired :: Integer

    The funding the command needs.

  • prepareFee :: Maybe Integer

    The best fee balancing measured, if any attempt balanced far enough to price one; Nothing when no spendable output exists.

  • prepareRemainderMin :: Integer

    The ledger minimum for returning everything the wallet holds.

  • prepareHeld :: Integer

    What the wallet may spend, in total lovelace.

  • prepareLargest :: Integer

    The largest spendable output, in lovelace.

Instances

Instances details
Show PrepareError Source # 
Instance details

Defined in Singular.Registry.TxBuilder.Prepare

Methods

showsPrec :: Int -> PrepareError -> ShowS Source #

show :: PrepareError -> String Source #

showList :: [PrepareError] -> ShowS Source #

Eq PrepareError Source # 
Instance details

Defined in Singular.Registry.TxBuilder.Prepare

Methods

(==) :: PrepareError -> PrepareError -> Bool Source #

(/=) :: PrepareError -> PrepareError -> Bool Source #

data Preparation Source #

A planned preparation: the balanced transaction and what it makes.

Constructors

Preparation 

Fields

  • prepSpent :: [(TxIn, TxOut ConwayEra)]

    The spent outputs: the fewest largest-first that cover it.

  • prepTx :: ConwayTx

    The balanced transaction to sign and submit as built.

  • prepFunding :: TxIn

    The made funding output: this transaction's first output.

  • prepFee :: Integer

    The ledger's fee for the built body.

  • prepFundingCoin :: Integer

    What the funding output holds: exactly the requirement.

  • prepRemainderCoin :: Integer

    What the remainder output holds: at or above its minimum.

Instances

Instances details
Show Preparation Source # 
Instance details

Defined in Singular.Registry.TxBuilder.Prepare

Methods

showsPrec :: Int -> Preparation -> ShowS Source #

show :: Preparation -> String Source #

showList :: [Preparation] -> ShowS Source #

Eq Preparation Source # 
Instance details

Defined in Singular.Registry.TxBuilder.Prepare

Methods

(==) :: Preparation -> Preparation -> Bool Source #

(/=) :: Preparation -> Preparation -> Bool Source #

planPreparation Source #

Arguments

:: PParams ConwayEra 
-> Addr 
-> Integer

The funding the command needs.

-> [(TxIn, TxOut ConwayEra)] 
-> Either PrepareError Preparation 

Plan one preparation transaction that makes an ada-only output holding the requirement: the fewest largest-first spendable outputs that cover it, balanced by the ledger's own fee. Returns the balanced transaction or the shortfall, before anything is signed or submitted.

spendableOuts :: [(TxIn, TxOut ConwayEra)] -> [(TxIn, TxOut ConwayEra)] Source #

What the wallet may spend: every output carrying no reference script, token-carrying or not. Reference-script outputs are never spent: a transaction may not both spend an output and reference it.

spendableHeld :: [(TxIn, TxOut ConwayEra)] -> Integer Source #

What the wallet may spend, in total lovelace.