/** * create a signed transaction sending all the found outputs to the given address * * @param $destinationAddress * @return array * @throws \Exception */ protected function createTransaction($destinationAddress) { if ($this->debug) { echo "\nCreating transaction to address {$destinationAddress}"; } // create raw transaction $inputs = []; foreach ($this->sweepData['utxos'] as $address => $data) { $inputs = array_merge($inputs, array_map(function ($utxo) use($address, $data) { return ['txid' => $utxo['hash'], 'vout' => $utxo['index'], 'scriptPubKey' => $utxo['script_hex'], 'value' => $utxo['value'], 'address' => $address, 'path' => $data['path'], 'redeemScript' => $data['redeem']]; }, $data['utxos'])); } $outputs = []; $fee = Wallet::estimateFee($this->sweepData['count'], 1); $outputs[$destinationAddress] = $this->sweepData['balance'] - $fee; //create the raw transaction $rawTransaction = RawTransaction::create($inputs, $outputs); if (!$rawTransaction) { throw new \Exception("Failed to create raw transaction"); } if ($this->debug) { echo "\nSigning transaction"; } //sign the raw transaction $transaction = $this->signTransaction($rawTransaction, $inputs); if (!$transaction['sign_count']) { throw new \Exception("Failed to sign transaction"); } return $transaction; }
public function testEstimateFee() { $this->assertEquals(30000, Wallet::estimateFee(1, 66)); $this->assertEquals(40000, Wallet::estimateFee(2, 71)); }