def buy_data_tokens(self, pool_address: str, amount: float, max_OCEAN_amount: float, from_wallet: Wallet) -> str: """ Buy data tokens from this pool, paying `max_OCEAN_amount_base` of OCEAN tokens. If total spent <= max_OCEAN_amount_base. - Caller is spending OCEAN tokens, and receiving `amount_base` DataTokens - OCEAN tokens are going into pool, DataTokens are going out of pool The transaction fails if total spent exceeds `max_OCEAN_amount_base`. :param pool_address: str address of pool contract :param amount: int number of data tokens to add to this pool in *base* :param max_OCEAN_amount: :param from_wallet: :return: str transaction id/hash """ ocean_tok = DataToken(self.ocean_address) ocean_tok.approve_tokens(pool_address, max_OCEAN_amount, from_wallet, wait=True) dtoken_address = self.get_token_address(pool_address) pool = BPool(pool_address) return pool.swapExactAmountOut( tokenIn_address=self.ocean_address, # entering pool maxAmountIn_base=to_base_18(max_OCEAN_amount), # "" tokenOut_address=dtoken_address, # leaving pool tokenAmountOut_base=to_base_18(amount), # "" maxPrice_base=2**255, # here we limit by max_num_OCEAN, not price from_wallet=from_wallet, )
def create(self, data_token_address: str, data_token_amount: float, OCEAN_amount: float, from_wallet: Wallet, data_token_weight: float = balancer_constants.INIT_WEIGHT_DT, swap_fee: float = balancer_constants.DEFAULT_SWAP_FEE) -> BPool: """ Create a new pool with bound datatoken and OCEAN token then finalize it. The pool will have publicSwap enabled and swap fee is set to `balancer_constants.DEFAULT_SWAP_FEE`. Balances of both data tokens and OCEAN tokens must be sufficient in the `from_wallet`, otherwise this will fail. :param data_token_address: str address of the DataToken contract :param data_token_amount: float amount of initial liquidity of data tokens :param OCEAN_amount: float amount of initial liquidity of OCEAN tokens :param from_wallet: Wallet instance of pool owner :param data_token_weight: float weight of the data token to be set in the new pool must be >= 1 & <= 9 :param swap_fee: float the fee taken by the pool on each swap transaction :return: BPool instance """ bfactory = BFactory(self.bfactory_address) pool_address = bfactory.newBPool(from_wallet) pool = BPool(pool_address) logger.debug(f'pool created with address {pool_address}.') assert 1 <= data_token_weight <= 9 base_weight = 10.0 - data_token_weight # Must approve datatoken and Ocean tokens to the new pool as spender dt = DataToken(data_token_address) tx_id = dt.approve_tokens(pool_address, data_token_amount, from_wallet, wait=True) if dt.get_tx_receipt(tx_id).status != 1: raise AssertionError( f'Approve datatokens failed, pool was created at {pool_address}' ) ot = DataToken(self.ocean_address) tx_id = ot.approve_tokens(pool_address, OCEAN_amount, from_wallet, wait=True) if ot.get_tx_receipt(tx_id).status != 1: raise AssertionError( f'Approve OCEAN tokens failed, pool was created at {pool_address}' ) tx_id = pool.setup(data_token_address, to_base_18(data_token_amount), to_base_18(data_token_weight), self.ocean_address, to_base_18(OCEAN_amount), to_base_18(base_weight), to_base_18(swap_fee), from_wallet) if pool.get_tx_receipt(tx_id).status != 1: raise AssertionError( f'pool.setup failed: txId={tx_id}, receipt={pool.get_tx_receipt(tx_id)}' ) logger.debug( f'create pool completed: poolAddress={pool_address}, pool setup TxId={tx_id}' ) return pool