Skip to content

Laravel Tron Module - laravel package for working with cryptocurrency Tron: create wallet, create addresses, get balances, send TRX, send TRC-20

Notifications You must be signed in to change notification settings

pavlo-dot-dev/laravel-tron-module

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

23 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Pest Laravel Expectations

Latest Version on Packagist Php Version Php Version Total Downloads

Website Telegram


Laravel Tron Module is a Laravel package for work with cryptocurrency Tron, with the support TRC-20 tokens.It allows you to generate HD wallets using mnemonic phrase, validate addresses, get addresses balances and resources, preview and send TRX/TRC-20 tokens. You can automate the acceptance and withdrawal of cryptocurrency in your application.

You can contact me for help in integrating payment acceptance into your project.

Examples

BIP39: Generate Mnemonic Phrase:

$mnemonic = Tron::mnemonicGenerate(15);
print_r($mnemonic); // string[] length 15 words

BIP39: Validate Mnemonic Phrase:

$mnemonic = 'record jelly ladder exotic hold access test minute target fortune duck disease express damp attend';
$isValid = Tron::mnemonicValidate($mnemonic);
echo $isValid ? 'OK' : 'ERROR';

BIP39: Get seed by Mnemonic Phrase:

$mnemonic = 'record jelly ladder exotic hold access test minute target fortune duck disease express damp attend';
$mnemonicPassphrase = 'passphrase string';
$seed = Tron::mnemonicSeed($mnemonic, $mnemonicPassphrase);
echo $seed; // Seed in hex format

Create HD Wallet:

$name = 'my-wallet';
$password = 'password for encrypt mnemonic, seed and private keys';
$mnemonic = 'record jelly ladder exotic hold access test minute target fortune duck disease express damp attend';
$mnemonicPassphrase = null;

$tronWallet = Tron::createWallet($name, $password, $mnemonic, $mnemonicPassphrase);
$tronWallet->save();

Unlock HD Wallet:

$password = 'password for encrypt mnemonic, seed and private keys';

$tronWallet = TronWallet::first();
$isUnlocked = $tronWallet->encrypted()->unlock($password);
echo $isUnlocked ? 'WALLET UNLOCKED' : 'INCORRECT PASSWORD';

echo $tronWallet->plainMnemonic; // Print mnemonic phrase

Generate Address:

$tronWallet = TronWallet::first();
$tronWallet->encrypted()->unlock($password);

$index = 0; // Address index (if null - automatic)
$tronAddress = Tron::createAddress($tronWallet, $index);
$tronAddress->save();

echo $tronAddress->address; // Print Address
echo $tronAddress->private_key; // Print private key

Import watch-only address:

$wallet = TronWallet::first();
$address = Tron::importAddress($wallet, 'my tron address');
$address->save();

Add TRC-20 token for tracking:

$contractAddress = 'TR7NHqjeKQxGTCi8q8ZY4pL8otSzgjLj6t'; // Contract Address Tether USDT
$tronTRC20 = Tron::createTRC20($contractAddress);
$tronTRC20->save();

$balanceOfAddress = 'THPvaUhoh2Qn2y9THCZML3H815hhFhn5YC';
echo $tronTRC20->contract()->balanceOf($balanceOfAddress); // Get TRC-20 Token balance of address

Get Address info (balances + resources):

$address = 'THPvaUhoh2Qn2y9THCZML3H815hhFhn5YC';

$getAccount = Tron::api()->getAccount($address);
print_r($getAccount->toArray());

$getAccountResources = Tron::api()->getAccountResources($address);
print_r($getAccountResources->toArray());

Validate Address:

$address = 'THPvaUhoh2Qn2y9THCZML3H815hhFhn5YC';

$isValid = Tron::api()->validateAddress($address); // bool

Get Address Transfers (only TRX):

$address = 'THPvaUhoh2Qn2y9THCZML3H815hhFhn5YC';

$transfers = Tron::api()->getTransfers($address)->limit(200);

