步骤1:获取估算 TRX (Get estimate TRX)
https://api.tronsave.io/v0/estimate-trx
{
"amount": 100000,
"buy_energy_type": "MEDIUM",
"duration_millisec": 259200000
}
{
"unit_price": 45,
"duration_millisec": 259200000,
"available_energy": 4298470,
"estimate_trx": 13500000,
}
步骤 2: 获取已签名交易 (Get Signed Transaction)
const dataSendTrx = await tronWeb.transactionBuilder.sendTrx('TRONSAVE_FUND_ADDRESS', estimate_trx, 'BUYER_ADDRESS')
const signed_tx = await tronWeb.trx.sign(dataSendTrx, 'PRIVATE_KEY');
TWZEhq5JuUVvGtutNgnRBATbF8BnHGyn4S
https://api.tronsave.io/v0/buy-energy
{
"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"]
}
}
{
"message": "651d2306e55c073f6ca0992e" //order_id
}
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());