18

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.


14

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 ...


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

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....


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 ...


7

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;


6

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 (...


6

You can pass an instance of the provider instead of a string as a second argument. So you can pass a websocket provider in: const webSocketProvider = new Web3.providers.WebsocketProvider(...); const walletProvider = new HDWalletProvider(mnemonic, webSocketProvider);


6

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(...


6

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)


6

Since these are public endpoints, they are probably monitoring your usage by your IP address. So you can only do 10K requests/5min from your IP. It doesn't matter if you have more applications or accounts (since you don't need an account to use these endpoints anyway) If you're looking for a higher rate limit BSC node, you should check out my company moralis....


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

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


5

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 ...


5

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. ...


4

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 ...


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

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.


3

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


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

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 ...


3

For parity : As documented in the options, available under parity --help not all API’s are exposed by default. However you can simply enable them by running parity with the flag: --jsonrpc-apis APIS exp : parity --jsonrpc-apis web3,rpc,personal,parity_accounts,eth,net,parity,parity_set,signer for geth start your geth with --rpcapi "eth,net,web3,personal"...


3

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....


3

I hope you are using web3 as browser module. As in their documentation, you can add the CDN link using <script> tags as below, <script src="https://cdn.jsdelivr.net/gh/ethereum/web3.js/dist/web3.min.js"></script>


3

OK, this seems to to the job: web3 = Web3(HTTPProvider("http://127.0.0.1:8545")) provider = web3.providers[0] provider.make_request("evm_increaseTime", [numOfSeconds])


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() { ...


3

Yes, Metamask uses Infura in the background to connect to the network. So Metamask is a user interface on top of Infura service. However, nothing forces you to use Metamask, it just happens to be handy for user interactions. But typically a dApp has also a separate backend connection to Ethereum: for example the backend needs to read token balances and ...


3

As I understand it MetaMask is a Remote Client. A Remote Client offers an API and allows you to use the transaction functionalities of a wallet. Using the (injected) API, e.g. web3.js, means it translates the JS-code into JSON RPC, which is actually the language of a web3 provider (e.g. Parity or Geth). Although MetaMask includes the functionality of a ...


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: '...


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