def apply_exchange_buy(): order = BuyOrder() bid = order print(eoslib.n2s(bid.buyer.name)) eoslib.requireAuth(bid.buyer.name) assert bid.quantity > 0, "invalid quantity" assert bid.expiration > eoslib.now(), "order expired" print(eoslib.n2s(bid.buyer.name), " created bid for ", order.quantity, " currency at price: ", order.price, "\n") buyer_account = Account(bid.buyer.name) buyer_account.eos_balance -= bid.quantity print('buyer_account:', buyer_account) lowest_ask = Ask.front_by_price() if not lowest_ask: print("\n No asks found, saving buyer account and storing bid\n") assert not order.fill_or_kill, "order not completely filled" bid.store() buyer_account.open_orders += 1 buyer_account.save() return print("ask: ", lowest_ask, "\n") print("bid: ", bid, "\n") seller_account = Account(lowest_ask.seller.name) while lowest_ask.price <= bid.price: print("lowest ask <= bid.price\n", lowest_ask.price, bid.price) match(bid, buyer_account, lowest_ask, seller_account) if lowest_ask.quantity == 0: seller_account.open_orders -= 1 seller_account.save() buyer_account.save() lowest_ask.remove() lowest_ask = Ask.front_by_price() if not lowest_ask: break seller_account = Account(lowest_ask.seller.name) else: break # buyer's bid should be filled print("lowest_ask >= bid.price or buyer's bid has been filled\n") if bid.quantity and not order.fill_or_kill: buyer_account.open_orders += 1 buyer_account.save() print("saving buyer's account\n") if bid.quantity: print(bid.quantity, " eos left over") assert not order.fill_or_kill, "order not completely filled" bid.store() return print("bid filled\n")
def apply_exchange_sell(): order = SellOrder() ask = order requireAuth(ask.seller.name) assert ask.quantity > 0, "invalid quantity" assert ask.expiration > now(), "order expired" print("\n\n", eoslib.n2s(ask.seller.name), " created sell for ", order.quantity, " currency at price: ", order.price, "\n") existing_ask = Ask.load_by_order_id(ask.seller) assert not existing_ask, "order with this id already exists" seller_account = Account(ask.seller.name) seller_account.currency_balance -= ask.quantity highest_bid = Bid.back_by_price() if not highest_bid: assert not order.fill_or_kill, "order not completely filled" print("\n No bids found, saving seller account and storing ask\n") ask.store() seller_account.open_orders += 1 seller_account.save() return print("\n bids found, lets see what matches\n") buyer_account = Account(highest_bid.buyer.name) while highest_bid.price >= ask.price: match(highest_bid, buyer_account, ask, seller_account) if highest_bid.quantity == 0: buyer_account.open_orders -= 1 seller_account.save() buyer_account.save() highest_bid.remove() highest_bid = Bid.back_by_price() if not highest_bid: break buyer_account = Account(highest_bid.buyer.name) else: break # buyer's bid should be filled if ask.quantity and not order.fill_or_kill: seller_account.open_orders += 1 seller_account.save() if ask.quantity: assert not order.fill_or_kill, "order not completely filled" print("saving ask\n") ask.store() return print("ask filled\n")