TokenCreateService.php 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. <?php
  2. namespace Services;
  3. use Libs\BashExecutor;
  4. class TokenCreateService
  5. {
  6. private \PDO $pdo;
  7. private string $easyCliPath;
  8. public function __construct()
  9. {
  10. if (!isset($GLOBALS['pdo']) || !$GLOBALS['pdo'] instanceof \PDO) {
  11. throw new \RuntimeException('Global PDO connection not initialized');
  12. }
  13. $this->pdo = $GLOBALS['pdo'];
  14. $this->easyCliPath = dirname(__DIR__) . '/bin/easycli';
  15. }
  16. public function createToken(
  17. int $tokenCommoditiesAmount,
  18. int $tokenCommoditiesValue,
  19. string $tokenUf,
  20. string $tokenCity,
  21. string $tokenContent,
  22. string $tokenFlag,
  23. int $walletId,
  24. int $chainId,
  25. int $commoditiesId,
  26. int $cprId,
  27. int $userId
  28. ): array {
  29. $mintData = $this->mintToken($tokenContent);
  30. $tokenExternalId = $mintData['tokenId'];
  31. $txHash = $mintData['tx'];
  32. $tokenId = $this->insertToken([
  33. 'token_external_id' => $tokenExternalId,
  34. 'token_commodities_amount' => $tokenCommoditiesAmount,
  35. 'token_commodities_value' => $tokenCommoditiesValue,
  36. 'token_uf' => $tokenUf,
  37. 'token_city' => $tokenCity,
  38. 'token_content' => $tokenContent,
  39. 'token_flag' => $tokenFlag,
  40. 'wallet_id' => $walletId,
  41. 'chain_id' => $chainId,
  42. 'commodities_id' => $commoditiesId,
  43. 'cpr_id' => $cprId,
  44. 'user_id' => $userId,
  45. ]);
  46. return [
  47. 'token_id' => $tokenId,
  48. 'token_external_id' => $tokenExternalId,
  49. 'tx_hash' => $txHash,
  50. 'raw_output' => $mintData['raw_output'],
  51. ];
  52. }
  53. private function mintToken(string $content): array
  54. {
  55. if (!is_file($this->easyCliPath) || !is_executable($this->easyCliPath)) {
  56. throw new \RuntimeException('easycli executable not found or not executable');
  57. }
  58. $command = sprintf('%s token mint %s', escapeshellarg($this->easyCliPath), escapeshellarg($content));
  59. $result = BashExecutor::run($command, 120);
  60. if (($result['exitCode'] ?? 1) !== 0) {
  61. $message = $result['error'] ?: $result['output'] ?: 'Unknown easycli error';
  62. throw new \RuntimeException('easycli mint failed: ' . $message);
  63. }
  64. $combinedOutput = trim(implode("\n", array_filter([
  65. trim($result['output'] ?? ''),
  66. trim($result['error'] ?? ''),
  67. ])));
  68. $tokenId = $this->extractValue('tokenId', $combinedOutput);
  69. $txHash = $this->extractValue('tx', $combinedOutput);
  70. if ($tokenId === null) {
  71. throw new \RuntimeException('Unable to parse tokenId from easycli output: ' . $combinedOutput);
  72. }
  73. return [
  74. 'tokenId' => $tokenId,
  75. 'tx' => $txHash,
  76. 'raw_output' => $combinedOutput,
  77. ];
  78. }
  79. private function extractValue(string $key, string $content): ?string
  80. {
  81. $pattern = sprintf('/%s\s*=\s*([^\s]+)/i', preg_quote($key, '/'));
  82. if (preg_match($pattern, $content, $matches)) {
  83. return trim($matches[1]);
  84. }
  85. return null;
  86. }
  87. private function insertToken(array $data): int
  88. {
  89. $stmt = $this->pdo->prepare(
  90. 'INSERT INTO "token" (
  91. token_external_id,
  92. token_commodities_amount,
  93. token_commodities_value,
  94. token_uf,
  95. token_city,
  96. token_content,
  97. token_flag,
  98. wallet_id,
  99. chain_id,
  100. commodities_id,
  101. cpr_id,
  102. user_id
  103. ) VALUES (
  104. :token_external_id,
  105. :token_commodities_amount,
  106. :token_commodities_value,
  107. :token_uf,
  108. :token_city,
  109. :token_content,
  110. :token_flag,
  111. :wallet_id,
  112. :chain_id,
  113. :commodities_id,
  114. :cpr_id,
  115. :user_id
  116. ) RETURNING token_id'
  117. );
  118. $stmt->execute($data);
  119. return (int) $stmt->fetchColumn();
  120. }
  121. }