104 votes
Accepted

How to get a contract's balance in Solidity?

You can do this by calling address.balance. To get the contract's balance, just do this.balance. Read the docs. Update: As of Solidity ^0.4.24, you need to do: address(this).balance (Copied from ...
0xcaff's user avatar
  • 2,467
41 votes

What are the pros and cons of Ethereum balances vs. UTXOs?

The main validation constraints in a Bitcoin UTXO system are: every referenced input must be valid and not yet spent the transaction must have a signature matching the owner of the input for every ...
Taylor Gerring's user avatar
32 votes

How to get ETH contract balance with Ethers.js

getBalance is a function of the Ether.js blockchain provider object, it is used this way : const balance = await provider.getBalance("address"); Note that you can use contract.address to ...
clement's user avatar
  • 4,302
29 votes

How to get a contract's balance in Solidity?

Small update from my side: You want to use the following, address(this).balance;
Vanja Dev's user avatar
  • 861
25 votes

Why is my ether balance 0 in geth, even though the sync is nearly complete?

geth --fast has an interesting effect: geth cannot provide any information about accounts or contracts until the sync is fully complete. Try querying the balance again after eth.syncing returns false. ...
carver's user avatar
  • 6,371
24 votes
Accepted

Can a contract with no payable function have ether?

Yes, a contract can have Ether balance without any payable function. There are four ways how it's possible: selfdestruction. Another contract self destructs (by using the selfdestruct functionality) ...
Lauri Peltonen's user avatar
22 votes

How to get a contract's balance in Solidity?

As of Solidity ^0.4.24, you need to do: address(this).balance You can see an example here. Alternatively, if you need to read the balance of a separate contract: address(contractVar).balance
Paul Razvan Berg's user avatar
21 votes
Accepted

How does the Ethereum ETH accounting system work and prevent double-spends?

Each account has a globally accessible nonce which prevents same-chain replay attacks and double spends. The nonce is the sequence number, which miners check, because a block that has a transaction ...
eth's user avatar
  • 85.4k
20 votes

How to programmatically detect and accept ETH and ERC20 deposits

It'd probably go something like this: User wants to deposit a token to your exchange If it's the first time the user is depositing that token then this occurs: Generate a new wallet key/pair on the ...
Miguel Mota's user avatar
  • 5,123
20 votes
Accepted

Cant get address balance using web3js

The issue with your testnet url, https://ropsten.etherscan.io/ is not running eth client. Find below code to connect ropsten node. const testnet = 'https://ropsten.infura.io/'; const walletAddress =...
Jitendra Kumar. Balla's user avatar
19 votes

How to retrieve the Contract account balance in web3 or in geth command line?

To check the balance you can do: web3.eth.getBalance(contract.options.address) Or: web3.eth.getBalance("0x045bfe22453a9ca06aff4bdc5d7f5870eba121bd") To transfer balance on a contract without ...
Mikko Ohtamaa's user avatar
15 votes
Accepted

How to transfer the whole balance of an account?

No. You cannot send the entire balance because the fees are deducted from your account's balance, not from the value being sent. To sweep an account you need to send balance-gas*price ETH as the ...
Péter Szilágyi's user avatar
14 votes

How to transfer Ether between accounts in Solidity?

address.transfer() throws on failure forwards 2,300 gas stipend, safe against reentrancy should be used in most cases as it's the safest way to send ether address.send() returns false on failure ...
medvedev1088's user avatar
12 votes

What are the pros and cons of Ethereum balances vs. UTXOs?

The accepted answer is a very good summary of the following wiki: https://eth.wiki/en/fundamentals/design-rationale#accounts-and-not-utxos See it for further details, including Ethereum's rationale: ...
eth's user avatar
  • 85.4k
12 votes

Etherscan API - Get all token balances

As previously mentioned it doesn't appear possible using etherscan. But it's possible using the (free) ethexplorer.io API. The format is: https://api.ethplorer.io/getAddressInfo/[YOUR_ADDRESS]?apiKey=...
Mark Thomson's user avatar
11 votes
Accepted

How to transfer Ether between accounts in Solidity?

