Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: added Uniswap v2 Factory contract (and sketch of Pool) #4

Open
wants to merge 11 commits into
base: main
Choose a base branch
from
1 change: 1 addition & 0 deletions uniswap_sdk/v2.json
johnson2427 marked this conversation as resolved.
Show resolved Hide resolved

Large diffs are not rendered by default.

32 changes: 32 additions & 0 deletions uniswap_sdk/v2.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
from typing import Iterator

from ape.contracts import ContractInstance
from ape.types import AddressType
from ape.utils import ManagerAccessMixin

from .packages import V2, get_contract_instance


class Factory(ManagerAccessMixin):
@property
def contract(self) -> ContractInstance:
return get_contract_instance(V2.UniswapV2Factory, self.provider.chain_id)

def get_pools(self, token: AddressType) -> Iterator["Pool"]:
# TODO: Use query manager to search once topic filtering is available
df = self.contract.PairCreated.query("event_arguments", start_block=-1000)
johnson2427 marked this conversation as resolved.
Show resolved Hide resolved
df_pairs = df.loc[(df["token0"] == token) | (df["token1"] == token)]
yield from map(Pool, df_pairs["pair"])

def get_all_pools(self) -> Iterator["Pool"]:
df = self.contract.PairCreated.query("event_arguments", start_block=-1000)
johnson2427 marked this conversation as resolved.
Show resolved Hide resolved
yield from map(Pool, df["pair"])


class Pool(ManagerAccessMixin):
def __init__(self, address: AddressType):
self.address = address

@property
def contract(self) -> ContractInstance:
return get_contract_instance(V2.UniswapV2Factory, self.provider.chain_id)