创建订单

创建订单

API Method:

POST https://api.tronsave.io/v2/buy-resource

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

请求参数 (Request params):

类型
描述

resourceType

string

“ENERGY”或“BANDWIDTH”,默认: “ENERGY”

unitPrice

number

填写价格以SUN为单位

resourceAmount*

number

资源数量

receiver*

string

资源接收者的地址。

durationSec

number

购买资源的持续时间,时间单位为秒。默认值:259200(3天)。

sponsor

string

赞助代码

signedTx

SignedTransaction

已签名交易,请注意它是第2步中的JSON对象响应 (signed_tx ->步骤2 )

options

Object

optional

options.onlyCreateWhenFulfilled

Boolean

[true] => 仅在订单能够完成时创建订单

[false] => 即使订单无法完成也会创建订单

默认值:false

options.allowPartialFill

Boolean

允许订单部分匹配或整体匹配。

options.preventDuplicateIncompleteOrders

Boolean

[true]=>仅在不存在具有相同参数的未完成订单时创建订单。

[false]=>无论是否存在未完成订单,始终创建新订单。

默认值:false

options.maxPriceAccepted

number

仅在预估价格小于该值时创建订单。

options.minResourceDelegateRequiredAmount

number

单个提供者委托的最小能量数量。

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

{
    "resourceType": "ENERGY",
    "receiver": "TFFbwz3UpmgaPT4UudwsxbiJf63t777777",
    "durationSec": 3600,
    "resourceAmount": 32000,
    "unitPrice": 80,
    "options": {
        "allowPartialFill": true,
        "onlyCreateWhenFulfilled": true,
        "preventDuplicateIncompleteOrders": false,
        "maxPriceAccepted": 100,
        "minResourceDelegateRequiredAmount": 100000
    },
    "signedTx": 
    {
        "visible": false,
        "txID": "795f8195893e8da2ef2f70fc3a1f2720f9077244c4b7b1c50c99c72fda675a32",
        "raw_data_hex": "0a02b32e2208ce1cb373c238875e4098fbd9a0eb325a68080112640a2d747970652e676f6f676c65617069732e636f6d2f70726f746f636f6c2e5472616e73666572436f6e747261637412330a1541417ca6f74356a5d4454498e3f4856c945a04ab01121541055756f33f419278d9ea059bd2b21120e6add74818e0e5a40170b8a6d6a0eb32",
        "raw_data": {
            "contract": [
                {
                    "parameter": {
                        "value": {
                            "to_address": "41055756f33f419278d9ea059bd2b21120e6add748",
                            "owner_address": "41417ca6f74356a5d4454498e3f4856c945a04ab01",
                            "amount": 2700000
                        },
                        "type_url": "type.googleapis.com/protocol.TransferContract"
                    },
                    "type": "TransferContract"
                }
            ],
            "ref_block_bytes": "b32e",
            "ref_block_hash": "ce1cb373c238875e",
            "expiration": 1746778095000,
            "timestamp": 1746778035000
        },
        "signature": [
            "xxxxxxxxxxxxxxxxxxx"
        ]
    }
}

回应 (Responses):

{
    "error": false,
    "message": "Success",
    "data": {
        "orderId": "6818426a65fa8ea36d119d2c"
    }
}

示例代码 (Example Code):

const CreateOrder = async (resourceAmount, signedTx, receiverAddress, unitPrice, durationSec, options) => {
    const url = TRONSAVE_API_URL + "/v2/buy-resource";
    const body = {
        resourceType: RESOURCE_TYPE,
        resourceAmount,
        unitPrice,
        allowPartialFill: true,
        receiver: receiverAddress,
        durationSec,
        signedTx,
        options
    };
    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": {
     *         "orderId": "6809fdb7b9ba217a41d726fd"
     *     }
     * }
     */
    return response;
};

Last updated