【发布时间】:2019-05-23 03:41:31
【问题描述】:
这整天都在我的脑海里,在涉水并设法将我的所有值转换为十六进制之后,我使用来自 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 中。提前致谢。
【问题讨论】:
-
你为什么使用十六进制编码的当前时间来进行交易随机数?
-
我认为nonce可以是任何值,只要它大于之前交易的值。不是这样吗?
-
帐号nonce将从0x0开始,每次加1。如果您自己没有在其他地方跟踪它,您可以使用
web3.eth.getTransactionCount()接收适当的随机数。 -
感谢您的帮助,我尝试摆弄随机数,它似乎正在工作。
-
这是个好消息!我已经在下面发布了答案,请标记为已接受的答案,以便有类似问题的其他人能够获得适当的帮助。