Closed facelessnoone99 closed 5 years ago
You can use the Web3.eth.Contract. https://web3js.readthedocs.io/en/1.0/web3-eth-contract.html#id10
Closed because of the answer from @princesinha19.
@princesinha19 thanks, but this way just get one erc20, I want get all if my address have
@facelessnoone99 @princesinha19 you don’t need to write solidity code to do this. You need to know the contract addresses for where the ERC20 tokens lives and call the contract method (normally balance) individually. You can do a foreach on this and call every contract address balance method you want which then builds up the list. This can all be achieved with JavaScript using web3 and with a standard ERC20 ABI but there is no way to just get them all with 1 JSONRPC call.
@jeffersondarcy thanks, the first I don't konw how many kind of erc20 , if have million kind of erc20 ,then I will do foreach million contract address to get the balance? I don't think so this way is the best way.
Balance is a contract method call and is the only way to query real-time using the blockchain to get the ERC20 token balance. Etherscan is just a big database which listens out for new block headers and does very clever things with nodes, all it does is store / update new balances if the transaction data is related to the ERC20 token in there db - so it’s not using the blockchain to query it everytime you load your address page, when it’s loading that it’s just reading from their DB database (hence why etherscan sometimes is a lot slower then your application getting a response and showing the completed transaction).
At the moment there is no JSONRPC call which is “get all ERC20 tokens for x address” if you wanted to do this without calling the actual contract method you will have to pay for etherscans API.
This is why most wallets only support a certain amount of tokens out the box and make you add “custom” ones suppling the address and symbol name which then allows the application to query new ERC20 tokens that they want to show. Examples of this is metamask and myetherwallet.
@facelessnoone99
@princesinha19 thanks
I have problem with call balanceOf
method
(node:93693) UnhandledPromiseRejectionWarning: TypeError: Cannot read property 'send' of undefined
at CallContractMethod._callee$ (.../web3-core-method/dist/web3-core-method.cjs.js:105:60)
My code
import Web3 from 'web3'; // 1.0.0-beta.55
async function go() {
const web3 = new Web3(process.env.GETH_IPC);
const abi : any = ... // JSON
const address = '0x89d24a6b4ccb1b6faa2625fe562bdd9a23260359'; // DAI
const contract = new web3.eth.Contract(abi, address);
const balanceOfTx = awaitcontract.methods.balanceOf('0x564eec05271710f2a01bd7c71b47ec73528d1184').call()
.then(res => {
console.log(res);
});
}
go();
@princesinha19 My code looks like in docs. Do you now problem? Thank you!
can you post the actual sample code for tutorial? @nivida
Version - web3@^1.2.2
const Web3 = require('web3'); const web3 = new Web3(new Web3.providers.HttpProvider(process.env.ETHNODE_URL)); var abiArrayToken = JSON.parse(fs.readFileSync('abi.json', 'utf-8')); var tokenAddress = process.env.TOKEN_ADDRESS; var contract = new web3.eth.Contract(abiArrayToken,tokenAddress)
var data = contract.methods.balanceOf(process.env.ACCOUNT_ADDRESS).call() data.then(function(val){ var str = web3.utils.fromWei(val); console.log("TokenBalance : " +JSON.stringify(str)) });
I have problem with call
balanceOf
method(node:93693) UnhandledPromiseRejectionWarning: TypeError: Cannot read property 'send' of undefined at CallContractMethod._callee$ (.../web3-core-method/dist/web3-core-method.cjs.js:105:60)
My code
import Web3 from 'web3'; // 1.0.0-beta.55 async function go() { const web3 = new Web3(process.env.GETH_IPC); const abi : any = ... // JSON const address = '0x89d24a6b4ccb1b6faa2625fe562bdd9a23260359'; // DAI const contract = new web3.eth.Contract(abi, address); const balanceOfTx = awaitcontract.methods.balanceOf('0x564eec05271710f2a01bd7c71b47ec73528d1184').call() .then(res => { console.log(res); }); } go();
@princesinha19 My code looks like in docs. Do you now problem? Thank you!
Hello, I can't see anything wrong with your code.
The await method waits for the Promise to do it's thing hence you don't need the .then method
const balanceOfTx = await contract.methods.balanceOf('0x564eec05271710f2a01bd7c71b47ec73528d1184').call()
This should definitely work.
FYI @facelessnoone99 and @all if you guys are seeking the same problem or want to update your existing or new project for the above Query of getting all ERC20 token balances (your address belongs). Moralis does provide this kind of solution ... give a try https://docs.moralis.io/moralis-server/web3-sdk/account#gettokenbalances
I tried for on mumbai matic network...
[
{
"token_address": "0x001b3b4d0f3714ca98ba10f6042daebf0b1b7b6f",
"name": "DAI",
"symbol": "DAI",
"logo": null,
"thumbnail": null,
"decimals": "18",
"balance": "85000000000000000"
},
{
"token_address": "0x326c977e6efc84e512bb9c30f76e30c160ed06fb",
"name": "ChainLink Token",
"symbol": "LINK",
"logo": null,
"thumbnail": null,
"decimals": "18",
"balance": "1000000000000000000"
}
]
I want to get all erc20 balance for one address, like etherscan, can list address balance and all erc20 balance below it