Demo

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,
                "only_create_when_fulfilled":false,
                "max_price_accepted":200,
                "add_order_incomplete":false
            })
        })
        console.log(await dataCreateOrder.text());

Last updated