เอกสารสำหรับนักพัฒนา
TRONWEB INTEGRATION
ผสานรวมระบบการมอบหมายอำนาจการจัดการ Energy TronEnergy โดยใช้ TronWeb ใน Node.js พร้อมตัวอย่างโค้ดที่พร้อมใช้งานได้ทันที
ที่อยู่การชำระเงิน API
TFqUiCu1JwLHHnBNeaaVKH7Csm4aA3YhZx
ส่ง TRX ไปยังที่อยู่นี้ แฮชธุรกรรมของคุณจะถูกใช้เพื่อขอรับสิทธิ์การมอบหมายพลังงาน
การผสานรวม Node.js อย่างสมบูรณ์โดยใช้ TronWeb แต่ละขั้นตอนเป็นบล็อกโค้ดแบบแยกส่วนที่คุณสามารถคัดลอกไปใช้ในโปรเจ็กต์ของคุณได้ ตัวอย่างแบบครบวงจรอยู่ที่ด้านล่าง
ข้อกำหนดเบื้องต้น: Node.js 18+
tronweb ติดตั้ง (npm install tronweb) กระเป๋าเงิน Tron ที่ได้รับเงินทุน
การไหล
ไม่ต้องใช้ API key ไม่ต้องลงทะเบียน โค้ดของคุณจะส่ง TRX ไปยังที่อยู่ชำระเงิน TronEnergy บนบล็อกเชน ลงนามในข้อความเพื่อพิสูจน์ความเป็นเจ้าของ จากนั้นจึงขอรับการมอบหมาย Energy จะมาถึงภายในเวลาประมาณ 3 วินาที จากนั้นโค้ดของคุณจะส่ง USDT โดยใช้พลังงานที่ได้รับมอบหมายนั้น
ราคาเป็นแบบเชิงเส้น: 16,250 Energy ต่อ TRX 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 comes back — there are no tiers or packages. For a single standard transfer, send 4. For a new-wallet transfer, send 8. For batch work, send more. The code below uses a
trxAmount variable so you can change it in one place.
ทีละขั้นตอน
1. การตั้งค่า
setup
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';
2. ส่ง TRX
send payment
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);
3. อ้างสิทธิ์ในการมอบหมายอำนาจ
claim delegation
// 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
4. ส่ง USDT
send 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);
การจัดการข้อผิดพลาด
handling errors
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);
ต้องใส่ลายเซ็นทุกครั้ง It proves you are the wallet that sent the TRX. Without it, the API rejects the request with
missing_signature. The signature is what makes the API safe to use without an API key: it cryptographically proves you authorised the delegation.