def all_pairs(self) -> List[Pair]: cf = ContractFactory(self.w3, Pair) pairs = [] for i in range(0, self.all_pairs_length()): address = self.contract.functions.allPairs(i).call() pairs.append(cf.load_contract(address=Address(address))) return pairs
async def get_tokens(self) -> List[GenericToken]: token_addresses = await self._call_async( self.contract.functions.getCurrentTokens()) cf = ContractFactory(self.w3, GenericToken) return list( map( (lambda a: cf.load_contract(owner_address=self.owner_address, address=a)), token_addresses, ))
class UniswapFactory(Contract): name = "UniswapV2Factory" protocol = "uniswap" def __init__(self, w3, contract, timeout, **kwargs): self.cf = ContractFactory(w3, UniswapPair) super().__init__(w3, contract, timeout, **kwargs) async def all_pairs_length(self) -> int: return await self._call_async(self.contract.functions.allPairsLength()) async def get_pair_address(self, index) -> str: return await self._call_async(self.contract.functions.allPairs(index)) async def all_pairs(self, sleep=0) -> List[UniswapPair]: number_of_pairs = await self.all_pairs_length() return await async_map(self._create_pair_index, range(number_of_pairs)) async def create_pair( self, token_a: GenericToken, token_b: GenericToken ) -> UniswapPair: transaction = self.contract.functions.createPair( token_a.get_address(), token_b.get_address() ) if not self._transact_info(transaction): raise DeployContractError("Failed to deploy UniswapPair") pair_nmb = await self.all_pairs_length() return await self._create_pair_index(pair_nmb - 1) async def setup_pair( self, tokens: List[GenericToken], amounts: List[BigNumber] ) -> UniswapPair: pair = await self.create_pair(tokens[0], tokens[1]) for token, amount in zip(tokens, amounts): token.transfer(pair.get_address(), amount) try: pair.mint(self.owner_address) except Exception: raise ValueError(f"Failed to mint tokens {tokens[0]},{tokens[1]}") return pair @time(CREATE_PAIR) async def _create_pair_index(self, index) -> UniswapPair: address = await self.get_pair_address(index) logger.info(f"Creating pair number {index}") return await run_async(self._load_pair, address) def _load_pair(self, address): return self.cf.load_contract(self.owner_address, address=address)
class BalancerFactory(Contract): name = "BFactory" protocol = "balancer" def __init__(self, w3, contract, **kwargs): self.cf = ContractFactory(w3, BalancerPool) super().__init__(w3, contract, **kwargs) async def setup_pool( self, tokens: List[GenericToken], weights: List[float], amounts: List[BigNumber], approve_owner=True, ) -> BalancerPool: pool = self.new_pool() for token, weight, amount in zip(tokens, weights, amounts): if approve_owner: await token.approve_owner() token.approve(pool.get_address(), amount) pool.bind(token.get_address(), amount, weight) pool.finalize() return pool def new_pool(self) -> BalancerPool: transaction = self.contract.functions.newBPool() status, address = self._transact_status_and_contract(transaction) if not status: raise DeployContractError("Failed to deploy BalancerPool.") return self.cf.load_contract(self.owner_address, address=address) async def all_pools(self, start=0, steps=100) -> List[BalancerPool]: last_block = self.w3.eth.blockNumber bf = EventFilter( self.contract.events.LOG_NEW_POOL, self._create_pools, start, last_block, steps, ) return await bf.find_events() def _create_pools(self, new_pool_events): loader = BalancerLoader(self.cf, self.owner_address) return loader.load_from_event(new_pool_events)
def __init__(self, w3, contract, timeout, **kwargs): self.cf = ContractFactory(w3, UniswapPair) super().__init__(w3, contract, timeout, **kwargs)
async def _get_token(self, function) -> GenericToken: token_address = await self._call_async(function) cf = ContractFactory(self.w3, GenericToken) return cf.load_contract(self.owner_address, address=token_address)
def __init__(self, w3, contract, **kwargs): self.cf = ContractFactory(w3, BalancerPool) super().__init__(w3, contract, **kwargs)
def token_factory(w3) -> ContractFactory: return ContractFactory(w3, GenericToken)
def factory(deploy_address, w3) -> Factory: return ContractFactory(w3, Factory).deploy_contract(deploy_address, deploy_address.value)