Rent Tron Energy PHP - IEXBase TronAPI

This guide will walk you through the process of using TronNRG to rent energy for your application programmatically. By integrating with TronNRG, you can create a seamless user experience for your customers. The following steps demonstrate how to perform this integration using PHP and The IEXBase TronAPI.

Please ensure that you have properly configured IEXBase TronAPI before proceeding.

TronNRG offers a unique solution for renting energy for Tron transactions, allowing users to rent only the amount of energy needed for a single transaction. This feature can significantly reduce transaction fees and enhance cost efficiency for your users.

By integrating with TronNRG, your business gains access to our cutting-edge energy rental platform, providing exceptional benefits for both you and your users. Our unique system allows companies to integrate our platform seamlessly into their existing systems with just a few lines of code. This opens up new revenue streams and enhances user experiences with cost-effective transactions.

Step 1: Install and configure IEXBASE TronAPI

First we need to install IEXBASE TronAPI using composer

> composer require iexbase/tron-api --ignore-platform-reqs

Step 2: initialize IEXBASE TronAPI and import the TronNRGSDK

Now we need to set up some IEXBASE and import the NRG SDK which can be downloaded below

26KB
Open
Download the NRG SDK

Now we need to set up our IEXBASE using...

  • Your private key

  • Your address

Then initialize the NRG class with the IEXBASE variable

<?php
require 'vendor/autoload.php';

use IEXBase\TronAPI\Tron;
use IEXBase\TronAPI\Exception\TronException;
require 'TronNRG.php';

$fullNode = new \IEXBase\TronAPI\Provider\HttpProvider('https://api.trongrid.io');
$solidityNode = new \IEXBase\TronAPI\Provider\HttpProvider('https://api.trongrid.io');
$eventServer = new \IEXBase\TronAPI\Provider\HttpProvider('https://api.trongrid.io');

// Initialize Tron
try {
    $tron = new Tron($fullNode, $solidityNode, $eventServer);
    $tron->setPrivateKey("Replace with your private key");
    $tron->setAddress("Replace with the address associated with your private key");
    $NRG = new TronNRG($tron);  // Initialize the TronNRG SDK
} catch (TronException $e) {
    exit($e->getMessage());
}

// Example values to use for our function call
$address = "Replace with your target address";  // Replace with your target address the address you wish to apply energy to
$unitType = 1;
$amount = 10000; // amount of energy units
$affiliate = 0;  // affiliate id if you have one from https://tronnrg.com/Register
$rentalPeriod = 1;  // For example, 1 hour

// Call the checkAndRentEnergy function this will check if their is enough available enegry and then rent it to the address 
$NRG->checkAndRentNRG($address, $unitType, $amount, $affiliate, $rentalPeriod);

?>

If you are applying energy to other users accounts you may consider joining the NRG affiliate program, which allows you to earn 5% commission from your sales. Get your ID and pass it into the call.

Call the buyEnergy function with the following parameters:

  • address: The address to delegate resources to

  • unitType: Integer value 1

  • amount: The amount of energy to rent

  • affiliate: The affiliate ID number, if available; otherwise, use 0 as the default

  • rentalPeriod: The length of the rental period in hours or days, depending on the unitType

Our easy to use SDK allows you to easy check current energy levels, get a quote and place an order in one easy to use function. You can also expose any of the functions to check energy levels and get a quote if you would like to do this separately below is the full SDK file.

That is everything you need to rent energy from TronNRG using PHP, If you have any questions or require support you can join our Telegram group and a member of the team will be happy to help you.

Last updated