使用cUrl和web3.php进行以太坊交易,我收到了收据,但交易未发送到网络

时间:2018-12-23 17:12:57

标签: php curl ethereum

经过漫长的工作并设法将我的所有值都转换为十六进制后,这整天都在起作用,我使用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中。预先感谢。

1 个答案:

答案 0 :(得分:0)

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

还请注意,您收到的是交易哈希,而不是收据。交易哈希表明您的交易已发送到网络。交易收据表示您的交易已成功开采/验证。