Docs

wallet/estimateenergy - Estimate TRON Energy Co...

Estimate energy consumption for TRON smart contract calls before execution. Optimize transaction costs and resource planning with Dwellir RPC.

Optimize Smart Contract Costs

Dwellir's TRON endpoints provide accurate energy estimation to help you optimize smart contract execution costs. Plan your transactions and avoid energy shortfalls with precise cost forecasting.

Optimize your energy usage

Estimates the energy consumption required for executing a smart contract call on the TRON network. Essential for cost planning and resource management.

When to Use This Method

wallet/estimateenergy is essential for:

  • Cost Planning - Estimate transaction costs before execution
  • Resource Management - Plan energy requirements for smart contracts
  • User Interfaces - Display estimated costs to users
  • Batch Operations - Calculate total energy needed for multiple calls
  • Energy Optimization - Compare different execution strategies

Request Parameters

Request
owner_addressstring

Address that will execute the contract call Example: `"TJmmqjb1DK9TTZbQXzRQ2AuA94z4gKAPFh"`

contract_addressstring

Smart contract address to call Example: `"TR7NHqjeKQxGTCi8q8ZY4pL8otSzgjLj6t"` (USDT)

function_selectorstring

Function signature to estimate Example: `"transfer(address,uint256)"`

parameterstring

Hex-encoded function parameters Example: `"0000000000000000000000004142b5e01c8c59a25d78acdbec2bfc7e89e5e86300000000000000000000000000000000000000000000000000000000000f4240"`

call_valueinteger

TRX amount to send (in SUN) for payable functions Default: `0`

visibleboolean

`true` - Use Base58 address format `false` - Use hex address format

Response Body

Response
resultOBJECT

`energy_required` - Estimated energy consumption `result` - Execution success/failure simulation `energy_used` - Base energy used for calculation

Implementation Examples

Bash
# Estimate energy for TRC20 transfer
curl -X POST "https://api-tron-mainnet.n.dwellir.com/YOUR_API_KEY/wallet/estimateenergy" \
  -H "Content-Type: application/json" \
  -d '{
    "owner_address": "TJmmqjb1DK9TTZbQXzRQ2AuA94z4gKAPFh",
    "contract_address": "TR7NHqjeKQxGTCi8q8ZY4pL8otSzgjLj6t",
    "function_selector": "transfer(address,uint256)",
    "parameter": "0000000000000000000000004142b5e01c8c59a25d78acdbec2bfc7e89e5e86300000000000000000000000000000000000000000000000000000000000f4240",
    "visible": true
  }'

# Extract energy estimation and calculate cost
curl -s -X POST "https://api-tron-mainnet.n.dwellir.com/YOUR_API_KEY/wallet/estimateenergy" \
  -H "Content-Type: application/json" \
  -d '{
    "owner_address": "TJmmqjb1DK9TTZbQXzRQ2AuA94z4gKAPFh",
    "contract_address": "TR7NHqjeKQxGTCi8q8ZY4pL8otSzgjLj6t",
    "function_selector": "transfer(address,uint256)",
    "parameter": "0000000000000000000000004142b5e01c8c59a25d78acdbec2bfc7e89e5e86300000000000000000000000000000000000000000000000000000000000f4240",
    "visible": true
  }' | jq '{
    energy_required: .energy_required,
    will_succeed: .result.result,
    estimated_cost_trx: (.energy_required * 0.00014),
    estimated_cost_sun: (.energy_required * 0.00014 * 1000000 | floor)
  }'

# Batch energy estimation script
#!/bin/bash
API_KEY="YOUR_API_KEY"
OWNER_ADDRESS="TJmmqjb1DK9TTZbQXzRQ2AuA94z4gKAPFh"

# Function to estimate energy
estimate_energy() {
  local contract_address=$1
  local function_selector=$2
  local parameters=$3
  local call_value=${4:-0}
  
  curl -s -X POST "https://api-tron-mainnet.n.dwellir.com/$API_KEY/wallet/estimateenergy" \
    -H "Content-Type: application/json" \
    -d "{
      \"owner_address\": \"$OWNER_ADDRESS\",
      \"contract_address\": \"$contract_address\",
      \"function_selector\": \"$function_selector\",
      \"parameter\": \"$parameters\",
      \"call_value\": $call_value,
      \"visible\": true
    }"
}

