PHP WebShell

Текущая директория: /var/www/bitcardoApp/models/crypto

Просмотр файла: tron_send_usdt.php

<?php

namespace Models\Crypto;

class TronSendUsdt
{
    private $config;
    private $network;

    public function __construct()
    {
        $this->config = include __DIR__ . '/../../config/tron_config.php';
        $this->network = $this->config['network'];
    }

    /**
     * Send USDT TRC20
     * @param string $privateKey Sender private key
     * @param string $fromAddress Sender address
     * @param string $toAddress Receiver address
     * @param int $amount Raw amount (USDT has 6 decimals)
     */
    public function send($privateKey, $fromAddress, $toAddress, $amount)
    {
        $contract = $this->config['usdt_contract'];

        // TRC20 transfer method ID: a9059cbb (transfer(address,uint256))
        $methodId = "a9059cbb";

        // Address must be 64 hex chars padded
        $hexToAddress = str_pad(substr($this->toHex($toAddress), 0, 64), 64, "0", STR_PAD_LEFT);

        // Amount padded to 64 chars
        $hexAmount = str_pad(dechex($amount), 64, "0", STR_PAD_LEFT);

        $data = $methodId . $hexToAddress . $hexAmount;

        // Build trigger smart contract payload
        $payload = [
            "contract_address" => $this->toHex($contract),
            "function_selector" => "transfer(address,uint256)",
            "parameter" => $data,
            "owner_address" => $this->toHex($fromAddress)
        ];

        $url = $this->network . "/wallet/triggersmartcontract";

        $trigger = $this->post($url, $payload);

        if (!isset($trigger['transaction'])) {
            return ['error' => 'Failed to trigger USDT contract'];
        }

        $transaction = $trigger['transaction'];

        // Sign transaction
        $signed = $this->signTransaction($transaction, $privateKey);

        // Broadcast
        $broadcast = $this->post($this->network . "/wallet/broadcasttransaction", $signed);

        return $broadcast;
    }

    private function toHex($address)
    {
        $decoded = base58check_decode($address);
        return bin2hex($decoded);
    }

    private function signTransaction($transaction, $privateKey)
    {
        $signature = sign_with_private_key(json_encode($transaction['raw_data']), $privateKey);
        $transaction['signature'] = [$signature];
        return $transaction;
    }

    private function post($url, $payload)
    {
        $ch = curl_init();

        curl_setopt_array($ch, [
            CURLOPT_URL            => $url,
            CURLOPT_RETURNTRANSFER => true,
            CURLOPT_POST           => true,
            CURLOPT_POSTFIELDS     => json_encode($payload),
            CURLOPT_HTTPHEADER     => [
                "Content-Type: application/json",
                "TRON-PRO-API-KEY: " . $this->config['api_key']
            ],
        ]);

        $result = curl_exec($ch);
        curl_close($ch);

        return json_decode($result, true);
    }
}

Выполнить команду


Для локальной разработки. Не используйте в интернете!