Hot answers tagged

245 votes
Accepted

What is the difference between a transaction and a call?

Call A call is a local invocation of a contract function that does not broadcast or publish anything on the blockchain. It is a read-only operation and will not consume any Ether. It simulates what ...
eth's user avatar
  • 85k
86 votes
Accepted

How to get contract internal transactions

There's not currently any way to do this using the web3 API. Internal transactions, despite the name (which isn't part of the yellowpaper; it's a convention people have settled on) aren't actual ...
Nick Johnson's user avatar
  • 8,094
73 votes
Accepted

What are Solidity events and how they are related to topics and logs?

Topics are indexed parameters to an event. topic[0] always refers to the hash of the hash of the event itself, and can have up to 3 indexed arguments, which will each be reflected in the topics. EVM ...
Mikko Ohtamaa's user avatar
65 votes
Accepted

What are the limitations to estimateGas and when would its estimate be considerably wrong?

estimateGas works by pretending the transaction was actually being included in the blockchain, and then returning the exact gas amount that would have been charged if that pretend operation was real. ...
Jeff Coleman's user avatar
57 votes
Accepted

How to detect if an address is a contract?

you can use getcode function. if the address is representing an External Owned Account you will get 0x as response otherwise you will get the contract's bytecodeode. for example : web3.eth.getCode("...
Badr Bellaj's user avatar
  • 18.7k
56 votes
Accepted

ecrecover from Geth and web3.eth.sign

I was stuck on this issue as well for a very long time. So the solution is: Add this prefix string to your Solidity smart contract. function verify(bytes32 hash, uint8 v, bytes32 r, bytes32 s) ...
Adil Haris's user avatar
53 votes
Accepted

Returning a struct and reading via Web3

Summary Return the fields of the struct as separate return variables. Edit: As of 2021, it's possible to return the struct directly. See this answer. Example I'm running this code in my local dev ...
BokkyPooBah's user avatar
  • 40.1k
53 votes
Accepted

error: replacement transaction underpriced

I have been running into this issue as well. The network thinks you are trying to replace an existing unmined transaction. There are two ways to avoid this: Adding a gas price %10 higher than the ...
M1Reeder's user avatar
  • 670
51 votes

What is the difference between a transaction and a call?

The difference between in a call and a transaction is the following: transactions are created by your client, signed and broadcasted to the network. They will eventually alter the state of the ...
q9f's user avatar
  • 32.8k
48 votes

How to get return values when a non view function is called?

It is not currently possible to return values from functions which modify the blockchain. To receive a return value, you can mark functions as "pure" or "view". For state-changing functions, the only ...
Taylor Gerring's user avatar
46 votes
Accepted

How to detect if on mainnet or testnet?

