From e9f47718b3f7e17180fa05f32324fe3c7df0950e Mon Sep 17 00:00:00 2001 From: Tritium-VLK <11885129+Tritium-VLK@users.noreply.github.com> Date: Sun, 22 Sep 2024 15:51:05 +0200 Subject: [PATCH 1/5] feat: lookup unique address on all chains. --- bal_addresses/__init__.py | 1 + bal_addresses/addresses.py | 78 ++++++++++++++++---------------------- 2 files changed, 33 insertions(+), 46 deletions(-) diff --git a/bal_addresses/__init__.py b/bal_addresses/__init__.py index 59fe3a4..a9fccbc 100644 --- a/bal_addresses/__init__.py +++ b/bal_addresses/__init__.py @@ -1,5 +1,6 @@ from .addresses import ( AddrBook, + get_address_all_chains, GITHUB_DEPLOYMENTS_RAW, GITHUB_DEPLOYMENTS_NICE, GITHUB_RAW_OUTPUTS, diff --git a/bal_addresses/addresses.py b/bal_addresses/addresses.py index f23b2ac..b11b326 100644 --- a/bal_addresses/addresses.py +++ b/bal_addresses/addresses.py @@ -12,23 +12,32 @@ from .utils import to_checksum_address -GITHUB_MONOREPO_RAW = ( - "https://raw.githubusercontent.com/balancer-labs/balancer-v2-monorepo/master" -) +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_DEPLOYMENTS_RAW = ( - "https://raw.githubusercontent.com/balancer/balancer-deployments/master" -) +GITHUB_DEPLOYMENTS_RAW = "https://raw.githubusercontent.com/balancer/balancer-deployments/master" GITHUB_DEPLOYMENTS_NICE = "https://github.com/balancer/balancer-deployments/blob/master" -GITHUB_RAW_OUTPUTS = ( - "https://raw.githubusercontent.com/BalancerMaxis/bal_addresses/main/outputs" -) -GITHUB_RAW_EXTRAS = ( - "https://raw.githubusercontent.com/BalancerMaxis/bal_addresses/main/extras" -) +GITHUB_RAW_OUTPUTS = "https://raw.githubusercontent.com/BalancerMaxis/bal_addresses/main/outputs" +GITHUB_RAW_EXTRAS = "https://raw.githubusercontent.com/BalancerMaxis/bal_addresses/main/extras" ZERO_ADDRESS = "0x0000000000000000000000000000000000000000" +def get_address_all_chains(search_string: str) -> dict: + """ + Finds addresses for a unqiue name acrosos all chains + """ + result = {} + for chain in AddrBook.chain_ids_by_name.keys(): + print(f"Processing {chain}") + addr_book = AddrBook(chain) + try: + info = addr_book.search_unique(search_string) + result[chain] = info + except NoResultError: + result[chain] = None + print("Not Found") + return result + + class AddrBook: chains = Munch.fromDict( json.load(open("extras/chains.json")) @@ -160,9 +169,7 @@ def rate_providers(self) -> Optional[Munch]: return self._rate_providers def populate_deployments(self) -> None: - chain_deployments = requests.get( - f"{GITHUB_DEPLOYMENTS_RAW}/addresses/{self.chain}.json" - ) + chain_deployments = requests.get(f"{GITHUB_DEPLOYMENTS_RAW}/addresses/{self.chain}.json") if chain_deployments.ok: # Remove date from key processed_deployment = self._process_deployment(chain_deployments.json()) @@ -190,9 +197,7 @@ def _process_deployment(self, deployment: Dict) -> Dict: } for contract in v["contracts"] } - contracts_by_contract = { - contract: data for contract, data in contracts.items() - } + contracts_by_contract = {contract: data for contract, data in contracts.items()} v["contracts"] = contracts_by_contract processed_deployment[deployment_identifier] = v return processed_deployment @@ -200,13 +205,9 @@ def _process_deployment(self, deployment: Dict) -> Dict: def populate_extras(self) -> None: 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: @@ -217,13 +218,9 @@ def populate_eoas(self) -> None: def populate_multisigs(self) -> None: 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 populate_pools(self) -> None: @@ -245,9 +242,7 @@ def populate_gauges(self) -> None: else requests.get(f"{GITHUB_RAW_OUTPUTS}/gauges.json").json() ) if gauges.get(self.chain): - self._gauges = Munch.fromDict( - self.checksum_address_dict(gauges[self.chain]) - ) + self._gauges = Munch.fromDict(self.checksum_address_dict(gauges[self.chain])) else: print(f"Warning: No gauges for chain {self.chain}") self._gauges = Munch.fromDict({}) @@ -260,9 +255,7 @@ def populate_root_gauges(self) -> None: else requests.get(f"{GITHUB_RAW_OUTPUTS}/root_gauges.json").json() ) if root_gauges.get(self.chain): - self._root_gauges = Munch.fromDict( - self.checksum_address_dict(root_gauges[self.chain]) - ) + self._root_gauges = Munch.fromDict(self.checksum_address_dict(root_gauges[self.chain])) else: print(f"Warning: No root gauges for chain {self.chain}") self._root_gauges = Munch.fromDict({}) @@ -290,9 +283,7 @@ def search_unique(self, substr): 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] @@ -313,13 +304,8 @@ def search_many_deployments(self, substr): 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): From 6ded887ecf7990d5e450db0fc17bc3c0e0c8e0a5 Mon Sep 17 00:00:00 2001 From: Tritium-VLK <11885129+Tritium-VLK@users.noreply.github.com> Date: Sun, 22 Sep 2024 15:53:38 +0200 Subject: [PATCH 2/5] update heredoc --- bal_addresses/addresses.py | 2 ++ 1 file changed, 2 insertions(+) diff --git a/bal_addresses/addresses.py b/bal_addresses/addresses.py index b11b326..706b61b 100644 --- a/bal_addresses/addresses.py +++ b/bal_addresses/addresses.py @@ -24,6 +24,8 @@ def get_address_all_chains(search_string: str) -> dict: """ Finds addresses for a unqiue name acrosos all chains + returns a dict with chain as keys and a dict with "path" and "address" as values + Will throw MultipleMatchesError if the string is not unqiue on each and every chain """ result = {} for chain in AddrBook.chain_ids_by_name.keys(): From c24dba144625f8de3efe5411a1728b36c77a1ed1 Mon Sep 17 00:00:00 2001 From: Tritium-VLK Date: Sun, 22 Sep 2024 13:55:29 +0000 Subject: [PATCH 3/5] style: ci lint with `black` --- bal_addresses/addresses.py | 61 ++++++++++++++++++++++++++++---------- 1 file changed, 46 insertions(+), 15 deletions(-) diff --git a/bal_addresses/addresses.py b/bal_addresses/addresses.py index 706b61b..7287a17 100644 --- a/bal_addresses/addresses.py +++ b/bal_addresses/addresses.py @@ -12,12 +12,20 @@ from .utils import to_checksum_address -GITHUB_MONOREPO_RAW = "https://raw.githubusercontent.com/balancer-labs/balancer-v2-monorepo/master" +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_DEPLOYMENTS_RAW = "https://raw.githubusercontent.com/balancer/balancer-deployments/master" +GITHUB_DEPLOYMENTS_RAW = ( + "https://raw.githubusercontent.com/balancer/balancer-deployments/master" +) GITHUB_DEPLOYMENTS_NICE = "https://github.com/balancer/balancer-deployments/blob/master" -GITHUB_RAW_OUTPUTS = "https://raw.githubusercontent.com/BalancerMaxis/bal_addresses/main/outputs" -GITHUB_RAW_EXTRAS = "https://raw.githubusercontent.com/BalancerMaxis/bal_addresses/main/extras" +GITHUB_RAW_OUTPUTS = ( + "https://raw.githubusercontent.com/BalancerMaxis/bal_addresses/main/outputs" +) +GITHUB_RAW_EXTRAS = ( + "https://raw.githubusercontent.com/BalancerMaxis/bal_addresses/main/extras" +) ZERO_ADDRESS = "0x0000000000000000000000000000000000000000" @@ -171,7 +179,9 @@ def rate_providers(self) -> Optional[Munch]: return self._rate_providers def populate_deployments(self) -> None: - chain_deployments = requests.get(f"{GITHUB_DEPLOYMENTS_RAW}/addresses/{self.chain}.json") + chain_deployments = requests.get( + f"{GITHUB_DEPLOYMENTS_RAW}/addresses/{self.chain}.json" + ) if chain_deployments.ok: # Remove date from key processed_deployment = self._process_deployment(chain_deployments.json()) @@ -199,7 +209,9 @@ def _process_deployment(self, deployment: Dict) -> Dict: } for contract in v["contracts"] } - contracts_by_contract = {contract: data for contract, data in contracts.items()} + contracts_by_contract = { + contract: data for contract, data in contracts.items() + } v["contracts"] = contracts_by_contract processed_deployment[deployment_identifier] = v return processed_deployment @@ -207,9 +219,13 @@ def _process_deployment(self, deployment: Dict) -> Dict: def populate_extras(self) -> None: 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: @@ -220,9 +236,13 @@ def populate_eoas(self) -> None: def populate_multisigs(self) -> None: 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 populate_pools(self) -> None: @@ -244,7 +264,9 @@ def populate_gauges(self) -> None: else requests.get(f"{GITHUB_RAW_OUTPUTS}/gauges.json").json() ) if gauges.get(self.chain): - self._gauges = Munch.fromDict(self.checksum_address_dict(gauges[self.chain])) + self._gauges = Munch.fromDict( + self.checksum_address_dict(gauges[self.chain]) + ) else: print(f"Warning: No gauges for chain {self.chain}") self._gauges = Munch.fromDict({}) @@ -257,7 +279,9 @@ def populate_root_gauges(self) -> None: else requests.get(f"{GITHUB_RAW_OUTPUTS}/root_gauges.json").json() ) if root_gauges.get(self.chain): - self._root_gauges = Munch.fromDict(self.checksum_address_dict(root_gauges[self.chain])) + self._root_gauges = Munch.fromDict( + self.checksum_address_dict(root_gauges[self.chain]) + ) else: print(f"Warning: No root gauges for chain {self.chain}") self._root_gauges = Munch.fromDict({}) @@ -285,7 +309,9 @@ def search_unique(self, substr): 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] @@ -306,8 +332,13 @@ def search_many_deployments(self, substr): 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): From b149b7ebcf2807fb096021e5a7749a122043e26c Mon Sep 17 00:00:00 2001 From: Tritium-VLK <11885129+Tritium-VLK@users.noreply.github.com> Date: Sun, 22 Sep 2024 16:04:19 +0200 Subject: [PATCH 4/5] fix spelling --- bal_addresses/addresses.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/bal_addresses/addresses.py b/bal_addresses/addresses.py index 706b61b..e0e2a3c 100644 --- a/bal_addresses/addresses.py +++ b/bal_addresses/addresses.py @@ -23,9 +23,9 @@ def get_address_all_chains(search_string: str) -> dict: """ - Finds addresses for a unqiue name acrosos all chains + Finds addresses for a unique name across all chains returns a dict with chain as keys and a dict with "path" and "address" as values - Will throw MultipleMatchesError if the string is not unqiue on each and every chain + Will throw MultipleMatchesError if the string is not unique on each and every chain """ result = {} for chain in AddrBook.chain_ids_by_name.keys(): From b216757e225ea7d631b04f03db6813da1f3c4bcb Mon Sep 17 00:00:00 2001 From: Tritium-VLK <11885129+Tritium-VLK@users.noreply.github.com> Date: Sun, 22 Sep 2024 16:14:48 +0200 Subject: [PATCH 5/5] try to make return more clear --- bal_addresses/addresses.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/bal_addresses/addresses.py b/bal_addresses/addresses.py index dc3f0e8..e9079fb 100644 --- a/bal_addresses/addresses.py +++ b/bal_addresses/addresses.py @@ -32,7 +32,7 @@ def get_address_all_chains(search_string: str) -> dict: """ Finds addresses for a unique name across all chains - returns a dict with chain as keys and a dict with "path" and "address" as values + returns a dict with chain as keys and a dict with "path" and "address" Will throw MultipleMatchesError if the string is not unique on each and every chain """ result = {}