Questions tagged [javascript]

JavaScript is a dynamic, weakly-typed language used for client-side as well as server-side scripting.

Filter by
Sorted by
Tagged with
95 votes
12 answers
49k views

Common useful JavaScript snippets for geth

In Geth you can loadscript any javascript file. I discovered this one on the wiki which I like a lot: function checkAllBalances() { var i =0; eth.accounts.forEach( function(e){ console.log(&...
47 votes
6 answers
40k views

How do I parse the transaction receipt log with web3.js?

The event parser in web3 provides a nice parsing functionality for events, and I use it for logging all events to a file, but it's very difficult to use for looking at individual events for a ...
  • 4,241
49 votes
5 answers
99k views

error: replacement transaction underpriced

Im trying to send a raw transaction using web3 to ropsten testnet and Im getting this error: replacement transaction underpriced where sometimes my tx works and sometimes I get this error. my tx ...
11 votes
1 answer
3k views

Can I unlock an account using JSON-RPC?

The only way I can work with ETH is through --rpc mode. Normally I would prefer to use the Python interface. But I found myself in a situation where the RPC is very limited. For example, I can't ...
  • 463
27 votes
3 answers
12k views

How does Solidity's sha3 / keccak256 hash uints?

This is not a question about the issue with Ethereum using a non-standard sha3. I have found the right JS hashing library, and am able to get matching hashes in JS and Solidity on byte strings. What I ...
  • 763
5 votes
2 answers
4k views

How do I print my account balances and TheDAO tokens from geth

Is there a convenient script to print out my account balances and TheDAO tokens from the go-ethereum client geth?
10 votes
1 answer
10k views

How to convert BN result into readable string or number in ethjs?

I'm receiving the result in BN format. How can I convert it into actual string or number? I'm using ethjs library to interact with Smart Contract. token.totalSupply().then((totalSupply) => { // ...
16 votes
6 answers
26k views

Better pattern to detect web3 default account when using metamask

Context: I want to use blockies to render an identicon on the page, i get the defaultAccount from web3, for this, the user has to be logged on to metamask with a selected address from his wallet. The ...
16 votes
3 answers
15k views

How to use bignumbers in truffle tests?

I am testing a smartcontract with Truffle V5. All works well if I do not use all decimals. If I set 18 decimals all explodes. The test code is this (from truffle docs): const MetaCoin = artifacts....
  • 163
16 votes
6 answers
16k views

How can dapps work on smartphones?

How can dapps on mobile web browsers interact with web3.js? Is there any tool like Metamask or Mist on the roadmap for mobile?
5 votes
1 answer
3k views

Listing transactions in a private blockchain?

In a private blockchain, is it possible to list the transactions based on from and to parameters in a transaction. Is it possible to check the latest transaction from or to an address? Also, is it ...
  • 3,900
3 votes
3 answers
15k views

How to generate Private key, public key and address

I can't find any documentation online about doing this in Javascript other than this. I want to know how does authentication work in a ethereum dApp's.. As per my understanding user seed somehow gets ...
  • 427
0 votes
0 answers
462 views

(NEW UPDATE: I need to run "testrpc") Signing a message using "ethereumjs-abi"? (Error: Invalid JSON RPC response: "")

I am trying to sign a message using following JavaScript code (from HERE) function constructPaymentMessage(contractAddress, amount) { return ethereumjs.ABI.soliditySHA3( ["address", "uint256"], ...
  • 2,618
26 votes
5 answers
22k views

How to listen for contract events in JavaScript tests?

I'm trying to trigger events in my solidity contract to output some debug information in my javascript tests. However the events seem to get never caught. That's the relevant contract code: pragma ...
  • 373
7 votes
1 answer
2k views

Event result parameters and arguments in console

I read about using events from this source http://solidity.readthedocs.io/en/latest/contracts.html#events I used a simple multiply contract: contract test1{ uint a; event event_res(string msg,...
  • 317
7 votes
2 answers
5k views

Automatically update account balance with web3.js without polling

I've made a user interface in JavaScript with web3.js where the user can select an account to use. The accounts are displayed together with their current balance for convenience. I load the account ...
  • 9,859
1 vote
1 answer
717 views

How to create an interaction between a smart contract and a HTML file with Node-js?

My Truffle project is this contract that just shows Yerevan is love: pragma solidity ^0.5.12; contract TEST{ string public note="Yerevan is love"; function rep() public view returns(string memory){ ...
  • 481
34 votes
7 answers
49k views

web3.eth.contract is not a function when making contract

Whenever I try and make a contract, I get: web3.eth.contract is not a function I'm using this code: const path = require('path'); const fs = require('fs'); const solc = require('solc'); ...
  • 531
17 votes
5 answers
14k views

web3.js with promisified API

I'd like to take away the pain from web3 development callback hell and use Promise pattern and async/await keywords on both client side and on Node.js. Do there exist any web3.js builds or forks that ...
17 votes
4 answers
7k views

How to open the JavaScript console on Parity?

There is nothing like parity console. How to open the JavaScript console on Parity?
  • 32.3k
14 votes
2 answers
20k views

How do Enums work?

I am just starting out with Solidity and JavaScript and am trying to understand Enums. I have a simple contract pragma solidity ^0.4.4; contract SimpleEnum { enum SomeData {Channel} SomeData ...
  • 409
12 votes
2 answers
18k views

Can someone explain 'signing a transaction' and how its different from sending ether?

Some times when using meta mask or other contracts, instead of sending an amount, I simply sign the transaction. see here Say I want a function() that renames a string. function string(string ...
  • 551
9 votes
1 answer
18k views

How to make transactions using private key in web3?

I have account private key, contract address and abi and I need to call contract function with this account. Is it possible?
  • 321
8 votes
4 answers
12k views

Web3 create account from mnemonic + passphrase

I'm trying to create an ethereum account using web3-js by providing a mnemonic + passphrase (created with BIP39-js) but can't seem to find an implementation of web3.eth.accounts.create() that accepts ...
7 votes
1 answer
5k views

JavaScript Code Required to Generate an Ethereum Address?

I am aware of several tools and websites that generate addresses for you, but I'm looking for an actual JavaScript code snippet to demonstrate the steps plainly.
  • 927
7 votes
2 answers
6k views

How can I run scripted geth commands with specified command line parameters?

I have a contract running on the testnet, and every time I need to call a function, after opening Geth I have to type (or paste): primary = eth.accounts[0]; personal.unlockAccount(primary, '...
7 votes
1 answer
11k views

How to get a token pair's uniswap trade history?

What I am trying to do is this :- Given an pair (say DAI/ETH pair), i want to display the latest 10-15 trades (buy, sell, add to pool and remove from pool) and also display a graph of price for that ...
6 votes
3 answers
3k views

web3 methods not implemented

I'm running a geth node on the testnet and trying to run web3.js method from my node server code. Here's my code var Web3 = require("web3"); var web3 = new Web3(new Web3.providers.HttpProvider('http:...
  • 439
6 votes
2 answers
1k views

Make data from smartcontracts / events only visible for certain address/users

I Have an use case where i need : 1- My data not to be visible to the public. 2- To be able to revoke my clients/users rights to view said data from my smart contract. As anyone done that before ? ...
  • 2,392
6 votes
1 answer
1k views

Is there any lightweight JS library that generates Ethereum addresses from 256-bit private keys?

I have a 256-bit private key and I want to get its public address without having to install Geth (which is quite heavy) on the machine. Is there any small JS lib that does that?
  • 3,157
5 votes
2 answers
6k views

Web3.js browser version only

I want to create a simple page to interact with my deployed contract. I don't want to use Node.js. I want to use html + web3.js browser version. I got a link: https://github.com/ethereum/web3.js/tree/...
5 votes
1 answer
2k views

Making web3.js work from node server code

I'm running a geth node on the testnet and trying to run web3.js method from my node server code. var coinbase = web3.eth.coinbase; console.log(coinbase); However, I'm getting a provider not set or ...
  • 439
5 votes
1 answer
125 views

Getting event history takes a really long time or doesn't work at all

I'm having trouble getting a history of events. I'm unsure if I am misunderstanding how events work, if I'm coding wrong, or if it has anything to do with the recent Ethereum attacks slowing down the ...
4 votes
1 answer
837 views

How to get the transaction hash once it is mined?

In JavaScript (web3.js), how do I get the transaction-hash corresponding to a function executed in my smart contract?
  • 43
3 votes
2 answers
2k views

Read message of require / revert statement in app.js

In my App.js I have called the smart contract function and the require statement in the function is not satisfied, so how do I get the message of that require statement in my App.js?
3 votes
1 answer
2k views

Ethers js deploy contract with balance, payable constructor

I have a smart contract with a payable constructor. I'm trying to figure out the syntax with ethers js how I can deploy the smart contract and send an initial balance with the deployment. const ...
  • 31
3 votes
2 answers
665 views

javascript local sign and sendRawTransaction to remote node

In order to use Web3 provided by MyEtherAPI, I need to locally sign a transaction, since myetherapi supports only eth_sendRawTransaction. I found a good source to do so on github, unfortunately it ...
  • 552
3 votes
1 answer
7k views

Window.ethereum undefined on tab with local webpage

I have the metamask extension. When I do typeof(window.ethereum) in the console for a chrome tab, I get the type as object. However if I open mywebpage.html in a tab, which is just a heading around: &...
  • 143
3 votes
1 answer
439 views

Geth - reading public address[] from contract

After setting up testrpc and attaching geth: testrpc -a 10 geth attach rpc:http://localhost:8545 I initialise the following contract on the geth command line: pragma solidity ^0.4.6; contract ...
  • 8,208
2 votes
1 answer
2k views

Could not find a declaration file for module 'solc''

I have installed solc without any errors or warning, but when i am trying to run node compile.js command in cmd then i am getting following error On checking the code, i found that "const solc = ...
1 vote
2 answers
1k views

how to customize my own commands in geth console?

How to customize my own commands in geth console? We know that in geth console, we can type eth.accounts and other commands. I found that there is a JavaScript parser/interpreter named Otto which ...
  • 983
1 vote
1 answer
563 views

How to bundle own web3 version and using Metamask

I am a bit of a javascript noob and have some questions about using web3js and Metamask. My objective (for now) is to display the ether balance as displayed on my Metamask (where I am logged in). ...
1 vote
1 answer
2k views

"Updated :" How to see "output" of "web3.personal.sign" in "browser"? ("Method personal_sign not supported")

Note: I updated the html / JavaScript code according to the answer and I used THIS META MASK sample code for web3 provider. However, I receive the same error. I eventually did not understand that do I ...
  • 2,618
0 votes
1 answer
97 views

Solidity function successful from Truffle Develop but fails from javascript

I have following Solidity Contract function, it returns values when called from truffle develop, but return empty object when called from javascript. Below is Solidity function function ...
20 votes
3 answers
18k views

Listening to events using ethers.js on a hardhat test network

I am currently migrating a truffle project to hardhat. I am facing a problem with listening to events. I have a simple ERC20 token and want to listen to the Transfer event. I am running the contract ...
14 votes
4 answers
15k views

How to automatically get user's wallet info if they are connected but NOT show the metamask pop up if they are not?

I don't want metamask to pop up on page load forcing a user to connect, so we only connect via a button - but after a user is connected if they refresh the page - they need to press the button again ...
  • 551
14 votes
3 answers
21k views

In ethers.js how to deal wait()?

In ethers.js, provider.sendTransaction(rawTransaction).then((transaction) => { // A full Transaction Response is returned // - from // - to // - gasLimit, gasPrice // - nonce ...
  • 1,125
12 votes
3 answers
20k views

Is it possible to send transaction in Binance Smart Chain using web3 and ethereumjs-tx?

I want to send transaction in Binance Smart Chain and I got an error: new Error("Chain with name " + chain + " not supported"); const Tx = require('ethereumjs-tx').Transaction; ...
  • 123
11 votes
7 answers
28k views

How to get value of a contract public property using web3.eth

using web3 javascript library how do you get the current value of a public property. e.g. contract MyContract { address public owner; ... } Here's a snippet of the abi: [{ "constant": ...
  • 487
10 votes
2 answers
2k views

Programmatically saving the contract address of a submitted contract

I understand how the below web3js code block works, adapted from here: https://github.com/ethereum/wiki/wiki/JavaScript-API#web3ethcontract But how does one save the contract address ...
  • 235