簡體   English   中英

發送原始交易以太坊infura nodejs npm

[英]Send Raw Transaction Ethereum infura nodejs npm

我目前正在嘗試對我的Typescript / Node項目實施以太坊節點連接。

我連接到“ Infura”節點服務器,需要在本地對我的交易進行簽名。 好吧,反正。 我正在使用npm包“ ethereumjs-tx”簽署我的交易,一切看起來都很好。 當我使用來自web3的“ sendRawTransaction”時,我的響應是一個tx-id,這意味着我的交易應該已經在區塊鏈中准備好了。 好吧...不是

我的簽名交易功能如下。

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

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

運行我的代碼如下所示:

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

因為sendRawTransaction會導致控制台日志中出現:[節點]事務完成=> 0xc1520ebfe0a225e6971e81953221c60ac1bfcd528e2cc17080b3f9b357003e34

一切都應該沒事。

有人遇到過同樣的問題嗎? 我希望有人能幫助我。 祝你今天愉快!

處理這些問題后無數次; 我很確定您發送的隨機數太高。

在這些情況下,該節點仍將向您返回事務哈希,但您的事務將保留在節點隊列中,並且不會進入內存池或傳播到其他節點UNTIL ,從而填補了現時缺口。

您可以嘗試以下方法:

  • 使用getTransactionCount(address,'pending')-包括作為int節點queue&內存池的tx。 但是這種方法不可靠,並且不能處理並發請求,因為該節點需要時間在任何給定時間評估正確的數量。

  • 保留自己的計數器,而不依賴於節點(除非您檢測到一些嚴重的錯誤)。

  • 對於更嚴重的項目,請使用鎖將您的計數器/每個地址保留在數據庫級別,以處理並發,確保為每個請求給出正確的隨機數。

干杯

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM