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)
from here
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
this is v6