How to Inherit the NRG Smart Contract for seamless energy rental

Inheriting the NRG smart contract empowers you to establish your own energy system without providing the energy itself. You can not only obtain quotes and rent energy from within your customized contract but also have the freedom to set your own pricing structure. This can be used as the backbone for your system or as the foundation for a completely new setup.

Follow the steps below to implement the NRG smart contract in your project:

It's important to note the the NRG contract will send back any TRX refund in the event of a price change. Tron smart contracts must have fall back functions or it will fail to trigger the buy energy functions as its possible there could be a refund.

    fallback() external payable {
        
    }

    receive() external payable {
        
    }

Step 1: Import the NRG Smart Contract Interface

First, you'll need to include the NRG smart contract's interface into your code. This will let you communicate and interact with the functionalities provided by the NRG contract.

interface INRGSalesLiveV3 {
    // Function definitions here
}

Step 2: Create Your Smart Contract

You can now create your own smart contract that will inherit from the NRG interface. This allows you to leverage the energy rental and calculation methods provided by the NRG contract.

contract MyEnergyContract is INRGSalesLiveV3 {
    // Your custom code here
}

Step 3: Get a Quote and Rent Energy

Within your contract, you can call methods to calculate the rental price and actually rent the energy. This will enable you to set up your own system and pricing, utilizing the existing NRG energy supply.

function getQuote(uint8 _energyRentalUnit, uint _energyAmount, uint rentalPeriod) public view returns (uint total, uint unfreezeTime) {
    // Code to get the quote
}

function rentEnergy( /* parameters */ ) public payable {
    // Code to rent the energy
}

Step 4: Customize as Needed

You can now extend the contract with your unique features, pricing structure, or additional functionalities. This could be tailored to your own energy system or to serve as the base for an entirely new application.

Full example

First, you'll need the interface for the NRG smart contract that allows for the calculation and rental of energy:

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.

interface INRGSalesLiveV3 {
    function calculatePrice(
        uint8 _energyRentalUnit,
        uint _energyAmount,
        uint rentalPeriod
    ) external view returns (uint total, uint unfreezeTime);

    function buyEnergy(
        address payable _receiver,
        uint8 _energyRentalUnit,
        uint _energyAmount,
        uint affiliate,
        uint _rentalPeriod
    ) external payable;
}

Now, you can create your own contract that inherits from this interface:

contract MyEnergyContract is INRGSalesLiveV3 {
    INRGSalesLiveV3 public NRGSalesLiveV3Instance;
    
// Live contract address TEeLFcbSc2LFSFrTZnWRCacZzo3ZtBybh2

    constructor(address _NRGSalesLiveV3Address) {
        NRGSalesLiveV3Instance = INRGSalesLiveV3(_NRGSalesLiveV3Address);
    }
    fallback() external payable {
        // add your logic 
    }

    receive() external payable {
        // add your logic 
    }
    function getQuote(
        uint8 _energyRentalUnit,
        uint _energyAmount,
        uint rentalPeriod
    ) public view returns (uint total, uint unfreezeTime) {
        (total, unfreezeTime) = NRGSalesLiveV3Instance.calculatePrice(_energyRentalUnit, _energyAmount, rentalPeriod);
        return (total, unfreezeTime);
    }

    function rentEnergy(
        address payable _receiver,
        uint8 _energyRentalUnit,
        uint _energyAmount,
        uint affiliate,
        uint _rentalPeriod
    ) public payable {
        (uint total, ) = getQuote(_energyRentalUnit, _energyAmount, _rentalPeriod);
        require(msg.value >= total, "The amount sent does not match the total cost of this rental");

        NRGSalesLiveV3Instance.buyEnergy{value: total}(_receiver, _energyRentalUnit, _energyAmount, affiliate, _rentalPeriod);
    }

    // Additional functions and customizations can go here
}

In this example, MyEnergyContract takes the address of an existing NRG smart contract and exposes methods to calculate the rental price (getQuote) and rent energy (rentEnergy). These methods can be integrated with your existing system and further customized as needed, providing the flexibility to add unique features or pricing structures.

If you require any further support of have questions you can always reach out to our team via our Telegram group

Last updated