这一直在做我一整天,在涉足并设法将我的所有值转换为十六进制之后,我使用 GitHub 中的 web3p/ethereum-tx 库创建并签署以太坊交易。我输入了带有参数的 cUrl 请求到infura mainet。我收到带有交易哈希的响应,但是当我在etherscan和其他人上搜索它时,它没有显示,知道我做错了什么吗?
use Web3Web3;
use Web3pEthereumTxTransaction;
$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只是我把结果倾倒在幼虫中。提前谢谢。
看起来您正在使用十六进制编码的当前时间作为事务随机数。这不是正确的预期随机数;您需要确保使用正确的预期帐户随机数。您可以通过调用 eth_getTransactionCount
来获取此随机数值。
另请注意,您收到的是交易哈希,而不是收据。交易哈希是您的交易已发送到网络的指标。交易收据是您的交易成功被挖掘/验证的指标。