From cc4efa1079680775ecb60e43b86a8a8c463facbf Mon Sep 17 00:00:00 2001
From: Birdmannn <tortipascal5000@gmail.com>
Date: Thu, 19 Dec 2024 19:43:54 +0100
Subject: [PATCH 1/2] feat: added fetch_portfolio

---
 web_app/contract_tools/blockchain_call.py | 41 +++++++++++++++++++----
 1 file changed, 35 insertions(+), 6 deletions(-)

diff --git a/web_app/contract_tools/blockchain_call.py b/web_app/contract_tools/blockchain_call.py
index b6349d67..965b2932 100644
--- a/web_app/contract_tools/blockchain_call.py
+++ b/web_app/contract_tools/blockchain_call.py
@@ -14,11 +14,10 @@
 import starknet_py.hash.selector
 import starknet_py.net.client_models
 import starknet_py.net.networks
+from constants import MULTIPLIER_POWER, ZKLEND_MARKET_ADDRESS, TokenParams
 from starknet_py.contract import Contract
 from starknet_py.net.full_node_client import FullNodeClient
 
-from .constants import ZKLEND_MARKET_ADDRESS, TokenParams, MULTIPLIER_POWER
-
 logger = logging.getLogger(__name__)
 
 
@@ -357,7 +356,7 @@ async def add_extra_deposit(
     async def withdraw_all(self, contract_address: str) -> dict[str, str]:
         """
         Withdraws all supported tokens from the contract by calling withdraw with amount=0.
-        
+
         :param contract_address: The contract address to withdraw from
         :return: A dictionary summarizing the results for each token.
         """
@@ -368,7 +367,7 @@ async def withdraw_all(self, contract_address: str) -> dict[str, str]:
             token_symbol = token.name
 
             try:
-                token_addr_int = self._convert_address(token.address)  
+                token_addr_int = self._convert_address(token.address)
 
             except ValueError as e:
                 logger.error(f"Invalid address format for {token_symbol}: {str(e)}")
@@ -376,11 +375,13 @@ async def withdraw_all(self, contract_address: str) -> dict[str, str]:
                 continue
 
             try:
-                logger.info(f"Withdrawing {token_symbol} from contract {contract_address}")
+                logger.info(
+                    f"Withdrawing {token_symbol} from contract {contract_address}"
+                )
                 await self._func_call(
                     addr=contract_addr_int,
                     selector="withdraw",
-                    calldata=[token_addr_int, 0] 
+                    calldata=[token_addr_int, 0],
                 )
                 results[token_symbol] = "Success"
             except Exception as e:
@@ -389,5 +390,33 @@ async def withdraw_all(self, contract_address: str) -> dict[str, str]:
 
         return results
 
+    async def fetch_portfolio(self, contract_address: str) -> dict:
+        """
+        Fetches the portfolio of the contract
+
+        :param contract_address: the contract address to fetch the portfolio from.
+        :return: A dictionary containing dictionaries of available tokens in the contract address,
+                and the balance
+        """
+        results = {}
+        z_addresses = await self.get_z_addresses()
+
+        for key, value in z_addresses.items():
+            decimals, z_address = value
+            balance: int = await self.get_balance(z_address, contract_address)
+
+            key = f"z{key}"
+            results[key] = {"balance": balance, "decimals": decimals}
+
+        return results
+
 
 CLIENT = StarknetClient()
+
+if __name__ == "__main__":
+    call = CLIENT
+    spotnet_address = (
+        "0x05685d6b0b493c7c939d65c175305b893870cacad780842c79a611ad9122815f"
+    )
+    res = asyncio.run(call.fetch_portfolio(spotnet_address))
+    print(res)

From a3ecb2b94f5d7e9b6c7d92183404b67df08c5589 Mon Sep 17 00:00:00 2001
From: Birdmannn <tortipascal5000@gmail.com>
Date: Thu, 19 Dec 2024 19:53:37 +0100
Subject: [PATCH 2/2] feat: added fetch_portfolio() in StarknetClient

---
 web_app/contract_tools/blockchain_call.py | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/web_app/contract_tools/blockchain_call.py b/web_app/contract_tools/blockchain_call.py
index 965b2932..eac1a3f7 100644
--- a/web_app/contract_tools/blockchain_call.py
+++ b/web_app/contract_tools/blockchain_call.py
@@ -14,7 +14,7 @@
 import starknet_py.hash.selector
 import starknet_py.net.client_models
 import starknet_py.net.networks
-from constants import MULTIPLIER_POWER, ZKLEND_MARKET_ADDRESS, TokenParams
+from .constants import MULTIPLIER_POWER, ZKLEND_MARKET_ADDRESS, TokenParams
 from starknet_py.contract import Contract
 from starknet_py.net.full_node_client import FullNodeClient