Prior to Web 1.0 you can use web3.version.getNetwork as following: web3.version.getNetwork((err, netId) => { switch (netId) { case "1": console.log('This is mainnet') break ...
Badr Bellaj's user avatar
  • 18.7k
46 votes
Accepted

How to convert string to bytes32 in web3js?

For web3 0.x: You need to use web3.fromAscii(val) to write correct bytes32 input to contract. And you are right, anyone can read it with web3.toAscii(val). Full web3.js code in terms of your ...
Victor Baranov's user avatar
43 votes
Accepted

How to call my contract's function using sendTransaction

Posting solution to my own question for anyone who needs it. To call a contract function from geth: const contractAbi = eth.contract(AbiOfContract); const myContract = contractAbi.at(contractAddress); ...
Prashant Prabhakar Singh's user avatar
42 votes

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

Do this: You'll need to pull code from web3, and it works best if your frontend is bundled using something like webpack or browserify: var SolidityCoder = require("web3/lib/solidity/coder.js"); var ...
Tim Coulter's user avatar
  • 1,049
38 votes
Accepted

How to find the date of an Ethereum transaction while parsing it with web3?

web3.eth.getTransaction({txhash}) will contain a blockNumber. Use web3.eth.getBlock to retrieve the block details and you will find the following field: timestamp: Number - the unix timestamp for ...
BokkyPooBah's user avatar
  • 40.1k
36 votes
Accepted

How to access the event log by knowing the contract address (web3)?

Take a look at web3.eth.filter and watch. Something like this: const filter = web3.eth.filter({ fromBlock: 0, toBlock: 'latest', address: contractAddress, topics: [web3.sha3('newtest(string,...
eth's user avatar
  • 85k
36 votes

How to get contract internal transactions

In the Ethereum protocol there's only transactions and message calls. A transaction is a type of message call. A transaction may perform other message calls, but these are not transactions (even ...
eth's user avatar
  • 85k
36 votes
Accepted

How can I detect change in account in Metamask?

As suggested by Metamask FAQs this might be an option: var account = web3.eth.accounts[0]; var accountInterval = setInterval(function() { if (web3.eth.accounts[0] !== account) { account = web3....
qbsp's user avatar
  • 4,347
34 votes

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

Solution: MyContract = new web3.eth.Contract(abi) Docs https://web3js.readthedocs.io/en/1.0/web3-eth-contract.html#eth-contract When using version 1.0.x you need to pass in the Application Binary ...
David's user avatar
  • 459
34 votes

How can I detect change in account in Metamask?

From the MetaMask docs: window.ethereum.on('accountsChanged', function (accounts) { // Time to reload your interface with accounts[0]! }) window.ethereum.on('networkChanged', function (networkId) { ...
rstormsf's user avatar
  • 4,327
33 votes
Accepted

Is there a way to find an account's current transaction nonce?

Your comment is correct: use web3.eth.getTransactionCount(accountAddress) Note that the client (Geth) should be in sync with the blockchain. If you don't have a node and want to trust a service, you ...
eth's user avatar
  • 85k
33 votes
Accepted

Getting ABI data for ERC20 tokens programatically with Web3

Thank you K.Fichter for the help, I just want to fully clarify the answer for anyone else who may run into this issue. Let's say you want to get the balance of a token at an Ethereum Address. Using ...
Shawn Tabrizi's user avatar
32 votes

How to get contract internal transactions

Fortunately, Geth EVM has new tools to get this done. It's possible to use debug_traceTransaction with RPC API. In NodeJS: var web3 = require('web3').web3; web3.currentProvider.sendAsync({ ...
Paul Exchange's user avatar
30 votes
Accepted

Can a contract listen to events of another contract?

A contract cannot listen to events of another contract. From Solidity docs: Log and event data is not accessible from within contracts (not even from the contract that created a log). web3.js ...
eth's user avatar
  • 85k
30 votes

What is msg.data?

I had to dig a little deeper into the contents of msg.data for this feature. I thought it would be worth noting that variable length parameters like arrays, bytes and strings produce a more complex ...
aflesher's user avatar
  • 401
30 votes
Accepted

What is the difference between web3.js and ethers.js?

For me, it is less about features (however there are a couple cool features/design choices in ethers.js). Ethers.js is: smaller well tested well documented well maintained less buggy If you want to ...
Marek Kirejczyk's user avatar
29 votes
Accepted

Is it a good practice to log an event every time I throw in Solidity?

With an exception, as with throw, all effects (including events) of a transaction are reverted, except for the payment to the miner. So there would be no benefit in firing an event before a throw. ...
eth's user avatar
  • 85k
29 votes

web3: How do I get past events of myContract.myEvent?

I finally figured out how to properly use .get() mentioned in the answer by Roland: myContract.myEvent({}, { fromBlock: 0, toBlock: 'latest' }).get((error, eventResult) => { if (error) ...
SCBuergel's user avatar
  • 8,704
29 votes

web3.js execution - server vs browser

You raise a topic that can be confusing for newcomers to the space. In summary, Ethereum can be accessed by a browser, or by a server; anything that can speak the protocol. The Mist browser is more ...
Rob Hitchens's user avatar
  • 54.7k
29 votes

'authentication needed: password or unlock' Error when trying to call smart contract method via web3

First, make sure you have an account. web3.personal.listAccounts If you get [] ... web3.personal.newAccount() Try again. Then ... web3.personal.unlockAccount(web3.personal.listAccounts[0],"&...
Rob Hitchens's user avatar
  • 54.7k

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