JsonRpcProvider广播发送函数

问题描述 投票:0回答:1

请您给我一些建议,将我签名的 TX 广播到网络中的方法是什么?可以找到提供者实例的任何%send%方法

这是我的代码片段:

export async function signAndBroadcast(address, rawTransaction, jrpcUrl, key) {
    const provider = new ethers.JsonRpcProvider(jrpcUrl)
    expect( await checkBalance(address, provider)).toBe(true)
    // Initialize a new Wallet instance
    const wallet = new ethers.Wallet(key, provider);
    // Parse the raw transaction
    const tx = ethers.Transaction.from(rawTransaction);
    tx.nonce = await provider.getTransactionCount(wallet.address)
    const { name, chainId } = await provider.getNetwork()
    tx.chainId = chainId
    tx.value = ethers.parseUnits('32', 'ether')
    tx.gasLimit = 1000000
    tx.gasPrice = undefined
    tx.type = 2
    // Sign the transaction
    const signedTransaction = await wallet.signTransaction(tx);
    console.log("TX signed: " + signedTransaction)
    // Send the signed transaction
    const transactionResponse = await provider.sendTransaction(signedTransaction) // no function sendTransaction

    transactionResponse
        .then((transactionResponse) => {
            console.log('Transaction broadcasted, transaction hash:', transactionResponse.hash);
        })
        .catch((error) => {
            console.error('Error:', error);
        }).finally(() => {
            console.log('Finished')
        });
}

提前谢谢您!

javascript blockchain rpc ethers.js json-rpc
1个回答
0
投票

从这里

以太坊中的签名者是以太坊账户的抽象,它可以 用于签署消息和交易并发送签名交易 到以太坊网络来执行状态更改操作。

ethers.js 中的 Provider 负责从以太坊网络读取数据。它允许您查询区块链、检索数据和观察事件,而无需发送交易。另一方面,签名者通过添加创建和发送交易的能力来扩展提供者的功能。它具有提供商的所有功能,但还允许交易签名,使您能够将交易发送到以太坊网络。

从这里使用 v5 和 v6

const ethers = require('ethers');
(async () => {
    const provider = new ethers.providers.JsonRpcProvider('QUICKNODE_HTTPS_URL');
    const signer = new ethers.Wallet(process.env.PRIVATE_KEY, provider);

      const tx = await signer.sendTransaction({
        to: '0x92d3267215Ec56542b985473E73C8417403B15ac',
        value: ethers.utils.parseUnits('0.001', 'ether'),
      });
      console.log(tx);
})();

这是 v6

const ethers = require('ethers');
(async () => {
    const provider = new ethers.JsonRpcProvider('QUICKNODE_HTTPS_URL');
    const signer = new ethers.Wallet(process.env.PRIVATE_KEY, provider);

      const tx = await signer.sendTransaction({
        to: '0x92d3267215Ec56542b985473E73C8417403B15ac',
        value: ethers.parseUnits('0.001', 'ether'),
      });
      console.log(tx);
})();
© www.soinside.com 2019 - 2024. All rights reserved.