【问题标题】:Ethereum Transaction Using cUrl and web3.php, I get a Receipt but the Transaction is not sent to the Network使用 cUrl 和 web3.php 进行以太坊交易,我收到了收据,但交易未发送到网络
【发布时间】:2019-05-23 03:41:31
【问题描述】:

这整天都在我的脑海里,在涉水并设法将我的所有值转换为十六进制之后,我使用来自 GitHub 的 web3p/ethereum-tx 库创建并签署了以太坊交易。我将带有参数的 cUrl 请求放入 infura mainet。我收到带有交易哈希的响应,但是当我在 etherscan 和其他人上搜索它时它没有显示,任何想法我做错了什么?

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 只是我将结果倾倒在 larvel 中。提前致谢。

【问题讨论】:

  • 你为什么使用十六进制编码的当前时间来进行交易随机数?
  • 我认为nonce可以是任何值,只要它大于之前交易的值。不是这样吗?
  • 帐号nonce将从0x0开始,每次加1。如果您自己没有在其他地方跟踪它,您可以使用web3.eth.getTransactionCount() 接收适当的随机数。
  • 感谢您的帮助,我尝试摆弄随机数,它似乎正在工作。
  • 这是个好消息!我已经在下面发布了答案,请标记为已接受的答案,以便有类似问题的其他人能够获得适当的帮助。

标签: php curl ethereum


【解决方案1】:

您似乎正在使用十六进制编码的当前时间作为事务随机数。这不是正确的预期随机数;您需要确保使用正确的预期帐户 nonce。你可以通过调用eth_getTransactionCount来获取这个nonce值。

另外请注意,您收到的是交易哈希,而不是收据。交易哈希是您的交易已发送到网络的指标。交易收据表明您的交易已成功挖掘/验证。

【讨论】:

    猜你喜欢
    • 2021-10-02
    • 2018-11-04
    • 2019-01-07
    • 2018-09-22
    • 1970-01-01
    • 2013-04-02
    • 1970-01-01
    • 2022-07-19
    • 2018-09-19
    相关资源
    最近更新 更多