Home  >  Q&A  >  body text

USDT TRC20 transaction failed due to insufficient energy

I tried broadcasting some USDT transactions every day, and after a few days, I received OUT_OF_ENERGY messages for some transactions.

I do a few trades every day, sometimes I do about 30 trades.

First, I tried staking about 20k TRX to gain energy and bandwidth, but it was not enough.

When I trigger the transaction using a PHP script based on the Tron grid documentation, it shows the fee limit is 10, so I increase the fee limit to 15 TRX (15M SUN) But TRX is still not used to submit the transaction. But if I provide them manually from TronLink wallet, everything is fine.

I don't know what I should do now.

I have used the transferTrc20 method on https://github.com/iexbase/tron-api to perform broadcast transactions on the TRX network.

P粉412533525P粉412533525274 days ago726

reply all(1)I'll reply

  • P粉512526720

    P粉5125267202023-12-25 18:46:34

    I don't know if it is needed, but I found the solution this way. Function triggerConstantContract can return the energy consumption before the actual network call, but for some reason iexbase does not implement such logic. Therefore, I added my own function after the public function triggerConstantContract in TransactionBuilder.php which returns the complete result including energy information

    public function triggerConstantContractFull($abi,
                                                $contract,
                                                $function,
                                                $params = [],
                                                $address = '410000000000000000000000000000000000000000')
        {
            $func_abi = [];
            foreach($abi as $key =>$item) {
                if(isset($item['name']) && $item['name'] === $function) {
                    $func_abi = $item + ['inputs' => []];
                    break;
                }
            }
    
            if(count($func_abi) === 0)
                throw new TronException("Function $function not defined in ABI");
    
            if(!is_array($params))
                throw new TronException("Function params must be an array");
    
            if(count($func_abi['inputs']) !== count($params))
                throw new TronException("Count of params and abi inputs must be identical");
    
    
            $inputs = array_map(function($item){ return $item['type']; },$func_abi['inputs']);
            $signature = $func_abi['name'].'(';
            if(count($inputs) > 0)
                $signature .= implode(',',$inputs);
            $signature .= ')';
    
            $eth_abi = new Ethabi([
                'address' => new Address,
                'bool' => new Boolean,
                'bytes' => new Bytes,
                'dynamicBytes' => new DynamicBytes,
                'int' => new Integer,
                'string' => new Str,
                'uint' => new Uinteger,
            ]);
            $parameters = substr($eth_abi->encodeParameters($func_abi, $params),2);
    
            $result = $this->tron->getManager()->request('wallet/triggerconstantcontract', [
                'contract_address' => $contract,
                'function_selector' => $signature,
                'parameter' => $parameters,
                'owner_address' =>  $address,
            ]);
    
            if(!isset($result['result'])){
                throw new TronException('No result field in response. Raw response:'.print_r($result,true));
            }
            if(isset($result['result']['result'])) {
                return $result;
            }
            $message = isset($result['result']['message']) ?
                $this->tron->hexString2Utf8($result['result']['message']) : '';
    
            throw new TronException('Failed to execute. Error:'.$message);
        }
    
    }

    So you can do something like this

    $transfer = $this->_tron->getTransactionBuilder()
        ->triggerConstantContractFull(
            $this->abiData,
            $this->_tron->address2HexString($this->contractAddress),
            'transfer',
            [$this->_tron->address2HexString($to), $tokenAmount],
            $this->_tron->address2HexString($from)
        );

    And responses to this

    array (
      'result' => 
      array (
        'result' => true,
      ),
      'energy_used' => 13430,
      'constant_result' => 
      array (
        0 => '0000000000000000000000000000000000000000000000000000000000000001',
      ),
      'logs' => 
      array (
        0 => 
        array (
          'address' => 'ea51342dabbb928ae1e576bd39eff8aaf070a8c6',
          'data' => '00000000000000000000000000000000000000000000000000000000004c4b40',
          'topics' => 
          array (
            0 => 'ddf252ad1be2c89b69c2b068fc378daa952ba7f163c4a11628f55a4df523b3ef',
            1 => '000000000000000000000000ea327173cc5e0fc7a39587ca1d767a6a1baaabcd',
            2 => '000000000000000000000000aad0e7d0dcd95d627ab12be594fdf011b939d8d9',
          ),
        ),
      ),
      'transaction' => 
      array (
        'ret' => 
        array (
          0 => 
          array (
          ),
        ),
        'visible' => false,
        'txID' => '97cba8afd15b5ce57cf2cd3a77e872da1dd6169c3d9fcecdfed1dbc08ba61a69',
        'raw_data' => 
        array (
          'contract' => 
          array (
            0 => 
            array (
              'parameter' => 
              array (
                'value' => 
                array (
                  'data' => 'a9059cbb000000000000000000000041aad0e7d0dcd95d627ab12be594fdf011b939d8d900000000000000000000000000000000000000000000000000000000004c4b40',
                  'owner_address' => '41ea327173cc5e0fc7a39587ca1d767a6a1baaabcd',
                  'contract_address' => '41ea51342dabbb928ae1e576bd39eff8aaf070a8c6',
                ),
                'type_url' => 'type.googleapis.com/protocol.TriggerSmartContract',
              ),
              'type' => 'TriggerSmartContract',
            ),
          ),
          'ref_block_bytes' => '0ab5',
          'ref_block_hash' => '7a928d3d5a95f196',
          'expiration' => 1677224169000,
          'timestamp' => 1677224111178,
        ),
        'raw_data_hex' => '0a020ab522087a928d3d5a95f19640a89ce792e8305aae01081f12a9010a31747970652e676f6f676c65617069732e636f6d2f70726f746f636f6c2e54726967676572536d617274436f6e747261637412740a1541ea327173cc5e0fc7a39587ca1d767a6a1baaabcd121541ea51342dabbb928ae1e576bd39eff8aaf070a8c62244a9059cbb000000000000000000000041aad0e7d0dcd95d627ab12be594fdf011b939d8d900000000000000000000000000000000000000000000000000000000004c4b4070cad8e392e830',
      ),
    )

    reply
    0
  • Cancelreply