Skip to content

Commit

Permalink
Merge pull request #232 from BalancerMaxis/support_all_web3_versions
Browse files Browse the repository at this point in the history
Support all web3 versions
  • Loading branch information
gosuto-inzasheru authored Mar 19, 2024
2 parents a3e3664 + 09ba9ba commit bea2990
Show file tree
Hide file tree
Showing 5 changed files with 23 additions and 11 deletions.
7 changes: 3 additions & 4 deletions bal_addresses/addresses.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,7 @@

import requests
from munch import Munch
from web3 import Web3

from .utils import to_checksum_address
GITHUB_MONOREPO_RAW = (
"https://raw.githubusercontent.com/balancer-labs/balancer-v2-monorepo/master"
)
Expand Down Expand Up @@ -275,7 +274,7 @@ def checksum_address_dict(addresses):
checksummed[k] = checksum_address_dict(v)
elif isinstance(v, str):
try:
checksummed[k] = Web3.toChecksumAddress(v)
checksummed[k] = to_checksum_address(v)
except:
checksummed[k] = v
else:
Expand Down Expand Up @@ -326,7 +325,7 @@ def checksum_address_dict(addresses):
checksummed[k] = checksum_address_dict(v)
elif isinstance(v, str):
try:
checksummed[k] = Web3.toChecksumAddress(v)
checksummed[k] = to_checksum_address(v)
except:
checksummed[k] = v
else:
Expand Down
9 changes: 4 additions & 5 deletions bal_addresses/pools_gauges.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
from typing import Dict
import json
import requests
from web3 import Web3
from .utils import to_checksum_address

from bal_addresses.subgraph import Subgraph
from bal_addresses.errors import NoResultError
Expand Down Expand Up @@ -31,22 +30,22 @@ def get_bpt_balances(self, pool_id: str, block: int) -> Dict[str, int]:
results = {}
if "pool" in data and data["pool"]:
for share in data["pool"]["shares"]:
user_address = Web3.toChecksumAddress(share["userAddress"]["id"])
user_address = to_checksum_address(share["userAddress"]["id"])
results[user_address] = float(share["balance"])
return results

def get_gauge_deposit_shares(
self, gauge_address: str, block: int
) -> Dict[str, int]:
gauge_address = Web3.toChecksumAddress(gauge_address)
gauge_address = to_checksum_address(gauge_address)
variables = {"gaugeAddress": gauge_address, "block": int(block)}
data = self.subgraph.fetch_graphql_data(
self.subgraph.BALANCER_GAUGES_SHARES_QUERY, variables
)
results = {}
if "data" in data and "gaugeShares" in data["data"]:
for share in data["data"]["gaugeShares"]:
user_address = Web3.toChecksumAddress(share["user"]["id"])
user_address = to_checksum_address(share["user"]["id"])
results[user_address] = float(share["balance"])
return results

Expand Down
2 changes: 1 addition & 1 deletion bal_addresses/requirements.txt
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
pathlib>=1.0
requests
pandas
web3==5.31.3
web3
dotmap
munch==4.0.0
gql[requests]
14 changes: 14 additions & 0 deletions bal_addresses/utils.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
from web3 import Web3


### These functions are to deal with differing web3 versions and the need to use 5.x for legacy brownie code
def to_checksum_address(address: str):
if hasattr(Web3, 'toChecksumAddress'):
return Web3.toChecksumAddress(address)
if hasattr(Web3, "to_checksum_address"):
return Web3.to_checksum_address(address)
def is_address(address: str):
if hasattr(Web3, "isAddress"):
return Web3.isAddress(address)
if hasattr(Web3, "is_address"):
return Web3.isAddress(address)
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
long_description=LONG_DESCRIPTION,
packages=find_packages(),
url="https://github.com/BalancerMaxis/bal_addresses",
install_requires=["setuptools>=42", "wheel", "munch==4.0.0", "web3==5.31.*", "gql[requests]"], # add any additional packages that
install_requires=["setuptools>=42", "wheel", "munch==4.0.0", "web3", "gql[requests]"], # add any additional packages that
# needs to be installed along with your package. Eg: 'caer'

keywords=['python', 'first package'],
Expand Down

0 comments on commit bea2990

Please sign in to comment.