| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849 |
- <?php
- namespace Services;
- use Libs\BashExecutor;
- class TokenTransferService
- {
- private string $easyCliPath;
- public function __construct(?string $easyCliPath = null)
- {
- $this->easyCliPath = $easyCliPath ?? dirname(__DIR__) . '/bin/easycli';
- }
- public function transferFrom(string $fromAddress, string $toAddress, string $tokenExternalId): array
- {
- $from = trim($fromAddress);
- $to = trim($toAddress);
- $token = trim($tokenExternalId);
- if ($from === '' || $to === '' || $token === '') {
- throw new \InvalidArgumentException('Parâmetros inválidos para transferência de token.');
- }
- if (!is_file($this->easyCliPath) || !is_executable($this->easyCliPath)) {
- throw new \RuntimeException('easycli executable not found or not executable');
- }
- $command = sprintf(
- '%s token transferFrom %s %s %s',
- escapeshellarg($this->easyCliPath),
- escapeshellarg($from),
- escapeshellarg($to),
- escapeshellarg($token)
- );
- $result = BashExecutor::run($command, 120);
- if (($result['exitCode'] ?? 1) !== 0) {
- $message = $result['error'] ?: $result['output'] ?: 'Unknown easycli error';
- throw new \RuntimeException('easycli transferFrom failed: ' . $message);
- }
- return [
- 'output' => trim((string)($result['output'] ?? '')),
- 'error' => trim((string)($result['error'] ?? '')),
- ];
- }
- }
|