PHP code example of jackillll / tron

1. Go to this page and download the library: Download jackillll/tron library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

jackillll / tron example snippets


'network' => 'mainnet',        // Network name: mainnet, testnet, nile
'use_solidity' => false,       // Boolean: true for Solidity node, false for Full node
'networks' => [
    'mainnet' => [
        'host' => 'https://api.trongrid.io',
        'explorer' => 'https://tronscan.org'
    ],
    'testnet' => [
        'host' => 'https://api.shasta.trongrid.io',
        'explorer' => 'https://shasta.tronscan.org'
    ],
    'nile' => [
        'host' => 'https://nile.trongrid.io',
        'explorer' => 'https://nile.tronscan.org'
    ]
]



namespace App\Http\Controllers;

use Jackillll\Tron\Tron;

class TronController extends Controller
{
    protected $tron;

    public function __construct(Tron $tron)
    {
        $this->tron = $tron;
    }

    public function getBalance($address)
    {
        return $this->tron->getBalance($address, true);
    }

    public function sendTrx($to, $amount)
    {
        return $this->tron->send($to, $amount);
    }
}



use Jackillll\Tron\Facades\Tron;

// Get account information
$account = Tron::getAccount('TR7NHqjeKQxGTCi8q8ZY4pL8otSzgjLj6t');

// Get balance
$balance = Tron::getBalance('TR7NHqjeKQxGTCi8q8ZY4pL8otSzgjLj6t', true);

// Send TRX
$result = Tron::send('TTo_Address_Here', 1.5);

// Get latest blocks
$blocks = Tron::getLatestBlocks(10);



namespace App\Services;

use Jackillll\Tron\Tron;

class TronService
{
    protected $tron;

    public function __construct(Tron $tron)
    {
        $this->tron = $tron;
    }

    public function getUserBalance($address)
    {
        try {
            return $this->tron->getBalance($address, true);
        } catch (\Exception $e) {
            \Log::error('Tron balance error: ' . $e->getMessage());
            return 0;
        }
    }

    public function transferTrx($fromPrivateKey, $toAddress, $amount)
    {
        try {
            $this->tron->setPrivateKey($fromPrivateKey);
            return $this->tron->send($toAddress, $amount);
        } catch (\Exception $e) {
            \Log::error('Tron transfer error: ' . $e->getMessage());
            return false;
        }
    }
}



ackillll\Tron\TronConfig;

// Method 1: Simple usage with defaults
$tron = TronConfig::create(); // mainnet + fullnode

// Method 2: Specify network and node type
$tron = TronConfig::create('mainnet', true); // mainnet + solidity node
$tron = TronConfig::create('testnet', false); // testnet + fullnode

// Method 3: With custom headers
$tron = TronConfig::create('mainnet', false, [
    'TRON-PRO-API-KEY' => 'your-api-key'
], 30000);

// Method 4: Using custom configuration
$config = [
    'network' => 'mainnet',
    'use_solidity' => false,
    'timeout' => 30000,
    'headers' => [
        'TRON-PRO-API-KEY' => 'your-api-key'
    ],
    'networks' => [
        'mainnet' => [
            'host' => 'https://api.trongrid.io',
            'explorer' => 'https://tronscan.org'
        ]
    ]
];

$tron = TronConfig::createFromConfig($config);



ackillll\Tron\Tron;
use Jackillll\Tron\Provider\HttpProvider;

// Create providers
$fullNode = new HttpProvider('https://api.trongrid.io');
$solidityNode = new HttpProvider('https://api.trongrid.io');
$eventServer = new HttpProvider('https://api.trongrid.io');

// Create Tron instance
try {
    $tron = new Tron($fullNode, $solidityNode, $eventServer);
} catch (\Jackillll\Tron\Exception\TronException $e) {
    exit($e->getMessage());
}



ackillll\Tron\TronConfig;

