跳转到内容

How to sell energy

此内容尚不支持你的语言。

This article shows how to fill an existing energy order by selling/delegating TRON energy through the API.

  1. Select the order and define delegation:
    const ID = 5383310;
    const AMOUNT = 1000;
    const RESOURCE = "ENERGY";
    const LOCK = true;
    const LOCK_PERIOD = 300;
  2. Creates the delegation transaction and sign it:
    const raw_tx = await tronWeb.transactionBuilder.delegateResource(...);
    const signed_tx = await tronWeb.trx.sign(raw_tx);
  3. Send the transaction to the API:
    axios.post(ENDPOINT, params)
const axios = require('axios');
const TronWeb = require('tronweb');
const TRON_NODE = "https://api.trongrid.io"; // or "https://api.nileex.io"
const PRIVATE_KEY = "YOUR_PRIVATE_KEY";
const ENDPOINT = "https://api.tronenergy.market/order/fill";
const tronWeb = new TronWeb(
new TronWeb.providers.HttpProvider(TRON_NODE),
new TronWeb.providers.HttpProvider(TRON_NODE),
TRON_NODE,
PRIVATE_KEY // this should match origin address
);
const ID = 5383310; // The order id to get filled
const ORIGIN_ADDRESS = "YOUR_ORIGIN_ADDRESS"; // OPTIONAL: add this only for multisignature wallets
const ADDRESS = "YOUR_SELL_ADDRESS";
const RECEIVER_ADDRESS = "YOUR_RECEIVER_ADDRESS";
const RESOURCE = "ENERGY";
const AMOUNT = 1000; // Energy amount, min 50000
const LOCK = true;
const LOCK_PERIOD = 300; // Time in seconds
async function sell() {
// signed_tx
const raw_tx = await tronWeb.transactionBuilder.delegateResource(AMOUNT, RECEIVER_ADDRESS, RESOURCE, ADDRESS, LOCK, LOCK_PERIOD);
const signed_tx = await tronWeb.trx.sign(raw_tx);
const params = {
id: ID,
address: ADDRESS,
origin_address: ORIGIN_ADDRESS,
signed_tx: JSON.stringify(signed_tx)
};
axios.post(ENDPOINT, params)
.then((res) => {
console.log("all ok, order filled");
console.log(res.data);
})
.catch((error) => {
console.error("woops, something wrong happened!");
console.error(error.response?.data);
});
}
sell();