使用签名交易 (Signed tx)

步骤1:获取估算 TRX (Get estimate TRX)

API Method: Post

https://api.tronsave.io/v0/estimate-trx

请求参数 (Request params):

地点
类型
必需
描述

amount

body

number

true

资源数量

buy_energy_type

body

string, number

true

"FAST", "MEDIUM", "SLOW" 等于快速,中等,慢速 或者按Sun单位填写想购买的价格

duration_millisec

body

number

true

购买资源的持续时间,时间单位为毫秒

request_address

body

string

false

请求者的地址

target_address

body

string

false

接收资源的地址

is_partial

body

boolean

false

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

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

{
      "amount": 100000,
      "buy_energy_type": "MEDIUM",
      "duration_millisec": 259200000
 }

回应 (Responses):

Field
Type
Required
Description

unit_price

number

true

适合您buy_energy_type 的能源价格(以SUN计)

duration_millisec

number

true

available_energy

number

true

unit_price相匹配的 Tronsave 市场上的总可用能源量

estimate_trx

number

true

预计将支付的总 TRX 价值,以 unit_priceduration_millisec内购买所有 available_energy

{
    "unit_price": 45,
    "duration_millisec": 259200000,
    "available_energy": 4298470,
     "estimate_trx": 13500000,
}

步骤 2: 获取已签名交易 (Get Signed Transaction)

在第一步之后,您将获得一个estimate_trx ,这是购买订单的成本。然后,您将使用transactionBuilder 创建一个转账交易,转账金额等于estimate_trx ,从买方地址转移到Tronsave市场的资金中:

const dataSendTrx = await tronWeb.transactionBuilder.sendTrx('TRONSAVE_FUND_ADDRESS', estimate_trx, 'BUYER_ADDRESS')
const signed_tx = await tronWeb.trx.sign(dataSendTrx, 'PRIVATE_KEY');

BUYER_ADDRESS 是买方的公共地址。

PRIVATE_KEY 是买方的私钥。

TRONSAVE_FUND_ADDRESS 是我们在主网上的 Tronsave 基金地址。

我们的 Tronsave 基金地址是 (TRONSAVE_FUND_ADDRESS ):

TWZEhq5JuUVvGtutNgnRBATbF8BnHGyn4S

步骤 3:创建订单

API Method: Post

https://api.tronsave.io/v0/buy-energy

请求参数 (Request params):

Field
Position
Type
Required
Description

resource_type

body

string

true

"ENERGY"

unit_price

body

number

true

填写价格以Sun为单位

allow_partial_fill

body

boolean

true

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

target_address

body

string

true

资源接收者的地址。

duration_millisec

body

number

true

购买资源的持续时间, 时间单位等于毫秒

tx_id

body

string

true

从第2步的已签名交易获取的交易ID

signed_tx

body

SignedTransaction

true

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

{
    "resource_type": "ENERGY",
    "unit_price": 45,
    "allow_partial_fill": true,
    "target_address": "TM6ZeEgpefyGWeMLuzSbfqTGkPv8Z6Jm4X",
    "duration_millisec": 259200000,
    "tx_id": "446eed36e31249b98b201db2e81a3825b185f1a3d8b2fea348b24fc021e58e0d",
    "signed_tx": {
      "visible": false,
      "txID": "446eed36e31249b98b201db2e81a3825b185f1a3d8b2fea348b24fc021e58e0d",
      "raw_data": {
        "contract": [
          {
            "parameter": {
              "value": {
                "amount": 13500000,
                "owner_address": "417a0d868d1418c9038584af1252f85d486502eec0",
                "to_address": "41055756f33f419278d9ea059bd2b21120e6add748"
              },
              "type_url": "type.googleapis.com/protocol.TransferContract"
            },
            "type": "TransferContract"
          }
        ],
        "ref_block_bytes": "0713",
        "ref_block_hash": "6c5f7686f4176139",
        "expiration": 1691465106000,
        "timestamp": 1691465046758
      },
      "raw_data_hex": "0a02071322086c5f7686f417613940d084b5999d315a68080112640a2d747970652e676f6f676c65617069732e636f6d2f70726f746f636f6c2e5472616e73666572436f6e747261637412330a15417a0d868d1418c9038584af1252f85d486502eec0121541055756f33f419278d9ea059bd2b21120e6add74818e0fcb70670e6b5b1999d31",
      "signature": ["xxxxxxxxx"]
    }
}

回应 (Responses):

{
    "message": "651d2306e55c073f6ca0992e" //order_id
}

测试用API列表 - Nile 测试网

  • 获取估算 TRX: POST https://api-dev.tronsave.io/v0/estimate-trx

  • 获取已签名交易: POST https://api-dev.tronsave.io/v0/signed-tx

  • 创建订单: POST https://api-dev.tronsave.io/v0/buy-energy

Code demo

const dataEstimateTrx = await fetch('https://api.tronsave.io/v0/estimate-trx', {
            method: 'POST',
            headers: {
                "Content-Type": "application/json",
            },
            body: JSON.stringify({
              "amount": 100000,
              "buy_energy_type": "MEDIUM",
              "duration_millisec": 259200000
   })
        })

        const { unit_price, duration_millisec, estimate_trx } = await dataEstimateTrx.json()
        console.log({ unit_price, duration_millisec, estimate_trx });

        const dataSendTrx = await tronWeb.transactionBuilder.sendTrx("TWZEhq5JuUVvGtutNgnRBATbF8BnHGyn4S", estimate_trx, 'BUYER_ADDRESS')
        const signed_tx = await tronWeb.trx.sign(dataSendTrx, 'PRIVATE_KEY');
        console.log(signed_tx);

        const dataCreateOrder = await fetch('https://api.tronsave.io/v0/buy-energy', {
            method: 'POST',
            headers: {
                "Content-Type": "application/json",
            },
            body: JSON.stringify({
                "resource_type": "ENERGY",
                unit_price,
                "allow_partial_fill": true,
                "target_address": "TM6ZeEgpefyGWeMLuzSbfqTGkPv8Z6Jm4X",
                duration_millisec,
                "tx_id": signed_tx.txID,
                signed_tx
            })
        })
        console.log(await dataCreateOrder.text());

Last updated