Copy <?php
require 'vendor/autoload.php';
use IEXBase\TronAPI\Tron;
class TronNRG
{
private $NRGSmartContractAddress = "TEeLFcbSc2LFSFrTZnWRCacZzo3ZtBybh2";
private $NRGAbi;
private $tron;
public function __construct(Tron $tron)
{
$this->NRGAbi = json_decode('[{ "stateMutability": "Nonpayable", "type": "Constructor" }, { "inputs": [{ "indexed": true, "name": "user", "type": "address" }, { "indexed": true, "name": "trxClaimed", "type": "uint256" }], "name": "commissionClaimed", "type": "Event" }, { "inputs": [{ "indexed": true, "name": "_buyer", "type": "address" }, { "name": "energyAmount", "type": "uint256" }, { "indexed": true, "name": "expireTime", "type": "uint256" }, { "indexed": true, "name": "affiliateID", "type": "uint256" }, { "name": "amountToFreeze", "type": "uint256" }, { "name": "rentalPeriod", "type": "uint256" }, { "name": "rentalTypeUnit", "type": "uint8" }], "name": "energyPurchase", "type": "Event" }, { "inputs": [{ "indexed": true, "name": "user", "type": "address" }, { "indexed": true, "name": "expireTime", "type": "uint256" }, { "name": "unfreezeAmount", "type": "uint256" }], "name": "energyUnfreeze", "type": "Event" }, { "inputs": [{ "indexed": true, "name": "user", "type": "address" }, { "indexed": true, "name": "nrgAmount", "type": "uint256" }, { "indexed": true, "name": "affiliate", "type": "uint256" }], "name": "nrgAdded", "type": "Event" }, { "inputs": [{ "indexed": true, "name": "user", "type": "address" }, { "indexed": true, "name": "nrgAmount", "type": "uint256" }, { "indexed": true, "name": "affiliate", "type": "uint256" }], "name": "nrgCompounded", "type": "Event" }, { "inputs": [{ "indexed": true, "name": "user", "type": "address" }, { "name": "nrgAmount", "type": "uint256" }], "name": "removeTimerStarted", "type": "Event" }, { "inputs": [{ "indexed": true, "name": "user", "type": "address" }, { "name": "unfreezeStarted", "type": "bool" }, { "name": "exitTime", "type": "uint256" }, { "name": "amount", "type": "uint256" }], "name": "unfreezeInfo", "type": "Event" }, { "inputs": [{ "name": "_switch", "type": "bool" }], "name": "BuySwitch", "stateMutability": "Nonpayable", "type": "Function" }, { "outputs": [{ "type": "address" }], "name": "DAOContract", "stateMutability": "View", "type": "Function" }, { "outputs": [{ "type": "bool" }], "inputs": [{ "type": "address" }], "name": "DAORole", "stateMutability": "View", "type": "Function" }, { "inputs": [{ "name": "_switch", "type": "bool" }], "name": "NRGProvidersSwitch", "stateMutability": "Nonpayable", "type": "Function" }, { "outputs": [{ "type": "address" }], "name": "NRGToken", "stateMutability": "View", "type": "Function" }, { "outputs": [{ "type": "address" }], "name": "ProxyContract", "stateMutability": "View", "type": "Function" }, { "inputs": [{ "name": "amount", "type": "uint256" }], "name": "StakeForEnergy", "stateMutability": "Nonpayable", "type": "Function" }, { "outputs": [{ "type": "address" }], "name": "StakingContract", "stateMutability": "View", "type": "Function" }, { "inputs": [{ "name": "contractAddress", "type": "address" }], "name": "addApprovedContract", "stateMutability": "Nonpayable", "type": "Function" }, { "inputs": [{ "name": "al", "type": "address" }], "name": "addToAL", "stateMutability": "Nonpayable", "type": "Function" }, { "inputs": [{ "name": "ar", "type": "address" }], "name": "addToAdminList", "stateMutability": "Nonpayable", "type": "Function" }, { "inputs": [{ "name": "dr", "type": "address" }], "name": "addToDaoList", "stateMutability": "Nonpayable", "type": "Function" }, { "name": "addToTeamBufferAndFreeze", "stateMutability": "Payable", "type": "Function" }, { "inputs": [{ "name": "amount", "type": "uint256" }], "name": "addToTeamBufferAndFreezeFromVotingRewards", "stateMutability": "Payable", "type": "Function" }, { "name": "addToTeamBufferWithoutFreeze", "stateMutability": "Payable", "type": "Function" }, { "outputs": [{ "name": "reward", "type": "uint256" }], "inputs": [{ "name": "user", "type": "address" }, { "name": "recipient", "type": "address" }], "name": "adminRewards", "stateMutability": "Nonpayable", "type": "Function" }, { "outputs": [{ "type": "bool" }], "inputs": [{ "type": "address" }], "name": "adminRole", "stateMutability": "View", "type": "Function" }, { "outputs": [{ "type": "bool" }], "inputs": [{ "type": "address" }], "name": "allowList", "stateMutability": "View", "type": "Function" }, { "outputs": [{ "name": "available", "type": "uint256" }], "name": "availableForFreeze", "stateMutability": "View", "type": "Function" }, { "inputs": [{ "name": "_receiver", "type": "address" }, { "name": "_energyRentalUnit", "type": "uint8" }, { "name": "_energyAmount", "type": "uint256" }, { "name": "affiliate", "type": "uint256" }, { "name": "_rentalPeriod", "type": "uint256" }], "name": "buyEnergy","outputs": [], "stateMutability": "Payable", "type": "Function" }, { "outputs": [{ "name": "total", "type": "uint256" }, { "name": "unfreezeTime", "type": "uint256" }], "inputs": [{ "name": "_energyRentalUnit", "type": "uint8" }, { "name": "_energyAmount", "type": "uint256" }, { "name": "rentalPeriod", "type": "uint256" }], "name": "calculatePrice", "stateMutability": "View", "type": "Function" }, { "outputs": [{ "type": "bool" }], "inputs": [{ "name": "user", "type": "address" }], "name": "canRemoveNRG", "stateMutability": "View", "type": "Function" }, { "outputs": [{ "name": "unstaked", "type": "bool" }], "inputs": [{ "name": "amount", "type": "uint256" }], "name": "canUnStakeTP", "stateMutability": "View", "type": "Function" }, { "name": "cancelremoveNRG", "stateMutability": "Nonpayable", "type": "Function" }, { "inputs": [{ "name": "user", "type": "address" }], "name": "cancelremoveNRGForUser", "stateMutability": "Nonpayable", "type": "Function" }, { "inputs": [{ "name": "comm", "type": "address" }], "name": "changeCommunityAddress", "stateMutability": "Nonpayable", "type": "Function" }, { "inputs": [{ "name": "_DAO", "type": "address" }], "name": "changeDAO", "stateMutability": "Nonpayable", "type": "Function" }, { "inputs": [{ "name": "amount", "type": "uint256" }], "name": "changeEnergyPrice", "stateMutability": "Nonpayable", "type": "Function" }, { "inputs": [{ "name": "_maxDays", "type": "uint256" }], "name": "changeMaxDays", "stateMutability": "Nonpayable", "type": "Function" }, { "inputs": [{ "name": "val", "type": "uint256" }], "name": "changeMinOrder", "stateMutability": "Nonpayable", "type": "Function" }, { "inputs": [{ "name": "_owner", "type": "address" }], "name": "changeOwner", "stateMutability": "Nonpayable", "type": "Function" }, { "inputs": [{ "name": "teamP", "type": "uint256" }, { "name": "userP", "type": "uint256" }, { "name": "commP", "type": "uint256" }, { "name": "stakeP", "type": "uint256" }], "name": "changePercentages", "stateMutability": "Nonpayable", "type": "Function" }, { "inputs": [{ "name": "_proxy", "type": "address" }], "name": "changeProxy", "stateMutability": "Nonpayable", "type": "Function" }, { "inputs": [{ "name": "val", "type": "uint256" }], "name": "changeRegenerationPeriodMultiplier", "stateMutability": "Nonpayable", "type": "Function" }, { "inputs": [{ "name": "_staking", "type": "address" }], "name": "changeStaking", "stateMutability": "Nonpayable", "type": "Function" }, { "inputs": [{ "name": "val", "type": "uint256" }], "name": "changeUnstakePeriod", "stateMutability": "Nonpayable", "type": "Function" }, { "outputs": [{ "name": "reward", "type": "uint256" }], "inputs": [{ "name": "user", "type": "address" }], "name": "claimRewards", "stateMutability": "Nonpayable", "type": "Function" }, { "outputs": [{ "name": "reward", "type": "uint256" }], "inputs": [{ "name": "user", "type": "address" }, { "name": "recipient", "type": "address" }], "name": "claimRewardsToDifferentAddress", "stateMutability": "Nonpayable", "type": "Function" }, { "outputs": [{ "name": "pending", "type": "uint256" }], "inputs": [{ "name": "userAddr", "type": "address" }], "name": "claimable", "stateMutability": "View", "type": "Function" }, { "outputs": [{ "type": "address" }], "name": "community", "stateMutability": "View", "type": "Function" }, { "outputs": [{ "type": "uint256" }], "name": "communityFunds", "stateMutability": "View", "type": "Function" }, { "outputs": [{ "type": "uint256" }], "name": "communityFundsP", "stateMutability": "View", "type": "Function" }, { "inputs": [{ "name": "user", "type": "address" }], "name": "compound", "stateMutability": "Nonpayable", "type": "Function" }, { "outputs": [{ "name": "dailyIncome", "type": "uint256" }, { "name": "unstakePeriod", "type": "uint256" }, { "name": "maxDays", "type": "uint256" }, { "name": "energyPrice", "type": "uint256" }, { "name": "staked", "type": "uint256" }, { "name": "accTokensPerShare", "type": "uint256" }, { "name": "amountScheduledOut", "type": "uint256" }, { "name": "frozen", "type": "uint256" }, { "name": "regenerationPeriodMultiplier", "type": "uint256" }, { "name": "minOrder", "type": "uint256" }], "name": "energyInfo", "stateMutability": "View", "type": "Function" }, { "outputs": [{ "type": "bool" }], "inputs": [{ "type": "address" }], "name": "exitBool", "stateMutability": "View", "type": "Function" }, { "outputs": [{ "type": "uint256" }], "inputs": [{ "type": "address" }], "name": "exitRequest", "stateMutability": "View", "type": "Function" }, { "outputs": [{ "type": "uint256" }], "inputs": [{ "type": "address" }], "name": "exitRequestAmount", "stateMutability": "View", "type": "Function" }, { "outputs": [{ "type": "int256" }], "name": "getAvailableEnergy", "stateMutability": "View", "type": "Function" }, { "outputs": [{ "type": "uint256" }], "inputs": [{ "name": "target", "type": "address" }], "name": "getAvailableUnfreezeV2Size", "stateMutability": "View", "type": "Function" }, { "outputs": [{ "name": "totalAmount", "type": "uint256" }, { "name": "frozenAmount", "type": "uint256" }], "name": "getBuffer", "stateMutability": "View", "type": "Function" }, { "outputs": [{ "type": "uint256" }], "name": "getCurrentEnegryRate", "stateMutability": "View", "type": "Function" }, { "outputs": [{ "type": "uint256" }], "inputs": [{ "name": "unitType", "type": "uint256" }], "name": "getDelegatableResource", "stateMutability": "View", "type": "Function" }, { "inputs": [{ "name": "user", "type": "address" }, { "name": "affiliate", "type": "uint256" }], "name": "getNRG", "stateMutability": "Payable", "type": "Function" }, { "outputs": [{ "type": "uint256" }, { "type": "uint256" }], "inputs": [{ "name": "target", "type": "address" }], "name": "getResourceUsage", "stateMutability": "View", "type": "Function" }, { "outputs": [{ "type": "uint256" }], "inputs": [{ "name": "b", "type": "address" }], "name": "getResourceV2", "stateMutability": "View", "type": "Function" }, { "outputs": [{ "type": "uint256" }], "inputs": [{ "name": "resourceType", "type": "uint256" }], "name": "getTotalAcquiredResource", "stateMutability": "View", "type": "Function" }, { "outputs": [{ "type": "uint256" }], "inputs": [{ "name": "resourceType", "type": "uint256" }], "name": "getTotalDelegatedResource", "stateMutability": "View", "type": "Function" }, { "outputs": [{ "type": "uint256" }], "inputs": [{ "name": "target", "type": "address" }], "name": "getTotalResource", "stateMutability": "View", "type": "Function" }, { "outputs": [{ "type": "bool" }], "inputs": [{ "type": "address" }], "name": "isApprovedContract", "stateMutability": "View", "type": "Function" }, { "name": "manageFunds", "stateMutability": "Nonpayable", "type": "Function" }, { "inputs": [{ "name": "_receiver", "type": "address" }, { "name": "_energyRentalUnit", "type": "uint8" }, { "name": "_energyAmount", "type": "uint256" }, { "name": "affiliate", "type": "uint256" }, { "name": "_rentalPeriod", "type": "uint256" }], "name": "orderBookBuy", "stateMutability": "Payable", "type": "Function" }, { "outputs": [{ "type": "address" }], "name": "owner", "stateMutability": "View", "type": "Function" }, { "outputs": [{ "type": "uint256" }], "inputs": [{ "name": "_owner", "type": "address" }], "name": "queryReceivedVoteCount", "stateMutability": "View", "type": "Function" }, { "outputs": [{ "type": "uint256" }], "name": "queryRewardBalance", "stateMutability": "View", "type": "Function" }, { "outputs": [{ "type": "uint256" }], "name": "queryTotalAvailableToVote", "stateMutability": "View", "type": "Function" }, { "outputs": [{ "type": "uint256" }], "name": "queryTotalVoteCount", "stateMutability": "View", "type": "Function" }, { "outputs": [{ "type": "uint256" }], "name": "queryUsedVoteCount", "stateMutability": "View", "type": "Function" }, { "outputs": [{ "type": "uint256" }], "inputs": [{ "name": "to", "type": "address" }], "name": "queryVoteCountBySr", "stateMutability": "View", "type": "Function" }, { "outputs": [{ "type": "uint256" }], "name": "readStaked", "stateMutability": "View", "type": "Function" }, { "inputs": [{ "name": "al", "type": "address" }], "name": "remFromAL", "stateMutability": "Nonpayable", "type": "Function" }, { "inputs": [{ "name": "ar", "type": "address" }], "name": "remFromAdminList", "stateMutability": "Nonpayable", "type": "Function" }, { "inputs": [{ "name": "dr", "type": "address" }], "name": "remFromDaoList", "stateMutability": "Nonpayable", "type": "Function" }, { "inputs": [{ "name": "contractAddress", "type": "address" }], "name": "removeApprovedContract", "stateMutability": "Nonpayable", "type": "Function" }, { "name": "removeCommFunds", "stateMutability": "Nonpayable", "type": "Function" }, { "inputs": [{ "name": "user", "type": "address" }], "name": "removeNRG", "stateMutability": "Nonpayable", "type": "Function" }, { "name": "removeTeamFunds", "stateMutability": "Nonpayable", "type": "Function" }, { "outputs": [{ "type": "uint256" }], "inputs": [{ "type": "address" }], "name": "rewardDebt", "stateMutability": "View", "type": "Function" }, { "inputs": [{ "name": "token", "type": "address" }, { "name": "staking", "type": "address" }, { "name": "proxy", "type": "address" }, { "name": "dao", "type": "address" }, { "name": "teamP", "type": "uint256" }, { "name": "userP", "type": "uint256" }, { "name": "commP", "type": "uint256" }, { "name": "stakeP", "type": "uint256" }], "name": "setUp", "stateMutability": "Nonpayable", "type": "Function" }, { "outputs": [{ "name": "setUpDone", "type": "bool" }, { "name": "userFailSafeEnabled", "type": "bool" }, { "name": "stopNRG", "type": "bool" }, { "name": "stopBuy", "type": "bool" }, { "name": "removeNRGOverride", "type": "bool" }, { "name": "allowCompound", "type": "bool" }], "name": "settings", "stateMutability": "View", "type": "Function" }, { "outputs": [{ "type": "uint256" }], "name": "stakingFunds", "stateMutability": "View", "type": "Function" }, { "outputs": [{ "type": "uint256" }], "name": "stakingFundsP", "stateMutability": "View", "type": "Function" }, { "inputs": [{ "name": "user", "type": "address" }, { "name": "amount", "type": "uint256" }], "name": "startRemoveTimer", "stateMutability": "Nonpayable", "type": "Function" }, { "outputs": [{ "type": "uint256" }], "name": "teamBufferFrozen", "stateMutability": "View", "type": "Function" }, { "outputs": [{ "type": "uint256" }], "name": "teamBufferTotal", "stateMutability": "View", "type": "Function" }, { "outputs": [{ "type": "uint256" }], "name": "teamFunds", "stateMutability": "View", "type": "Function" }, { "outputs": [{ "type": "uint256" }], "name": "teamFundsP", "stateMutability": "View", "type": "Function" }, { "inputs": [{ "name": "amount", "type": "uint256" }], "name": "unStakeTP", "stateMutability": "Nonpayable", "type": "Function" }, { "inputs": [{ "name": "user", "type": "address" }, { "name": "amount", "type": "uint256" }], "name": "unfreezeForUser", "stateMutability": "Nonpayable", "type": "Function" }, { "name": "unfreezeToContract", "stateMutability": "Nonpayable", "type": "Function" }, { "outputs": [{ "type": "uint256" }], "name": "unstakePeriodInDaysTime", "stateMutability": "View", "type": "Function" }, { "outputs": [{ "type": "uint256" }], "name": "unstakePeriodNetworkSetting", "stateMutability": "View", "type": "Function" }, { "inputs": [{ "name": "amount", "type": "uint256" }], "name": "unstakeTeamBuffer", "stateMutability": "Nonpayable", "type": "Function" }, { "inputs": [{ "name": "_allowCompound", "type": "bool" }], "name": "updateAllowCompound", "stateMutability": "Nonpayable", "type": "Function" }, { "inputs": [{ "name": "_newVal", "type": "bool" }], "name": "updateNRGRemoveOverride", "stateMutability": "Nonpayable", "type": "Function" }, { "inputs": [{ "name": "_from", "type": "address" }, { "name": "_to", "type": "address" }, { "name": "_amount", "type": "uint256" }, { "name": "_caller", "type": "address" }], "name": "updateUserRewardDebt", "stateMutability": "Nonpayable", "type": "Function" }, { "name": "userFailSafe", "stateMutability": "Nonpayable", "type": "Function" }, { "inputs": [{ "name": "enabled", "type": "bool" }], "name": "userFailSafeEnabledToggle", "stateMutability": "Nonpayable", "type": "Function" }, { "name": "userFailSafeMaintainingRewardDebt", "stateMutability": "Nonpayable", "type": "Function" }, { "outputs": [{ "type": "uint256" }], "name": "userFunds", "stateMutability": "View", "type": "Function" }, { "outputs": [{ "type": "uint256" }], "name": "userFundsP", "stateMutability": "View", "type": "Function" }, { "outputs": [{ "type": "bool" }], "name": "viewUserFailSafeEnabled", "stateMutability": "View", "type": "Function" }, { "inputs": [{ "name": "srList", "type": "address[]" }, { "name": "tpList", "type": "uint256[]" }], "name": "voteWitness", "stateMutability": "Nonpayable", "type": "Function" }, { "inputs": [{ "name": "amount", "type": "uint256" }], "name": "widthdrawTeamBuffer", "stateMutability": "Nonpayable", "type": "Function" }, { "name": "withdrawVotingReward", "stateMutability": "Nonpayable", "type": "Function" }, { "name": "withdrawVotingRewardToContract", "stateMutability": "Nonpayable", "type": "Function" }]',true);
$this->tron = $tron;
}
/**
* Calculate the available energy.
*
* This function computes the energy available based on the:
* 1. Delegatable Resource
* 2. Scheduled energy amount
* 3. Current Energy Rate
*
* The formula used is:
* energyAvailableNow = (getDelegatableResource - amountScheduledOut) / 1,000,000 * currentEnegryRate
*
* @return mixed Returns the calculated available energy, or false if there's an error.
*/
public function getAvailableEnergy() {
try {
// Fetch the delegatable resource. This represents the amount of energy that can be delegated or used.
$getDelegatableResource = ceil((int)$this->readTransaction("getDelegatableResource",[1])[0]->value);
// Get the amount of energy that is scheduled to be used or released.
$amountScheduledOut = round((int)$this->readTransaction("energyInfo",[])["amountScheduledOut"]->value);
// Get the current rate at which energy is consumed or generated.
$currentEnegryRate = (int)$this->readTransaction("getCurrentEnegryRate",[])[0]->value;
// Calculate the amount of energy available now.
$energyAvailableNow = ceil(($getDelegatableResource - $amountScheduledOut) / 1000000 * $currentEnegryRate);
error_log("INFO: Calculated energy available now: $energyAvailableNow");
return $energyAvailableNow;
} catch (Exception $e) {
error_log("ERROR: Error while calculating available energy: " . $e->getMessage());
return false;
}
}
/**
* Read a transaction from the blockchain.
*
* This function triggers a constant contract, which reads data from the blockchain without
* It uses the provided method name and parameters to fetch the desired
* data.
*
* @param string $methodName The name of the contract method to trigger.
* @param array $params The parameters to pass to the contract method. Default is an empty array.
* @return mixed The result of the triggered contract method, or false if there's an error.
*/
public function readTransaction($methodName, $params = []) {
try {
$transactionBuilder = $this->tron->getTransactionBuilder();
$result = $transactionBuilder->triggerConstantContract(
$this->NRGAbi,
$this->tron->toHex($this->NRGSmartContractAddress),
$methodName,
$params,
$this->tron->getAddress()["hex"]
);
error_log("INFO: Successfully read transaction using method: $methodName");
return $result;
} catch (Exception $e) {
error_log("ERROR: Error while reading transaction using method: $methodName. Error: " . $e->getMessage());
return false;
}
}
/**
* Send a transaction to the blockchain.
*
* This function triggers a smart contract which incurs a transaction cost. It uses
* the provided method name, parameters, and call value to execute the desired transaction.
*
* @param string $methodName The name of the contract method to trigger.
* @param array $params The parameters to pass to the contract method.
* @param int $callValue The amount of TRX to send with the transaction.
* @return mixed The result of the triggered contract method, or false if there's an error.
*/
public function sendTransaction($methodName, $params = [], $callValue) {
try {
$transactionBuilder = $this->tron->getTransactionBuilder();
return $transactionBuilder->triggerSmartContract(
$this->NRGAbi,
$this->tron->toHex($this->NRGSmartContractAddress),
$methodName,
$params,
450000000,
$this->tron->getAddress()["hex"],
$callValue
);
} catch (Exception $e) {
error_log("ERROR: Error while sending transaction using method: $methodName. Error: " . $e->getMessage());
return false;
}
}
/**
* Rent energy from the blockchain.
*
* This function will calculate the required cost to rent the energy and then initiate the
* necessary transaction to execute the rental.
*
* @param string $address The address to delegate resources to.
* @param int $unitType The type of unit. 1 is the default.
* @param int $amount The amount of energy to rent.
* @param int $affiliate The affiliate ID number; use 0 if not provided.
* @param int $rentalPeriod The length of the rental period in hours or days, based on unitType.
* @return mixed The result of the rental transaction, or false if there's an error.
*/
public function rentEnergy($address, $unitType = 1, $amount, $affiliate = 0, $rentalPeriod) {
try {
// Ensure the address is in the right format
$hexAddress = $this->tron->toHex($address);
// Parameters for the buyEnergy method in the smart contract
$params = [$hexAddress, $unitType, $amount, $affiliate, $rentalPeriod];
// Calculate the required call value based on the desired energy amount and other parameters
$callValue = $this->getQuote($unitType, $amount, $rentalPeriod);
$result = $this->sendTransaction("buyEnergy", $params, (int)$callValue);
$signedTransaction = $this->tron->signTransaction($result);
$res = $this->tron->sendRawTransaction($signedTransaction);
error_log("INFO: Successfully rented energy.");
return $res;
} catch (Exception $e) {
error_log("ERROR: Error while renting energy. Error: " . $e->getMessage());
return false;
}
}
/**
* Checks the available energy, gets a quote for the specified energy amount,
* and rents energy if there is enough energy available.
*
* @param string $address The address to delegate resources to.
* @param int $unitType Type of unit (e.g., 1 represents some specific type).
* @param int $amount The amount of energy to rent.
* @param int $affiliate The affiliate ID number; use 0 if not available.
* @param int $rentalPeriod The length of the rental period in hours or days, based on the unitType.
*/
function checkAndRentNRG($address, $unitType, $amount, $affiliate, $rentalPeriod) {
// Fetch the available energy
$availableEnergy = $this->getAvailableEnergy();
// Display the current available energy
echo "Available energy: " . $availableEnergy . " units.\n";
// Check if there's enough energy available to meet the desired amount
if ($availableEnergy >= $amount) {
// If there's enough energy, get a quote for renting the specified amount of energy
$quote = $this->getQuote($unitType, $amount, $rentalPeriod);
// Display the quote for the desired energy amount and rental period
echo "Quote for renting {$amount} units for {$rentalPeriod} time unit(s): {$quote} TRX.\n";
// Proceed to rent the desired energy amount
$this->rentEnergy($address, $unitType, $amount, $affiliate, $rentalPeriod);
// Confirm that the energy has been rented
echo "Successfully rented energy!\n";
} else {
// If not enough energy is available, display an error message
echo "Not enough energy available for rent.\n";
}
}
/**
* Get a quote for renting energy.
*
* This function retrieves the expected cost for renting a specific amount of energy
* over a given period.
*
* @param int $unitType The type of unit. 1 is the default.
* @param int $amount The amount of energy to rent.
* @param int $rentalPeriod The length of the rental period in hours or days, based on unitType.
* @return int The expected cost in TRX for renting the desired amount of energy.
*/
public function getQuote($unitType = 1, $amount, $rentalPeriod) {
try {
// Parameters for the calculatePrice method in the smart contract
$params = [$unitType, $amount, $rentalPeriod];
// Fetch the cost from the smart contract
$cost = $this->readTransaction("calculatePrice", $params)["total"]->value;
error_log("INFO: Successfully fetched quote for renting energy. Expected cost: {$cost}");
return $cost;
} catch (Exception $e) {
error_log("ERROR: Error while fetching quote for renting energy. Error: " . $e->getMessage());
return false;
}
}
}
?>