When trying the following code I will get an an wrong JSON RPC response.
const fnc = myContract.methods.transferToken(...).encodeABI();
const tx = Object.assign({
nonce: nonce,
to: address,
data: fnc,
gasPrice: 2e9,
gas: 1e6,
from: accounts[account].address
}, options);
try {
let signed = await web3.eth.signTransaction(tx, Buffer.from(accounts[account].key, 'hex'));
return (await web3.sendSignedTransaction(signed.raw)).transactionHash
} catch (e) {
console.log(e);
}
There was a long exception thrown:
Error: Invalid JSON RPC response: "<!DOCTYPE html>\n<!--[if lt IE 7]> <html class=\"no-js ie6 oldie\" lang=\"en-US\"> <![endif]-->\n<!--[if IE 7]> <html class=\"no-js ie7 oldie\" lang=\"en-US\"> <![endif]-->\n<!--[if IE 8]> <html class=\"no-js ie8 oldie\" lang=\"en-US\"> <![endif]-->\n<!--[if gt IE 8]><!--> <html class=\"no-js\" lang=\"en-US\"> <!--<![endif]-->\n<head>\n\n\n<title>sokol.poa.network | 504: Gateway time-out</title>\n<meta charset=\"UTF-8\" />\n<meta http-equiv=\"Content-Type\" content=\"text/html; charset=UTF-8\" />\n<meta http-equiv=\"X-UA-Compatible\" content=\"IE=Edge,chrome=1\" />\n<meta name=\"robots\" content=\"noindex, nofollow\" />\n<meta name=\"viewport\" content=\"width=device-width,initial-scale=1,maximum-scale=1\" />\n<script src=\"/cdn-cgi/apps/head/_094Mcj5aMZ4AsztcCkph7SSPfE.js\"></script><link rel=\"stylesheet\" id=\"cf_styles-css\" href=\"/cdn-cgi/styles/cf.errors.css\" type=\"text/css\" media=\"screen,projection\" />\n<!--[if lt IE 9]><link rel=\"stylesheet\" id='cf_styles-ie-css' href=\"/cdn-cgi/styles/cf.errors.ie.css\" type=\"text/css\" media=\"screen,projection\" /><![endif]-->\n<style type=\"text/css\">body{margin:0;padding:0}</style>\n\n\n\n\n</head>\n<body>\n<div id=\"cf-wrapper\">\n\n \n\n <div id=\"cf-error-details\" class=\"cf-error-details-wrapper\">\n <div class=\"cf-wrapper cf-error-overview\">\n <h1>\n \n <span class=\"cf-error-type\">Error</span>\n <span class=\"cf-error-code\">504</span>\n <small class=\"heading-ray-id\">Ray ID: 46dcbf8efe187cba • 2018-10-22 14:46:32 UTC</small>\n </h1>\n <h2 class=\"cf-subheadline\">Gateway time-out</h2>\n </div><!-- /.error-overview -->\n \n <div class=\"cf-section cf-highlight cf-status-display\">\n <div class=\"cf-wrapper\">\n <div class=\"cf-columns cols-3\">\n \n<div id=\"cf-browser-status\" class=\"cf-column cf-status-item cf-browser-status \">\n <div class=\"cf-icon-error-container\">\n <i class=\"cf-icon cf-icon-browser\"></i>\n <i class=\"cf-icon-status cf-icon-ok\"></i>\n </div>\n <span class=\"cf-status-desc\">You</span>\n <h3 class=\"cf-status-name\">Browser</h3>\n <span class=\"cf-status-label\">Working</span>\n</div>\n\n<div id=\"cf-cloudflare-status\" class=\"cf-column cf-status-item cf-cloudflare-status \">\n <div class=\"cf-icon-error-container\">\n <i class=\"cf-icon cf-icon-cloud\"></i>\n <i class=\"cf-icon-status cf-icon-ok\"></i>\n </div>\n <span class=\"cf-status-desc\">Munich</span>\n <h3 class=\"cf-status-name\">Cloudflare</h3>\n <span class=\"cf-status-label\">Working</span>\n</div>\n\n<div id=\"cf-host-status\" class=\"cf-column cf-status-item cf-host-status cf-error-source\">\n <div class=\"cf-icon-error-container\">\n <i class=\"cf-icon cf-icon-server\"></i>\n <i class=\"cf-icon-status cf-icon-error\"></i>\n </div>\n <span class=\"cf-status-desc\">sokol.poa.network</span>\n <h3 class=\"cf-status-name\">Host</h3>\n <span class=\"cf-status-label\">Error</span>\n</div>\n\n </div>\n \n </div>\n </div><!-- /.status-display -->\n\n <div class=\"cf-section cf-wrapper\">\n <div class=\"cf-columns two\">\n <div class=\"cf-column\">\n
<h2>What happened?</h2>\n <p>The web server reported a gateway time-out error.</p>\n </div>\n \n <div class=\"cf-column\">\n <h2>What can I do?</h2>\n <p>Please try again in a few minutes.</p>\n </div>\n </div>\n \n </div><!-- /.section -->\n\n <div class=\"cf-error-footer cf-wrapper\">\n <p>\n <span class=\"cf-footer-item\">Cloudflare Ray ID: <strong>46dcbf8efe187cba</strong></span>\n <span class=\"cf-footer-separator\">•</span>\n <span class=\"cf-footer-item\"><span>Your IP</span>: 88.217.136.37</span>\n <span class=\"cf-footer-separator\">•</span>\n <span class=\"cf-footer-item\"><span>Performance & security by</span> <a href=\"https://www.cloudflare.com/5xx-error-landing?utm_source=error_footer\" id=\"brand_link\" target=\"_blank\">Cloudflare</a></span>\n \n </p>\n</div><!-- /.error-footer -->\n\n\n </div><!-- /#cf-error-details -->\n</div><!-- /#cf-wrapper -->\n</body>\n</html>\n"
The error message is The web server reported a gateway time-out
. It happens when the signTransaction
function was called.
I also tried the sample from the documentation:
const fnc = myContract.methods.transferToken(...).encodeABI();
var Tx = require('ethereumjs-tx');
var privateKey = new Buffer('e331b6d69882b4cb4ea581d88e0b604039a3de5967688d3dcffdd2270c0fd109', 'hex')
var rawTx = {
nonce: '0x00',
gasPrice: '0x09184e72a000',
gasLimit: '0x2710',
to: '0x0000000000000000000000000000000000000000',
value: '0x00',
data: fnc
}
var tx = new Tx(rawTx);
tx.sign(privateKey);
var serializedTx = tx.serialize();
web3.eth.sendSignedTransaction('0x' + serializedTx.toString('hex'))
.on('receipt', console.log);
The behaviour was really strange: The transaction was executed but it seems the values in the contract hasn't been changed. I switched back to the old web3 and this method worked fine.