我正在创建这个非常简单的程序,它将ETH从一个地址发送到另一个地址。我正在使用 rinkeby.infura.io 获得免费的以太网并用于测试目的。
我的代码似乎运行正常,因为当我在 http://localhost:8545 上的testrpc中测试它并使用虚拟帐户地址时,一切正常。但是,当我更改为真实帐户地址并将HttpProvider更改为 https://rinkeby.infura.io/ 时,我收到以下错误:
错误:无效的JSON RPC响应:"" at Object.InvalidResponse(D:\ Documents \ Projects \ Eth \ node_modules \ web3 \ lib \ web3 \ errors.js:38:16) 在HttpProvider.send(D:\ Documents \ Projects \ Eth \ node_modules \ web3 \ lib \ web3 \ httpprovider.js:91:22) 在RequestManager.send(D:\ Documents \ Projects \ Eth \ node_modules \ web3 \ lib \ web3 \ requestmanager.js:58:32) 在Eth.send [as sendTransaction](D:\ Documents \ Projects \ Eth \ node_modules \ web3 \ lib \ web3 \ method.js:145:58) 在sendBalance(D:\ Documents \ Projects \ Eth \ routes \ index.js:57:11) 在D:\ Documents \ Projects \ Eth \ routes \ index.js:80:12 在Layer.handle [as handle_request](D:\ Documents \ Projects \ Eth \ node_modules \ express \ lib \ router \ layer.js:95:5) 在下一个(D:\ Documents \ Projects \ Eth \ node_modules \ express \ lib \ router \ route.js:137:13) 在Route.dispatch(D:\ Documents \ Projects \ Eth \ node_modules \ express \ lib \ router \ route.js:112:3) 在Layer.handle [as handle_request](D:\ Documents \ Projects \ Eth \ node_modules \ express \ lib \ router \ layer.js:95:5)
我注意到我忘记在代码 web3.eth.sendTransaction({})的部分包含一个回调函数,因此我将其更改为 web3.eth.sendTransaction ({},function(err,hash){})。它删除了无效的JSON RPC错误消息,但它根本不执行整个 sendTransaction() ,因为我可以看到余额未受影响。
以下是使用rinkeby.infura.io和回调函数的输出:
供您参考,以下是我的代码的两个版本:(1)带有 sendTransaction() 的回调函数,(2)没有回调函数< / p>
(1)使用回调功能
var express = require('express');
var router = express.Router();
var util = require('ethereumjs-util');
var tx = require('ethereumjs-tx');
try {
var lightwallet = require('eth-lightwallet');
} catch (err) {
delete global._bitcore
var lightwallet = require('eth-lightwallet');
}
var txutils = lightwallet.txutils;
var Web3 = require('web3');
var web3 = new Web3(new Web3.providers.HttpProvider('https://rinkeby.infura.io/'));
function balance(addr) {
var start = Date.now();
var latestBalance = web3.eth.getBalance(addr, "latest");
var nonce = web3.eth.getTransactionCount(addr, "latest");
var end = Date.now();
return nonce;
}
function showBalance(addr) {
var start = Date.now();
var latestBalance = web3.eth.getBalance(addr, "latest");
var nonce = web3.eth.getTransactionCount(addr, "latest");
var end = Date.now();
var res = {
"address": addr,
"balance": latestBalance + " Wei / " + web3.fromWei(latestBalance).toString() + " ETH",
"nonce": nonce.toString(),
"timestamp": (end - start).toString() + "ms"
}
return res;
}
function sendBalance(_from, _to) {
var nonce = balance(_from);
var amount = 0.05;
var rawTx = {
nonce: web3.toHex(nonce),
gasPrice: web3.toHex(10),
gasLimit: web3.toHex(21000),
to: _to,
value: web3.toHex(amount * 1000000000),
data: "",
chainId: 1
}
var addFromPrivateKey = new Buffer('4ce80ef53f9c13e5d68737ff078e0660e803f87735fab9c79bf408335be8963d', 'hex');
var transaction = new tx(rawTx);
transaction.sign(addFromPrivateKey);
var res ="";
var serializedTx = transaction.serialize().toString('hex');
web3.eth.sendTransaction({
from: _from,
to: _to,
value: web3.toWei(amount, "ether"),
gas: rawTx.gasLimit,
price: rawTx.gasPrice,
nonce: rawTx.nonce
}, function(error, hash){res = error;});
var txLog = "";
// web3.eth.sendRawTransaction('0x' + serializedTx, function(err, result) {
// if(err) {
// txLog = err;
// } else {
// txLog = result;
// }
// });
res = "Status: " + res + " |||| from: " + _from + "\nsendValueETH: " + amount + "\nrawTx: " + JSON.stringify(rawTx, null, 4) + " " + txLog;
return res;
}
// Get Homepage
router.get('/', function(req, res) {
var dti = sendBalance('0xa5d1274a05ab92e3830d81e0e4302498ccb5b22f', '0x6a1366a19b6dbb4d9fe4781f38538e9dc2ec0698');
dto = showBalance('0xa5d1274a05ab92e3830d81e0e4302498ccb5b22f');
dtt = showBalance('0x6a1366a19b6dbb4d9fe4781f38538e9dc2ec0698');
data = {
"dta": {
"sender": dto,
"recipient": dtt,
"transaction": dti
}
}
res.render('index', {data});
});
module.exports = router;
(2)没有回叫功能
var express = require('express');
var router = express.Router();
var util = require('ethereumjs-util');
var tx = require('ethereumjs-tx');
try {
var lightwallet = require('eth-lightwallet');
} catch (err) {
delete global._bitcore
var lightwallet = require('eth-lightwallet');
}
var txutils = lightwallet.txutils;
var Web3 = require('web3');
var web3 = new Web3(new Web3.providers.HttpProvider('https://rinkeby.infura.io/'));
function balance(addr) {
var start = Date.now();
var latestBalance = web3.eth.getBalance(addr, "latest");
var nonce = web3.eth.getTransactionCount(addr, "latest");
var end = Date.now();
return nonce;
}
function showBalance(addr) {
var start = Date.now();
var latestBalance = web3.eth.getBalance(addr, "latest");
var nonce = web3.eth.getTransactionCount(addr, "latest");
var end = Date.now();
var res = {
"address": addr,
"balance": latestBalance + " Wei / " + web3.fromWei(latestBalance).toString() + " ETH",
"nonce": nonce.toString(),
"timestamp": (end - start).toString() + "ms"
}
return res;
}
function sendBalance(_from, _to) {
var nonce = balance(_from);
var amount = 0.05;
var rawTx = {
nonce: web3.toHex(nonce),
gasPrice: web3.toHex(10),
gasLimit: web3.toHex(21000),
to: _to,
value: web3.toHex(amount * 1000000000),
data: "",
chainId: 1
}
var addFromPrivateKey = new Buffer('4ce80ef53f9c13e5d68737ff078e0660e803f87735fab9c79bf408335be8963d', 'hex');
var transaction = new tx(rawTx);
transaction.sign(addFromPrivateKey);
var res ="";
var serializedTx = transaction.serialize().toString('hex');
web3.eth.sendTransaction({
from: _from,
to: _to,
value: web3.toWei(amount, "ether"),
gas: rawTx.gasLimit,
price: rawTx.gasPrice,
nonce: rawTx.nonce
});
var txLog = "";
// web3.eth.sendRawTransaction('0x' + serializedTx, function(err, result) {
// if(err) {
// txLog = err;
// } else {
// txLog = result;
// }
// });
res = "Status: " + res + " |||| from: " + _from + "\nsendValueETH: " + amount + "\nrawTx: " + JSON.stringify(rawTx, null, 4) + " " + txLog;
return res;
}
// Get Homepage
router.get('/', function(req, res) {
var dti = sendBalance('0xa5d1274a05ab92e3830d81e0e4302498ccb5b22f', '0x6a1366a19b6dbb4d9fe4781f38538e9dc2ec0698');
dto = showBalance('0xa5d1274a05ab92e3830d81e0e4302498ccb5b22f');
dtt = showBalance('0x6a1366a19b6dbb4d9fe4781f38538e9dc2ec0698');
data = {
"dta": {
"sender": dto,
"recipient": dtt,
"transaction": dti
}
}
res.render('index', {data});
});
module.exports = router;
P.S。值得一提的是,这些是样本私钥,地址仅用于演示目的。而且,您可以忽略有关rawTx的任何事情:)
以下是使用testrpc的示例成功输出:
同样,它适用于testrpc,但不适用于rinkeby.infura。我认为它绝对不会对真正的以太坊网络起作用。我是以太坊和区块链开发的新手。任何帮助,即我的问题的解决方案,对我的代码的建议,我将非常感谢它们!提前谢谢!