# Function to calculate costs
calculate_costs() {
  local energy_required=$1
  local energy_cost_per_unit=0.00014
  
  local cost_trx=$(echo "scale=8; $energy_required * $energy_cost_per_unit" | bc -l)
  local cost_sun=$(echo "scale=0; $cost_trx * 1000000 / 1" | bc -l)
  
  echo "Energy required: $energy_required"
  echo "Cost in TRX: $cost_trx"
  echo "Cost in SUN: $cost_sun"
}

# Function to check if execution can be free
check_free_execution() {
  local address=$1
  local energy_needed=$2
  
  local resources=$(curl -s -X POST "https://api-tron-mainnet.n.dwellir.com/$API_KEY/wallet/getaccountresource" \
    -H "Content-Type: application/json" \
    -d "{\"address\": \"$address\", \"visible\": true}")
  
  local energy_limit=$(echo "$resources" | jq -r '.EnergyLimit // 0')
  local energy_used=$(echo "$resources" | jq -r '.EnergyUsed // 0')
  local available_energy=$((energy_limit - energy_used))
  
  if [ "$available_energy" -ge "$energy_needed" ]; then
    echo "✅ Can execute for free using staked energy"
    echo "Available energy: $available_energy"
  else
    echo "❌ Insufficient staked energy"
    echo "Available: $available_energy, Needed: $energy_needed"
    echo "Shortfall: $((energy_needed - available_energy))"
  fi
}

# Estimate common operations
estimate_common_operations() {
  local contract_address=$1
  
  echo "=== Energy Estimation for Common Operations ==="
  echo "Contract: $contract_address"
  echo "Owner: $OWNER_ADDRESS"
  echo ""
  
  # TRC20 Transfer
  echo "1. TRC20 Transfer"
  local transfer_params="0000000000000000000000004142b5e01c8c59a25d78acdbec2bfc7e89e5e86300000000000000000000000000000000000000000000000000000000000f4240"
  local transfer_result=$(estimate_energy "$contract_address" "transfer(address,uint256)" "$transfer_params")
  local transfer_energy=$(echo "$transfer_result" | jq -r '.energy_required // 0')
  
  calculate_costs "$transfer_energy"
  check_free_execution "$OWNER_ADDRESS" "$transfer_energy"
  echo ""
  
  # TRC20 Approve
  echo "2. TRC20 Approve"
  local approve_result=$(estimate_energy "$contract_address" "approve(address,uint256)" "$transfer_params")
  local approve_energy=$(echo "$approve_result" | jq -r '.energy_required // 0')
  
  calculate_costs "$approve_energy"
  check_free_execution "$OWNER_ADDRESS" "$approve_energy"
  echo ""
  
  # Balance query (view function)
  echo "3. Balance Query (view function)"
  local balance_params="0000000000000000000000004142b5e01c8c59a25d78acdbec2bfc7e89e5e863"
  local balance_result=$(estimate_energy "$contract_address" "balanceOf(address)" "$balance_params")
  local balance_energy=$(echo "$balance_result" | jq -r '.energy_required // 0')
  
  calculate_costs "$balance_energy"
  echo ""
  
  # Summary
  local total_energy=$((transfer_energy + approve_energy + balance_energy))
  echo "=== Summary ==="
  echo "Total energy for all operations: $total_energy"
  calculate_costs "$total_energy"
}

