4

I have an error after I send signedTransaction always return error node:

(node:16924) UnhandledPromiseRejectionWarning: Error: Transaction has been reverted by the EVM: { "blockHash": ........

look my code and error:

CODE:

var Web3 = require('web3')
var web3= new Web3(new
Web3.providers.HttpProvider('https://mainnet.infura.io/v3/.....'))
var tokenABI=[{"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":"success","type":"bool"}],"payable":false,"stateMutability":"nonpayable","type":"function"},{"constant":true,"inputs":[],"name":"totalSupply","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":true,"inputs":[{"name":"","type":"address"}],"name":"balanceOf","outputs":[{"name":"","type":"uint256"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":true,"inputs":[],"name":"symbol","outputs":[{"name":"","type":"string"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":false,"inputs":[{"name":"_to","type":"address"},{"name":"_value","type":"uint256"}],"name":"transfer","outputs":[],"payable":false,"stateMutability":"nonpayable","type":"function"},{"constant":false,"inputs":[{"name":"_spender","type":"address"},{"name":"_value","type":"uint256"},{"name":"_extraData","type":"bytes"}],"name":"approveAndCall","outputs":[{"name":"success","type":"bool"}],"payable":false,"stateMutability":"nonpayable","type":"function"},{"constant":true,"inputs":[{"name":"","type":"address"},{"name":"","type":"address"}],"name":"allowance","outputs":[{"name":"","type":"uint256"}],"payable":false,"stateMutability":"view","type":"function"},{"inputs":[],"payable":false,"stateMutability":"nonpayable","type":"constructor"},{"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"}]
var contractAddr='0x63C94B6221B4021b564F4563ec72688655b2604A'
var destinityAddr='0xaB3C01D51B6E301dA185601BFEe39AE29F6C16ce'
var pKey='xxxxxxxx' //private key
var sendingAddr='0x14e56f3C2C4736181cEEE69FDb5F89847f42a9AF'
var nonces=""
web3.eth.getTransactionCount(sendingAddr,function(err,res){nonces=res})
var myContractes= new web3.eth.Contract(tokenABI,contractAddr)
var Tx= require('ethereumjs-tx')
var dataso= myContractes.methods.transfer(destinityAddr,web3.utils.toHex(1e18)).encodeABI()
var rawTx={nonce:nonces,gasPrice:web3.utils.toHex('20000000000'),gasLimit:web3.utils.toHex('21000'),to:contractAddr,value:0,data:dataso}
var tx= new Tx(rawTx)
var privateKeyBuffer= Buffer.from(pKey,'hex')
tx.sign(privateKeyBuffer)
var serializedTx=tx.serialize()
//and here sent signed transaction
web3.eth.sendSignedTransaction('0x'+serializedTx.toString('hex'),function(err,hash){console.log(hash)}else{console.log(err)})

and when I executed shows the hash: 0x48b75130d6041d8ff19c063892e77dc4c7e2d5a94049a5a8ab4de6ee054cbc19

all is OK till here, however after waiting some seconds it always breaks and shows an error:

ERROR:

(node:16924) UnhandledPromiseRejectionWarning: Error: Transaction has been reverted by the EVM: { "blockHash": "0xe458f379a150700e1fd32209d0d52d7348b2005bb010785bf22c5f629bd70922", "blockNumber": 6793039, "contractAddress": null, "cumulativeGasUsed": 2460800, "from": "0x14e56f3c2c4736181ceee69fdb5f89847f42a9af", "gasUsed": 23920, "logs": [], "logsBloom": "0x00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000", "status": false, "to": "0x63c94b6221b4021b564f4563ec72688655b2604a", "transactionHash": "0x48b75130d6041d8ff19c063892e77dc4c7e2d5a94049a5a8ab4de6ee054cbc19", "transactionIndex": 43 } at /home/markus/Documents/wallets/node_modules/web3-core-method/src/index.js:364:46 at process._tickCallback (internal/process/next_tick.js:68:7) (node:16924) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). (rejection id: 1) (node:16924) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.

