<?php
namespace App\Controller;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Annotation\Route;
use App\Document\Currencies;
use App\Document\Price;
use Symfony\Component\HttpFoundation\JsonResponse ;
use Symfony\Component\HttpFoundation\Request;
use Doctrine\ODM\MongoDB\DocumentManager;
use Symfony\Contracts\HttpClient\HttpClientInterface;
use App\Document\Symbol as SymbolDocument;
use App\Document\Symbol2 as SymbolDocument2;
use App\Document\BackSymbol;
class CurrenciesController extends AbstractController
{
private $documentManager;
private $client;
public function __construct( DocumentManager $documentManager , HttpClientInterface $client ) {
$this->documentManager = $documentManager;
$this->httpclient = $client ;
}
#[Route('/pub/crypto/alllist',methods: ['GET'], name: 'currencies_all_price')]
public function currenciesallprice( DocumentManager $doctrine , Request $request ): JsonResponse
{
// $list = [
// "BTC",
// "ETH",
// "USDT",
// "BNB",
// "SOL",
// "XRP",
// "USDC",
// "ADA",
// "AVAX",
// "DOGE",
// "TRX",
// "DOT",
// "LINK",
// "MATIC",
// "TON",
// "ICP",
// "SHIB",
// "DAI",
// "LTC",
// "BCH",
// "ETC",
// "UNI",
// "ATOM",
// "LEO",
// "OP",
// "NEAR",
// "XLM",
// "OKB",
// "APT",
// "INJ",
// "TIA",
// "FIL",
// "LDO",
// "XMR",
// "IMX",
// "ARB",
// "HBAR",
// "KAS",
// "STX",
// "CRO",
// "MNT",
// "VET",
// "TUSD",
// "FDUSD",
// "MKR",
// "BSV",
// "SEI",
// "GRT",
// "ORDI",
// "RUNE",
// "ALGO",
// "AAVE",
// "RNDR",
// "QNT",
// "EGLD",
// "SUI",
// "1000SATS",
// "MINA",
// "FLOW",
// "HNT",
// "SNX",
// "FTM",
// "XTZ",
// "AXS",
// "SAND",
// "THETA",
// "BTT",
// "FTT",
// "KCS",
// "BEAM",
// "WEMIX",
// "BONK",
// "OSMO",
// "NEO",
// "MANA",
// "EOS",
// "BGB",
// "KAVA",
// "ASTR",
// "ENS",
// "CHZ",
// "WOO",
// "IOTA",
// "ROSE",
// "GALA",
// "CAKE",
// "BLUR",
// "USDD",
// "LUNC",
// "XDC",
// "AR",
// "XEC",
// "CFX",
// "RPL",
// "FXS",
// "KLAY",
// "AKT",
// "CRV",
// "SC",
// "FLR"
// ] ;
// foreach ($list as $crypto) {
// $url = 'https://api.kucoin.com/api/v1/market/stats?symbol='.$crypto.'-USDT';
// $response = $this->httpclient->request(
// 'GET',
// $url
// );
// $j[] = json_decode($response->getContent()) ;
// }
$url = 'https://pro-api.coinmarketcap.com/v1/cryptocurrency/listings/latest';
$parameters = [
'start' => '1',
'limit' => '100',
];
$headers = [
'Accepts: application/json',
'X-CMC_PRO_API_KEY: 7c933642-53f5-4bd0-b310-0a318159b1e1'
];
$qs = http_build_query($parameters); // query string encode the parameters
$request = "{$url}?{$qs}"; // create the request URL
$curl = curl_init(); // Get cURL resource
// Set cURL options
curl_setopt_array($curl, array(
CURLOPT_URL => $request, // set the request URL
CURLOPT_HTTPHEADER => $headers, // set the headers
CURLOPT_RETURNTRANSFER => 1 // ask for raw response instead of bool
));
$response = curl_exec($curl); // Send the request, save the response
// print json decoded response
curl_close($curl); // Close request
$data = json_decode($response)->data ;
$res = ["status" => "200" , "msg" => "currencies list", "currencies" => $data ] ;
return new JsonResponse($res);
}
#[Route('/pub/crypto/list',methods: ['GET'], name: 'currencies_price')]
public function currenciesprice( DocumentManager $doctrine , Request $request ): JsonResponse
{
$pairs = $this->documentManager->createQueryBuilder(SymbolDocument::class)
->field('quoteCurrency')->equals('USDT')
->sort('baseCurrency', 'asc')
->hydrate(false)
->getQuery()
->execute()
->toArray();
$cryptos = $this->documentManager->createQueryBuilder(Price::class)
->field('type')->equals('buy')
->hydrate(false)
->getQuery()
->execute()
->toArray();
$res = ["status" => "200" , "msg" => "currencies list", "currencies" => ($cryptos) , "pairs" => $pairs ] ;
return new JsonResponse($res);
}
#[Route('/api/crypto/list2',methods: ['POST'], name: 'currencies_list_2')]
public function currencieslist2( DocumentManager $doctrine , Request $request ): JsonResponse
{
// $entityManager = $doctrine;
// $symbolRepository = $entityManager->getRepository(BackSymbol::class);
// // Fetch all symbols
// $symbols = $symbolRepository->findAll();
// // Group symbols by 'symbol' field
// $symbolGroups = [];
// foreach ($symbols as $symbol) {
// $symbolName = $symbol->getSymbol();
// $symbolGroups[$symbolName][] = $symbol;
// }
// foreach ($symbolGroups as $symbolName => $symbolList) {
// if (count($symbolList) > 1) {
// $symbolWithIcon = null;
// $symbolWithoutIcon = [];
// foreach ($symbolList as $symbol) {
// if ($symbol->getIcon()) { // Assuming getIcon() returns null if no icon exists
// $symbolWithIcon = $symbol;
// } else {
// $symbolWithoutIcon[] = $symbol;
// }
// }
// // If there is a symbol with an icon, delete all without an icon
// if ($symbolWithIcon) {
// foreach ($symbolWithoutIcon as $symbol) {
// $entityManager->remove($symbol);
// }
// } else {
// // If none have an icon, delete all but one
// array_shift($symbolList); // Keep one and delete the rest
// foreach ($symbolList as $symbol) {
// $entityManager->remove($symbol);
// }
// }
// }
// }
// // Apply deletions
// $entityManager->flush();
$currencies = $this->documentManager->createQueryBuilder(BackSymbol::class)
->field('quoteCurrency')->equals('USDT')
->hydrate(false)
->getQuery()
->execute()
->toArray();
$res = ["status" => "200" , "msg" => "currencies list", "currencies" => $currencies] ;
return new JsonResponse($res);
}
#[Route('/api/crypto/list',methods: ['POST'], name: 'currencies_list')]
public function currencieslist( DocumentManager $doctrine , Request $request ): JsonResponse
{
$currencies = $this->documentManager->createQueryBuilder(SymbolDocument::class)
->field('quoteCurrency')->equals('USDT')
->hydrate(false)
->getQuery()
->execute()
->toArray();
$res = ["status" => "200" , "msg" => "currencies list", "currencies" => $currencies] ;
return new JsonResponse($res);
}
public function currencyadd($name,$slug,$icon,$fee,$tradefee ,$status ): JsonResponse
{
$currency_exist = $this->documentManager->createQueryBuilder(Currencies::class)
->field('slug')->equals($slug)
->getQuery()
->getSingleResult();
if ($currency_exist) {
$currency = $currency_exist ;
}
else {
$currency = new Currencies();
}
$currency->setName($name);
$currency->setSlug($slug);
$currency->setIcon($icon);
$currency->setFee($fee);
$currency->setTradefee($tradefee);
$currency->setStatus($status);
$this->documentManager->persist($currency);
$this->documentManager->flush();
$res = ["status" => "200" , "msg" => "currencies added"] ;
return new JsonResponse($res);
}
public function getcurrenciesfromexchange(){
$need_wallets = [
['name' => 'RIAL', 'slug' => 'rial'],
['name' => 'USDT', 'slug' => 'usdt'],
['name' => 'BTC', 'slug' => 'btc'],
['name' => 'ETH', 'slug' => 'eth'],
['name' => 'DOGE', 'slug' => 'doge'],
['name' => 'ADA', 'slug' => 'ada'],
['name' => 'SOL', 'slug' => 'sol'],
['name' => 'DOT', 'slug' => 'dot'],
['name' => 'DAI', 'slug' => 'dai'],
['name' => 'TRX', 'slug' => 'trx'],
['name' => 'BNB', 'slug' => 'bnb'],
['name' => 'LTC', 'slug' => 'ltc'],
];
foreach($need_wallets as $crypto) {
$this->currencyadd($crypto['name'],$crypto['slug'],'-','10000','0.3','active' );
}
return new Response("ok");
}
}