Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add token and get balance #130

Open
irfandi-fm opened this issue Nov 20, 2023 · 3 comments
Open

Add token and get balance #130

irfandi-fm opened this issue Nov 20, 2023 · 3 comments

Comments

@irfandi-fm
Copy link

Hi!
I'm new to the blockchain technology, and I'm currently building a simple wallet using flutter.
I can get the balance of my wallet with below code.

  Future<EtherAmount> getBalance(
      {required String privateKey, required String rpcUrl}) async {
    final client = Web3Client(rpcUrl, Client());
    final credentials = EthPrivateKey.fromHex(privateKey);
    final address = credentials.address;
    final balance = await client.getBalance(address);
    debugPrint(balance.toString());

    return balance;
  }

Next, I want to implement the add token just like in metamask. How do do that? I see that we need to supply the contract address. Then how do we get the balance of that token?

@xclud
Copy link
Owner

xclud commented Nov 20, 2023

Hi, all you need is to have the contract address. You get balance of the contract address.

@irfandi-fm
Copy link
Author

Hi, all you need is to have the contract address. You get balance of the contract address.

Can you give me an example code? Because the parameter to get the balance is only the wallet address. Where to supply the contract address?

@reasje
Copy link

reasje commented Feb 14, 2024

@irfandi-fm I did these to get token balance :

  1. Generate dart class from contract ABI (There are descriptions about how to do that in the readme of this repo HERE)
  2. Make an instance of that class with It's contract address
  3. call balanceOf func of that instance and get the balance
final ercToken =
    contracts.ErcToken(client: _web3Client, address: data);

final tokenBalanceResponse = await ercToken.balanceOf(address);

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants