Back to Ccxt

Binance Fetch Balance Snapshot Watch Balance Updates

wiki/examples/php/binance-fetch-balance-snapshot-watch-balance-updates.md

4.5.562.4 KB
Original Source
php
<?php
namespace ccxt;
include_once (__DIR__.'/../../ccxt.php');
// ----------------------------------------------------------------------------

// PLEASE DO NOT EDIT THIS FILE, IT IS GENERATED AND WILL BE OVERWRITTEN:
// https://github.com/ccxt/ccxt/blob/master/CONTRIBUTING.md#how-to-contribute-code

// -----------------------------------------------------------------------------

error_reporting(E_ALL);
date_default_timezone_set('UTC');

use ccxt\Precise;
use React\Async;
use React\Promise;


// AUTO-TRANSPILE //
'use strict';


var_dump('CCXT Version:', $ccxt->version); // eslint-disable-line import/no-named-as-default-member


// This example will run silent and will return your balance only when the balance is updated.
//
// 1. launch the example with your keys and keep it running
// 2. go to the trading section on the website
// 3. place a order on a spot market
// 4. see your balance updated in the example
//
// Warning! This example might produce a lot of output to your screen
function watch_balance($exchange) {
   return Async\async(function () use ($exchange) {
       $balance = \React\Async\await($exchange->fetch_balance());
       var_dump('------------- Initial -------------');
       var_dump($exchange->iso8601($exchange->milliseconds()));
       var_dump($balance);
       while (true) {
           try {
               $update = \React\Async\await($exchange->watch_balance());
               $balance = $exchange->deep_extend($balance, $update);
               // it will print the balance update when the balance changes
               // if the balance remains unchanged the exchange will not send it
               var_dump('------------- Update -------------');
               var_dump($exchange->iso8601($exchange->milliseconds()));
               var_dump($balance);
           } catch(Exception $e) {
               var_dump($e);
               break;
           }
       }
   }) ();
}


function main() {
   return Async\async(function () {
       $exchange = new \ccxt\pro\binance(array(
           'apiKey' => 'YOUR_API_KEY',
           'secret' => 'YOUR_SECRET',
       ));
       \React\Async\await($exchange->load_markets());
       // exchange.verbose = true // uncomment for debugging purposes if necessary
       \React\Async\await(watch_balance($exchange));
       \React\Async\await($exchange->close());
   }) ();
}


main();