0

I am Writing a simple script for ERC20 token transfer method. which sign transaction offline and then submit it to network.

Transfer method encoded data :

var encoded = await MyContract.methods
              .transfer(receiver_address, 10);
            

To get nonce of sender account :

var nonce = await web3.eth.getTransactionCount(
              sender_address,
              "pending"
            );

To get estimateed Gas of latest block:

let estimatedGas = await web3.eth.estimateGas({
            "from"      : sender_address,       
            "nonce"     : nonce, 
            "to"        : address_contract,     
            "data"      : encoded.encodeABI(),
        });

Here i create a tx object using ethereumjs-tx cdn link:

let privateKey = new ethereumjs.Buffer.Buffer(key, 'hex')
            var tx = {
              nonce: nonce,
              gasPrice: web3.utils.toHex(gasPrice),
              gasLimit: web3.utils.toHex( estimatedGas + 1000000),
              to: address_contract,
              data: encoded.encodeABI(),
            };
            let tx_object = new ethereumjs.Tx(tx , { chain: 'ropsten' })
            tx_object.sign(privateKey)
            let serializedTx = tx_object.serialize();

Then i submit transaction to ropsten test net:

     web3.eth.sendSignedTransaction('0X'+serializedTx.toString('hex'))
              .on("transactionHash", function (hash) {
                document.getElementById("error").innerHTML =
                  "Transaction Hash :" + hash;
              });

Gas Limit of transaction is :

Gas Limit:1051110 also tried with gas Limit of latest block 8000029

Gas Price of transaction is :

Gas Price:0.000000002 Ether (2 Gwei)

But my problem here is that transactions get stuck at pending state from two days. i am attaching image and link to contract address

enter image description here

link of contract: https://ropsten.etherscan.io/address/0xdc11d98aa50b99a948a4fc2e159c30c5ba9a8364

following are version of web3 and etherumjs-tx:

  • web3 version 1.0.0-beta.34 (also tried 1.0.0-beta.26 and @latest)
  • Ethereumjs-tx version ethereumjs-tx-1.3.3

I don't think that there is problem with my gas Price or gas Limit.

0

Your gas price is too low.

enter image description here

You can check over here for an "estimate", currently 2 gwei as I write: https://ropsten-stats.parity.io/

I imagine you mean to say 8 gwei but it is off by several orders of magnitude.

This is an opportunity to learn about stuck transactions and now to resolve them which any process that sends transactions at scale should address.

Things to know.

Transactions from any account are assured that they will be mined in nonce order. Great, but if the sender (or wallet) keeps increasing the nonce, none of those transactions will be mined either. The stuck transaction must be mined, or must be replaced.

This is better than nothing but it is not reliable.

var nonce = await web3.eth.getTransactionCount(
  sender_address,
  "pending"
);

The unreliability stems from the pending pool being subjective from each node's perspective. They can't report on pending transactions they don't know about. So, you want to manage the nonce yourself and increment it as you go along. You can use a function like that to initialize it and use what you get, but do so with the knowledge that you might cancel or compete with pending transactions your node hasn't heard about.

That gives us a clue about how to cancel a transaction.

CANCEL

To cancel a pending transaction, get another transaction with the same nonce to mine first.

When your automated process (or you) decide that it's been too long, attend to off-chain concerns like your records and then emit a transaction to cancel the target. It should have a gas price higher than the transaction to cancel (I would be aggressive about the gas price just to get feedback quickly), it should have the same nonce as the target and it should be from the same account.

You can send { from: me, to: me, value: 0, nonce: victim } with no data. It's a "do nothing" transaction, but it's valid. If you price it accordingly, it will be mined quickly and you will be able to confirm that it mined like any other transaction. That will positively prove that the original target will never be mined, so it is safe to do it (whatever it was) again.

Hope it helps.

| improve this answer | |
  • problem was in my first transaction stuck at pending state. problem was fixed by increasing gas price of first stuck transaction. i just replace nonce and provide nonce of first transaction like below: ` var tx = {` nonce: web3.utils.toHex(0), gasPrice: web3.utils.toHex(gasPrice), gasLimit: web3.utils.toHex( estimatedGas + 1000000), to: address_contract, }; all transaction are mined like a charm. – Block Crasher Aug 2 at 16:10

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.