Tronsave
🇬🇧 English
Search
⌃K

Step by Step Connect And Buy Energy TRONSAVE

This is a typical example of integrating fast energy purchase and sending transactions

Step 1: Install package and Connect

Download SDK:
npm i @tronsave/sdk tronweb
Connect to Tronsave SDK:
import { TronSave } from '@tronsave/sdk'
const options = {
network: "mainnet",
}
const tronSave = new TronSave(window.tronWeb, options)
You can follow the link and install the package.: https://tronsave.gitbook.io/tronsave/build/connect-tronsave​

Step 2: Estimate Energy for Request

Estimate the energy required for the transaction you want to send.
Create function estimate like transaction send():
  • Example Approve token:
const instance = await window.tronWeb.contract(
ABI_TRC20,
tokenAddress
);
const resApprove = await instance
.approve(senderAddress, valueApprove)
.send({feeLimit: 1e9});
  • Estimate Energy for transaction approve:
Options method estimateV2() like method send()
const dataEstimate = await tronSave
.contract(ABI_TRC20, tokenAddress)
.approve(senderAddress, valueApprove)
.estimateV2({feeLimit: 1e9});
Response dataEstimate:
{
total_estimate_energy:number,
available_energy:number
}

Step3: Estimate TRX payout (optional)

If you want to check in advance the amount of TRX you need to pay for the energy you want to purchase...
Determine the amount of TRX that needs to be paid for the desired amount of energy.
const valueTRX =await tronSave.estimatePayout({
amount:1000000,
buy_energy_type: "MEDIUM",
duration_millisec: 3600000,//1 hours
target_address: "TKVsa...XYZ",
is_partial:true,
});
Response:
{
"estimateBuy": {
"unit_price": 90,//price buy Energy(SUN)
"duration_millisec": 3600,//time buy (seconds)
"estimate_trx": 5287085,// TRX payout for buy Energy
"available_energy": 58745 //energy buy for transaction
}
}
Params
Type
Description
Required
amount
number
amount energy buy for transaction Approve
true
buy_energy_type
"FAST"| "MEDIUM"|"SLOW"
  • "FAST" : If market ready to fill = 100%, FAST = MEDIUM. If market ready to fill < 100%, FAST = MEDIUM + 10. If market ready to fill = 0%, FAST = SLOW + 20.
  • "MEDIUM" : The lowest price for the maximum market fill for this order. If market ready to fill = 0%, MEDIUM = SLOW + 10.
  • "SLOW": The lowest price that can be set for this order.
  • If price is number, price unit is equal to sun.
true
target_address
string
The address of receiver resource.
true
is_partial
boolean
Allow the order to be filled partially or not.
true

Step 4: Buy Energy

You need to transfer the amount of TRX that you have to pay for the amount of energy you want to purchase.
Info data from step 3:
{
"estimateBuy": {
"unit_price": 90,//price buy Energy(SUN)
"estimate_trx": 5287085,// TRX payout for buy Energy
"available_energy": 58745 //energy buy for transaction
...
}
}
Event buy energy:
const orderID = await tronSave.buyEnergy({
amount: 5287085,//estimateBuy.estimate_trx
price: "MEDIUM",
duration: 3600000,//1 hours
allowPartialFill: true,
target: "TKVsa...XYZ",
fulfilledOnly: true,//only fulfill transaction
sponsor:"sponsor" // if you want sponsor not required
});
​
Response:
dataBuy : "1544aca76c81df1264fcafb9" //order id

Step 5: Check status transaction

Your can check status order with API: ( buy success when fullFillPercent =100)
const fullFillPercent = await tronSave.getDetailOrder(dataBuy);
Response:
data:100//percent fill
The system is currently purchasing energy from the market, please wait for 10 Seconds - 3 minutes.
You can Interval fetch status order id:
const LIMIT_TIME_CHECK_BUY=25
if (dataBuy) {
let i = 0;
let timer = setInterval(async () => {
if (i <= LIMIT_TIME_CHECK_BUY) {
const data = await tronSave.getDetailOrder(dataBuy);
if (i === LIMIT_TIME_CHECK_BUY) {
return "failed;
}
if (data === 100) {
clearInterval(timer);
return "success";
} else {
i++;
}
}
}, 3000);
}
Once you've confirmed that fullFillPercent is 100%, you can proceed to send the transaction with energy.