Я совершенно новичок в криптовалютах. Я установил geth и web3 . Я хочу создать веб-страницу, показывающую баланс счета. Я запускаю geth с помощью команды ниже
build/bin/geth --rpc --rpcaddr 127.0.0.1 --rpcport 8545 --rpcapi web3,db,eth,net,personal
[ec2-user@ip ethereum]$ curl http://127.0.0.1:8545
[ec2-user@ip ethereum]$ curl http://127.0.0.1:8545
[ec2-user@ip ethereum]$ sudo netstat -nlp | grep :8545 tcp 0 0 127.0.0.1:8545 0.0.0.0:* LISTEN 18238/build/bin/get
Мой код в stest.html:
<script src="web3.js/dist/web3.js"></script>
<script>
if (typeof web3 !== 'undefined') {
web3 = new Web3(web3.currentProvider);
} else {
// set the provider you want from Web3.providers
var web3 = new Web3(new Web3.providers.HttpProvider("http://127.0.0.1:8545"));
//var web3 = new Web3('http://localhost:8545'); // TypeError: this.provider.send is not a function
}
console.log(web3); // {eth: .., shh: ...} // it's here!
if(!web3.isConnected()) {
console.log("fail");
} else {
console.log("succ");
}
//var coinbase = web3.eth.coinbase;
//var balance = web3.eth.getBalance(coinbase);
//console.log(web3.eth.accounts);
var version = web3.version.api;
console.log(version); // "0.2.0"
var version = web3.version.node;
console.log(version);
Я получаю следующие ошибки в консоли браузера при запуске stest.html:
Object { _requestManager: {…}, currentProvider: {…}, eth: {…}, db: {…}, shh: {…}, net: {…}, personal: {…}, bzz: {…}, settings: {…}, version: {…}, … }
stest.html:13:1
fail
0.20.5
Error: CONNECTION ERROR: Couldn't connect to node http://127.0.0.1:8545.
web3.js:3118:16
Source map error: request failed with status 404
Resource URL: http://34.215.167.9/plugins/ethereum/web3.js/dist/web3.js
Source Map URL: web3.js.map
Ниже информация, которую я получил от терминала:
[ec2-user@ip go-ethereum]$ build/bin/geth version
Geth
Version: 1.8.0-unstable
Git Commit: 722bac84fa503199b9c485c1a3e2bfba03bc487d
Architecture: amd64
Protocol Versions: [63 62]
Network Id: 1
Go Version: go1.8.4
Operating System: linux
GOPATH=
GOROOT=/usr/lib/golang
ОБНОВЛЕНИЕ : теперь CURL дает результат после запуска с помощью приведенной ниже команды geth.
build/bin/geth --rpc --rpccorsdomain "http://localhost:8545"
$ curl -H "Content-Type: application/json" -X POST --data '{"jsonrpc":"2.0","method":"web3_clientVersion","params":[],"id":67}' http://127.0.0.1:8545
{"jsonrpc":"2.0","id":67,"result":"Geth/v1.8.0-unstable-722bac84/linux-amd64/go1.8.4"}
Но в консоли браузера ошибка изменилась на
ReferenceError: Web3 is not defined
Одной из возможностей является проблема CORS. Не могу быть уверен с предоставленной информацией. Добавьте следующую опцию в команду запуска geth
--rpccorsdomain "*"
Error: CONNECTION ERROR: Couldn't connect to node http://127.0.0.1:8545.
Проблема остается.
Сумант
СМЖ
СМЖ
СМЖ
<script> var Web3 = require('web3'); if (typeof web3 !== 'undefined') { web3 = new Web3(web3.currentProvider); } else { // set the provider you want from Web3.providers var web3 = new Web3(new Web3.providers.HttpProvider("http://127.0.0.1:8545")); //var web3 = new Web3('http://localhost:8545'); // TypeError: this.provider.send is not a function }