Skip to content

Commit

Permalink
style: lint
Browse files Browse the repository at this point in the history
  • Loading branch information
gosuto-inzasheru committed Dec 15, 2023
1 parent 9119404 commit a5911c5
Showing 1 changed file with 59 additions and 49 deletions.
108 changes: 59 additions & 49 deletions bal_addresses/addresses.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,7 @@
GITHUB_MONOREPO_RAW = (
"https://raw.githubusercontent.com/balancer-labs/balancer-v2-monorepo/master"
)
GITHUB_MONOREPO_NICE = (
"https://github.com/balancer/balancer-v2-monorepo/blob/master"
)
GITHUB_MONOREPO_NICE = "https://github.com/balancer/balancer-v2-monorepo/blob/master"
GITHUB_DEPLOYMENTS_RAW = (
"https://raw.githubusercontent.com/balancer/balancer-deployments/master"
)
Expand All @@ -26,15 +24,17 @@
ZERO_ADDRESS = "0x0000000000000000000000000000000000000000"




class AddrBook:
chains = Munch.fromDict(requests.get(
"https://raw.githubusercontent.com/BalancerMaxis/bal_addresses/main/extras/chains.json"
).json())
fx_description_by_name = Munch.fromDict(requests.get(
"https://raw.githubusercontent.com/BalancerMaxis/bal_addresses/main/extras/func_desc_by_name.json"
).json())
chains = Munch.fromDict(
requests.get(
"https://raw.githubusercontent.com/BalancerMaxis/bal_addresses/main/extras/chains.json"
).json()
)
fx_description_by_name = Munch.fromDict(
requests.get(
"https://raw.githubusercontent.com/BalancerMaxis/bal_addresses/main/extras/func_desc_by_name.json"
).json()
)
chain_ids_by_name = chains.CHAIN_IDS_BY_NAME

def __init__(self, chain, jsonfile=False):
Expand Down Expand Up @@ -127,72 +127,87 @@ def _process_deployment(self, deployment: Dict) -> Dict:
# Change all - to underscores
deployment_identifier = k.lstrip("0123456789-").replace("-", "_")
# Flatten contracts list to dict with name as key
if isinstance(v.get('contracts'), list):
contracts = {contract['name']: {**contract, 'deployment': k, 'path': f"{k}/{contract['name']}"} for
contract in v['contracts']}
contracts_by_contract = {contract: data for contract, data in contracts.items()}
if isinstance(v.get("contracts"), list):
contracts = {
contract["name"]: {
**contract,
"deployment": k,
"path": f"{k}/{contract['name']}",
}
for contract in v["contracts"]
}
contracts_by_contract = {
contract: data for contract, data in contracts.items()
}
v["contracts"] = contracts_by_contract
processed_deployment[deployment_identifier] = v
return processed_deployment

def populate_extras(self) -> None:
chain_extras = requests.get(
f"{GITHUB_RAW_EXTRAS}/{self.chain}.json"
)
chain_extras = requests.get(f"{GITHUB_RAW_EXTRAS}/{self.chain}.json")
if chain_extras.ok:
self._extras = Munch.fromDict(self.checksum_address_dict(chain_extras.json()))
self._extras = Munch.fromDict(
self.checksum_address_dict(chain_extras.json())
)
else:
print(f"Warning: No extras for chain {self.chain}, extras must be added in extras/chain.json")
print(
f"Warning: No extras for chain {self.chain}, extras must be added in extras/chain.json"
)
self._extras = Munch.fromDict({})

def populate_eoas(self) -> None:
eoas = requests.get(
f"{GITHUB_RAW_EXTRAS}/signers.json"
)
eoas = requests.get(f"{GITHUB_RAW_EXTRAS}/signers.json")
if eoas.ok:
self._eoas = Munch.fromDict(self.checksum_address_dict(eoas.json()))

def populate_multisigs(self) -> None:
msigs = requests.get(
f"{GITHUB_RAW_EXTRAS}/multisigs.json"
).json()
msigs = requests.get(f"{GITHUB_RAW_EXTRAS}/multisigs.json").json()
if msigs.get(self.chain):
self._multisigs = Munch.fromDict(self.checksum_address_dict(msigs[self.chain]))
self._multisigs = Munch.fromDict(
self.checksum_address_dict(msigs[self.chain])
)
else:
print(f"Warning: No multisigs for chain {self.chain}, multisigs must be added in extras/multisig.json")
print(
f"Warning: No multisigs for chain {self.chain}, multisigs must be added in extras/multisig.json"
)
self._multisigs = Munch.fromDict({})



def search_unique(self, substr):
results = [s for s in self.flatbook.keys() if substr in s]
if len(results) > 1:
raise MultipleMatchesError(f"{substr} Multiple matches found: {results}")
if len(results) < 1:
raise NoResultError(f"{substr}")
return Munch.fromDict({
"path": results[0],
"address": self.flatbook[results[0]]
})
return Munch.fromDict(
{"path": results[0], "address": self.flatbook[results[0]]}
)

def search_unique_deployment(self, substr):
results = [s for s in self.deployments_only.keys() if substr in s]
if len(results) > 1:
raise MultipleMatchesError(f"{substr} Multiple matches found: {results}")
if len(results) < 1:
raise NoResultError(f"{substr}")
return Munch.fromDict({
"deployment": results[0],
"addresses_by_contract": self.deployments_only[results[0]]
})
return Munch.fromDict(
{
"deployment": results[0],
"addresses_by_contract": self.deployments_only[results[0]],
}
)

def search_many_deployments(self, substr):
search = [s for s in self.deployments_only.keys() if substr in s]
return search

def search_many(self, substr):
output = []
results = {path: address for path, address in self.flatbook.items() if substr in path}
outputs = [Munch.fromDict({"path": path, "address": address}) for path, address in results.items()]
results = {
path: address for path, address in self.flatbook.items() if substr in path
}
outputs = [
Munch.fromDict({"path": path, "address": address})
for path, address in results.items()
]
return outputs

def latest_contract(self, contract_name):
Expand All @@ -203,12 +218,8 @@ def latest_contract(self, contract_name):
if len(deployments) == 0:
raise NoResultError(contract_name)
deployments.sort(reverse=True)
address = self.deployments_only[deployments[0]][contract_name]
return Munch.fromDict({
"path": self.reversebook[address],
"address": address
})

address = self.deployments_only[deployments[0]][contract_name]
return Munch.fromDict({"path": self.reversebook[address], "address": address})

@staticmethod
def checksum_address_dict(addresses):
Expand All @@ -229,7 +240,7 @@ def checksum_address_dict(addresses):
checksummed[k] = v
return checksummed

def flatten_dict(self, d, parent_key='', sep='/'):
def flatten_dict(self, d, parent_key="", sep="/"):
items = []
d = dict(d)
for k, v in d.items():
Expand All @@ -249,8 +260,7 @@ def generate_flatbook(self):
for deployment, ddata in self.deployments.items():
for contract, infodict in ddata["contracts"].items():
flatbook[infodict.path] = infodict.address
flatbook = {**flatbook,
**self.flatten_dict(self.extras)}
flatbook = {**flatbook, **self.flatten_dict(self.extras)}
flatbook["multisigs"] = self.flatten_dict(self.multisigs)
flatbook["EOA"] = self.flatten_dict(self.EOAs)
return self.flatten_dict(flatbook)
Expand Down

0 comments on commit a5911c5

Please sign in to comment.