QNimate

  • CoursesVideos
  • WP PremiumPlugins
  • DemosLab
  • Bitcoin BankingBuy, Sell, Store and Earn Interest
  • Home
  • QIdea
  • QTrack
Home Carbon Ads Calculating Nonce for Raw Transactions in Geth

Calculating Nonce for Raw Transactions in Geth

You can simply use web3.eth.getTransactionCount to get the transaction nonce but it doesn’t include the transactions pending in the transaction pool. The purpose of this API is to simply retrieve total transactions count from the blockchain only till a particular block (by default latest block).

So when you are pushing a lot of transactions from a single account at a very fast rate then transactions will start getting overwritten as you cannot have two transactions with same nonce.

Here is the code to calculate the transaction nonce for raw transactions in Ethereum’s geth client which includes the total transactions in pool too.

function getNonce(address, callback) {
    web3.eth.getTransactionCount(address, function(error, result) {
        var txnsCount = result;
        web3.currentProvider.sendAsync({
            method: "txpool_content",
            params: [],
            jsonrpc: "2.0",
            id: new Date().getTime()
        }, function(error, result) {
            if (result.result.pending) {
                if (result.result.pending[address]) {
                    txnsCount = txnsCount +
                        Object.keys(result.result.pending[address]).length;
                    callback(null, txnsCount);
                } else {
                    callback(null, txnsCount);
                }
            } else {
                callback(null, txnsCount);
            }
        })
    })
}

Make sure you have tx_pool API enabled.

May 16, 2017Narayan Prusty
Pointing Domain to AWS Elastic Load BalancingWriting Upgradable Smart Contracts in Solidity
Comments: 0

    Leave a Reply Cancel reply

    To create code blocks or other preformatted text, indent by four spaces:

        This will be displayed in a monospaced font. The first four
        spaces will be stripped off, but all other whitespace
        will be preserved.
        
        Markdown is turned off in code blocks:
         [This is not a link](http://example.com)
    

    To create not a block, but an inline code span, use backticks:

    Here is some inline `code`.

    Narayan Prusty

    I am a full-stack web developer. I specialize in Blockchain and JavaScript. This is my personal blog where I write about programming and technologies that I learn and feel interesting to share.

    3 years ago 2 Comments Ethereum
    Share this
    0
    GooglePlus
    0
    Facebook
    0
    Twitter
    0
    Linkedin
    Related Articles
    • Writing Upgradable Smart Contracts in Solidity
    • Preloading Resources In Browser using rel=”prefetch”
    • Playing Audio Using JavaScript – Web Audio API
    • JavaScript Limit Function Call Rate
    • WordPress $notoptions Array
    Our Sponsor
    Freelance: I am available
    @narayanprusty
    Hi, I am a full-stack developer - focusing on JavaScript, WordPress, Blockchain, DevOps, Serverless and Cordova. You can outsource your app development to me.





    Will get back to you soon!!!
    WordPress
    ReactJS
    Meteor
    Blockchain
    Serverless
    Kubernetes
    DevOps
    DB & Storage
    Ejabberd
    Let's get started
    My Books

    2014 - 2015 © QNimate
    All tutorials MIT license