async def batch_hard_cancel_orders(self, orders: List[Order]) -> str: order_tuples: List[Tuple] = [convert_order_to_tuple(order) for order in orders] makerAddress = orders[0]['makerAddress'] # Set gas to 1, so it avoids estimateGas call in Web3, which will revert batchHardCancelOrderData = self._exchange_contract.functions.batchCancelOrders( order_tuples ).buildTransaction({'gas': 1}) data = batchHardCancelOrderData['data'] self._current_gas_price = self._wallet.gas_price + 10 transaction = self._generate_signed_zero_ex_transaction(data, makerAddress, self._chain_id) tx_hash = await self._submit_coordinator_transaction( transaction, Web3.toChecksumAddress(makerAddress), transaction['signature'], [], 0 ) return tx_hash
async def batch_fill_orders(self, orders: List[Order], taker_asset_fill_amounts: List[Decimal], signatures: List[str]) -> Tuple[str, Decimal]: order_tuples: List[Tuple] = [ convert_order_to_tuple(order) for order in orders ] signatures: List[bytes] = [ self._w3.toBytes(hexstr=signature) for signature in signatures ] taker_asset_fill_amounts: List[int] = [ int(amount) for amount in taker_asset_fill_amounts ] # Set gas to 1, so it avoids estimateGas call in Web3, which will revert batchFillOrderData = self._exchange_contract.functions.batchFillOrders( order_tuples, taker_asset_fill_amounts, signatures).buildTransaction({'gas': 1}) data = batchFillOrderData['data'] self._current_gas_price = self._wallet.gas_price + 10 tx_hash, protocol_fee = await self._handle_fills( data, self._wallet.address, orders) return tx_hash, protocol_fee
async def soft_cancel_order(self, order: Order) -> bool: order_tuple: Tuple = convert_order_to_tuple(order) # Set gas to 1, so it avoids estimateGas call in Web3, which will revert cancelOrderData = self._exchange_contract.functions.cancelOrder( order_tuple ).buildTransaction({'gas': 1}) data = cancelOrderData['data'] transaction = self._generate_signed_zero_ex_transaction(data, order['makerAddress'], self._chain_id) endpoint = await self._get_server_endpoint_or_throw(order['feeRecipientAddress']) response = await self._execute_server_request(transaction, order['makerAddress'], endpoint) if response['isError']: approvedOrders = [] cancellations = [] errors = { **response, 'orders': [order] } raise ZeroExCoordinatorFailedException( approvedOrders, cancellations, errors, ) else: return True
def fill_order(self, order: Order, taker_asset_fill_amount: Decimal, signature: str) -> Tuple[str, Decimal]: order_tuple: Tuple = convert_order_to_tuple(order) signature: bytes = self._w3.toBytes(hexstr=signature) # Add 10 wei to the standard price to beat the default gas price ppl. gas_price: int = self._wallet.gas_price + 10 protocol_fee = PROTOCOL_FEE_MULTIPLIER * gas_price tx_hash: str = self._wallet.execute_transaction( self._contract.functions.fillOrder(order_tuple, int(taker_asset_fill_amount), signature), gasPrice=gas_price, value=protocol_fee) return tx_hash, Decimal(protocol_fee)
def market_sell_orders(self, orders: List[Order], taker_asset_fill_amount: Decimal, signatures: List[str]) -> Tuple[str, Decimal]: order_tuples: List[Tuple] = [convert_order_to_tuple(order) for order in orders] signatures: List[bytes] = [self._w3.toBytes(hexstr=signature) for signature in signatures] # Add 10 wei to the standard price to beat the default gas price ppl. gas_price: int = self._wallet.gas_price + 10 protocol_fee = PROTOCOL_FEE_MULTIPLIER * len(orders) * gas_price tx_hash: str = self._wallet.execute_transaction( self._contract.functions.marketSellOrdersFillOrKill( order_tuples, int(taker_asset_fill_amount), signatures ), gasPrice=gas_price, value=protocol_fee ) return tx_hash, Decimal(protocol_fee)
async def batch_soft_cancel_orders(self, orders: List[Order]) -> bool: order_tuples: List[Tuple] = [ convert_order_to_tuple(order) for order in orders ] makerAddress = orders[0]['makerAddress'] # Set gas to 1, so it avoids estimateGas call in Web3, which will revert batchCancelOrderData = self._exchange_contract.functions.batchCancelOrders( order_tuples).buildTransaction({'gas': 1}) data = batchCancelOrderData['data'] serverEndpointsToOrders = await self._map_server_endpoints_to_orders( orders) errorResponses: [] = [] successResponses: [] = [] transaction = self._generate_signed_zero_ex_transaction( data, makerAddress, self._chain_id) for endpoint in serverEndpointsToOrders: response = await self._execute_server_request( transaction, makerAddress, endpoint) if response['isError']: errorResponses.append(response) else: successResponses.append(response['body']) if len(errorResponses) == 0: return True else: errorsWithOrders = [] for errorResponse in errorResponses: errorsWithOrders.append({ **errorResponse, 'orders': serverEndpointsToOrders[ errorResponse['coordinatorOperator']] }) approvedOrders = [] cancellations = successResponses raise ZeroExCoordinatorFailedException(approvedOrders, cancellations, errorsWithOrders)
async def fill_order(self, order: Order, taker_asset_fill_amount: Decimal, signature: str) -> Tuple[str, Decimal]: order_tuple: Tuple = convert_order_to_tuple(order) signature: bytes = self._w3.toBytes(hexstr=signature) # Set gas to 1, so it avoids estimateGas call in Web3, which will revert fillOrderData = self._exchange_contract.functions.fillOrder( order_tuple, int(taker_asset_fill_amount), signature ).buildTransaction({'gas': 1}) data = fillOrderData['data'] self._current_gas_price = self._wallet.gas_price + 10 tx_hash, protocol_fee = await self._handle_fills(data, self._wallet.address, [order]) return tx_hash, protocol_fee
async def hard_cancel_order(self, order: List[Order]) -> str: order_tuple: Tuple = convert_order_to_tuple(order) # Set gas to 1, so it avoids estimateGas call in Web3, which will revert hardCancelOrderData = self._exchange_contract.functions.cancelOrder( order_tuple).buildTransaction({'gas': 1}) data = hardCancelOrderData['data'] self._current_gas_price = self._wallet.gas_price + 10 transaction = self._generate_signed_zero_ex_transaction( data, order['makerAddress'], self._chain_id) tx_hash = await self._submit_coordinator_transaction( transaction, Web3.toChecksumAddress(order['makerAddress']), transaction['signature'], [], 0) return tx_hash
def cancel_order(self, order: Order) -> str: order_tuple = convert_order_to_tuple(order) tx_hash: str = self._wallet.execute_transaction(self._contract.functions.cancelOrder(order_tuple)) return tx_hash