首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用cUrl和web3.php进行事务处理,我收到了收据,但事务没有发送到网络

使用cUrl和web3.php进行事务处理,我收到了收据,但事务没有发送到网络
EN

Stack Overflow用户
提问于 2018-12-23 17:12:57
回答 1查看 2.1K关注 0票数 1

这一整天都在做我的头,在费力地完成并设法将所有的值转换为十六进制之后,我使用来自GitHub的web3p/ ethereum库创建并签署了ethereum事务。我将带有参数的cUrl请求输入到infura。我得到了一个事务散列的响应,但是当我在以太扫描和其他搜索中搜索它时,它没有出现,有什么想法我做错了吗?

代码语言:javascript
复制
use Web3\Web3;
use Web3p\EthereumTx\Transaction;

  $balance = bcdiv($balanceInWei, "1000000000000000000", 18);
  $gasTotal = 4000000000 * 21004;
  $value = bcsub($balanceInWei, $gasTotal);
  $gas = dechex(21004);
  $gasPrice = dechex(4000000000);

  function bcdechex($dec) {
    $hex = '';
  do {    
    $last = bcmod($dec, 16);
    $hex = dechex($last).$hex;
    $dec = bcdiv(bcsub($dec, $last), 16);
  } while($dec>0);
    return $hex;
  }

  $hexValue = bcdechex($value);
  $nonce = time();
  $hexNonce = dechex($nonce);

  echo $wallet_address;
    // with chainId
    $transaction = new Transaction([
        'nonce' => '0x'.$hexNonce,
        'from' => $wallet_address,
        'to' => '0xMyWalletAddress',
        'gas' => '0x'.$gas,
        'gasPrice' => '0x'.$gasPrice,
        'value' => '0x'.$hexValue,
        'chainId' => 1,
        'data' => '0x0'
    ]);
    $signedTransaction = $transaction->sign($databaseContainer->private_key);

    $url = "https://mainnet.infura.io/v3/MyApiKey";
    $data = array(
            "jsonrpc" => "2.0",
            "method" => "eth_sendRawTransaction",
            "params" => array("0x".$signedTransaction),
            "id" => 1
    );
    $json_encoded_data = json_encode($data);

    var_dump($json_encoded_data);

    $ch = curl_init($url);
    curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
    curl_setopt($ch, CURLOPT_POSTFIELDS, $json_encoded_data);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch, CURLOPT_HTTPHEADER, array(
            'Content-Type: application/json',
            'Content-Length: ' . strlen($json_encoded_data))
    );

    $result = json_decode(curl_exec($ch));
    curl_close($ch);
    dd($result);

dd只是我把结果扔到了大桶里。提前谢谢。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2018-12-23 21:08:01

看起来,您正在使用十六进制编码的当前时间作为事务的当前时间。这不是正确的期望值;您需要确保您使用的是正确的预期帐户。您可以通过调用eth_getTransactionCount获得这个当前值。

还请注意,您正在接收的是事务散列,而不是收据。事务哈希指示您的事务已发送到网络。事务收据是成功挖掘/验证事务的指示器。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/53905585

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档