I spend a lot of time, read in every website and there are no solution, how can I solve this issue?

Ismael
  • 30,570
  • 21
  • 53
  • 96
sneg
  • 53
  • 1
  • 5

2 Answers2

6

Try the following rawTx data:

var rawTx = {
            "from": fromAddress, // Add this
            "nonce": "0x" + nonces.toString(16),
            "gasPrice": web3.utils.toHex('20000000000'),
            "gasLimit": web3.utils.toHex('21000'),
            "to": contractAddr,
            "value": "0x0",
            "data": dataso,
            "chainId": 1 // For mainnet // Use 3 for ropsten testnet
        };

Keep in mind that the private key should be of the fromAddress

Click Here to find out a list of well known chainId values.

EDIT AFTER HAVING A LOOK AT YOUR SMART CONTRACT:

I observed that your decimals is 3 (in your smart contract) and you're converting your value upto 18 decimals (le18) but what you need is le3.

So changing this variable dataso like this may help:

var dataso=myContractes.methods.transfer(destinityAddr,web3.utils.toHex(1e3)).encodeABI();

itsHarshad
  • 491
  • 3
  • 8
  • Thank you for your help! well i did different tests but the error still appear after send signedTransaction, i can see that it happens only when i tried to send tokens from one specific contract, but i can send other tokens without problem, maybe this issue is from the smart contract? and also it only happens with my code, because if i use metamask or myetherwallet to send this specific tokens all is ok, and don't show any error – sneg Nov 29 '18 at 13:09
  • 1
    I just checked your txn hash and I found that you are sending 1000000000000000000 tokens which you don't have and that is the reason your txn is failing. I also checked your smart contract and you've given decimals = 3 but while sending you're sending with decimals = 18:web3.utils.toHex(1e18). So instead send like this: web3.utils.toHex(1e3) in your dataso variable – itsHarshad Nov 29 '18 at 15:30
  • yes ! Exactly ! it was the error, how can i forgot about that decimals are not 18 as other tokens !!! Really Thank you so much for your time and help ! – sneg Dec 01 '18 at 11:58
  • i can't vote yet because i need 15 points, when i have 15 points immediately i will vote positive for your helping commentaries – sneg Dec 01 '18 at 12:00
  • Your welcome! Keep coding and keep helping. – itsHarshad Dec 02 '18 at 08:26
  • 1
    as I promised before (8 months ago), there is my point for you, I can already give points ))) – sneg Sep 04 '19 at 12:12
  • hahaha.. :))))) – itsHarshad Sep 05 '19 at 06:03
2

I have the same issue and found that there may be a bug in web3 or I miss smth: https://github.com/ethereum/web3.js/issues/2587

I did a workaround for it:

  const promiseEvent = ethProvider.sendSignedTransaction('0x' + serializedTx.toString('hex'));
    return promiseEvent
        .on('transactionHash', (txHash) => {
                return onTxHash(txHash);
            }
        )
        .on('confirmation', (confirmNumber, receipt) => {
                logger.info({ ethereumTransactionConfirmed: { confirmNumber, receipt } })
                return onConfirm && onConfirm(receipt, confirmNumber);
            }
        )
        .on('error', (error => {
                // Workaround to for a web3 issue that emit error event while tx was successful
                const reverterErrorMsg = 'Transaction has been reverted by the EVM:';
                if (error.message.startsWith(reverterErrorMsg)) {
                    const receiptString = error.message.slice(reverterErrorMsg.length);
                    const receiptJSON = JSON.parse(receiptString);
                    if (receiptJSON.status === true) {
                        return;
                    }
                }
                logger.error({ ethereumTransactionError: error });
                return onError && onError(error);
            })
        );
user51720
  • 21
  • 1