开发者快速开始
TronSave API 开发者快速开始:约 10 分钟完成首次调用——获取 API 密钥并充值 TRX、预估费用、创建 TRON 能量订单并确认成交,附完整代码示例与测试网说明。
Last updated
GET https://api.tronsave.io/v2/user-info
Headers: { "apikey": "YOUR_API_KEY" }{
"error": false,
"data": {
"balance": "50000000", // 50 TRX(单位:SUN,1 TRX = 1,000,000 SUN)
"depositAddress": "TKVSa...", // 向此地址转入 TRX 进行充值
"representAddress": "TKVSa..." // 下单时作为 "requester" 字段使用的地址
}
}POST https://api.tronsave.io/v2/estimate-buy-resource
Content-Type: application/json{
"receiver": "YOUR_TRON_RECEIVER_ADDRESS",
"resourceType": "ENERGY",
"resourceAmount": 65000,
"durationSec": 900,
"unitPrice": "MEDIUM"
}{
"error": false,
"data": {
"unitPrice": 64,
"durationSec": 900,
"estimateTrx": 4230000, // 预计费用:4.23 TRX
"availableResource": 65000 // 当前市场有足够的能量可用。
}
}POST https://api.tronsave.io/v2/buy-resource
Headers: { "apikey": "YOUR_API_KEY" }
Content-Type: application/json{
"receiver": "YOUR_TRON_RECEIVER_ADDRESS",
"resourceType": "ENERGY",
"resourceAmount": 65000,
"durationSec": 900,
"unitPrice": "MEDIUM",
"options": {
"allowPartialFill": true,
"preventDuplicateIncompleteOrders": true
}
}// For production bots (most common setup)
{
"allowPartialFill": true, // 当市场供应不足时,允许部分成交
"preventDuplicateIncompleteOrders": true, // 防止重试时创建重复的未完成订单
"maxPriceAccepted": 100 // 当单价超过 100 SUN 时拒绝下单
}
// For urgent / must-fill-now orders
{
"allowPartialFill": true,
"onlyCreateWhenFulfilled": true
}{
"error": false,
"data": {
"orderId": "6818426a65fa8ea36d119999"
}
}GET https://api.tronsave.io/v2/order/6818426a65fa8ea36d119d2c
Headers: { "apikey": "YOUR_API_KEY" }{
"error": false,
"data": {
"id": "6818426a65fa8ea36d119d2c",
"resourceAmount": 65000,
"resourceType": "ENERGY",
"fulfilledPercent": 100, // 100 表示订单已完全撮合
"remainAmount": 0,
"price": 64, // 实际单价(SUN)
"payoutAmount": 4230000, // 总扣费金额(SUN)
"durationSec": 900,
"delegates": [
{
"delegator": "THnnMC...", // 提供并委托能量的供应方地址
"amount": 65000,
"txid": "19d3fa..." // 链上交易 ID
}
]
}
}const TRONSAVE_API = "https://api.tronsave.io";
const API_KEY = "YOUR_API_KEY";
const RECEIVER = "YOUR_TRON_RECEIVER_ADDRESS";
async function buyEnergy(amount = 32000, durationSec = 259200) {
// Step 1: Estimate cost
const estimateRes = await fetch(`${TRONSAVE_API}/v2/estimate-buy-resource`, {
method: "POST",
headers: { "Content-Type": "application/json" },
body: JSON.stringify({
receiver: RECEIVER,
resourceType: "ENERGY",
resourceAmount: amount,
durationSec,
unitPrice: "MEDIUM",
}),
});
const { data: estimate } = await estimateRes.json();
console.log(`Estimated cost: ${estimate.estimateTrx / 1e6} TRX`);
// Step 2: Create a buy order
const buyRes = await fetch(`${TRONSAVE_API}/v2/buy-resource`, {
method: "POST",
headers: {
"apikey": API_KEY,
"Content-Type": "application/json",
},
body: JSON.stringify({
receiver: RECEIVER,
resourceType: "ENERGY",
resourceAmount: amount,
durationSec,
unitPrice: "MEDIUM",
options: {
allowPartialFill: true,
preventDuplicateIncompleteOrders: true,
maxPriceAccepted: 100,
},
}),
});
const { data: order } = await buyRes.json();
console.log(`Order created: ${order.orderId}`);
// Step 3: Wait and check order status
await new Promise((r) => setTimeout(r, 5000)); // Wait 5 seconds
const statusRes = await fetch(`${TRONSAVE_API}/v2/order/${order.orderId}`, {
headers: { "apikey": API_KEY },
});
const { data: status } = await statusRes.json();
if (status.fulfilledPercent === 100) {
console.log(`Successfully delegated ${status.resourceAmount} Energy to ${RECEIVER}`);
} else {
console.log(`Order pending... ${status.fulfilledPercent}% filled`);
}
return status;
}
buyEnergy();npm install tronsave-sdk