A contract can read any balance using address.balance like: _buyer.balance A contract can send its Ether in units of wei using address.send like: seller.send(price) A contract cannot subtract from ...
eth's user avatar
  • 85.4k
11 votes

Automatically update account balance with web3.js without polling

You can install a block filter and only update balances when a new block arrives. const Web3 = require('web3'); const web3 = new Web3(new Web3.providers.HttpProvider("http://localhost:8545")); ...
Ismael's user avatar
  • 30.5k
10 votes

How does the Ethereum ETH accounting system work and prevent double-spends?

I am not providing an answer here but I am elaborating on a small discussion with @eth which is relevant to the OP's question, and I wanted to share what I have learnt, thanks to @eth and a bit of ...
T9b's user avatar
  • 1,185
10 votes

How are Ether balances stored on the Ethereum blockchain

The related question linked in comments by Richard Horrocks answers the question pretty well IMO. Here's what the Ethereum yellowpaper has to say about this (search for balance, it's hit number 2 ATM ...
Noel Maersk's user avatar
10 votes
Accepted

How does etherscan fetch the number of token holders from a smart contract?

They keep track of Transfer events emitted by that smart contract. Most likely they record them in an SQL database and maintain a ledger based on these events. The event is defined in the ERC20 https:/...
medvedev1088's user avatar
10 votes
Accepted

Truffle test: How to test Ether transfer? Balance of specific account?

To assert the balance of an account, try this: let expectedBalance = web3.toBigNumber(web3.toWei(3, 'ether')); let actualBalance = await web3.eth.getBalance(accounts[1]); assert.deepEqual(...
Henk's user avatar
  • 4,266
9 votes
Accepted

How can I get notification from geth when I receive ether?

personally I registered my addresses on https://etherscan.io that provides such a service, by email. If you really want to do it by yourself, the simplest I can think would be to register a cron job ...
euri10's user avatar
  • 4,640
9 votes
Accepted

Getting Metamask account's balance using fromWei

Apparently, this is not the same inside the anonymous callback function you're passing to getBalance. Try replacing it with a fat arrow function, that binds the outer this value to the inner scope. ...
martriay's user avatar
  • 427
9 votes
Accepted

API for ERC20 token balance - EtherScan

From https://etherscan.io/apis#tokens: Get ERC20-Token Account Balance for TokenContractAddress https://api.etherscan.io/api?module=account&action=tokenbalance&contractaddress=...
user19510's user avatar
  • 27.9k
8 votes
Accepted

What are the balances of The DAO and it's child DAOs on the Ethereum Classic chain?

I'm using the same unmodified script from How many ethers have been drained through the recursive call attacks on The DAO? to calculate the balance of The DAO and it's child DAOs. For a finer ...
BokkyPooBah's user avatar
  • 40.3k
8 votes

How to transfer Ether between accounts in Solidity?

Please refer to address methods documentation. You should prefer to use transfer over send: There are some dangers in using send: The transfer fails if the call stack depth is at 1024 (this can ...
Robert Zaremba's user avatar
8 votes
Accepted

How can I list The DAO proposals and vote from the command line?

Yes. Here Is theDAOVoter Description theDAOVoter v1.0000000000000003 is a little Perl script (~808 lines, 738 source lines) that will allow you to: List The DAO proposals. List your accounts, ...
BokkyPooBah's user avatar
  • 40.3k
8 votes
Accepted

How do I know how "confirmed" the result of calling getBalance API is?

getBalance With A Block Number You can pass in the block number as part of the parameter. Here is Kraken's address: 0x2910543af39aba0cd09dbb2d50200b3e800a63d2. Let's check what the latest block ...
BokkyPooBah's user avatar
  • 40.3k
8 votes

How to get ETH contract balance with Ethers.js

In the context of testing a contract with Hard Hat: const { ethers, waffle} = require("hardhat"); const provider = waffle.provider; const balanceInWei = await provider.getBalance(contract....
MetaZebre's user avatar
  • 181
8 votes
Accepted

Hardhat, ether.js - fetching balance of signers locally shows no ether

The issue was the provider returned by getDefaultProvider connected to the main net, while I was trying to test locally. I ended up at this conclusion by calling the getNetwork call on the provider, ...
td-bn's user avatar
  • 371

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