登录  /  注册
USDT TRC20交易因能量不足而失败
P粉412533525
P粉412533525 2023-12-24 20:46:43
[PHP讨论组]

我尝试每天广播一些 USDT 交易,几天后,我收到了一些交易的 OUT_OF_ENERGY 消息。

我每天都会进行一些交易,有时我会进行大约 30 笔交易。

首先,我尝试质押大约 20k TRX 来获取能量和带宽,但这还不够。

当我使用基于 Tron grid 文档的 PHP 脚本触发交易时,它显示 费用限制 为 10,因此我将 费用限制 增加到 15 TRX ( 15M SUN ) 但是仍然没有使用TRX来提交交易。但如果我从 TronLink 钱包手动提供它们,一切都很好。

我不知道我现在应该做什么。

我已经在 https://github.com/iexbase/tron-api 上使用 transferTrc20 方法在 TRX 网络上进行广播交易。

P粉412533525
P粉412533525

全部回复(1)
P粉512526720

我不知道是否需要,但我通过这种方式找到了解决方案。函数triggerConstantContract可以返回实际网络调用之前的能量消耗,但由于某种原因iexbase没有实现这样的逻辑。因此,我在 TransactionBuilder.php 中的 public function triggerConstantContract 之后添加了自己的函数,该函数返回包含能量信息的完整结果

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);
    }

}

因此你可以做类似的事情

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

以及对此的回应

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',
  ),
)
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 意见反馈 讲师合作 广告合作 最新更新
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习
PHP中文网抖音号
发现有趣的

Copyright 2014-2024 //m.sbmmt.com/ All Rights Reserved | php.cn | 湘ICP备2023035733号