TokenTransferService.php 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. <?php
  2. namespace Services;
  3. use Libs\BashExecutor;
  4. class TokenTransferService
  5. {
  6. private string $easyCliPath;
  7. public function __construct(?string $easyCliPath = null)
  8. {
  9. $this->easyCliPath = $easyCliPath ?? dirname(__DIR__) . '/bin/easycli';
  10. }
  11. public function transferFrom(string $fromAddress, string $toAddress, string $tokenExternalId): array
  12. {
  13. $from = trim($fromAddress);
  14. $to = trim($toAddress);
  15. $token = trim($tokenExternalId);
  16. if ($from === '' || $to === '' || $token === '') {
  17. throw new \InvalidArgumentException('Parâmetros inválidos para transferência de token.');
  18. }
  19. if (!is_file($this->easyCliPath) || !is_executable($this->easyCliPath)) {
  20. throw new \RuntimeException('easycli executable not found or not executable');
  21. }
  22. $command = sprintf(
  23. '%s token transferFrom %s %s %s',
  24. escapeshellarg($this->easyCliPath),
  25. escapeshellarg($from),
  26. escapeshellarg($to),
  27. escapeshellarg($token)
  28. );
  29. $result = BashExecutor::run($command, 120);
  30. if (($result['exitCode'] ?? 1) !== 0) {
  31. $message = $result['error'] ?: $result['output'] ?: 'Unknown easycli error';
  32. throw new \RuntimeException('easycli transferFrom failed: ' . $message);
  33. }
  34. return [
  35. 'output' => trim((string)($result['output'] ?? '')),
  36. 'error' => trim((string)($result['error'] ?? '')),
  37. ];
  38. }
  39. }