JsonRpcProvider broadcasted sending function

48 Views Asked by At

Would you please give me advice on what is a method to broadcast my signed TX into the network? Can find any %send% method for provider instance

Here is my code snippet:

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')
        });
}

Thank you in advance!

*** RESOLVED ***

--- rewrite to typescript just not to make a mess ts/js

console.log("Sign TX")
            // Initialize a new Wallet instance
            const wallet = new ethers.Wallet(pk, provider);
            // Parse the raw transaction
            const tx = ethers.Transaction.from(serializeTx);    
            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);
            expect(signedTransaction).not.toBe("");
            
            console.log("Broadcast TX");
            const transactionResponse = await provider.broadcastTransaction(signedTransaction);
            const txHash = transactionResponse.hash
            console.log("TxHash => ", txHash);
            console.log("Goerli Etherscan =>\n", "https://goerli.etherscan.io/tx/" + txHash)
1

There are 1 best solutions below

0
Yilmaz On

from here

A Signer in ethers is an abstraction of an Ethereum Account, which can be used to sign messages and transactions and send signed transactions to the Ethereum Network to execute state changing operations.

The Provider in ethers.js is responsible for reading data from the Ethereum network. It allows you to query the blockchain, retrieve data, and observe events without the ability to send transactions. The Signer, on the other hand, extends the functionality of the Provider by adding the ability to create and send transactions. It has all the capabilities of a Provider but also allows for transaction signing, enabling you to send transactions to the Ethereum network.

from here using v5 and 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);
})();

this is 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);
})();