src/Controller/CurrenciesController.php line 33

Open in your IDE?
  1. <?php
  2. namespace App\Controller;
  3. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  4. use Symfony\Component\HttpFoundation\Response;
  5. use Symfony\Component\Routing\Annotation\Route;
  6. use App\Document\Currencies;
  7. use App\Document\Price;
  8. use Symfony\Component\HttpFoundation\JsonResponse ;
  9. use Symfony\Component\HttpFoundation\Request;
  10. use Doctrine\ODM\MongoDB\DocumentManager;
  11. use Symfony\Contracts\HttpClient\HttpClientInterface;
  12. use App\Document\Symbol as SymbolDocument;
  13. use App\Document\Symbol2 as SymbolDocument2;
  14. use App\Document\BackSymbol;
  15. class CurrenciesController extends AbstractController
  16. {
  17.     private $documentManager;
  18.     private $client;
  19.     public function __constructDocumentManager $documentManager HttpClientInterface $client  ) {
  20.         
  21.           $this->documentManager $documentManager;
  22.           $this->httpclient $client ;
  23.         
  24.         
  25.     }
  26.     #[Route('/pub/crypto/alllist',methods: ['GET'], name'currencies_all_price')]
  27.     public function currenciesallpriceDocumentManager  $doctrine Request $request ): JsonResponse
  28.     {
  29. // $list = [
  30. //         "BTC",
  31. //         "ETH",
  32. //         "USDT",
  33. //         "BNB",
  34. //         "SOL",
  35. //         "XRP",
  36. //         "USDC",
  37. //         "ADA",
  38. //         "AVAX",
  39. //         "DOGE",
  40. //         "TRX",
  41. //         "DOT",
  42. //         "LINK",
  43. //         "MATIC",
  44. //         "TON",
  45. //         "ICP",
  46. //         "SHIB",
  47. //         "DAI",
  48. //         "LTC",
  49. //         "BCH",
  50. //         "ETC",
  51. //         "UNI",
  52. //         "ATOM",
  53. //         "LEO",
  54. //         "OP",
  55. //         "NEAR",
  56. //         "XLM",
  57. //         "OKB",
  58. //         "APT",
  59. //         "INJ",
  60. //         "TIA",
  61. //         "FIL",
  62. //         "LDO",
  63. //         "XMR",
  64. //         "IMX",
  65. //         "ARB",
  66. //         "HBAR",
  67. //         "KAS",
  68. //         "STX",
  69. //         "CRO",
  70. //         "MNT",
  71. //         "VET",
  72. //         "TUSD",
  73. //         "FDUSD",
  74. //         "MKR",
  75. //         "BSV",
  76. //         "SEI",
  77. //         "GRT",
  78. //         "ORDI",
  79. //         "RUNE",
  80. //         "ALGO",
  81. //         "AAVE",
  82. //         "RNDR",
  83. //         "QNT",
  84. //         "EGLD",
  85. //         "SUI",
  86. //         "1000SATS",
  87. //         "MINA",
  88. //         "FLOW",
  89. //         "HNT",
  90. //         "SNX",
  91. //         "FTM",
  92. //         "XTZ",
  93. //         "AXS",
  94. //         "SAND",
  95. //         "THETA",
  96. //         "BTT",
  97. //         "FTT",
  98. //         "KCS",
  99. //         "BEAM",
  100. //         "WEMIX",
  101. //         "BONK",
  102. //         "OSMO",
  103. //         "NEO",
  104. //         "MANA",
  105. //         "EOS",
  106. //         "BGB",
  107. //         "KAVA",
  108. //         "ASTR",
  109. //         "ENS",
  110. //         "CHZ",
  111. //         "WOO",
  112. //         "IOTA",
  113. //         "ROSE",
  114. //         "GALA",
  115. //         "CAKE",
  116. //         "BLUR",
  117. //         "USDD",
  118. //         "LUNC",
  119. //         "XDC",
  120. //         "AR",
  121. //         "XEC",
  122. //         "CFX",
  123. //         "RPL",
  124. //         "FXS",
  125. //         "KLAY",
  126. //         "AKT",
  127. //         "CRV",
  128. //         "SC",
  129. //         "FLR"
  130. //     ] ;
  131.     
  132.     
  133. //     foreach ($list as $crypto) {
  134. //          $url = 'https://api.kucoin.com/api/v1/market/stats?symbol='.$crypto.'-USDT';
  135. //                  $response = $this->httpclient->request(
  136. //             'GET',
  137. //             $url
  138. //         );
  139. //              $j[] = json_decode($response->getContent()) ;
  140. // }
  141. $url 'https://pro-api.coinmarketcap.com/v1/cryptocurrency/listings/latest';
  142. $parameters = [
  143.   'start' => '1',
  144.   'limit' => '100',
  145. ];
  146. $headers = [
  147.   'Accepts: application/json',
  148.   'X-CMC_PRO_API_KEY: 7c933642-53f5-4bd0-b310-0a318159b1e1'
  149. ];
  150. $qs http_build_query($parameters); // query string encode the parameters
  151. $request "{$url}?{$qs}"// create the request URL
  152. $curl curl_init(); // Get cURL resource
  153. // Set cURL options
  154. curl_setopt_array($curl, array(
  155.   CURLOPT_URL => $request,            // set the request URL
  156.   CURLOPT_HTTPHEADER => $headers,     // set the headers 
  157.   CURLOPT_RETURNTRANSFER => 1         // ask for raw response instead of bool
  158. ));
  159. $response curl_exec($curl); // Send the request, save the response
  160.  // print json decoded response
  161. curl_close($curl); // Close request
  162. $data json_decode($response)->data ;
  163.         $res = ["status" => "200" "msg" => "currencies list""currencies" =>  $data ] ;
  164.         return new JsonResponse($res);
  165.     }
  166.     #[Route('/pub/crypto/list',methods: ['GET'], name'currencies_price')]
  167.     public function currenciespriceDocumentManager  $doctrine Request $request ): JsonResponse
  168.     {
  169.         
  170.         
  171.         
  172.         
  173.         
  174.         
  175.                     
  176. $pairs $this->documentManager->createQueryBuilder(SymbolDocument::class)
  177.      ->field('quoteCurrency')->equals('USDT')
  178.              ->sort('baseCurrency''asc')
  179.     ->hydrate(false)
  180.     ->getQuery()
  181.     ->execute()
  182.     ->toArray();
  183.         
  184.         
  185. $cryptos =   $this->documentManager->createQueryBuilder(Price::class)
  186.        ->field('type')->equals('buy')
  187.   
  188.        ->hydrate(false)
  189.        ->getQuery()
  190.        ->execute()
  191.        ->toArray();
  192.       
  193.         $res = ["status" => "200" "msg" => "currencies list""currencies" =>  ($cryptos) , "pairs" => $pairs     ] ;
  194.         return new JsonResponse($res);
  195.     }
  196.     #[Route('/api/crypto/list2',methods: ['POST'], name'currencies_list_2')]
  197.     public function currencieslist2DocumentManager  $doctrine Request $request ): JsonResponse
  198.     {
  199. // $entityManager = $doctrine;
  200. // $symbolRepository = $entityManager->getRepository(BackSymbol::class);
  201. // // Fetch all symbols
  202. // $symbols = $symbolRepository->findAll();
  203. // // Group symbols by 'symbol' field
  204. // $symbolGroups = [];
  205. // foreach ($symbols as $symbol) {
  206. //     $symbolName = $symbol->getSymbol();
  207. //     $symbolGroups[$symbolName][] = $symbol;
  208. // }
  209. // foreach ($symbolGroups as $symbolName => $symbolList) {
  210. //     if (count($symbolList) > 1) {
  211. //         $symbolWithIcon = null;
  212. //         $symbolWithoutIcon = [];
  213. //         foreach ($symbolList as $symbol) {
  214. //             if ($symbol->getIcon()) { // Assuming getIcon() returns null if no icon exists
  215. //                 $symbolWithIcon = $symbol;
  216. //             } else {
  217. //                 $symbolWithoutIcon[] = $symbol;
  218. //             }
  219. //         }
  220. //         // If there is a symbol with an icon, delete all without an icon
  221. //         if ($symbolWithIcon) {
  222. //             foreach ($symbolWithoutIcon as $symbol) {
  223. //                 $entityManager->remove($symbol);
  224. //             }
  225. //         } else {
  226. //             // If none have an icon, delete all but one
  227. //             array_shift($symbolList); // Keep one and delete the rest
  228. //             foreach ($symbolList as $symbol) {
  229. //                 $entityManager->remove($symbol);
  230. //             }
  231. //         }
  232. //     }
  233. // }
  234. // // Apply deletions
  235. // $entityManager->flush();
  236. $currencies $this->documentManager->createQueryBuilder(BackSymbol::class)
  237.      ->field('quoteCurrency')->equals('USDT')
  238.     ->hydrate(false)
  239.     ->getQuery()
  240.     ->execute()
  241.     ->toArray();
  242.         
  243.         $res = ["status" => "200" "msg" => "currencies list""currencies" => $currencies] ;
  244.         return new JsonResponse($res);
  245.     }
  246.     #[Route('/api/crypto/list',methods: ['POST'], name'currencies_list')]
  247.     public function currencieslistDocumentManager  $doctrine Request $request ): JsonResponse
  248.     {
  249. $currencies $this->documentManager->createQueryBuilder(SymbolDocument::class)
  250.      ->field('quoteCurrency')->equals('USDT')
  251.     ->hydrate(false)
  252.     ->getQuery()
  253.     ->execute()
  254.     ->toArray();
  255.         
  256.         $res = ["status" => "200" "msg" => "currencies list""currencies" => $currencies] ;
  257.         return new JsonResponse($res);
  258.     }
  259.     public function currencyadd($name,$slug,$icon,$fee,$tradefee ,$status ): JsonResponse
  260.     {
  261.         
  262.         
  263.         
  264.          $currency_exist $this->documentManager->createQueryBuilder(Currencies::class)
  265.         ->field('slug')->equals($slug)
  266.         ->getQuery()
  267.         ->getSingleResult();
  268.   
  269.         if ($currency_exist) {
  270.             
  271.             $currency $currency_exist ;
  272.             
  273.         }
  274.          else {
  275.              
  276.               $currency = new Currencies();
  277.              
  278.          }
  279.         
  280.         
  281.       
  282.      
  283.        
  284.         $currency->setName($name);
  285.         $currency->setSlug($slug);
  286.         $currency->setIcon($icon);
  287.         $currency->setFee($fee);
  288.         $currency->setTradefee($tradefee);
  289.         $currency->setStatus($status);
  290.        
  291.         $this->documentManager->persist($currency);
  292.         $this->documentManager->flush();
  293.         $res = ["status" => "200" "msg" => "currencies added"] ;
  294.         return new JsonResponse($res);
  295.     }
  296.  
  297.     public function getcurrenciesfromexchange(){
  298.      $need_wallets = [
  299.     ['name' => 'RIAL''slug' => 'rial'],
  300.     ['name' => 'USDT''slug' => 'usdt'],
  301.     ['name' => 'BTC''slug' => 'btc'],
  302.     ['name' => 'ETH''slug' => 'eth'],
  303.     ['name' => 'DOGE''slug' => 'doge'],
  304.     ['name' => 'ADA''slug' => 'ada'],
  305.     ['name' => 'SOL''slug' => 'sol'],
  306.     ['name' => 'DOT''slug' => 'dot'],
  307.     ['name' => 'DAI''slug' => 'dai'],
  308.     ['name' => 'TRX''slug' => 'trx'],
  309.     ['name' => 'BNB''slug' => 'bnb'],
  310.     ['name' => 'LTC''slug' => 'ltc'],
  311. ];
  312.    
  313.     foreach($need_wallets as $crypto) {
  314.   
  315.   
  316.   
  317.        $this->currencyadd($crypto['name'],$crypto['slug'],'-','10000','0.3','active' );
  318.   
  319.   
  320.     }
  321.   
  322.    
  323.           return new Response("ok");
  324.     }
  325. }