|
| 1 | +# CompleteTxBuilder Workflow Diagram |
| 2 | + |
| 3 | +```mermaid |
| 4 | +graph TD |
| 5 | + A["Start: complete() Function"] --> B["Phase 1: Setup & Validation"] |
| 6 | + B --> B1["Fetch Wallet UTxOs"] |
| 7 | + B --> B2["Derive Change Address"] |
| 8 | + B --> B3["Validate Options"] |
| 9 | + |
| 10 | + B1 --> C["Phase 2: Initial Coin Selection"] |
| 11 | + B2 --> C |
| 12 | + B3 --> C |
| 13 | + |
| 14 | + C --> C1["Calculate Asset Delta<br/>outputs + fee - collected - minted"] |
| 15 | + C1 --> C2["Filter Required Assets<br/>positive amounts only"] |
| 16 | + C2 --> C3["Recursive UTxO Selection<br/>largest-first strategy"] |
| 17 | + |
| 18 | + C3 --> D{"Has Plutus Scripts?"} |
| 19 | + |
| 20 | + D -->|No| F["Phase 5: Skip Script Evaluation"] |
| 21 | + D -->|Yes| E["Phase 3: Script Evaluation"] |
| 22 | + |
| 23 | + E --> E1["Build Evaluation Transaction<br/>CML.build_for_evaluation()"] |
| 24 | + E1 --> E2{"Local UPLC?"} |
| 25 | + |
| 26 | + E2 -->|Yes| E3["WASM UPLC Evaluation<br/>evalTransaction()"] |
| 27 | + E2 -->|No| E4["Provider Evaluation<br/>evalTransactionProvider()"] |
| 28 | + |
| 29 | + E3 --> E5["Apply UPLC Results<br/>applyUPLCEval()"] |
| 30 | + E4 --> E6["Apply Provider Results<br/>applyUPLCEvalProvider()"] |
| 31 | + |
| 32 | + E5 --> G["Phase 4: Refined Coin Selection"] |
| 33 | + E6 --> G |
| 34 | + |
| 35 | + G --> G1["Recalculate Fee with Script Costs"] |
| 36 | + G1 --> G2["Check if Additional UTxOs Needed"] |
| 37 | + G2 --> G3{"Need More UTxOs?"} |
| 38 | + |
| 39 | + G3 -->|Yes| G4["Select Additional UTxOs"] |
| 40 | + G3 -->|No| H["Phase 5: Collateral Management"] |
| 41 | + |
| 42 | + G4 --> G5{"Script Budget Changed?"} |
| 43 | + G5 -->|Yes| E1 |
| 44 | + G5 -->|No| H |
| 45 | + |
| 46 | + H --> H1["Calculate Collateral Amount<br/>150% of estimated fee"] |
| 47 | + H1 --> H2["Find Collateral UTxOs<br/>ADA-only, max 3"] |
| 48 | + H2 --> H3["Apply Collateral to Transaction"] |
| 49 | + |
| 50 | + H3 --> I["Phase 6: Final Assembly"] |
| 51 | + |
| 52 | + I --> I1["Complete Partial Programs<br/>Build redeemers with indices"] |
| 53 | + I1 --> I2["Final CML Transaction Build"] |
| 54 | + I2 --> I3["Apply Final ExUnits"] |
| 55 | + |
| 56 | + I3 --> J["Return Built Transaction"] |
| 57 | + |
| 58 | + F --> I |
| 59 | + |
| 60 | + style A fill:#4a90e2,color:#ffffff,stroke:#2171b5,stroke-width:3px |
| 61 | + style B fill:#9b59b6,color:#ffffff,stroke:#8e44ad,stroke-width:3px |
| 62 | + style C fill:#27ae60,color:#ffffff,stroke:#229954,stroke-width:3px |
| 63 | + style D fill:#f39c12,color:#ffffff,stroke:#e67e22,stroke-width:3px |
| 64 | + style E fill:#e74c3c,color:#ffffff,stroke:#c0392b,stroke-width:3px |
| 65 | + style F fill:#95a5a6,color:#ffffff,stroke:#7f8c8d,stroke-width:3px |
| 66 | + style G fill:#1abc9c,color:#ffffff,stroke:#16a085,stroke-width:3px |
| 67 | + style H fill:#f1c40f,color:#2c3e50,stroke:#f39c12,stroke-width:3px |
| 68 | + style I fill:#34495e,color:#ffffff,stroke:#2c3e50,stroke-width:3px |
| 69 | + style J fill:#2ecc71,color:#ffffff,stroke:#27ae60,stroke-width:4px |
| 70 | + |
| 71 | + classDef phaseBox fill:#ecf0f1,stroke:#34495e,stroke-width:3px,color:#2c3e50 |
| 72 | + classDef decision fill:#fff3cd,stroke:#856404,stroke-width:3px,color:#856404 |
| 73 | + classDef subprocess fill:#e8f4f8,stroke:#2980b9,stroke-width:2px,color:#2c3e50 |
| 74 | + classDef success fill:#d4edda,stroke:#155724,stroke-width:3px,color:#155724 |
| 75 | + |
| 76 | + class B,C,E,G,H,I phaseBox |
| 77 | + class D,E2,G3,G5 decision |
| 78 | + class B1,B2,B3,C1,C2,C3,E1,E3,E4,E5,E6,G1,G2,G4,H1,H2,H3,I1,I2,I3 subprocess |
| 79 | + class J success |
| 80 | +``` |
| 81 | + |
| 82 | +## Detailed Flow Explanation |
| 83 | + |
| 84 | +### Phase Transitions and Decision Points |
| 85 | + |
| 86 | +1. **Setup → Initial Selection**: Always proceeds after validation |
| 87 | +2. **Initial Selection → Script Check**: Determines if script evaluation needed |
| 88 | +3. **Script Evaluation → Refined Selection**: Only for Plutus script transactions |
| 89 | +4. **Refined Selection Loop**: Continues until stable UTxO selection achieved |
| 90 | +5. **Collateral Management**: Only applies to script transactions |
| 91 | +6. **Final Assembly**: Always completes the transaction building |
| 92 | + |
| 93 | +### Critical Decision Points |
| 94 | + |
| 95 | +#### Script Detection (`Has Plutus Scripts?`) |
| 96 | +```typescript |
| 97 | +// Determines evaluation path |
| 98 | +if (hasPlutusScriptExecutions) { |
| 99 | + // Proceed to script evaluation |
| 100 | +} else { |
| 101 | + // Skip to collateral/final assembly |
| 102 | +} |
| 103 | +``` |
| 104 | + |
| 105 | +#### UPLC vs Provider Evaluation (`Local UPLC?`) |
| 106 | +```typescript |
| 107 | +if (localUPLCEval !== false) { |
| 108 | + // Use WASM UPLC evaluation |
| 109 | + applyUPLCEval(uplcResults, txBuilder) |
| 110 | +} else { |
| 111 | + // Use external provider evaluation |
| 112 | + applyUPLCEvalProvider(providerResults, txBuilder) |
| 113 | +} |
| 114 | +``` |
| 115 | + |
| 116 | +#### UTxO Selection Stability (`Need More UTxOs?`) |
| 117 | +```typescript |
| 118 | +// Check if script costs require additional funds |
| 119 | +if (newEstimatedFee > currentCapacity) { |
| 120 | + // Select more UTxOs and potentially re-evaluate scripts |
| 121 | + return selectAdditionalUTxOs() |
| 122 | +} |
| 123 | +``` |
| 124 | + |
| 125 | +#### Script Budget Changes (`Script Budget Changed?`) |
| 126 | +```typescript |
| 127 | +// If new inputs change script execution context |
| 128 | +if (inputSetChanged && hasSignificantBudgetChange) { |
| 129 | + // Re-evaluate scripts with new input context |
| 130 | + return reEvaluateScripts() |
| 131 | +} |
| 132 | +``` |
| 133 | + |
| 134 | +### Error Paths (Not Shown in Diagram) |
| 135 | + |
| 136 | +Each phase can fail with specific error types: |
| 137 | +- **Phase 1**: Wallet access errors, configuration validation errors |
| 138 | +- **Phase 2**: Insufficient funds errors, UTxO availability errors |
| 139 | +- **Phase 3**: Script evaluation errors, UPLC compilation errors |
| 140 | +- **Phase 4**: Fee calculation errors, UTxO selection errors |
| 141 | +- **Phase 5**: Collateral selection errors, protocol limit errors |
| 142 | +- **Phase 6**: Redeemer building errors, transaction assembly errors |
| 143 | + |
| 144 | +### Performance Considerations |
| 145 | + |
| 146 | +#### Iterative Loops |
| 147 | +- **Coin Selection Loop**: May iterate multiple times for complex asset requirements |
| 148 | +- **Script Evaluation Loop**: May re-evaluate if input set changes significantly |
| 149 | +- **Minimum ADA Loop**: Continues until change outputs meet minimum requirements |
| 150 | + |
| 151 | +#### Expensive Operations |
| 152 | +- **Script Evaluation**: Most expensive operation, especially for complex scripts |
| 153 | +- **UTxO Sorting**: O(n log n) for large UTxO sets |
| 154 | +- **Recursive Selection**: May examine many UTxO combinations |
| 155 | + |
| 156 | +### State Dependencies |
| 157 | + |
| 158 | +```mermaid |
| 159 | +graph LR |
| 160 | + A[Wallet UTxOs] --> B[Available Inputs] |
| 161 | + B --> C[Selected UTxOs] |
| 162 | + C --> D[Draft Transaction] |
| 163 | + D --> E[Script Evaluation] |
| 164 | + E --> F[Final Transaction] |
| 165 | + |
| 166 | + G[Protocol Parameters] --> B |
| 167 | + G --> E |
| 168 | + G --> F |
| 169 | + |
| 170 | + H[Transaction Outputs] --> C |
| 171 | + H --> D |
| 172 | + |
| 173 | + I[Minted Assets] --> C |
| 174 | + I --> D |
| 175 | +``` |
| 176 | + |
| 177 | +This workflow represents one of the most complex transaction building systems in the Cardano ecosystem, with sophisticated handling of script evaluation, UTxO management, and fee calculation. |
0 commit comments