使用API密钥通过API延长订单
这是使用API密钥扩展订单的完整示例。
获取API密钥的方法
const API_KEY = `your_api_key`;
const TRONSAVE_API_URL = "https://api.tronsave.io/v0"
const RECEIVER = "the_address_that_receive_resource"
/**
* @param {*} extend_to 你想要延长的时间(以毫秒为单位)
* @param {*} max_price 你想支付的最大延长费用
* @returns
*/
const GetEstimateExtendData = async (extend_to, max_price) => {
const url = TRONSAVE_API_URL + `/get-extendable-delegates`
const body = {
"extend_to": extend_to, // 你想要延长的时间(以毫秒为单位)
"receiver": RECEIVER, // 接收资源委托的地址
"max_price": max_price, // 可选。你想支付的最大延长费用
}
const data = await fetch(url, {
method: "POST",
headers: {
'apikey': API_KEY,
"content-type": "application/json",
},
body: JSON.stringify(body)
})
const response = await data.json()
/**
* 示例响应
* @link //TODO
{
"extend_order_book": [
{
"price": 133,
"value": 64319
}
],
"total_delegate_amount": 64319,
"total_available_extend_amount": 64319,
"total_estimate_trx": 8554427,
"is_able_to_extend": true,
"your_balance": 20000000,
"extend_data": [
{
"delegator": "TMN2uTdy6rQYaTm4A5g732kHRf72tKsA4w",
"is_extend": true,
"extra_amount": 0,
"extend_to": 1728459019000
}
]
}
*/
return response
}
/**
* @param {*} extend_to 你想要延长的时间(以毫秒为单位)
* @param {*} max_price 最大你愿意支付的延长费用(数字)
* @returns
*/
const SendInternalExtendRequest = async (extend_to, max_price) => {
const url = TRONSAVE_API_URL + `/internal-extend-request`
const estimate_response = await GetEstimateExtendData(extend_to, max_price)
if (estimate_response.extend_data && estimate_response.extend_data.length) {
const body = {
"extend_data": estimate_response.extend_data,
"receiver": RECEIVER,
}
const data = await fetch(url, {
method: "POST",
headers: {
'apikey': API_KEY,
"content-type": "application/json",
},
body: JSON.stringify(body)
})
const response = await data.json()
/**
* 示例响应
* @link //TODO
[<order_id>]
*/
return response
}
return []
}
// 示例运行代码
const ClientCode = () => {
const extend_to = +new Date() + 86400 * 1000 // 延长至下一个一天
const max_price = 200
SendInternalExtendRequest(extend_to, max_price).then(console.log)
}
ClientCode()
Last updated