# 估算TRX

## 获取估算 TRX (Get estimate TRX)

#### <mark style="color:orange;">`POST`</mark>  `https://api.tronsave.io/v2/estimate-buy-resource` <a href="#api-method" id="api-method"></a>

速率限制 (Rate limit): 每1秒15个请求。

**请求参数 (Request params):**

<table><thead><tr><th width="198.33333333333331">域</th><th width="102">类型</th><th>描述</th></tr></thead><tbody><tr><td>resourceAmount<mark style="color:red;">*</mark></td><td>number</td><td>资源数量</td></tr><tr><td>unitPrice</td><td>string, number</td><td>"FAST", "MEDIUM", "SLOW" 等于快速，中等，慢速 或者按Sun单位填写想购买的价格:<br>-"FAST"：如果市场准备填满 = 100%，则 "FAST" = "MEDIUM"。如果市场准备填满 &#x3C; 100%，则 "FAST" = "MEDIUM" + 10。如果市场准备填满 = 0%，则 "FAST" = "SLOW" + 20。<br><br>-"MEDIUM"：这个订单的最大市场填充的最低价格。如果市场准备填满 = 0%，"MEDIUM" = "SLOW" + 10。<br><br>-"SLOW"：此订单可以设置的最低价格。<br><br>-如果价格是数字，则价格单位等于SUN。</td></tr><tr><td>durationSec</td><td>number</td><td>购买资源的持续时间，时间单位为毫秒</td></tr><tr><td>requester</td><td>string</td><td>请求者的地址</td></tr><tr><td>receiver</td><td>string</td><td>接收资源的地址</td></tr><tr><td>resourceType</td><td>string</td><td>“ENERGY”或“BANDWIDTH”，默认： “ENERGY”</td></tr><tr><td>options</td><td>Object</td><td><em>optional</em></td></tr><tr><td>options.allowPartialFill</td><td>boolean</td><td>允许订单部分匹配或整体匹配。</td></tr><tr><td>minResourceDelegateRequiredAmount</td><td>number</td><td>单个提供者委托的最低能量数量。</td></tr></tbody></table>

**请求参数示例 (Request params example):**

```json
{
    "resourceType": "ENERGY",
    "receiver": "TFwUFWr3QV376677Z8VWXxGUAMF123456",
    "durationSec": 259200,
    "resourceAmount": 32000,
    "unitPrice": "MEDIUM",
    "options": {
        "allowPartialFill": true,
        "minResourceDelegateRequiredAmount": 32000
    }
}
```

**回应 (Responses):**

```json
{
    "error": false,
    "message": "Success",
    "data": {
        "unitPrice": 64,
        "durationSec": 259200,
        "estimateTrx": 6144000,
        "availableResource": 32000
    }
}
```

**示例代码 (Example code):**

{% tabs %}
{% tab title="Javascript" %}

```javascript
const GetEstimate = async (requestAddress, receiverAddress, resourceAmount, durationSec) => {
    const url = TRONSAVE_API_URL + "/v2/estimate-buy-resource";
    const body = {
        resourceAmount,
        unitPrice: "MEDIUM",
        resourceType: RESOURCE_TYPE,
        durationSec,
        requester: requestAddress,
        receiver: receiverAddress,
        options: {
            allowPartialFill: true,
        },
    };
    const data = await fetch(url, {
        method: "POST",
        headers: {
            "content-type": "application/json",
        },
        body: JSON.stringify(body),
    });
    const response = await data.json();
    /**
     * Example response 
   {
        "error": false,
        "message": 'Success',
        "data": {
            "unitPrice": 50,
            "durationSec": 259200,
            "estimateTrx": 7680000,
            "availableResource": 32000
        }
    }
     */
    return response;
};
```

{% endtab %}

{% tab title="PHP" %}

```php
function getEstimate(): array {
    $url = TRONSAVE_API_URL . "/v2/estimate-buy-resource";
    $body = [
        'resourceAmount' => BUY_AMOUNT,
        'unitPrice' => "MEDIUM",
        'resourceType' => RESOURCE_TYPE,
        'durationSec' => DURATION_SEC,
        'requester' => REQUEST_ADDRESS,
        'receiver' => RECEIVER_ADDRESS,
        'options' => [
            'allowPartialFill' => true,
        ]
    ];

    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_POST, true);
    curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($body));
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch, CURLOPT_HTTPHEADER, [
        'Content-Type: application/json'
    ]);
    $response = curl_exec($ch);
    curl_close($ch);
    return json_decode($response, true);
}
```

{% endtab %}

{% tab title="Python" %}

```python
def get_estimate() -> Dict[str, Any]:
    """Get estimate for buying resource"""
    url = f"{TRONSAVE_API_URL}/v2/estimate-buy-resource"
    body = {
        'resourceAmount': BUY_AMOUNT,
        'unitPrice': "MEDIUM",
        'resourceType': RESOURCE_TYPE,
        'durationSec': DURATION_SEC,
        'requester': REQUEST_ADDRESS,
        'receiver': RECEIVER_ADDRESS,
        'options': {
            'allowPartialFill': True,
        }
    }
    
    response = requests.post(url, json=body)
    return response.json()
```

{% endtab %}
{% endtabs %}


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://docs.tronsave.io/chinese/developer/buy-resources-v2/use-signed-transaction/estimate-trx.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
