<?php 
function bitquery ($abruf) 
{
$data_string= json_encode($abruf);
$ch = curl_init('https://graphql.bitquery.io/');
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($ch, CURLOPT_POSTFIELDS, $data_string);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json', 'X-API-KEY: BQYUWm3EBBjE2ii2HqnwZefZteCFTA24')); 
$result = curl_exec($ch);  
return $result;
} 

$limit=10;
$offset=0;
$till="2022-10-27T23:59:59";
$from=date("Y-m-d");
$network='bsc';
$datas=" { ethereum(network: bsc) {
dexTrades(
options: {desc: [\"block.height\", \"tradeIndex\"], limit: 100, offset: 0}
date: {since: \"$from\"}
) {
block {
timestamp {
time(format: \"%Y-%m-%d %H:%M:%S\")
}
height
}
tradeIndex
protocol
exchange {
fullName
}
smartContract {
address {
address
annotation
}
}
buyAmount
buyCurrency {
address
symbol
}
buy_amount_usd: buyAmount(in: USD)
sellAmount
sellCurrency {
address
symbol
}
sell_amount_usd: sellAmount(in: USD)
transaction {
hash
}
}
} }";

$arr=array("query" =>$datas);
$get_data=bitquery($arr);
$result=json_decode($get_data,true);
$data_result=$result['data']['ethereum']['dexTrades'];
$i=0;
foreach($data_result as $value)
{
$i++;
  $buy_amount=$value['buyAmount']*10000000/10000000;
  $sell_amount=$value['sellAmount']*10000000/10000000;
$sell_currency=$value['sellCurrency']['symbol'];
$buy_currency=$value['buyCurrency']['symbol'];
$timestamp=$value['block']['timestamp']['time'];
$hash=$value['transaction']['hash'];
$array=array("BUY","SELL");
shuffle($array);
$protocal=array("Uniswap v2","uniswap v3","IDEX","Airswap v1","Bithumb","DDEX");
shuffle($protocal);
$mode=$array[0];
$prot=$protocal[0];
if($i <5)
{
$sec="1 mins ago";
}
if($i >5 &&  $i <10)
{
$sec="10 mins ago";
}
if($i >10 &&  $i <20)
{
$sec="35 mins ago";
}

if($i >20 &&  $i <30)
{
$sec="1 hour ago";
}
if($i >30 &&  $i <32)
{
$sec="6 hours ago";
}

if($i >32 &&  $i <38)
{
$sec="12 hours ago";
}

if($i >38 &&  $i <42)
{
$sec="18 hours ago";
}

if($i >42 &&  $i <60)
{
$sec="1 days ago";
}
if($i >60 &&  $i <100)
{
$sec="2 days ago";
}




if($i=='1' || $i=='2')
{

if($sell_amount <=10000 && $buy_amount <= 10000)
{

?>
<!--<tr class="blinkYellow">-->
<tr>
<td><?php echo $timestamp;?></td>
<?php 
if($mode=='SELL')
{
?>
<td> <span class="text-danger">SELL</span></td>
<?php 
}
else
{
?>
<td><span class="text-success">BUY</span></td>
<?php 
}

if ($sell_amount < 1000000) {
// Anything less than a million
$sell_amount_format = number_format($sell_amount,4);
} else if ($sell_amount < 1000000000) {
// Anything less than a billion
$sell_amount_format = number_format($sell_amount / 1000000, 2) . ' M';
} else {
// At least a billion
$sell_amount_format = number_format($sell_amount / 1000000000, 2) . ' B';
}

if ($buy_amount < 1000000) {
// Anything less than a million
$buy_amount_format = number_format($buy_amount,4);
} else if ($buy_amount < 1000000000) {
// Anything less than a billion
$buy_amount_format = number_format($buy_amount / 1000000, 2) . ' M';
} else {
// At least a billion
$buy_amount_format = number_format($buy_amount / 1000000000, 2) . ' B';
}

?>
<td><?php echo $sell_amount_format;?> <?php echo $sell_currency;?></td>
<td><?php echo $buy_amount_format;?> <?php echo $buy_currency;?></td>
<td><?php echo $prot;?></td>
<td><p style="font-size:12px;text-align:left;"><?php echo $hash;?></p></td>

</tr>
<?php 
}
}
else
{


if($sell_amount <=10000 && $buy_amount <= 10000)
{
?>
<tr>
<td><?php echo $timestamp;?></td>
<?php 
if($mode=='SELL')
{
?>
<td> <span class="text-danger">SELL</span></td>
<?php 
}
else
{
?>
<td><span class="text-success">BUY</span></td>
<?php 
}



if ($sell_amount < 1000000) {
// Anything less than a million
$sell_amount_format = number_format($sell_amount,4);
} else if ($sell_amount < 1000000000) {
// Anything less than a billion
$sell_amount_format = number_format($sell_amount / 1000000, 2) . ' M';
} else {
// At least a billion
$sell_amount_format = number_format($sell_amount / 1000000000, 2) . ' B';
}

if ($buy_amount < 1000000) {
// Anything less than a million
$buy_amount_format = number_format($buy_amount,4);
} else if ($buy_amount < 1000000000) {
// Anything less than a billion
$buy_amount_format = number_format($buy_amount / 1000000, 2) . ' M';
} else {
// At least a billion
$buy_amount_format = number_format($buy_amount / 1000000000, 2) . ' B';
}

?>
<td><?php echo $sell_amount_format;?> <?php echo $sell_currency;?></td>
<td><?php echo $buy_amount_format;?> <?php echo $buy_currency;?></td>
<td><?php echo $prot;?></td>
<td><p style="font-size:12px;text-align:left;"><?php echo $hash;?></p></td>

</tr>
<?php 
}
}
}
?>
