通过 Infura 发送的 Web3 SendSignedTransaction 不会出现在 Etherscan 事务列表中



我已经开始这个Udacity区块链纳米学位课程,我开始编程一些区块链应用程序。

从课程开始,我开始编写一个简单的程序,将一些Ether从一个Metamask帐户发送到另一个帐户,两个帐户都在同一个测试网(Rinkeby)。

这是我目前使用的程序:

  • 与Rinkeby Testnet中的2个帐户进行元掩码。
  • Web3版本1.5.1
  • ethereumjs-tx version 2.1.2
  • Infura Rinkeby Endpoint

问题

问题是课程过时了,他们的大部分代码不再工作了。因此,我在Web3网站(链接:https://web3js.readthedocs.io/en/v1.4.0/index.html)搜索了3天后开始实现我的代码,我设法编写了您可以在代码片段中看到的代码。

代码在我这端没有抛出错误,当我检查事务的数量(包括挂起的)时,每次我运行代码时,事务的数量都在增加。但是,当我检查Rinkeby Etherscan网站(链接:https://rinkeby.etherscan.io/)时,交易列表中找不到交易(已完成,待处理,失败,传出和传入交易)。

  • 我的代码有什么问题?我怎么解决它/他们?
  • 我如何计算汽油价格和如何确定汽油限额?我只想在没有数据的情况下进行交易,发送X数量的以太币。
  • 我试图使用相同的GasPrice和GasLimit作为Metamask,但它给了我错误"固有气体太低"。为什么会这样呢?因为,我在Metamask中使用这些值没有问题,并且从我的一个Metamask账户向另一个账户发送一些以太币的交易花费了不到3分钟的时间来完成(我从Metamask插件发送了交易)。
  • 链的分叉与这个问题有关吗?如果是这样,我如何检查我的两个账户在Metamask中的分叉是正确的?

注意

我通过代码共享了sendaccount的私钥,因为这2个帐户仅用于在Rinkeby测试网中测试此特定代码。我不打算把它们当钱包用。

// STEP 1: LOADING DEPENDENCIES
const Web3 = require('web3');
const web3 = new Web3('https://rinkeby.infura.io/v3/4fa53ccf01504cc69f0dcbdfdaa38acf');
const Transaction = require('ethereumjs-tx').Transaction;
async function sendTransaction() {
// STEP 2: INSTANCIATING ADDRESSES
const sendingAddress = '0x5Be6e93fE99374E506F4e3803e91EbDFe35D6A39';
const receivingAddress = '0x24620ddf8474c89C0Fc0c916acBcF4029C4eB47F';
// STEP 3: CONSTRUCTING THE TRANSACTION
const rawTx = {
from        :   web3.utils.toHex(sendingAddress),
to          :   web3.utils.toHex(receivingAddress),
value       :   web3.utils.toHex(900000000000000),
gasPrice    :   web3.utils.toHex(1000000000),
gasLimit    :   web3.utils.toHex(210000),
data        :   web3.utils.toHex(''),
nonce       :   web3.utils.toHex(await web3.eth.getTransactionCount(sendingAddress, 'pending')),
};
// STEP 4: GENERATING PRIVATE KEY FROM PRIVATE KEY OF ACCOUNT
const privateKey = Buffer.from('e603c35185142cc8779c47f9c88a81a52446aaa1398286abf3340178aee11c36', 'hex');
// STEP 5: INITIALIZATING THE TRANSACTION
const tx = new Transaction(rawTx, { chain: 'rinkeby', hardfork: 'istanbul' });
// STEP 6: SIGN TRANSACTION
tx.sign(privateKey);
// STEP 7: SERIALIZE TRANSACTION
const serializedTx = tx.serialize();
web3.eth.sendSignedTransaction('0x' + serializedTx.toString('hex')).on('receipt', console.log);
// BONUS: CHECKING NUMBER OF TRANSACTIONS
console.log(await web3.eth.getTransactionCount(sendingAddress, 'pending'));
}
sendTransaction();

好吧,我发现代码是不完整的,我需要实现更多的东西。这里是代码完成和100%工作。

// THIS IS THE LEGACY FORM TO SEND TRANSACTIONS
// Loading dependencies
const fs = require( 'fs' ).promises;
const Web3 = require( 'web3' );
const HDWalletProvider = require( '@truffle/hdwallet-provider' );
const { mnemonicGenerate } = require( '@polkadot/util-crypto' );
const Transaction = require('ethereumjs-tx').Transaction;
const Common = require('ethereumjs-common').default;
async function main () {  
// Infura rinkeby's url
const infuraRinkeby = INFURA_HTTPS;
// Generating bip39 mnemonic
// const mnemonic = mnemonicGenerate();
// save the mnemonic in a JSON file in your project directory
// console.log(mnemonic);
// Loading previously generated mnemonic
const mnemonic = ( JSON.parse( await fs.readFile(
"FILE_WITH_MNEMONIC.json" ,
"utf8" 
) ) ).mnemonic;
// Generating provider
const provider = new HDWalletProvider( mnemonic , infuraRinkeby );
const web3 = new Web3( provider );
// Declaring rinkeby testnet
const chain = new Common( 'rinkeby' , 'istanbul' );
// Getting sending and receiving addresses
//YOU CAN CHANGE 0 TO SELECT OTHER ADDRESSES
const sendingAddress = ( await web3.eth.getAccounts() )[0]; 
const receivingAddress = "DESTINATION_ADDRESS";
// Getting the private key for the account
const preKey = ( provider.wallets )[ sendingAddress.toLowerCase() ]
.privateKey.toString( 'hex' );
const privateKey = Buffer.from( preKey , 'hex' );
// Constructing the raw transaction
const rawTx = {
from        :   web3.utils.toHex( sendingAddress ),
to          :   web3.utils.toHex( receivingAddress ),
gasPrice    :   web3.utils.toHex( web3.utils.toWei( '1' , 'gwei' ) ),
gasLimit    :   web3.utils.toHex( 200000 ),
value       :   web3.utils.toHex( web3.utils.toWei( '0.25' , 'ether' ) ),
data        :   web3.utils.toHex( 'Hello World!' ),
nonce       :   web3.utils.toHex( await web3.eth.getTransactionCount( 
sendingAddress ,
'pending'
) ),
};
// Creating a new transaction
const tx = new Transaction( rawTx , { common : chain } );
// Signing the transaction
tx.sign( privateKey );
// Sending transaction
await web3.eth.sendSignedTransaction( '0x' + tx.serialize().toString( 'hex' ) )
.on( 'receipt', function( receipt ) {
console.log( receipt );
})
.on( 'error' , function( error ) {
console.error( error );
});   
};
main();

最新更新