15

The one method that is more reliable than the in-page accounts array is web3.eth.getAccounts(accounts => console.log(accounts[0])). This will asynchronously request the accounts array, and call back whenever it's available.


13

I've replaced const web3 = new Web3(new Web3.providers.HttpProvider("http://rpc.ethapi.org:8545")); with: const web3 = new Web3(new Web3.providers.HttpProvider("https://ropsten.infura.io/TOKEN")); and everything works. This https://ropsten.infura.io/TOKEN was given to me after registration on infura.io


12

Infura currently doesn't support WebSockets (required for events using Web3 v1, otherwise you get the error "The current provider doesn't support subscriptions" when using infura as HttpProvider), so what you have to do is run a local geth node that connects and syncs to the network. Here we enable the websocket flag and allow any origin to connect to the ...


11

In Truffle 0.2.x or 0.3.x with the truffle-default-builder, app.js in the ./build folder will include considerable bootstrap code that sets the stage. This is why the HTML should link to the ./build folder when the source appears to reside in ./app (usually). It's the build output that should be served. Truffle includes a simple Migrations.sol contract in ...


10

Here's what I use in my Dapp. It seems to work pretty well. function getWeb3(callback) { if (typeof window.web3 === 'undefined') { // no web3, use fallback console.error("Please use a web3 browser"); } else { // window.web3 == web3 most of the time. Don't override the provided, // web3, just wrap it in your Web3. var myWeb3 = new ...


9

