Skip to main content
105 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,477
34 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,332
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
  • 871
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,381
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

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
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,183
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

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
  • 86.2k
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.4k
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,281
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
  • 28.1k
9 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
  • 191
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
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

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

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
7 votes
Accepted

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

getAllBalances Including TheDAO Tokens Here's a version of getAllBalances that also displays TheDAO tokens. The following shell script works in the Linux and Mac environments where you are already ...
BokkyPooBah's user avatar
  • 40.3k
7 votes

How to get a contract's balance in Solidity?

In case of invalid opcode, use a local variable of type address payable as workaround: address payable self = address(this); uint256 balance = self.balance; In solidity 0.5.14, I get ...
Marc Wäckerlin's user avatar
7 votes

Etherscan api for Ropsten testnet

According to the docs, you must simply append "-ropsten" to the subdomain. Your URL should look as follows: https://api-ropsten.etherscan.io/api?module=account&action=tokenbalance&...
Shane Fontaine's user avatar
6 votes
Accepted

When will the Baddies have access to the hacked funds on the Ethereum Classic chain?

The Baddies joined into the Split Proposal #59 where they were not the curator. From this split, the Baddies created a number of split proposals where the Split Proposal #59.10 was used to split out ...
BokkyPooBah's user avatar
  • 40.3k
6 votes

Multiple ways to check the balance of ERC20 tokens of smart contract: which to use?

I think you are confusing address(this).balance which will give you the native token balance (ETH, MATIC, BNB...) of the contract, with IERC20(tokenAddress).balanceOf(address(this)) which gives you ...
Kirienzo Eth's user avatar
6 votes

How to send ether to a contract (even if the contract doesn't implement receive or fallback functions)?

The last 2 ways listed for sending balance allow to send balance to a Smart Contract even if it doesn't implement receive and payable fallback functions: You have multiple ways to transfer balance ...
Eric Nordelo Galiano's user avatar

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