# Energy optimization advisor
energy_optimization_advisor() {
  local total_energy_needed=$1
  
  echo "=== Energy Optimization Advisor ==="
  echo "Total energy needed: $total_energy_needed"
  echo ""
  
  # Calculate different strategies
  local pay_trx_cost=$(echo "scale=6; $total_energy_needed * 0.00014" | bc -l)
  local stake_trx_needed=$(echo "scale=2; $total_energy_needed / 4000" | bc -l)
  local rent_cost=$(echo "scale=6; $pay_trx_cost * 0.7" | bc -l)
  
  echo "Strategy Options:"
  echo ""
  echo "1. Pay with TRX"
  echo "   Cost: $pay_trx_cost TRX"
  echo "   Pros: Immediate execution, no staking required"
  echo "   Cons: Higher cost per transaction"
  echo ""
  echo "2. Stake TRX for Energy"
  echo "   Stake needed: ~$stake_trx_needed TRX"
  echo "   Pros: Reusable energy, long-term savings"
  echo "   Cons: TRX locked for 3 days"
  echo ""
  echo "3. Rent Energy"
  echo "   Cost: ~$rent_cost TRX"
  echo "   Pros: Lower cost than direct payment"
  echo "   Cons: One-time use, requires third-party"
  echo ""
  
  # Recommendation
  if (( $(echo "$pay_trx_cost < 1" | bc -l) )); then
    echo "💡 Recommendation: Pay with TRX (low cost)"
  elif (( $(echo "$stake_trx_needed < 100" | bc -l) )); then
    echo "💡 Recommendation: Consider staking TRX for energy"
  else
    echo "💡 Recommendation: Rent energy for better cost efficiency"
  fi
}

# Batch estimation for multiple contracts
batch_estimate() {
  local contracts_file=$1
  
  if [ ! -f "$contracts_file" ]; then
    echo "Error: Contracts file not found: $contracts_file"
    echo "Create a file with format: CONTRACT_ADDRESS:FUNCTION_SELECTOR:PARAMETERS"
    return 1
  fi
  
  echo "=== Batch Energy Estimation ==="
  echo "Processing contracts from: $contracts_file"
  echo ""
  
  local total_energy=0
  local operation_count=0
  
  while IFS=':' read -r contract_address function_selector parameters; do
    # Skip empty lines
    [ -z "$contract_address" ] && continue
    
    ((operation_count++))
    echo "Operation $operation_count: $function_selector"
    
    local result=$(estimate_energy "$contract_address" "$function_selector" "$parameters")
    local energy=$(echo "$result" | jq -r '.energy_required // 0')
    local will_succeed=$(echo "$result" | jq -r '.result.result // false')
    
    echo "  Energy: $energy"
    echo "  Will succeed: $will_succeed"
    
    total_energy=$((total_energy + energy))
    echo ""
  done < "$contracts_file"
  
  echo "=== Batch Summary ==="
  echo "Total operations: $operation_count"
  echo "Total energy required: $total_energy"
  
  if [ "$total_energy" -gt 0 ]; then
    calculate_costs "$total_energy"
    energy_optimization_advisor "$total_energy"
  fi
}

# Usage examples
case "${1:-common}" in
  "common")
    # Default: estimate common operations for USDT
    estimate_common_operations "TR7NHqjeKQxGTCi8q8ZY4pL8otSzgjLj6t"
    ;;
  "single")
    # Estimate single operation
    if [ $# -lt 3 ]; then
      echo "Usage: $0 single CONTRACT_ADDRESS FUNCTION_SELECTOR [PARAMETERS]"
      exit 1
    fi
    
    local result=$(estimate_energy "$2" "$3" "${4:-}")
    local energy=$(echo "$result" | jq -r '.energy_required // 0')
    
    echo "Energy estimation result:"
    echo "$result" | jq .
    echo ""
    calculate_costs "$energy"
    check_free_execution "$OWNER_ADDRESS" "$energy"
    ;;
  "batch")
    # Batch estimation
    batch_estimate "$2"
    ;;
  "optimize")
    # Energy optimization for specific amount
    energy_optimization_advisor "$2"
    ;;
  *)
    echo "Usage: $0 {common|single|batch|optimize} [args...]"
    echo ""
    echo "Commands:"
    echo "  common                                  - Estimate common TRC20 operations"
    echo "  single CONTRACT FUNCTION [PARAMS]      - Estimate single operation"
    echo "  batch CONTRACTS_FILE                   - Batch estimation from file"
    echo "  optimize ENERGY_AMOUNT                 - Get optimization advice"
    ;;
esac

Response Examples

Successful Energy Estimation

JSON
{
  "energy_required": 13326,
  "result": {
    "result": true
  },
  "energy_used": 13326
}

Failed Estimation (Insufficient Balance)

JSON
{
  "energy_required": 0,
  "result": {
    "result": false,
    "code": "REVERT",
    "message": "revert Insufficient balance"
  }
}

Need help? Contact our support team or check the TRON documentation.