当前位置: 首页 > 知识库问答 >
问题:

Web3通过Infura发送SendSignedTransaction未出现在Etherscan交易列表中

微生阳平
2023-03-14

概述

大家好,我既不是开发人员也不是程序员,但我已经开始了这个Udacity区块链Nanodegree项目,并开始编写一些区块链应用程序

在课程中,我开始编写一个简单的程序,从一个元掩码帐户发送一些以太到另一个元掩码帐户,两个帐户在同一个testnet (Rinkeby)中。

这是我目前为止使用的程序:

    < Li > rinke by Testnet中2个帐户的元掩码。 < li>Web3版本1.5.1 < li>ethereumjs-tx版本2.1.2 < li>Infura Rinkebyendpoint

问题

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

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

问题

  • 我的代码有什么问题?我如何解决它/它们
  • 如何计算GasPrice和如何确定GasLimit?我只想做一个交易,在没有数据的情况下发送X数量的以太
  • 我尝试使用与Metamask相同的GasPrice和GasLimit,但它给我带来了“固有气体过低”的错误。为什么会这样?因为,我在Metamask中使用这些值时没有问题,并且从我的一个Metamask帐户向另一个帐户发送一些以太的事务不到3分钟就完成了(我从Metamask插件发送了该事务)
  • 链条的叉子与这个问题有关吗?如果是,如何在Metamask中为我的两个帐户检查正确的分叉

注意

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

密码

// 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();

共有1个答案

钮出野
2023-03-14

好吧,我发现代码不完整,我需要实现更多的东西。这里是代码完成和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();
 类似资料:
  • web3.eth.sendTransaction()方法向以太坊网络提交一个交易。 调用: web3.eth.sendTransaction(transactionObject [, callback]) 参数: transactionObject:Object - 要发送的交易对象,包含以下字段: from - String|Number: 交易发送方账户地址,不设置该字段的话,则使用web3

  • web3.eth.sendSignedTransaction()方法用来发送已经签名的交易,例如,可以使用web3.eth.accounts.signTransaction() 方法进行签名。 调用: web3.eth.sendSignedTransaction(signedTransactionData [, callback]) 参数: signedTransactionData:Strin

  • 问题内容: 我正在使用第一个Android应用程序来使用Google Cloud Messaging(GCM)服务进行推送通知。我已经可以从服务器应用程序成功发送消息,并将消息的内容记录在客户端应用程序的GCMIntentService类中的onMessage事件中。但是,我在设备上看不到任何视觉信息,表明已收到消息。我希望该消息会出现在手机的下拉通知列表中,就像iPhone一样。是否必须手动编码

  • 问题内容: 好的,我已经看到有关此问题的大量问题,但实际上没有一个答案对我有用,这是我的AJAX: var“ jsonFilters”包含一个包含以下数据的数组: 这是我的控制器: jsonFilters始终为null …我也尝试过添加到AJAX调用中…但是那实际上并没有做任何事情 最后,该类的结构如下: 关于我可能会丢失或正在发生的事情有什么想法吗? 到目前为止我尝试过的事情: 使用JSON.s

  • 我有一个配置有1个组织,2个对等体,5个订单(筏集群)的超级账本。网络有一个带有简单链码的通道。 当使用Hyperledger Caliper(最多7个客户端)将交易发送到网络时,对于每秒较低的交易数量,它可以正常工作。当我将TPS增加到60以上时,它会对一些事务抛出以下错误。 增加订购者数量可以减少被拒绝的交易数量。这看起来像是订购方中的缓冲区限制。 Fabric声称有大约1000 TPS,但是

  • 我正在使用RabbitMQ,我对使用扇出交换和类的(或)方法感到困惑。 例如,我有两个持久队列的使用者QUEUE-01和QUEUE-02,它们绑定到持久扇出交换fanout-01。并将1个发布服务器发送到FANOUT-01。我理解当消息使用(或)方法发布时会发生什么,消息将被复制到每个队列并由每个使用者处理。但我不确定如果我将调用方法会发生什么?我会从哪位消费者那里得到回复?有什么特别的行为吗?我