Send Raw Transaction Ethereum infura nodejs npm
I am currently trying to implement an ethereum Node connection to my Typescript / Node project.
I am connected to the "Infura" Node where I need to sign my transaction locally. Well, anyway. I sign my transaction using the "ethereumjs-tx" npm package and everything looks great. When I use "sendRawTransaction" from web3, my response is tx-id, which means my transaction should already be in the Blockchain. Well ... it's not
The sign "My signature" is shown below.
private signTransactionLocally(amountInWei: number, to: string, privateKey: string = <PRIVATE_KEY>, wallet: string = <MY_WALLET>) {
const pKeyBuffer = Buffer.from(privateKey, "hex");
const txParams = {
nonce: this.getNonce(true,wallet),
//gas: this.getGasPrice(true),
gasLimit: this.getGasLimit2(true),
to: to,
value: amountInWei,
data: '0x000000000000000000000000000000000000000000000000000000000000000000000000',
chainId: "0x1"
};
// console.log(JSON.stringify(txParams));
const tx = new this.ethereumTx(txParams);
tx.sign(pKeyBuffer);
return tx.serialize().toString("hex");
}
Functions used in "signTransactionLocally":
private getGasLimit2(hex: boolean = false) {
const latestGasLimit = this.web3.eth.getBlock("latest").gasLimit;
return hex ? this.toHex(latestGasLimit) : latestGasLimit;
}
private getNonce(hex:boolean = false, wallet: string = "0x60a22659E0939a061a7C9288265357f5d26Cf98a") {
return hex ? this.toHex(this.eth().getTransactionCount(wallet)) : this.eth().getTransactionCount(wallet);
}
Running my code looks like this:
this.dumpInformations();
const signedTransaction = this.signTransactionLocally(this.toHex((this.getMaxAmountToSend(false, "0x60a22659E0939a061a7C9288265357f5d26Cf98a") / 3 )), "0x38bc48f1d19fdf7c8094a4e40334250ce1c1dc66" );
console.log(signedTransaction);
this.web3.eth.sendRawTransaction("0x" + signedTransaction, function(err: any, res: any) {
if (err)
console.log(err);
else
console.log("transaction Done=>" + res);
});
since sendRawTransaction results in console log: [Node] transaction Done => 0xc1520ebfe0a225e6971e81953221c60ac1bfcd528e2cc17080b3f9b357003e34
everything should be fine.
Has anyone had the same problem? Hope someone can help me. Have a nice day!
source to share
After solving these issues countless times; I'm sure you are sending the nonce too high.
In these cases, the node will still return the transaction hash to you, but your transaction will remain in the node queue and will not enter the memory pool or propagate to other nodes BEFORE you fill in the nonce gaps.
You can try this:
-
Use getTransactionCount (address, 'pending') - to enable txs, which is a queue of int nodes and a memory pool. But this method is unreliable and will not handle concurrent requests because the node takes time to estimate the correct amount at any given time.
-
Keep your counter by not relying on the node (unless you find some serious bugs).
-
For more serious projects, keep the counter / address at the database level with locks to handle concurrency, making sure you issue the correct nonce for each request.
Hooray
source to share