foreach( $transfers as $transfer ) {
    print_r($transfer->toArray());
}

Get Address TRC-20 Transfers:

$address = 'THPvaUhoh2Qn2y9THCZML3H815hhFhn5YC';

$transfers = Tron::api()->getTRC20Transfers($address)->limit(200);

foreach( $transfers as $transfer ) {
    print_r($transfer->toArray());
}

Get Transaction Info:

$txid = '...';
$info = Tron::api()->getTransactionInfo($txid);
print_r($info->toArray());

Create TRX transfer, preview and send:

$from = 'THPvaUhoh2Qn2y9THCZML3H815hhFhn5YC';
$to = 'TR7NHqjeKQxGTCi8q8ZY4pL8otSzgjLj6t';
$amount = 1;

$walletPassword = 'here wallet password';
$wallet = TronWallet::first();
$wallet->encrypted()->unlock($walletPassword);

$address = $wallet->addresses->first();
$privateKey = $wallet->encrypted()->decode($address->private_key);

$transfer = Tron::api()->transfer($from, $to, $amount);
$preview = $transfer->preview();
print_r($preview->toArray());

$send = $transfer->send($privateKey);
print_r($send->toArray());

Create TRC-20 transfer, preview and send:

$contractAddress = 'TR7NHqjeKQxGTCi8q8ZY4pL8otSzgjLj6t';
$from = 'THPvaUhoh2Qn2y9THCZML3H815hhFhn5YC';
$to = 'TR7NHqjeKQxGTCi8q8ZY4pL8otSzgjLj6t';
$amount = 1;

$walletPassword = 'here wallet password';
$wallet = TronWallet::first();
$wallet->encrypted()->unlock($walletPassword);

$address = $wallet->addresses->first();
$privateKey = $wallet->encrypted()->decode($address->private_key);

$transfer = Tron::api()->transferTRC20($contractAddress, $from, $to, $amount);
$preview = $transfer->preview();
print_r($preview->toArray());

$send = $transfer->send($privateKey);
print_r($send->toArray());

Helpers

Convert Base58 address to Hex:

$addressBase58 = 'TR7NHqjeKQxGTCi8q8ZY4pL8otSzgjLj6t';
$addressHex = AddressHelper::toHex($addressBase58);

Convert Hex address to Base58:

$addressHex = '11234....412412';
$addressBase58 = AddressHelper::toBase58($addressHex);

Install

> composer require pavlo-dot-dev/laravel-tron-module
> php artisan vendor:publish --tag=tron-config
> php artisan migrate

In file app/Console/Kernel in method schedule(Schedule $schedule) add

$schedule->command('tron:scan')->everyMinute();

In .env file add:

TRONGRID_API_KEY="..."

Commands

Scan transactions and update balances:

> php artisan tron:scan

Create TRC-20 Token:

> php artisan tron:new-trc20

Create Wallet:

> php artisan tron:new-wallet

Generate Address:

> php artisan tron:generate-address

WebHook

You can set up a WebHook that will be called when a new incoming or outgoing TRX/TRC-20 transfer is detected.

In file config/tron.php you can set param:

'webhook_handler' => \PavloDotDev\LaravelTronModule\Handlers\EmptyWebhookHandler::class,

Example WebHook handler:

class EmptyWebhookHandler implements WebhookHandlerInterface
{
    public function handle(TronAddress $address, TronTransaction $transaction): void
    {
        Log::error('NEW TRANSACTION FOR ADDRESS '.$address->id.' = '.$transaction->txid);
    }
}

Requirements

The following versions of PHP are supported by this version.

  • PHP 8.1 and older
  • PHP Extensions: Decimal, GMP, BCMath, CType.
  • Laravel Queues

About

Laravel Tron Module - laravel package for working with cryptocurrency Tron: create wallet, create addresses, get balances, send TRX, send TRC-20

Resources

Stars

Watchers

Forks

Packages

No packages published

Languages