这一整天都在做我的头,在费力地完成并设法将所有的值转换为十六进制之后,我使用来自GitHub的web3p/ ethereum库创建并签署了ethereum事务。我将带有参数的cUrl请求输入到infura。我得到了一个事务散列的响应,但是当我在以太扫描和其他搜索中搜索它时,它没有出现,有什么想法我做错了吗?
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只是我把结果扔到了大桶里。提前谢谢。
发布于 2021-05-18 22:51:02
在我看来你的现在是不对的。Nonce应该是一个递增的事务计数器,而不是从unixtime戳派生的。另外,您似乎缺少gasLimit参数吗?
https://ethereum.stackexchange.com/questions/64518
复制相似问题