اسناد توسعهدهنده
ادغام TronWeb
ادغام واگذاری مسئولیت TronEnergy Energy با استفاده از TronWeb در Node.js. نمونههای کد آماده را کپی و پیست کنید.
آدرس پرداخت API
TFqUiCu1JwLHHnBNeaaVKH7Csm4aA3YhZx
TRX به این آدرس ارسال کنید. هش تراکنش شما برای دریافت انرژی مورد نیاز استفاده میشود.
ادغام کامل Node.js با استفاده از TronWeb . هر مرحله یک بلوک کد مستقل است که میتوانید در پروژه خود کپی کنید. مثال کامل از ابتدا تا انتها در پایین آمده است.
پیشنیازها: نود جی اس ۱۸+
tronweb نصب شده (npm install tronweb)، یک کیف پول Tron تأمین مالی شده.
جریان
بدون کلید API . بدون ثبت نام. کد شما TRX را به صورت درون زنجیرهای به آدرس پرداخت TronEnergy ارسال میکند، پیامی را برای اثبات مالکیت امضا میکند، سپس وکالت را مطالبه میکند. Energy تقریباً در عرض ۳ ثانیه میرسد. سپس کد شما USDT را با استفاده از انرژی وکالت داده شده ارسال میکند.
قیمتگذاری خطی است: ۱۶۲۵۰ Energy به ازای هر TRX . حداقل سفارش ۴ TRX (۶۵۰۰۰ Energy - یک انتقال استاندارد USDT )، حداکثر ۱۰۰۰ TRX (۱۶.۲۵ میلیون Energy ). مبلغی که ارسال میکنید دقیقاً میزان Energy برگشتی را تعیین میکند - هیچ سطح یا بستهای وجود ندارد. برای یک انتقال استاندارد، ۴ ارسال کنید. برای انتقال کیف پول جدید، ۸ ارسال کنید. برای کار دستهای، بیشتر ارسال کنید. کد زیر از ... استفاده میکند.
trxAmount متغیر است، بنابراین میتوانید آن را در یک جا تغییر دهید.
گام به گام
۱. راهاندازی
راه اندازی
const { TronWeb } = require('tronweb'); // destructured: the default import is broken in v6
const tronWeb = new TronWeb({
fullHost: 'https://api.trongrid.io',
privateKey: process.env.TRON_PRIVATE_KEY,
});
const API = 'https://api.tronnrg.com';
const ADDR = 'TFqUiCu1JwLHHnBNeaaVKH7Csm4aA3YhZx'; // API payment address
const USDT = 'TR7NHqjeKQxGTCi8q8ZY4pL8otSzgjLj6t';
۲. ارسال TRX
ارسال پرداخت
const TRX_AMOUNT = 4; // Linear: 16,250 energy per TRX. Min 4, max 1000.
// 4 → 65k (standard) · 8 → 130k (new wallet) · 40 → 650k (10 transfers)
const payment = await tronWeb.trx.sendTransaction(ADDR, TRX_AMOUNT * 1e6);
console.log('Payment tx:', payment.txid);
۳. درخواست نمایندگی
تفویض ادعا
// Sign: proves you are the sender
const msg = `${payment.txid}:${tronWeb.defaultAddress.base58}`;
const sig = await tronWeb.trx.signMessageV2(msg);
const delegation = await fetch(`${API}/delegate`, {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({
tx_hash: payment.txid,
delegate_to: tronWeb.defaultAddress.base58,
signature: sig,
}),
}).then(r => r.json());
if (delegation.error) {
throw new Error(delegation.message);
}
console.log('Energy delegated:', delegation.energy); // TRX_AMOUNT × 16,250
۴. ارسال USDT
ارسال با دلار آمریکا
const contract = await tronWeb.contract().at(USDT);
const tx = await contract.transfer(
recipientAddress, Math.round(usdtAmount * 1e6)
).send({ feeLimit: 50_000_000 });
console.log('USDT sent:', tx);
مدیریت خطا
مدیریت خطاها
const result = await fetch(`${API}/delegate`, { ... })
.then(r => r.json());
if (result.error) {
switch (result.error) {
case 'payment_verification_failed':
// Payment not yet indexed on-chain. Wait 3s and retry.
break;
case 'hash_already_used':
// Already claimed. Don't retry.
break;
case 'signature_mismatch':
// Sender of TRX != signer of the message. Sign with the same key.
break;
case 'delegation_failed':
// Provider could not deliver. Retry or contact support with result.ref.
break;
}
}
مثال کامل
این را در یک فایل کپی کنید، متغیرهای محیطی خود را تنظیم کنید و آن را اجرا کنید. اسکریپت TRX را ارسال میکند، پیام را امضا میکند، با تلاش مجدد، وکالت را مطالبه میکند و سپس USDT ارسال میکند.
delegate-energy.js
const { TronWeb } = require('tronweb'); // destructured: default import is broken in v6
const tronWeb = new TronWeb({
fullHost: 'https://api.trongrid.io',
privateKey: process.env.TRON_PRIVATE_KEY,
});
const API = 'https://api.tronnrg.com';
const ADDR = 'TFqUiCu1JwLHHnBNeaaVKH7Csm4aA3YhZx';
const USDT = 'TR7NHqjeKQxGTCi8q8ZY4pL8otSzgjLj6t';
async function claimWithRetry(txHash, delegateTo, signature, retries = 3) {
for (let i = 0; i < retries; i++) {
const res = await fetch(`${API}/delegate`, {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({ tx_hash: txHash, delegate_to: delegateTo, signature }),
}).then(r => r.json());
if (!res.error) return res;
if (res.error !== 'payment_verification_failed') throw new Error(res.message);
await new Promise(r => setTimeout(r, 3000));
}
throw new Error('Transaction not found after retries');
}
async function main() {
const recipient = 'TRecipientWallet';
const trxAmount = 4; // min 4, max 1000 — you get trxAmount × 16,250 energy
// 1. Send TRX (linear pricing: 16,250 energy per TRX)
const payment = await tronWeb.trx.sendTransaction(ADDR, trxAmount * 1e6);
console.log('Payment:', payment.txid);
// 2. Claim delegation
// Sign: proves you are the sender
const message = `${payment.txid}:${recipient}`;
const signature = await tronWeb.trx.signMessageV2(message);
const result = await claimWithRetry(payment.txid, recipient, signature);
console.log('Delegated:', result.energy, 'energy');
console.log('Delegation tx:', result.delegations[0].tx); // verify on TronScan
console.log('Ref:', result.ref);
// 3. Send USDT
const contract = await tronWeb.contract().at(USDT);
const tx = await contract.transfer(recipient, 10 * 1e6).send();
console.log('USDT sent:', tx);
}
main().catch(console.error);
همیشه امضا را هم ضمیمه کنید. این ثابت میکند که شما کیف پولی هستید که TRX را ارسال کردهاید. بدون آن، API درخواست را با ... رد میکند.
missing_signature.