javascript - 通过 Infura 的 Web3 SendSignedTransaction 未出现在 Etherscan 交易列表中
问题描述
概述
大家好,我既不是开发人员也不是程序员,但我已经启动了这个 Udacity 区块链纳米学位项目,并且开始编写一些区块链应用程序。
从课程开始,我开始编写一个简单的程序,将一些以太币从一个 Metamask 帐户发送到另一个帐户,两个帐户都在同一个测试网(Rinkeby)中。
到目前为止,这是我用于程序的内容:
- 带有 Rinkeby 测试网中 2 个帐户的元掩码。
- Web3 版本 1.5.1
- ethereumjs-tx 版本 2.1.2
- Infura Rinkeby 端点
问题
问题是课程已经过时,他们的大部分代码都不再工作了。因此,在通过 Web3 网站(链接: https ://web3js.readthedocs.io/en/v1.4.0/index.html)搜索 3 天后,我开始实现我的代码,我设法编写了你可以看到的代码在片段中。
该代码最终没有引发错误,当我检查事务数量(包括待处理的事务)时,每次运行代码时事务数量都会不断增加。但是,当我查看 Rinkeby Etherscan 网站(链接:https ://rinkeby.etherscan.io/ )时,在交易列表中找不到交易(已完成、待处理、失败、传出和传入交易)。
问题
- 我的代码有什么问题?我该如何解决它/他们?
- 如何计算 GasPrice 以及如何确定 GasLimit?我只想进行交易以发送没有数据的 X 数量的以太币。
- 我尝试使用与 Metamask 相同的 GasPrice 和 GasLimit,但它向我抛出错误“Intrinsic gas too low”。为什么会这样?因为,我在 Metamask 中使用这些值没有问题,并且从我的一个 metamask 帐户向另一个帐户发送一些以太币的交易不到 3 分钟就完成了(我从 Metamask 插件发送了交易)。
- 链条的叉子和这个问题有关系吗?如果是这样,我如何在 Metamask 中检查我的两个帐户的正确分叉?
笔记
我通过代码共享 senderAccount 的私钥,因为这两个帐户仅用于在 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();
推荐阅读
- java - Xamarin 错误仅 Android 6.0 Mono.AndroidTools.InstallFailedException 和 NSTALL_PARSE_FAILED_MANIFEST_MALFORMED
- python - 我们如何从 Gpy 获得多维输出
- python - DJango REST Framework:如何使用 ModelViewSet 编写 Create、List、Update 和 Delete
- sql - 如何根据匹配的列变量组合行
- clojure - 在 Clojure 中除以零
- javascript - 为什么我的导航栏下拉动画不起作用?
- python - 无法在 Pygame 中加载音乐
- java - 数组中的 LocalDate 给了我空结果
- .net-core - 使用 NuGet for InsuranceBot 示例在 VS 2017 中返回 .NetCore 错误
- python - 悬停在按钮上的顶层窗口和顶层窗口本身