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

feat: allow comparing currency values to currency strings #2149

Merged
merged 2 commits into from
Jun 17, 2024

Conversation

antazoey
Copy link
Member

What I did

def test_balance(accounts):
    acct = accounts[0]
    # Before
    assert acct.balance == convert("1000000 ETH", int)
    # After
    assert acct.balance == "1000000 ETH"

fixes: #324

How I did it

super simple int wrapper in a couple cases

How to verify it

Checklist

  • All changes are completed
  • New test cases have been added
  • Documentation has been updated

Copy link
Member

@fubuloubu fubuloubu left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👀

@antazoey antazoey enabled auto-merge (squash) June 17, 2024 19:30
@antazoey antazoey merged commit e44a953 into ApeWorX:main Jun 17, 2024
15 checks passed
@antazoey antazoey deleted the feat/balanace-compare-to-str branch June 17, 2024 22:56
@banteg
Copy link
Contributor

banteg commented Jun 20, 2024

do i read it right it only works with account.balance? what would it take to make it also work with contract call return values like token.balanceOf?

@antazoey
Copy link
Member Author

do i read it right it only works with account.balance? what would it take to make it also work with contract call return values like token.balanceOf?

We would have to detect balanceOf name in the ABI and wrap it CurrencyValue during the result.
Alternatively, can make the name of CurrencyValue more generic like CurrencyComparable and just wrap all return data ints in that.

@banteg
Copy link
Contributor

banteg commented Jun 21, 2024

yeah i think it's the best to just make every int support this

@antazoey
Copy link
Member Author

yeah i think it's the best to just make every int support this

I don't think we can make every int support this though, without modifying python or something?
We can make every int returned from decode_returndata() handle this tho

@fubuloubu
Copy link
Member

I think adding comparison of return values to values decoded by the conversion manager for types like int will satisfy 90% of the use cases

We already have custom __eq__ methods for things like structs, I think only Decimal would be the other python type to handle, maybe AddressType might also be useful e.g. assert c.owner() == "ychad.eth"
https://github.com/ApeWorX/ape/blob/main/src/ape/managers/converters.py#L260-L275

@banteg
Copy link
Contributor

banteg commented Jun 21, 2024

We can make every int returned from decode_returndata() handle this tho

yes, sure, i meant exactly this. and ints in events too.

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

Successfully merging this pull request may close these issues.

Testing: Equality handling between currency units and int
3 participants