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

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

Ethereum用户
提问于 2018-12-23 18:11:42
回答 1查看 1.2K关注 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

Ethereum用户

发布于 2021-05-18 22:51:02

在我看来你的现在是不对的。Nonce应该是一个递增的事务计数器,而不是从unixtime戳派生的。另外,您似乎缺少gasLimit参数吗?

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

https://ethereum.stackexchange.com/questions/64518

复制
相关文章

相似问题

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