2

I want to call an action on a contract that is deployed on rinkeby test net. But transaction confirmation seems to take forever and I can't find it on rinkeby etherscan. Is there something wrong with my code?

let Web3= require('web3');
let web3 = new Web3 (new Web3.providers.HttpProvider("http://127.0.0.1:8545"));
var Tx = require('ethereumjs-tx');

tokenAbi = [{"constant":true,"inputs":[],"name":"mintingFinished","outputs":[{"name":"","type":"bool"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":true,"inputs":[],"name":"name","outputs":[{"name":"","type":"string"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":false,"inputs":[{"name":"_spender","type":"address"},{"name":"_value","type":"uint256"}],"name":"approve","outputs":[{"name":"","type":"bool"}],"payable":false,"stateMutability":"nonpayable","type":"function"},{"constant":true,"inputs":[],"name":"totalSupply","outputs":[{"name":"","type":"uint256"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":true,"inputs":[],"name":"supplyLimit","outputs":[{"name":"","type":"uint256"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":false,"inputs":[{"name":"_from","type":"address"},{"name":"_to","type":"address"},{"name":"_value","type":"uint256"}],"name":"transferFrom","outputs":[{"name":"success","type":"bool"}],"payable":false,"stateMutability":"nonpayable","type":"function"},{"constant":true,"inputs":[],"name":"decimals","outputs":[{"name":"","type":"uint8"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":false,"inputs":[],"name":"getAddress","outputs":[{"name":"","type":"address"}],"payable":false,"stateMutability":"nonpayable","type":"function"},{"constant":false,"inputs":[{"name":"_to","type":"address"},{"name":"_amount","type":"uint256"}],"name":"mint","outputs":[{"name":"","type":"bool"}],"payable":false,"stateMutability":"nonpayable","type":"function"},{"constant":false,"inputs":[{"name":"addr","type":"address"},{"name":"unlockTime","type":"uint256"}],"name":"lockTill","outputs":[],"payable":false,"stateMutability":"nonpayable","type":"function"},{"constant":true,"inputs":[],"name":"transfersAreLocked","outputs":[{"name":"","type":"bool"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":false,"inputs":[{"name":"_spender","type":"address"},{"name":"_subtractedValue","type":"uint256"}],"name":"decreaseApproval","outputs":[{"name":"","type":"bool"}],"payable":false,"stateMutability":"nonpayable","type":"function"},{"constant":true,"inputs":[{"name":"_owner","type":"address"}],"name":"balanceOf","outputs":[{"name":"","type":"uint256"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":false,"inputs":[],"name":"renounceOwnership","outputs":[],"payable":false,"stateMutability":"nonpayable","type":"function"},{"constant":false,"inputs":[],"name":"finishMinting","outputs":[{"name":"","type":"bool"}],"payable":false,"stateMutability":"nonpayable","type":"function"},{"constant":true,"inputs":[],"name":"owner","outputs":[{"name":"","type":"address"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":true,"inputs":[],"name":"symbol","outputs":[{"name":"","type":"string"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":true,"inputs":[],"name":"tokenOwner","outputs":[{"name":"","type":"address"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":false,"inputs":[{"name":"_to","type":"address"},{"name":"_value","type":"uint256"}],"name":"transfer","outputs":[{"name":"success","type":"bool"}],"payable":false,"stateMutability":"nonpayable","type":"function"},{"constant":false,"inputs":[{"name":"locked","type":"bool"}],"name":"changeTransferLock","outputs":[],"payable":false,"stateMutability":"nonpayable","type":"function"},{"constant":false,"inputs":[{"name":"_spender","type":"address"},{"name":"_addedValue","type":"uint256"}],"name":"increaseApproval","outputs":[{"name":"","type":"bool"}],"payable":false,"stateMutability":"nonpayable","type":"function"},{"constant":true,"inputs":[{"name":"_owner","type":"address"},{"name":"_spender","type":"address"}],"name":"allowance","outputs":[{"name":"","type":"uint256"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":false,"inputs":[{"name":"_newOwner","type":"address"}],"name":"transferOwnership","outputs":[],"payable":false,"stateMutability":"nonpayable","type":"function"},{"anonymous":false,"inputs":[{"indexed":true,"name":"to","type":"address"},{"indexed":false,"name":"amount","type":"uint256"}],"name":"Mint","type":"event"},{"anonymous":false,"inputs":[],"name":"MintFinished","type":"event"},{"anonymous":false,"inputs":[{"indexed":true,"name":"previousOwner","type":"address"}],"name":"OwnershipRenounced","type":"event"},{"anonymous":false,"inputs":[{"indexed":true,"name":"previousOwner","type":"address"},{"indexed":true,"name":"newOwner","type":"address"}],"name":"OwnershipTransferred","type":"event"},{"anonymous":false,"inputs":[{"indexed":true,"name":"owner","type":"address"},{"indexed":true,"name":"spender","type":"address"},{"indexed":false,"name":"value","type":"uint256"}],"name":"Approval","type":"event"},{"anonymous":false,"inputs":[{"indexed":true,"name":"from","type":"address"},{"indexed":true,"name":"to","type":"address"},{"indexed":false,"name":"value","type":"uint256"}],"name":"Transfer","type":"event"}]

const tokenAddress = '0xAfff042F602762B59442660aCDF34fdE8681D016';
var Contract = new web3.eth.Contract(tokenAbi, tokenAddress, {from: '0x6C7bc8e30cAE5409b30c16eFA458eADAaE6e6810'});
const tokenReceiver = '0x426e238593d7969f1beb174287a367dd9f41362F';
const tokenAmount = '100000000000000000000';
const PK = 'cdfa2d70c2f309106665188dc69996e3150983737b4b0882b768b11221d8a603'

const trxData = Contract.methods.transfer(tokenReceiver, tokenAmount).encodeABI();

web3.eth.getTransactionCount('0x6C7bc8e30cAE5409b30c16eFA458eADAaE6e6810').then(function(nonce){

  var rawTx = {
  to: '0xafff042f602762b59442660acdf34fde8681d016',
  data: trxData,
  gasPrice: 1000000000,
  gasLimit: 7000000,
  nonce: nonce,
  chainId: 4
}

var tx = new Tx(rawTx);
const privateKey = Buffer.from(PK, 'hex')
tx.sign(privateKey);

var serializedTx = tx.serialize();

web3.eth.sendSignedTransaction('0x' + serializedTx.toString('hex'))
.on('receipt', console.log);

})
0

The gasPrice is too low, try: 2000000000 (2Gwei) The gasLimit is too low, is even lower than the base price for a transaction, try increasing it (remember that the non-used gas is returned to you)

The nonce should be updated after every transaction, use:

web3.eth.getTransactionCount(address [, defaultBlock] [, callback])

Hope this helps.

  • 1
    Nope, still the same problem. Transaction takes forever to execute and I can't find it on rinkeby block explorer. Here is my updated code: ' var rawTx = { to: '0xafff042f602762b59442660acdf34fde8681d016', data: trxData, gasPrice: 2000000000, gasLimit: 7000000, nonce: nonce } ' – JustinZ Dec 4 '18 at 10:01
  • can you please update the question with your new code?, also can you share the hash of the transaction that is taking forever? – Jaime Dec 4 '18 at 10:40
  • Updated the code above – JustinZ Dec 4 '18 at 11:07
  • Your nonce is still incorrect, why are you adding 2 to the value returned by the function? The transaction has a nonce that is above the current one, and will not be processed until the missing values are observed in the network. – Jaime Dec 4 '18 at 13:22
  • Updated the code and used different account. But problem remains still – JustinZ Dec 4 '18 at 13:53
0

Most probably, gs limit of 100 is too low. Transaction might not be getting picked-up up for inclusion on pending block.

Try increasing the gas limit.

  • I have made tx limit 999. If I make it over 999 I get 'exceeds block limit' error. Altho rinkeby gas limit is 7119155. Is it something wrong here? – JustinZ Dec 3 '18 at 16:40
  • 1. What is the Transaction hash that you get after submitting transaction ? 2. Do you increment nonce for each transaction ? – Shamit Verma Dec 3 '18 at 17:12
  • transaction hash 0x65986d6a4df8e4b8c38977795d87b82b3089c60d7ba7183c9c8768963c927efe I can't find it on the Rinkeby network. And yes, I do increment it, because otherwise, it gives me an error. – JustinZ Dec 4 '18 at 8:36
  • Do you have a local node ? Does that node provide any info? (Like eth.getTransaction(txHash) on geth console . – Shamit Verma Dec 4 '18 at 16:55
  • Yes, I have a local node. And yes node provides information about the submitted transaction with transaction hash. – JustinZ Dec 5 '18 at 7:49
0

I have solved the problem. The problem was that there were too many stuck transactions in the tx pool, so I had to clean them by turning my node off and deleting transactions.rlp file

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.