Corey
Corey

Reputation: 78

Ethereum Transaction Using cUrl and web3.php, I get a Receipt but the Transaction is not sent to the Network

This has been doing my head in all day, after wading through and managing to get all my values converted to hex I create and sign the ethereum transaction using the web3p/ethereum-tx library from GitHub. I put in the cUrl request with the parameters to the infura mainet. I get a response with a transaction hash but when I search for it on etherscan and others it does not show up, any ideas what I'm doing wrong?

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 is just me dumping the result in larvel. Thanks in advance.

Upvotes: 1

Views: 2804

Answers (1)

Ben
Ben

Reputation: 2706

It looks like you're using the hex-encoded current time as the transaction nonce. This is not not the correct expected nonce; you need to make sure you're using the correct expected account nonce. You can get this nonce value by calling eth_getTransactionCount.

Also note, that you're receiving a transaction hash, not a receipt. The transaction hash is an indicator that your transaction has been sent to the network. A transaction receipt is an indicator of your transaction being successfully mined/validated.

Upvotes: 1

Related Questions