public function determineFeeAndChange(TransactionBuilder $txBuilder) { $send = $txBuilder->getOutputs(); $utxos = $txBuilder->getUtxos(); $fee = $txBuilder->getFee(); $change = null; // if the fee is fixed we just need to calculate the change if ($fee !== null) { $change = $this->determineChange($utxos, $send, $fee); // if change is not dust we need to add a change output if ($change > Blocktrail::DUST) { $send[] = ['address' => 'change', 'value' => $change]; } else { // if change is dust we do nothing (implicitly it's added to the fee) $change = 0; } } else { $fee = $this->determineFee($utxos, $send); $change = $this->determineChange($utxos, $send, $fee); if ($change > 0) { $changeIdx = count($send); // set dummy change output $send[$changeIdx] = ['address' => 'change', 'value' => $change]; // recaculate fee now that we know that we have a change output $fee2 = $this->determineFee($utxos, $send); // unset dummy change output unset($send[$changeIdx]); // if adding the change output made the fee bump up and the change is smaller than the fee // then we're not doing change if ($fee2 > $fee && $fee2 > $change) { $change = 0; } else { $change = $this->determineChange($utxos, $send, $fee2); // if change is not dust we need to add a change output if ($change > Blocktrail::DUST) { $send[$changeIdx] = ['address' => 'change', 'value' => $change]; } else { // if change is dust we do nothing (implicitly it's added to the fee) $change = 0; } } } } $fee = $this->determineFee($utxos, $send); return [$fee, $change]; }