Bitcoind

如何使用從 PHP 或 bitcoind 支付中提取的佣金發送比特幣

  • April 26, 2020

首先,我有一個簡單的問題。我想向客戶發送一些比特幣,但我不想為他支付費用。我想從付款中扣除費用,所以所有佣金都將由客戶支付。

我認為退款給客戶應該是所有商家的一個簡單而普遍的問題。但是我搜尋了幾個小時,找不到任何關於如何實現此功能“ receiver-pays-commissions ”的詳細解釋。

這裡有一些我發現有用的連結,但仍然沒有確切的答案

如何在 bitcoind 中創建原始交易

如何在 PHP 中創建原始事務

如何計算交易規模和費用

簡單的說。我想發送一些比特幣,這樣我的賬戶餘額就會完全相等1.8000000。現在稍微高一點:

在此處輸入圖像描述

令人驚訝的是,這個簡單而常見的問題花了幾個小時的Google搜尋,所以我提出這個問題只是為了與公眾分享解決方案,因為我終於找到了它。

(或者至少我認為我已經找到了,所以非常歡迎批評和評論。)

這是沒有程式碼的算法:

  1. createrawtransaction``amount假設費用是您要發送的0
  2. fundrawtransaction要讓您的 bitcoind 建構最終交易,請輸入您的change地址(在您從未使用的交易中獲得零錢的地方 -非常重要!)併fees計算該交易將用於您的帳戶。
  3. fees從初始計算中減去amount
  4. 使用新的降低金額重複步驟 1-2。
  5. 利潤。

這是PHP程式碼。

我們將需要功能來創建和資助新交易

function prepareRawTransaction($amountToSend, $addressToSendTo) {

   $bitcoin = new Bitcoin('bitcoinrpc', 'xxx');

   $collectedAmount = 0;
   $sendToArray = [];
   $sendToArray[$addressToSendTo] = $amountToSend;

   $unspentTransactions = $bitcoin->listunspent(); //First we find out all our unspent transactions from where we can withdraw
   $collectedTransactions = [];

   foreach ($unspentTransactions as $unspentTransaction) {

       if ($collectedAmount < $amountToSend) {
           $collectedTransactions[] = [
               'txid'  => $unspentTransaction['txid'],
               'vout'  => $unspentTransaction['vout'],
           ];

           $collectedAmount += $unspentTransaction['amount'];
       } else {

           break;
       }
   }

   //Now we create raw transactions
   $rawTransaction = $bitcoin->createrawtransaction($collectedTransactions, $sendToArray);

   //And fund it
   $newTransaction = $bitcoin->fundrawtransaction($rawTransaction);
   return $newTransaction;
}

現在我們可以使用該函式進行算法

$bitcoin = new Bitcoin('bitcoinrpc', 'xxx');

$amountToSend = 0.09999556; //That's exact amount that we want to send INCLUDING commission
$addressToSendTo = '2N8hwP1WmJrFF5QWABn38y63uYLhnJYJYTF';

//createrawtransaction with amount that you want to send assuming that fees are 0
$transactionWithoutCommission = prepareRawTransaction($amountToSend, $addressToSendTo);

$decoded = $bitcoin->decoderawtransaction($transactionWithoutCommission['hex']);
print_r($decoded); //You can see that fees now set up

//Substract caclulated fees from initial amount
$amountToSend =  $amountToSend - $transactionWithoutCommission['fee'];

//Repeat steps 1-2 with new lowered amount
$transactionWithCommission = prepareRawTransaction($amountToSend, $addressToSendTo);

$decoded = $bitcoin->decoderawtransaction($transactionWithCommission['hex']);
print_r($decoded); //Here you can see that total amount + commission equal needed amount

$signed = $bitcoin->signrawtransaction($transactionWithCommission['hex']);
print_r($signed); //should say complete:1 if everything was correctly put

$published = $bitcoin->sendrawtransaction($signed['hex']);
print_r($published); //will output you tx id

和利潤:您已經發送了您想要的金額,包括佣金

在此處輸入圖像描述

你可以做的是使用 $txid = $bitcoin- >sendtoaddress("User's bitcoIN address here ","amount of btc to send","comment 1","comment 2", true); // echo $txid; 真正的東西,它確保費用已經包含在你想要設置的金額中(不確定庫是否有故障,如果是,那麼 true 可能無法正常工作)

引用自:https://bitcoin.stackexchange.com/questions/69676