跳转到内容

How to buy energy

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

In this article there are 2 examples of how to buy energy:

  • With credit

    • You already have a credit balance with the service.
    • You send your API key along with the order details.
    • No blockchain transaction or private key is required.
  • Without credit:

    • You pay for the order directly with TRX.
    • Your application creates a TRX transaction for the required payment.
    • The transaction is signed using your private key.
    • The signed transaction is then sent to the API together with the energy rental order.

This article shows two ways to buy TRON energy through the API:

This script creates an energy rental order and pays for it in credit.

const axios = require('axios');
const TronWeb = require('tronweb');
const TRON_NODE = "https://api.trongrid.io";
const tronWeb = new TronWeb(
new TronWeb.providers.HttpProvider(TRON_NODE),
new TronWeb.providers.HttpProvider(TRON_NODE),
TRON_NODE
);
async function BuyTest()
{
const API_KEY = "YOUR_API_KEY";
const API_SERVER = "TEMkRxLtCCdL4BCwbPXbbNWe4a9gtJ7kq7";
const MARKET = 'Open';
const ORIGIN = "YOUR_ORIGIN_ADDRESS";
const TARGET = "YOUR_TARGET_ADDRESS";
const RESOURCE = 0; // 0 energy, 1 bandwidth
const PRICE = 50;
const AMOUNT = 64500; // Energy amount points, min 100000
const DURATION = 300; // 300 = 5 minutes
const PAYMENT = parseInt(((PRICE * AMOUNT * (DURATION + ((DURATION < 86400) ? 86400 : 0))) / 86400).toFixed(0));
const PARTFILL = true; // true = allow several addresses to fill your order
// No need for signature, only api-key
// We send the order
const params =
{
market: MARKET,
address: ORIGIN,
target: TARGET,
amount: AMOUNT,
resource: RESOURCE,
duration: DURATION,
price: PRICE,
partfill: PARTFILL,
signed_ms: undefined,
signed_tx: undefined,
api_key: API_KEY,
};
console.log(params);
const API_URL = `https://api.tronenergy.market/order/new`;
axios.post(API_URL, params)
.then((res) =>
{
console.log("all ok, order created");
console.log(res.data); // order created response in res.data { order: 1644 }
})
.catch((error) =>
{
console.error("woops, something wrong happened!");
});
}
BuyTest();
Example script of how to buy energy without credit:
Section titled “Example script of how to buy energy without credit:”

This script creates an energy rental order and pays for it in TRX instead of with credit.

const axios = require('axios');
const TronWeb = require('tronweb');
const TRON_NODE = "https://api.trongrid.io/";
const PRIVATE_KEY = "YOUR_PK";
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 AMOUNT = 10000 //energy amount points, min 100000
const DURATION = 300 //Duration in seconds. 300 (5 minutes)
const PRICE = 40 //suns per day, whatever price you want
const SERVER_ADDRESS = "TEMkRxLtCCdL4BCwbPXbbNWe4a9gtJ7kq7";
const PAYMENT = parseInt(((PRICE * AMOUNT * (DURATION + ((DURATION < 86400) ? 86400 : 0))) / 86400).toFixed(0));//we need to increment 1 day of duration per orders smaller than 24 hours / 86400 seconds
const ORIGIN = tronWeb.defaultAddress.base58;
async function BuyTest()
{
//we sign the tx
const raw_tx = await tronWeb.transactionBuilder.sendTrx(SERVER_ADDRESS, PAYMENT , ORIGIN);
const signed_tx = await tronWeb.trx.sign( raw_tx );
//we send the order
let params =
{
market: 'Open',
address: "TEMkRxLtCCdL4BCwbPXbbNWe4a9gtJ7kq7",
target: "TAtPNH8sNWHJXFaZPAQJu9fMasGZMTnbnj",
amount: AMOUNT, //energy amount points, min 100000
resource: 0, //0 energy, 1 bandwidth
duration: DURATION, //Duration in seconds. 300 (5 minutes)
price: PRICE, //suns per day, whatever price you want
partfill: true, //true for allowing several address to fill. false will force 1 address to fill it all
signed_tx: JSON.stringify(signed_tx),
}
const API_URL = `https://api.tronenergy.market/order/new`;
axios.post(API_URL, params,)
.then((res)=>
{
console.log("all ok, order created");
console.log(res.data); //order created response in res.data { order: 1644 }
})
.catch((error) =>
{
console.error("woops, something wrong happened!");
console.error(error.response.data);
});
}
BuyTest();