የገንቢ ሰነዶች
TronWeb ውህደት
በNode.js ውስጥ TronWeb በመጠቀም TronEnergy Energy ውክልናን ማዋሃድ። የኮድ ቅጂ-ለጥፍ ዝግጁ የሆኑ ምሳሌዎች።
API የክፍያ አድራሻ
TFqUiCu1JwLHHnBNeaaVKH7Csm4aA3YhZx
TRX ወደዚህ አድራሻ ይላኩ። የቴክስ ሃሽዎ የኢነርጂ ውክልናን ለመጠየቅ ጥቅም ላይ ይውላል።
TronWeb በመጠቀም የNode.js ውህደትን ያጠናቅቁ። እያንዳንዱ እርምጃ ወደ ፕሮጀክትዎ መቅዳት የሚችሉት ራሱን የቻለ የኮድ ብሎክ ነው። ሙሉው ከጫፍ እስከ ጫፍ ያለው ምሳሌ ከታች ይገኛል።
ቅድመ ሁኔታዎች፡ ኖድ.ጄኤስ 18+፣
tronweb ተጭኗል (npm install tronweb) የገንዘብ ድጋፍ የሚደረግለት Tron ቦርሳ።
ፍሰቱ
API ቁልፍ የለም። ምዝገባ የለም። ኮድዎ TRX በሰንሰለት ወደ TronEnergy የክፍያ አድራሻ ይልካል፣ የባለቤትነት ማረጋገጫ መልእክት ይፈርማል፣ ከዚያም ውክልናውን ይጠይቃል። Energy በግምት በ3 ሰከንዶች ውስጥ ይደርሳል። ከዚያም ኮድዎ የተወከለውን ኃይል በመጠቀም USDT ይልካል።
ዋጋው መስመራዊ ነው፡ 16,250 በ TRX Energy ። ዝቅተኛው ትዕዛዝ 4 TRX (65,000 Energy - አንድ መደበኛ USDT ዝውውር)፣ ከፍተኛው 1,000 TRX (16.25 ሚሊዮን Energy )። የሚልኩት መጠን ምን ያህል Energy እንደሚመለስ በትክክል ይወስናል - ደረጃዎች ወይም ፓኬጆች የሉም። ለአንድ መደበኛ ዝውውር፣ 4 ይላኩ። ለአዲስ የኪስ ቦርሳ ዝውውር፣ 8 ይላኩ። ለባች ሥራ፣ ተጨማሪ ይላኩ። ከታች ያለው ኮድ የሚከተለውን ይጠቀማል
trxAmount ተለዋዋጭ ስለዚህ በአንድ ቦታ መቀየር ይችላሉ።
ደረጃ በደረጃ
1. ማዋቀር
ማዋቀር
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 ን ይላኩ
ክፍያ ላክ
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. ውክልናውን ይጠይቁ
የይገባኛል ጥያቄ ውክልና
// 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 ይላኩ
ዩኤስዲቲ ይላኩ
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.