Created
July 6, 2024 11:12
-
-
Save nCally/09f550f09e1bf8da846b53db696369bb to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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