I have been using Infura with web3 1.0 in mainnet. Here is my code, hope it helps var Web3 = require('web3') var request = require('request'); var contract = require('truffle-contract') var zastrin_pay_artifacts = require('./build/contracts/ZastrinPay.json') var ws_provider = 'wss://mainnet.infura.io/ws' var web3 = new Web3(new Web3.providers....


5

Try this, it also reconnects to the node. Using web3js@1.0 beta web3.eth.net.isListening().then((s) => { console.log('We\'re still connected to the node'); }).catch((e) => { console.log('Lost connection to the node, reconnecting'); web3.setProvider(your_provider_here); })


5

Checking web3.eth.accounts[0] every 100 milliseconds is way faster in some cases than web3.eth.getAccounts(): // Option 1: web3.eth.getAccounts(console.log); // Option 2: (function loop() { if (web3.eth.accounts[0]) { console.log(web3.eth.accounts[0]); } else { setTimeout(loop, 100); } }()); When using a proxy, Option 2 was up to 35 seconds (...


5

You are asking about very niche and complex problem, so one cannot expect ready made tutorials to be available. I would suggest Reverse engineering web3.js Reverse engineering MetaMask Reverse engineering Trust wallet Reverse engineering web3.py (might be the most readable source code of all these)


4

You can run the ethereum node on a dedicated server as you mentioned but rather than opening port 8545 and talk to it directly, Create a nodeJS API server on that instance(which will use web3.js) and talk to that nodejs server instead. NodeJS server will communicate to Ethereum node with the help of web3.js APIs, which will make your design secure and ...


4

Using web3js 1.0.0: const privateKey = 'e0f34403.................................29c8c861937'; const account = web3.eth.accounts.privateKeyToAccount('0x' + privateKey); web3.eth.accounts.wallet.add(account); web3.eth.defaultAccount = account.address;


4

The response you are getting is the receipt of the transaction. Return values from functions which create transactions, to my knowledge, can only be true or false. However, you should be able to emit an event containing the address of the contract after it is created in the factory. event ContractCreated(address newAddress); Then, within your newContract(...


4

So... I figured out. The reason is Web3 takes the private key in 0xa0b1c2... format. I used to use ethereumjs-tx package, which takes a private key in const privateKey = new Buffer('a0b1c2...') without 0x I changed to const privateKey = '0xa0b1c2...' and it worked. Hope this helps whoever visits this page.


4

What metamask do is inject javascript into the document when you access it. The browser will execute this javascript, and is that execution that will define de variable web3. But when you are accessing a document by accessing the file (your url will start with file://) then metamask will not inject the javascript, so the variable web3 will be undefined. ...


3

https://api.myetherapi.com/eth is a provider. Web3 provider is a website running geth or parity node which talks to Ethereum network.


3

A couple things I've noticed from using MetaMask. defaultAccount isn't always populated. It's safer to use web3.eth.accounts[0] as your check to see whether or not an account is selected. You'll need to poll this value periodically if you want to know if the selected account changes, as well. The load event doesn't always seem reliable in some of my ...


3

You should be careful using Infura because it can relay you incorrect information. Infura is architected with multiple nodes behind a load balancer. All of these nodes are not in sync, so you can receive information from nodes that contain stale blocks or nodes that are not synced with the most recent block. We used to use Infura, which was great because ...


3

If I were to deploy this code on a website, what would I set this variable to, and how does that work? You can replace it with your own working node or you can use some service like infura.io which allows you to "access Ethereum via the Infura load-balanced nodes and smart architecture the same way you would via your own nodes." And what if that user is ...


3

According to https://truffleframework.com/docs/truffle/reference/configuration there is a field called provider. Using this field an arbitrary web3 connection provider can be injected. For IPC on Windows it looks like: var Web3 = require("web3"); var net = require("net"); module.exports = { networks: { dev: { provider: function() { ...


2

First double check that IP addresses are correct in your scripts. Then use telnet command to ensure that you can connect to RPC port from the client computer: telnet 163.xxx.xxx.xxx 8545 You should see raw HTTP output and it should give you a hint of what could be wrong. If this doesn't connect then fix whatever network/firewall/etc. issues you have ...


2

This is how I've managed to listen to events on the backend. The problem is that the events don't fire consistently. const Web3 = require('web3'); const ProviderEngine = require('web3-provider-engine/index.js'); const ZeroClientProvider = require('web3-provider-engine/zero.js'); const engine = ZeroClientProvider({ getAccounts: function(){}, rpcUrl: '...


2

Still haven't got enoguh reputation to comment so I'm just dropping a line as an answer to say you should remove your access code from the example you provided! ;) const web3 = new Web3(new Web3.providers.HttpProvider("https://ropsten.infura.io/***********"));


2

Encoding a JSON string does not seem like a good idea. See this. Instead of supplying an object as data, you should define your contract function to take the relevant inputs (such as function f(string key1, uint key2){}) and use a transaction library such as ethereumjs-tx to compute the data according to your parameters. Here is an example with only Web3.


2

If you are using this for learning and testing purposes, I'd suggest using testrpc which simulates Ethereum node. It's fast and runs locally so you won't encounter any connection problems at all.


2

This is probably because the Ethereum node started does not expose personal api of the geth interface. This can be changed by starting the rpc server with the rpcapi flag specifying which api's of geth to expose on RPC. By default when a rpc server is started it does not expose all the api of geth through RPC due to security reasons. You can find how to ...


2

A provider links to a running node. For instance parity or geth. A node has the ability to view and interact with the blockchain.


2

You have to use sendRawTransaction method for all messages. That require you to sign every message with senders private key (in your case contract owners) and does not depend on any installed wallets. You could use web3js-raw to make this process easy and sample using this can be found here.


2

A solution is provided here. Essentially, you build the HDProvider, just as you would in truffle, and then pass it to Web3.


2

Let me answer each of your questions: 1. At what point is the connection actually made The connection is actually made after creating a WebsocketProvider object. Taking a snippet from your example: const web3 = new Web3('ws://localhost:8546') const provider = web3.providers.WebsocketProvider const { connection } = provider At this point, the connection ...


2

I don't have 50 reputation to ask a comment question, but I'm assuming the issue you're running into is because your setters need to be promises and require a .then(function(response, error) {}) chain. So you're not waiting for the response to come back and immediately trying to get the values(which will not be set). This site might help: https://coursetro....


Only top voted, non community-wiki answers of a minimum length are eligible