Skip to content

Commit

Permalink
Lazily cache LedgerContract in auction providers.
Browse files Browse the repository at this point in the history
  • Loading branch information
dowlandaiello committed Apr 12, 2024
1 parent 27786a5 commit c4a00f3
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 12 deletions.
30 changes: 23 additions & 7 deletions src/contracts/auction.py
Original file line number Diff line number Diff line change
@@ -1,19 +1,35 @@
from src.util import NEUTRON_NETWORK_CONFIG
from cosmpy.aerial.contract import LedgerContract
from cosmpy.aerial.client import LedgerClient
from cosmpy.aerial.contract import LedgerContract # type: ignore
from cosmpy.aerial.client import LedgerClient # type: ignore
from typing import Any
from functools import cached_property


class AuctionProvider:
"""
Provides pricing and asset information for an arbitrary auction on valenece.
"""

def __init__(self, contract: LedgerContract, asset_a: str, asset_b: str):
self.contract = contract
def __init__(
self,
deployment_info: dict[str, Any],
client: LedgerClient,
address: str,
asset_a: str,
asset_b: str,
):
self.deployment_info = deployment_info
self.client = client
self.address = address
self.asset_a_denom = asset_a
self.asset_b_denom = asset_b

@cached_property
def contract(self) -> LedgerContract:
return LedgerContract(
self.deployment_info["auction"]["src"], self.client, address=self.address
)

def exchange_rate(self) -> float:
auction_info = self.contract.query("get_auction")

Expand Down Expand Up @@ -64,9 +80,9 @@ def auctions(self) -> dict[str, dict[str, AuctionProvider]]:
asset_a, asset_b = pair

provider = AuctionProvider(
LedgerContract(
self.deployment_info["auction"]["src"], self.client, address=addr
),
self.deployment_info,
self.client,
addr,
asset_a,
asset_b,
)
Expand Down
8 changes: 4 additions & 4 deletions src/contracts/pool/astroport.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
from src.contracts.pool.provider import PoolProvider
from src.util import NEUTRON_NETWORK_CONFIG
from cosmpy.aerial.contract import LedgerContract
from cosmpy.aerial.client import LedgerClient
from typing import Any, cast, Callable
from cosmpy.aerial.contract import LedgerContract # type: ignore
from cosmpy.aerial.client import LedgerClient # type: ignore
from typing import Any, cast, Callable, Union, List
from functools import cached_property


Expand Down Expand Up @@ -130,7 +130,7 @@ def pools(self) -> dict[str, dict[str, AstroportPoolProvider]]:

# Load all pools in 10-pool batches
pools = []
prev_pool_page = None
prev_pool_page: Union[None, List[dict[str, Any]]] = None

while prev_pool_page is None or len(prev_pool_page) > 0:
next_pools = self.directory_contract.query(
Expand Down
2 changes: 1 addition & 1 deletion src/util.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from cosmpy.aerial.client import NetworkConfig
from cosmpy.aerial.client import NetworkConfig # type: ignore
import json
from typing import Any
import sys
Expand Down

0 comments on commit c4a00f3

Please sign in to comment.