<?php

use Lin\Ftx\Ftx;
// $time = "1659967641";
// $date = date('Y-m-d H:i:s',$time);

$orderid = "171111497444";
//     echo "<pre>";
//     print_r($date);
//     echo "<pre>";
//     die;
function order_ftx($orderid,$ftx){
    $result = $ftx->orders()->get([
        'order_id'=>$orderid,
    ]);
    if($result['result']['status'] != "new")
    return $result;
    else
        $this->order_ftx($orderid,$ftx);
}

$t = microtime(true);
$micro = sprintf("%06d",($t - floor($t)) * 1000000);
$d = new DateTime( date('Y-m-d H:i:s.'.$micro, $t) );

$key = "8wTnMLh7gCGD-qi6GwtltbqNqJ7p7gSRetnaO8Bq";
$secret = "ZhwIWArWDm7arJ7IpgtHPMR5uUFPc9ZDf3Rlsago";

$ftx=new Ftx($key,$secret);

try {
    $result=$ftx->wallet()->getBalances();
}catch (\Exception $e){
    print_r($e->getMessage());
}
echo "<pre>";
print_r($result);
echo "</pre>";
die;

 $result = $ftx->orders()->get([
        'order_id'=>$orderid,
    ]);
    echo "<pre>";
    print_r($result);
    echo "<pre>";
    die;
 $order_pre1=$ftx->orders()->post([
        'market'=>'ETH/USDT',
        'side'=>'sell',
        'price'=>NULL,
        'type'=>'market',
        'size'=>'0.028'
        //'clientId'=>'1234567890',
    ]);
// $result = $ftx->markets()->get([
//         'market_name'=>'BTC/USDT',// BTC/USD   BTC-PERP  BTC-0626
//         //'depth'=>'10'
//     ]);
    
  echo "<pre>";
print_r($order_pre1);
echo "</pre>";
// die;



 $order_pre = $order_pre1['result'];
        if($order_pre){
                if($order_pre['status'] == 'new'){
                   $order = order_ftx($order_pre['id'],$ftx);
                }else{
                    $order = $order_pre;
                }
            }

            echo "<pre>";
            print_r($order);
            echo "</pre>";



//You can set special needs
$ftx->setOptions([
    //Set the request timeout to 60 seconds by default
    'timeout'=>60,

    //https://github.com/guzzle/guzzle
    //'proxy'=>[],

    //https://www.php.net/manual/en/book.curl.php
    //'curl'=>[],

    //FTX-SUBACCOUNT (optional): URI-encoded name of the subaccount to use. Omit if not using subaccounts.
    //'headers'=>['FTX-SUBACCOUNT'=>'xxxx']
]);








$result=$ftx->markets()->gets();
try {
    $result=$ftx->markets()->gets();
}catch (\Exception $e){
    print_r($e->getMessage());
}



$result = $ftx->markets()->get([
        'market_name'=>'BTC/USDT',// BTC/USD   BTC-PERP  BTC-0626
        //'depth'=>'10'
    ]);
echo"<pre>";
print_r($result);
echo"</pre>";
die;

try {
    $result=$ftx->markets()->gets();
}catch (\Exception $e){
    print_r($e->getMessage());
}

$i=0;
$required_pairs = ['1INCH','ADA','AAVE','ATOM','ALGO','AION','AVAX','AKRO','ANT','APE','BAT','BTC','BCH','BNB','BTCST','BAKE','CRV','COMP','CAKE','COS','DOGE','DASH','DOT','ETH','EOS','ETC','FTM','FTT','FIL','GALA','GRT','GMT','HBAR','HOT','IOTA','ICP','IOST','JST','KAVA','KSM','LTC','LINK','LIT','LUNA','MANA','MATIC','MDX','NEO','OMG','ONT','QTUM','REEF','RSR','SUN','SHIB','SLP','SOL','SUSHI','SXP','TRX','THETA','TFUEL','UNI','VET','WRX','XRP','XMR','XEM','XLM','XTZ','YFI','YFII','ZEC','ZEN'];
if(!empty($result['result'])) { 
foreach ($result['result'] as $key => $value) {
  if(substr($value['name'], -5) == "/USDT"){
    if(in_array(substr_replace($value['name'] ,"",-5), $required_pairs)){
      $output[$i]['symbol'] = str_replace('/','',$value['name']);
      $output[$i]['price'] = $value['last'];
      $output[$i]['change'] = $value['change24h'];
    }
      $i++;
  }
}
}

try {
    $result=$ftx->wallet()->getBalances();
}catch (\Exception $e){
    print_r($e->getMessage());
}


try {
    $result=$ftx->account()->getPositions();
    print_r($result);
}catch (\Exception $e){
    print_r($e->getMessage());
}

