Skip to content

Commit

Permalink
Merge pull request #413 from Birdmannn/feat/SmartContract]-Fetch-port…
Browse files Browse the repository at this point in the history
…folio-from-our-smart-contract

feat: added fetch_portfolio
  • Loading branch information
djeck1432 authored Dec 20, 2024
2 parents a1eebe4 + a3ecb2b commit 3e868e6
Showing 1 changed file with 35 additions and 6 deletions.
41 changes: 35 additions & 6 deletions web_app/contract_tools/blockchain_call.py
Original file line number Diff line number Diff line change
Expand Up @@ -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__)


Expand Down Expand Up @@ -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.
"""
Expand All @@ -368,19 +367,21 @@ 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)}")
results[token_symbol] = f"Failed: Invalid address format"
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:
Expand All @@ -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)

0 comments on commit 3e868e6

Please sign in to comment.