Skip to content

Instantly share code, notes, and snippets.

@nCally
Created July 6, 2024 11:12
Show Gist options
  • Select an option

  • Save nCally/09f550f09e1bf8da846b53db696369bb to your computer and use it in GitHub Desktop.

Select an option

Save nCally/09f550f09e1bf8da846b53db696369bb to your computer and use it in GitHub Desktop.
const sendSignedTransaction = async (
privateKey: string,
provider: string,
tx: any,
contractAddress: string) => {
const web3 = new Web3(provider);
const client = privateKeyToAddress(provider, privateKey);
const networkId = await web3.eth.net.getId();
const nonce = await web3.eth.getTransactionCount(client)
const gas = (await tx.estimateGas({ from: client, nonce }));
const gasPrice = await web3.eth.getGasPrice();
const data = tx.encodeABI()
const signedTx = await web3.eth.accounts.signTransaction({
to: contractAddress,
data,
gas,
gasPrice,
nonce,
chainId: networkId
}, privateKey)
// @ts-ignore
const receipt = await web3.eth.sendSignedTransaction(signedTx.rawTransaction);
return receipt;
}
export default sendSignedTransaction;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment