计算能量成本
此接口端点计算能量购买成本而不创建交易。
POST /v1/calculate
请求
请求参数
字段 | 类型 | 必需 | 描述 |
---|---|---|---|
address | string | 是 | 波场钱包地址(34个字符) |
energy | integer | 是 | 要购买的能量数量(最少60000) |
duration | integer | 是 | 持续时间(小时)(1小时或24小时) |
bash
#!/bin/bash
API_TOKEN="your_api_token"
API_SECRET="your_api_secret"
REQUEST_BODY='{
"address": "TRX_ADDRESS",
"energy": 60000,
"duration": 1
}'
# 计算签名
SIGNATURE=$(echo -n "${REQUEST_BODY}${API_SECRET}" | sha256sum | cut -d' ' -f1)
# 发送API请求
curl -X POST "https://api.tronzap.com/v1/calculate" \
-H "Authorization: Bearer ${API_TOKEN}" \
-H "X-Signature: ${SIGNATURE}" \
-H "Content-Type: application/json" \
-d "${REQUEST_BODY}"
javascript
const crypto = require('crypto');
const axios = require('axios');
const apiToken = 'your_api_token';
const apiSecret = 'your_api_secret';
const requestBody = JSON.stringify({
address: 'TRX_ADDRESS',
energy: 60000,
duration: 1
});
// 计算签名
const signature = crypto
.createHash('sha256')
.update(requestBody + apiSecret)
.digest('hex');
// 发送API请求
axios({
method: 'post',
url: 'https://api.tronzap.com/v1/calculate',
headers: {
'Authorization': `Bearer ${apiToken}`,
'X-Signature': signature,
'Content-Type': 'application/json'
},
data: requestBody
})
.then(response => console.log(response.data))
.catch(error => console.error(error));
php
<?php
$apiToken = 'your_api_token';
$apiSecret = 'your_api_secret';
$requestBody = json_encode([
'address' => 'TRX_ADDRESS',
'energy' => 60000,
'duration' => 1
]);
// 计算签名
$signature = hash('sha256', $requestBody . $apiSecret);
// 发送API请求
$ch = curl_init('https://api.tronzap.com/v1/calculate');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $requestBody);
curl_setopt($ch, CURLOPT_HTTPHEADER, [
'Authorization: Bearer ' . $apiToken,
'X-Signature: ' . $signature,
'Content-Type: application/json'
]);
$response = curl_exec($ch);
curl_close($ch);
echo $response;
?>
python
import hashlib
import json
import requests
api_token = 'your_api_token'
api_secret = 'your_api_secret'
request_body = json.dumps({
'address': 'TRX_ADDRESS',
'energy': 60000,
'duration': 1
})
# 计算签名
signature = hashlib.sha256((request_body + api_secret).encode()).hexdigest()
# 发送API请求
headers = {
'Authorization': f'Bearer {api_token}',
'X-Signature': signature,
'Content-Type': 'application/json'
}
response = requests.post(
'https://api.tronzap.com/v1/calculate',
headers=headers,
data=request_body
)
print(response.json())
响应
响应提供能量购买的详细计算信息。
响应字段
字段 | 类型 | 描述 |
---|---|---|
code | integer | 响应代码(0 = 成功) |
result | object | 响应数据 |
result.address | string | 请求中提供的波场地址 |
result.energy | integer | 请求中提供的能量数量 |
result.duration | integer | 请求中提供的持续时间(小时) |
result.price | float | 能量购买的总价格 |
result.activation_fee | float | 地址激活价格(如果需要) |
result.total | float | 包括激活费用的最终价格(如果需要) |
响应示例
json
{
"code": 0,
"result": {
"address": "TRX_ADDRESS",
"energy": 60000,
"duration": 1,
"price": 1.67,
"activation_fee": 0,
"total": 1.67
}
}
响应示例(地址未激活)
json
{
"code": 0,
"result": {
"address": "TRX_ADDRESS",
"energy": 60000,
"duration": 1,
"price": 3.67,
"activation_fee": 1.4,
"total": 5.07
}
}
可能的错误
错误代码 | 描述 |
---|---|
1 | 身份验证错误(令牌或签名不正确) |
2 | 无效的服务或参数 |
5 | 未找到钱包 |
10 | 无效的波场地址 |
11 | 无效的能量数量 |
12 | 无效的持续时间 |
500 | 内部服务器错误 |
注释
- 能量数量必须至少为60000。
- 支持1小时和24小时的持续时间。
- 此接口端点仅计算成本而不创建交易。
- 要创建实际的能量购买交易,请使用创建交易接口端点。