try {
    // Initialize Tron with mainnet
    $tron = TronConfig::create('mainnet', false, [
        'TRON-PRO-API-KEY' => 'your-api-key'
    ]);

    // Set private key for transactions
    $tron->setPrivateKey('your-private-key');
    $tron->setAddress('your-address');

    // Get account balance
    $balance = $tron->getBalance('TR7NHqjeKQxGTCi8q8ZY4pL8otSzgjLj6t', true);
    echo "Balance: " . $balance . " TRX\n";

    // Get account information
    $account = $tron->getAccount('TR7NHqjeKQxGTCi8q8ZY4pL8otSzgjLj6t');
    echo "Account Type: " . $account['type'] . "\n";

    // Send TRX (

// Different network instances
$mainnetTron = TronConfig::create('mainnet');
$testnetTron = TronConfig::create('testnet');
$nileTron = TronConfig::create('nile');

// Different node types
$fullNodeTron = TronConfig::create('mainnet', false);
$solidityNodeTron = TronConfig::create('mainnet', true);



ackillll\Tron\TronConfig;

// Quick setup with new configuration
$tron = TronConfig::create('mainnet', false, [
    'TRON-PRO-API-KEY' => 'your-api-key'
]);

// Generate new address
$generateAddress = $tron->createAccount();
echo $generateAddress['address']; // TKttnV3FSY1iEoAwB4N52WK2DxdV94KpDd
echo $generateAddress['privateKey']; // 7d507c48da396a6b0dbe09f21f729dd2ba29d9a5e5bf8edde5fc9144c7ebe261
echo $generateAddress['publicKey']; // 0337e1a88b108c438266c3ca5c5a73a8b6803443c4f7e1b1d140b11106a4e7d88f

// Get account balance
$balance = $tron->getBalance('TKttnV3FSY1iEoAwB4N52WK2DxdV94KpDd', true);

// Send TRX
$tron->setPrivateKey('7d507c48da396a6b0dbe09f21f729dd2ba29d9a5e5bf8edde5fc9144c7ebe261');
$tron->setAddress('TKttnV3FSY1iEoAwB4N52WK2DxdV94KpDd');

$transfer = $tron->send('TLsV52sRDL79HXGGm9yzwKibb6BeruhUzy', 0.1);



ackillll\Tron\Tron;
use Jackillll\Tron\Provider\HttpProvider;

$fullNode = new HttpProvider('https://api.trongrid.io');
$solidityNode = new HttpProvider('https://api.trongrid.io');
$eventServer = new HttpProvider('https://api.trongrid.io');

try {
    $tron = new Tron($fullNode, $solidityNode, $eventServer);
} catch (\Jackillll\Tron\Exception\TronException $e) {
    exit($e->getMessage());
}

// Use the same way as above...

// Get contract instance
$contract = $tron->contract('TR7NHqjeKQxGTCi8q8ZY4pL8otSzgjLj6t'); // USDT contract

// Call contract method
$result = $contract->call('balanceOf', 'TAddress...');

// Send contract transaction
$tron->setPrivateKey('your-private-key');
$result = $contract->send('transfer', 'TToAddress...', 1000000); // 1 USDT

// Create transaction
$transaction = $tron->getTransactionBuilder()->sendTrx('TTo...', 1000000, 'TFrom...');

// Sign transaction
$signedTransaction = $tron->signTransaction($transaction);

// Broadcast transaction
$result = $tron->sendRawTransaction($signedTransaction);

// Get latest blocks
$blocks = $tron->getLatestBlocks(10);

// Get block by number
$block = $tron->getBlock(12345);

// Get transaction by ID
$transaction = $tron->getTransaction('transaction_id_here');

// Get account transactions
$transactions = $tron->getTransactionsFromAddress('TAddress...', 50);

// Old way (still supported)
'host' => 'https://api.trongrid.io',
'node_type' => 'fullnode',

// New way (recommended)
'network' => 'mainnet',
'use_solidity' => false,

use Jackillll\Tron\Exception\TronException;

try {
    $tron = TronConfig::create('mainnet');
    $balance = $tron->getBalance('invalid_address');
} catch (TronException $e) {
    echo "Tron API Error: " . $e->getMessage();
} catch (\Exception $e) {
    echo "General Error: " . $e->getMessage();
}
bash
php artisan vendor:publish --provider="Jackillll\Tron\TronServiceProvider"