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

Account balance access problems with Moccasin and Web3 #201

Closed
scwall opened this issue Feb 3, 2025 · 3 comments
Closed

Account balance access problems with Moccasin and Web3 #201

scwall opened this issue Feb 3, 2025 · 3 comments
Labels

Comments

@scwall
Copy link

scwall commented Feb 3, 2025

Hello,

First of all, thank you for your work! I’ve learned a lot from your online courses and the various products you provide. I really appreciate the quality of your content.

I have a question regarding Moccasin, specifically about how it handles account data. In the documentation ([link to Moccasin docs](https://cyfrin.github.io/moccasin/core_concepts/networks/forked_networks.html)), it is mentioned that Moccasin uses lazy loading, meaning it only fetches blockchain data when needed.

For example, if I want to get the balance of an account, Moccasin should only download that specific balance when my script requests it. However, I couldn't find any built-in function like account.get_balance() that allows me to do this directly.

Instead, I have to retrieve the information manually using Web3, which confuses me. Am I missing something, or is this the expected behavior? Is there a recommended way to handle account interactions more efficiently within Moccasin?

Thanks in advance for your help!

@s3bc40
Copy link
Contributor

s3bc40 commented Feb 4, 2025

Hey @scwall!

Thanks for your message and the appreciation on Patrick's and Cyfrin's work!

Regarding your issue, from my understanding, Moccasin is using Titanoboa under the hood. And asking for the balance of your account depends on the token you will request.


If you are asking for ETH, Titanoboa has built in method under their env namespace get_balance():

Doc ref (wip)

import boa

account_eth_balance = boa.env.get_balance(mox_account.address)
# You can also use boa.env.eoa if you want to run some test and also use it prank too

The Titanoboa doc is still under in its building phase, so some functionalities are not in the doc. But they do exist if we check their code base


If you are asking for a specific token balance, other than ETH, then you'll have to ask for their proper method to get the balance. I think it is token.balanceOf() for ERC20, but you need to check their ABI. The course on Algorithmic trading from Patrick is a great example on how you set this up and follow the balance of tokens:

# If you have their ABIContract you can do this
usdc_balance = usdc.balanceOf(boa.env.eoa) # or your address instead of boa.env.eoa

If you want to check: Youtube, or directly on CyfrinUpdraft platform. And the GitHub repo.

Hope my solution is adapted to your issue 👼

@PatrickAlphaC
Copy link
Member

I was gonna write this long comment out, and then I just said F it and added it. Good feature request.

#202

(I also fixed some issues with the zksync tests. I musta skipped running them before... oops)

@scwall
Copy link
Author

scwall commented Feb 7, 2025

Hello,

Super thanks for your answers :)

Have a nice evening

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

No branches or pull requests

3 participants