生成原始trx并将其发送到网络会显示“交易价格过低”错误

时间:2018-09-02 00:31:08

标签: php transactions ethereum signature

我正在尝试通过 php 和这些库进行以太坊原始交易:

https://github.com/simplito/elliptic-php

https://github.com/kornrunner/php-keccak

https://github.com/web3p/rlp

这是我的代码:

use Elliptic\EC;
use kornrunner\Keccak;
use Web3p\RLP\RLP;

$privateKeyHex = '.....'; // wallet private key
$toWallet = '118086be6247fBDa3BC64B4A11F07F3894aA1fAF';

$ec = new EC('secp256k1');
$key = $ec->keyFromPrivate($privateKeyHex );
$publicKeyHex = $key->getPublic('hex');
// $publicKeyHex => 0445e2caf0f227247dfa10440765812492e4d4c9df7b4e74d0d5cd3279fa80f5ef987a70e061ca20c06f09690957c9ba365cf06541181d1291e14c847d0d826583

$nonce= 0;
$gasPrice = 1e9;
$gasLimit = 21000;
$to = hex2bin($toWallet);
$value = 1e14-($gasLimit*$gasPrice)-1;
$inputData = 0;
//*********** EIP_155 *********
$chain_id = 1;
$r = 0;
$s = 0;
//*****************************

$SignData = [$nonce,$gasPrice,$gasLimit,$to,$value,$inputData,$chain_id,$r,$s];
$SignRlpData = rlpEncode($SignData);
$signHash = Keccak::hash(hex2bin($SignRlpData), 256);

$signature = $ec->sign($signHash ,$key);
$r = $signature->r->toString('hex');
$s = $signature->s->toString('hex');
$v = $chain_id*2 + ($signature->recoveryParam +35);

$trxData = [$nonce,$gasPrice,$gasLimit,$to,$value,$inputData,$v,hex2bin($r),hex2bin($s)];
$trxRlpData = rlpEncode($trxData );
// trxRlpData => f86980843b9aca0082520894118086be6247fbda3bc64b4a11f07f3894aa1faf8647d99eefefff8026a08a53214c92ff615c82eede2e51ab9d4d22d6a393f3ab8acee63c23d04e8e2fa7a07661f758f8b968d7597449ce05027edf3f2891fef0d1278f7330e99545109a2b




function rlpEncode($a){
    $rlp = new RLP;
    $encodedBuffer = $rlp->encode($a);
    return $encodedBuffer->toString('hex');
}

现在我通过https://etherscan.io/pushTx$trxRlpData的值发送到以太坊网络后,向我显示此错误消息:

Error! Unable to broadcast Tx : {"jsonrpc":"2.0","id":1,"error":{"code":-32000,"message":"transaction underpriced"}}

但是哪里出了问题?

2 个答案:

答案 0 :(得分:0)

最后,我通过增加一些 $ gasLimit 解决了这个问题。

$gasLimit = 21001;

答案 1 :(得分:0)

如果有一个待处理的交易,而您尝试发送一个与nonce相同的新交易,则会看到此错误。

如果您需要在尚未确认旧交易的情况下进行新交易,则必须获取待处理交易的地址getTransactionCount,并用作nonce