Create order
To use this feature, you must have the API key. Read here to view how to get our API key
Create a new order by API key
POST https://api.tronsave.io/v2/buy-resource
Create a new buy energy order by API key
Rate limit: 15 requests per 1 second
Headers
apikey*
String
Tronsave api key that present for internal account
Request Body
resourceType
String
"ENERGY" or "BANDWIDTH", default: ENERGY
unitPrice
Number, String
"FAST", "MEDIUM", "SLOW", or a number. Default: "MEDIUM"
-"FAST": If the market is ready to fill = 100%, FAST = MEDIUM. If the market is 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 is ready to fill = 0%, MEDIUM = SLOW + 10.
-"SLOW": The lowest price that can be set for this order.
-If the price is a number, the price unit is equal to SUN
resourceAmount*
Number
The number of resources.
receiver*
String
Resource receiving address
durationSec
Number
The duration of the bought resource, time unit, is in seconds. Default: 259200 (3 days).
sponsor
String
sponsor code
options
Object
optional
options.allowPartialFill
Boolean
Allow the order to be filled partially or not
options.onlyCreateWhenFulfilled
Boolean
[true]=> order only creates when it can be fulfilled
[false]=> order will create even if it can not be fulfilled
Default value: false
options.maxPriceAccepted
Number
Only create order when the estimate price less than this value.
options.preventDuplicateIncompleteOrders
Boolean
[true]=> Only create if no uncompleted order with the same parameters exists.
[false]=> Always create a new order, regardless of existing unfinished ones.
Default value: false
options.minResourceDelegateRequiredAmount
Number
The minimum resource amount delegated by a single provider.
Return the order ID if successful
{
"error": false,
"message": "Success",
"data": {
"orderId": "6818426a65fa8ea36d119d2c"
}
}{
"MISSING_PARAMS": "Missing some params in body",
"INVALID_PARAMS": "Some params are invalid",
"MIN_PRICE_INVALID": "minPrice is less than the system's minimum price or not in the correct format."
"INTERNAL_ACCOUNT_NOT_FOUND": "internal account does not exist",
"INTERNAL_BALANCE_ACCOUNT_TOO_LOW": "Balance is not enough",
"CANNOT_FULFILLED": "The order requires an immediate full match, but the system cannot fulfill 100% of it.",
"MUST_BE_WAIT_PREVIOUS_ORDER_FILLED": "A pending order with the same parameters already exists in the system.",
"PRICE_EXCEED_MAX_PRICE_REQUIRED": "The price of the order exceeds the maximum price accepted."
}{
"RATE_LIMIT": "Rate limit reached",
}Example
{
"resourceType": "ENERGY",
"receiver": "TFFbwz3UpmgaPT4UudwsxbiJf63t777777",
"durationSec": 3600,
"resourceAmount": 32000,
"unitPrice": "MEDIUM",
"options": {
"allowPartialFill": true,
"onlyCreateWhenFulfilled": true,
"preventDuplicateIncompleteOrders": false,
"maxPriceAccepted": 100,
"minResourceDelegateRequiredAmount": 100000
}
}{
"apikey": <YOUR_API_KEY>
}{
"error": false,
"message": "Success",
"data": {
"orderId": "6818426a65fa8ea36d119d2c"
}
}Example Code
const BuyResource = async (apiKey, receiverAddress, resourceAmount, durationSec, maxPriceAccepted) => {
const url = `${TRONSAVE_API_URL}/v2/buy-resource`
const body = {
resourceType: RESOURCE_TYPE,
unitPrice: "MEDIUM", //price in sun or "SLOW"|"MEDIUM"|"FAST"
resourceAmount, //Amount of resource wanted to buy
receiver: receiverAddress,
durationSec, //order duration in sec. Default: 259200 (3 days)
options: {
allowPartialFill: true,
onlyCreateWhenFulfilled: false,
maxPriceAccepted,
}
}
const data = await fetch(url, {
method: "POST",
headers: {
'apikey': apiKey,
"content-type": "application/json",
},
body: JSON.stringify(body)
})
const response = await data.json()
/**
* Example response
* {
* "error": false,
* "message": "Success",
* "data": {
* "orderId": "6809fdb7b9...a41d726fd"
* }
* }
*/
return response
}function buyResource($apiKey, $receiverAddress, $resourceAmount, $durationSec, $maxPriceAccepted) {
$url = TRONSAVE_API_URL . "/v2/buy-resource";
$body = [
'resourceType' => RESOURCE_TYPE,
'unitPrice' => "MEDIUM",
'resourceAmount' => $resourceAmount,
'receiver' => $receiverAddress,
'durationSec' => $durationSec,
'options' => [
'allowPartialFill' => true,
'onlyCreateWhenFulfilled' => false,
'maxPriceAccepted' => $maxPriceAccepted,
]
];
$ch = curl_init();
curl_setopt_array($ch, [
CURLOPT_URL => $url,
CURLOPT_POST => true,
CURLOPT_POSTFIELDS => json_encode($body),
CURLOPT_RETURNTRANSFER => true,
CURLOPT_HTTPHEADER => [
'apikey: ' . $apiKey,
'Content-Type: application/json'
]
]);
$response = curl_exec($ch);
curl_close($ch);
return json_decode($response, true);
}def buy_resource(amount: int, duration_sec: int, max_price_accepted: int) -> Dict[str, Any]:
"""Buy resource"""
url = f"{TRONSAVE_API_URL}/v2/buy-resource"
headers = {
'apikey': API_KEY,
'Content-Type': 'application/json'
}
body = {
'resourceType': RESOURCE_TYPE,
'unitPrice': "MEDIUM",
'amount': amount,
'receiver': RECEIVER_ADDRESS,
'durationSec': duration_sec,
'options': {
'allowPartialFill': True,
'onlyCreateWhenFulfilled': False,
'maxPriceAccepted': max_price_accepted,
}
}
response = requests.post(url, headers=headers, json=body)
return response.json()Get detailed orders here
Last updated