DEV Community

Cover image for [DEV: PHP] Writing code for payout in cryptocurrencies
Apironе
Apironе

Posted on

[DEV: PHP] Writing code for payout in cryptocurrencies

Banner networks, gambling, options, trading, monthly payroll, and referral networks often use the same payout strategy for their customers:
collect requests from customers
the security department reviews the list and approves it
a periodic payment is made

To do this, we have a convenient masspayment tool for up to 255 recipients in one transaction. In the case of a larger number of recipients, there will be split in several transactions.
Attention! you pay a fixed commission only once in each transaction, which is very convenient and more cost-efficient than making 255 transactions and paying a commission on each.

Test wallet: tbtc-bde1cfc70297e4ff7068334b25986819
Test Transfer_key: hyl1aQkd5vwdzT1uExgUCOXOcekUg4Yc

Mass payment with fixed fee

Code example:

<?php
  $WalletID = "tbtc-bde1cfc70297e4ff7068334b25986819";

  $json_data = '{"transfer_key": "hyl1aQkd5vwdzT1uExgUCOXOcekUg4Yc"}' .
  array ( 
    'destinations' => array (        
        array('address' => "2N4obeEuEidHmuXLCLvWeA4tH9FhB8W8EyD", 'amount' => "100000"),
        array('address' => "2N1SRgR6Tj4LTxsMiTfM95yHSrBSQTkfvco", 'amount' => "750000")
    )
  );

  $api_base = file_get_contents("https://apirone.com/api/v2/wallets/" .
              $WalletID . "/transfer");

  $curl = curl_init($api_base);
  curl_setopt($curl, CURLOPT_HTTPHEADER, array("Content-Type: application/json"));
  curl_setopt($curl, CURLOPT_POST, 1);
  curl_setopt($curl, CURLOPT_POSTFIELDS, json_encode($json_data));
  curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
  $http_status_code = curl_getinfo($curl, CURLINFO_HTTP_CODE);
  $response = curl_exec($curl);
  curl_close($curl);

  $decoded = json_decode($response, true);
  echo "Transaction hash: " . $decoded["txs"]["0"];
?>
Enter fullscreen mode Exit fullscreen mode

Where 'destinations' is an array of recipient addresses and amounts. The amount is indicated in minor units, e. g. for bitcoin it is satoshi.
Also, you can specify the amount as a percentage of the total wallet balance.
For example:

  'destinations' => array (        
        array('address' => "2N4obeEuEidHmuXLCLvWeA4tH9FhB8W8EyD", 'amount' => "52.57%"),
        array('address' => "2N1SRgR6Tj4LTxsMiTfM95yHSrBSQTkfvco", 'amount' => "47.43%")
    )
Enter fullscreen mode Exit fullscreen mode

By default, the network fee is current, but you can choose a higher one to make the transaction be confirmed sooner.

As a result of execution, you receive a transaction or a list of transactions, a copy of the array of recipients, amounts, and an address of change if there is any. More details about the parameters can be found at apirone.com

Top comments (0)