How to sell energy
Esta página aún no está disponible en tu idioma.
This article shows how to fill an existing energy order by selling/delegating TRON energy through the API.
Explanation of the code
Section titled “Explanation of the code”- Select the order and define delegation:
const ID = 5383310;const AMOUNT = 1000;const RESOURCE = "ENERGY";const LOCK = true;const LOCK_PERIOD = 300;
- Creates the delegation transaction and sign it:
const raw_tx = await tronWeb.transactionBuilder.delegateResource(...);const signed_tx = await tronWeb.trx.sign(raw_tx);
- Send the transaction to the API:
axios.post(ENDPOINT, params)
Example of how to sell energy:
Section titled “Example of how to sell energy:”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 filledconst ORIGIN_ADDRESS = "YOUR_ORIGIN_ADDRESS"; // OPTIONAL: add this only for multisignature walletsconst ADDRESS = "YOUR_SELL_ADDRESS";const RECEIVER_ADDRESS = "YOUR_RECEIVER_ADDRESS";const RESOURCE = "ENERGY";const AMOUNT = 1000; // Energy amount, min 50000const 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();