def place_order_function(new_order: NewOrder): assert (isinstance(new_order, NewOrder)) if new_order.is_sell: pay_token = self.token_sell().address buy_token = self.token_buy().address else: pay_token = self.token_buy().address buy_token = self.token_sell().address transact = self.otc.make(pay_token=pay_token, pay_amount=new_order.pay_amount, buy_token=buy_token, buy_amount=new_order.buy_amount).transact( gas_price=self.gas_price) if transact is not None and transact.successful and transact.result is not None: return Order(market=self.otc, order_id=transact.result, maker=self.our_address, pay_token=pay_token, pay_amount=new_order.pay_amount, buy_token=buy_token, buy_amount=new_order.buy_amount, timestamp=0) else: return None
def apply_take(self, order_book: List[Order], log_take: LogTake): this_order = next( filter(lambda order: order.order_id == log_take.order_id, order_book), None) if this_order is not None: assert this_order.pay_token == log_take.pay_token assert this_order.buy_token == log_take.buy_token remaining_orders = list( filter(lambda order: order.order_id != log_take.order_id, order_book)) this_order = Order( self.otc, order_id=this_order.order_id, pay_amount=this_order.pay_amount - log_take.take_amount, pay_token=this_order.pay_token, buy_amount=this_order.buy_amount - log_take.give_amount, buy_token=this_order.buy_token, maker=this_order.maker, timestamp=this_order.timestamp) if this_order.pay_amount > Wad(0) and this_order.buy_amount > Wad( 0): return remaining_orders + [this_order] else: return remaining_orders else: return order_book
def apply_make(self, order_book: List[Order], log_make: LogMake) -> List[Order]: return order_book + [Order(self.otc, order_id=log_make.order_id, pay_amount=log_make.pay_amount, pay_token=log_make.pay_token, buy_amount=log_make.buy_amount, buy_token=log_make.buy_token, maker=log_make.maker, timestamp=log_make.timestamp)]
def place_order_function(self, new_order: NewOrder): assert(isinstance(new_order, NewOrder)) if new_order.is_sell: buy_or_sell = "SELL" pay_token = self.token_sell.address buy_token = self.token_buy.address new_order.buy_amount = self.buy_token.unnormalize_amount(new_order.buy_amount) b_token = self.buy_token p_token = self.sell_token new_order.pay_amount = self.sell_token.unnormalize_amount(new_order.pay_amount) token_name = self.sell_token.name quote_token = self.buy_token.name else: buy_or_sell = "BUY" pay_token = self.token_buy.address buy_token = self.token_sell.address new_order.pay_amount = self.buy_token.unnormalize_amount(new_order.pay_amount) p_token = self.buy_token b_token = self.sell_token new_order.buy_amount = self.sell_token.unnormalize_amount(new_order.buy_amount) token_name = self.sell_token.name quote_token = self.buy_token.name transact = self.otc.make(p_token=p_token, pay_amount=new_order.pay_amount, b_token=b_token, buy_amount=new_order.buy_amount).transact(gas_price=self.gas_price) if new_order.is_sell: new_order.buy_amount = self.buy_token.normalize_amount(new_order.buy_amount) new_order.pay_amount = self.sell_token.normalize_amount(new_order.pay_amount) buy_or_sell_price = new_order.buy_amount/new_order.pay_amount amount = new_order.pay_amount else: new_order.pay_amount = self.buy_token.normalize_amount(new_order.pay_amount) new_order.buy_amount = self.sell_token.normalize_amount(new_order.buy_amount) buy_or_sell_price = new_order.pay_amount/new_order.buy_amount amount = new_order.buy_amount if transact is not None and transact.successful and transact.result is not None: self.logger.info(f'Placing {buy_or_sell} order of amount {amount} {token_name} @ price {buy_or_sell_price} {quote_token}') self.logger.info(f'Placing {buy_or_sell} order pay token: {p_token.name} with amount: {new_order.pay_amount}, buy token: {b_token.name} with amount: {new_order.buy_amount}') return Order(market=self.otc, order_id=transact.result, maker=self.our_address, pay_token=pay_token, pay_amount=new_order.pay_amount, buy_token=buy_token, buy_amount=new_order.buy_amount, timestamp=0) else: return None