সম্পূর্ণ পিএইচপি ইন্টিগ্রেশন ব্যবহার করে iexbase/tron-api লাইব্রেরি। প্রতিটি ধাপ একটি স্বতন্ত্র কোড ব্লক যা আপনি আপনার প্রজেক্টে কপি করতে পারেন। যেকোনো পিএইচপি ফ্রেমওয়ার্ক বা সাধারণ পিএইচপি-র সাথে কাজ করে।
পূর্বশর্তসমূহ: পিএইচপি ৭.৪+, কম্পোজার, ext-gmp ও ext-bcmath সক্রিয়, একটি অর্থায়িত Tron ওয়ালেট।
পিএইচপি-তে সাইন ইন করার বিষয়ে: TronEnergy API জন্য একটি ওয়ালেট স্বাক্ষর প্রয়োজন যা ব্যবহার করে tronWeb.trx.signMessageV2(). iexbase/tron-api লাইব্রেরিটি এই নির্দিষ্ট সাইনিং প্রিমিটিভটি প্রদান করে না, তাই এই নির্দেশিকাটি সাইনিং ধাপের জন্য একটি ছোট Node.js হেল্পার ব্যবহার করে। বাকি সবকিছু ( TRX পাঠানো, API কল করা, USDT পাঠানো) PHP-তেই থাকে। যদি আপনার কাছে একটি সম্পূর্ণ-PHP সাইনিং সলিউশন থাকে যা একটি বৈধ signMessageV2 সিগনেচার তৈরি করে, তাহলে আপনি অন্য কোনো ধাপ পরিবর্তন না করেই হেল্পারটি বদলে নিতে পারেন।
প্রবাহ
কোনো API কী নেই। কোনো সাইন-আপের প্রয়োজন নেই। আপনার কোড অন-চেইনে TronEnergy পেমেন্ট অ্যাড্রেসে TRX পাঠায়, মালিকানার প্রমাণস্বরূপ একটি মেসেজে স্বাক্ষর করে, এবং তারপর ডেলিগেশনটি ক্লেইম করে। Energy প্রায় ৩ সেকেন্ডের মধ্যে পৌঁছে যায়। এরপর আপনার কোড সেই ডেলিগেট করা এনার্জি ব্যবহার করে USDT পাঠায়।
1
TRX পাঠান
অন-চেইন পেমেন্ট অ্যাড্রেসে ৪ বা তার বেশি TRX পাঠান (সর্বনিম্ন ৪, সর্বোচ্চ ১০০০)।
2
চিহ্ন
আপনিই প্রেরক তা প্রমাণ করতে tx_hash:delegate_to-তে স্বাক্ষর করুন।
3
দাবি
ট্রানজ্যাকশন হ্যাশ এবং সিগনেচার সহ POST /delegate রিকোয়েস্টটি পাঠান। Energy আসতে প্রায় ৩ সেকেন্ড সময় লাগবে।
মূল্য নির্ধারণ রৈখিক: প্রতি TRX এ ১৬,২৫০ Energy । Minimum order 4 TRX (65,000 Energy — one standard USDT transfer), maximum 1,000 TRX (16.25M Energy). The amount you send determines exactly how much Energy is delegated back — no tiers, no packages. For a single standard transfer, use $trxAmount = 4. For a new-wallet transfer, use 8. For batch work, use more. The code below uses a $trxAmount variable so you can change it in one place.
ধাপে ধাপে
১. ইনস্টল করুন
composer require iexbase/tron-api
২. সেটআপ
require_once 'vendor/autoload.php';
use IEXBase\TronAPI\Tron;
$tron = new Tron();
$tron->setPrivateKey('YOUR_PRIVATE_KEY');
$tron->setAddress('YOUR_WALLET_ADDRESS');
$api = 'https://api.tronnrg.com';
$addr = 'TFqUiCu1JwLHHnBNeaaVKH7Csm4aA3YhZx'; // API payment address
$usdt = 'TR7NHqjeKQxGTCi8q8ZY4pL8otSzgjLj6t'; // USDT contract
কখনোই ক্রেডেনশিয়াল হার্ডকোড করবেন না। Use environment variables or a secrets manager. The example above is for illustration only.
৩. পেমেন্টের তথ্য জানুন (ঐচ্ছিক)
// Get pricing and payment address (optional, energy is always available)
$supply = json_decode(
file_get_contents("${api}/supply"),
true
);
echo "Pay to: " . $supply['pay_to'] . "\n";
echo "Energy per TRX: " . $supply['energy_per_trx'] . "\n";
৪. TRX পাঠান
// Send TRX to the API payment address — pricing is linear.
// 16,250 energy per TRX. Min 4 TRX, max 1,000 TRX.
// $trxAmount = 4 → 65,000 energy (standard USDT transfer)
// $trxAmount = 8 → 130,000 energy (new wallet transfer)
// $trxAmount = 40 → 650,000 energy (10 standard transfers)
// $trxAmount = 1000 → 16,250,000 energy (max)
$trxAmount = 4;
$payment = $tron->sendTrx($addr, $trxAmount);
if (!isset($payment['result']) || !$payment['result']) {
throw new Exception('TRX transfer failed');
}
$txHash = $payment['txid'];
echo "Payment sent: ${txHash}\n";
৫. বার্তাটিতে স্বাক্ষর করুন।
API জন্য এমন একটি সিগনেচার প্রয়োজন যা প্রমাণ করে যে, যে ওয়ালেটটি TRX পাঠিয়েছে, সেটিই ডেলিগেশনের জন্য অনুরোধ করছে। আমরা একটি ছোট Node.js হেল্পারের মাধ্যমে এটি করে থাকি। এটিকে এভাবে সেভ করুন: sign.js আপনার PHP ফাইলের পাশে:
// Usage: node sign.js <tx_hash> <delegate_to>
// Outputs the signature to stdout. Reads private key from TRON_PRIVATE_KEY env var.
const { TronWeb } = require('tronweb');
const [, , txHash, delegateTo] = process.argv;
const tronWeb = new TronWeb({
fullHost: 'https://api.trongrid.io',
privateKey: process.env.TRON_PRIVATE_KEY,
});
tronWeb.trx.signMessageV2(`${txHash}:${delegateTo}`)
.then(sig => process.stdout.write(sig))
.catch(e => { console.error(e.message); process.exit(1); });
একই ফোল্ডারে TronWeb ইনস্টল করুন: npm install tronwebতারপর PHP থেকে এটিকে কল করুন:
// Both the sender (in $tron) and the signer must be the SAME wallet.
// Make sure TRON_PRIVATE_KEY in your environment matches the wallet that sent the TRX.
$delegateTo = 'TWalletThatNeedsEnergy';
$signature = trim(shell_exec(
sprintf('node sign.js %s %s',
escapeshellarg($txHash),
escapeshellarg($delegateTo)
)
));
if (!$signature) {
throw new Exception('Signing failed. Check that node and tronweb are installed and TRON_PRIVATE_KEY is set.');
}
echo "Signed: " . substr($signature, 0, 20) . "...\n";
৬. প্রতিনিধিদল দাবি করুন
// $txHash, $delegateTo, and $signature are all defined in the previous steps.
$ch = curl_init("${api}/delegate");
curl_setopt_array($ch, [
CURLOPT_POST => true,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_HTTPHEADER => ['Content-Type: application/json'],
CURLOPT_POSTFIELDS => json_encode([
'tx_hash' => $txHash,
'delegate_to' => $delegateTo,
'signature' => $signature,
]),
]);
$response = curl_exec($ch);
$httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
curl_close($ch);
$result = json_decode($response, true);
if (isset($result['error'])) {
throw new Exception("Delegation failed: " . $result['message']);
}
echo "Delegated: " . $result['energy'] . " energy\n";
echo "Ref: " . $result['ref'] . "\n";
৭. USDT পাঠান
// Send USDT using the delegated energy
$usdtContract = $tron->contract($usdt);
// Amount in smallest unit (6 decimals for USDT)
// 10 USDT = 10 * 1,000,000 = 10000000
$amount = 10 * pow(10, 6);
$recipient = 'TRecipientAddress';
$transfer = $usdtContract->transfer($recipient, $amount);
echo "USDT sent: " . $transfer . "\n";
ত্রুটি পরিচালনা
ক্লেইম কলটিকে একটি ছোট রিট্রাই হেল্পারের মধ্যে রাখুন। সবচেয়ে সাধারণ ত্রুটিটি হলো payment_verification_failed যখন ট্রানজ্যাকশনটি এখনও অন-চেইনে ইনডেক্স করা হয়নি — কয়েক সেকেন্ড অপেক্ষা করুন এবং একবার পুনরায় চেষ্টা করুন।
function claimDelegation($api, $txHash, $delegateTo, $signature, $retries = 3) {
for ($i = 0; $i < $retries; $i++) {
$ch = curl_init("${api}/delegate");
curl_setopt_array($ch, [
CURLOPT_POST => true,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_HTTPHEADER => ['Content-Type: application/json'],
CURLOPT_POSTFIELDS => json_encode([
'tx_hash' => $txHash,
'delegate_to' => $delegateTo,
'signature' => $signature,
]),
]);
$result = json_decode(curl_exec($ch), true);
curl_close($ch);
if (!isset($result['error'])) {
return $result; // Success
}
switch ($result['error']) {
case 'payment_verification_failed':
// Most common: tx not yet indexed. Wait and retry.
sleep(3);
continue 2;
case 'hash_already_used':
throw new Exception('This tx hash has already been claimed');
case 'signature_mismatch':
throw new Exception('Signer does not match payment sender. Sign with the same wallet that sent TRX.');
case 'delegation_failed':
// Refund queued automatically if payment was verified
throw new Exception('Delegation failed: ' . $result['message']);
default:
throw new Exception($result['message'] ?? 'Unknown error');
}
}
throw new Exception('Transaction not found after retries');
}
সম্পূর্ণ উদাহরণ
<?php
require_once 'vendor/autoload.php';
use IEXBase\TronAPI\Tron;
$tron = new Tron();
$tron->setPrivateKey(getenv('TRON_PRIVATE_KEY'));
$tron->setAddress(getenv('TRON_WALLET_ADDRESS'));
$api = 'https://api.tronnrg.com';
$addr = 'TFqUiCu1JwLHHnBNeaaVKH7Csm4aA3YhZx';
try {
$delegateTo = 'TRecipientWallet';
$trxAmount = 4; // min 4, max 1000 — energy = trxAmount × 16,250
// 1. Send TRX (linear pricing: 16,250 energy per TRX)
$payment = $tron->sendTrx($addr, $trxAmount);
$txHash = $payment['txid'];
echo "Payment: ${txHash}\n";
// 2. Sign via Node helper (see Step 5 above)
$signature = trim(shell_exec(
sprintf('node sign.js %s %s',
escapeshellarg($txHash),
escapeshellarg($delegateTo)
)
));
if (!$signature) throw new Exception('Signing failed');
// 3. Claim delegation (with retry)
$result = claimDelegation($api, $txHash, $delegateTo, $signature);
echo "Energy: " . $result['energy'] . "\n";
echo "Ref: " . $result['ref'] . "\n";
// 4. Send USDT (energy is now available)
$contract = $tron->contract('TR7NHqjeKQxGTCi8q8ZY4pL8otSzgjLj6t');
$transfer = $contract->transfer($delegateTo, 10 * pow(10, 6));
echo "USDT sent: ${transfer}\n";
} catch (Exception $e) {
echo "Error: " . $e->getMessage() . "\n";
}