async def record_trade(contract, event_name, event): block_number = coerce_to_int(event["blockNumber"]) log_index = coerce_to_int(event["logIndex"]) date = datetime.fromtimestamp(block_timestamp(App().web3, event["blockNumber"]), tz=None) insert_args = (block_number, Web3.toBytes(hexstr=event["transactionHash"]), log_index, Web3.toBytes(hexstr=event["args"]["tokenGive"]), event["args"]["amountGive"], Web3.toBytes(hexstr=event["args"]["tokenGet"]), event["args"]["amountGet"], Web3.toBytes(hexstr=event["args"]["give"]), Web3.toBytes(hexstr=event["args"]["get"]), date) async with App().db.acquire_connection() as connection: insert_retval = await connection.execute(INSERT_TRADE_STMT, *insert_args) _, _, did_insert = parse_insert_status(insert_retval) if did_insert: logger.debug("recorded trade txid=%s, logidx=%i", event["transactionHash"], log_index) return bool(did_insert)
async def record_cancel(contract, event_name, event): order = event["args"] order_maker = order["user"] signature = make_order_hash(order) date = datetime.fromtimestamp(block_timestamp(App().web3, event["blockNumber"]), tz=None) if "r" in order and order["r"] is not None: source = OrderSource.OFFCHAIN else: source = OrderSource.ONCHAIN upsert_args = ( source.name, Web3.toBytes(hexstr=signature), Web3.toBytes(hexstr=order["tokenGive"]), order["amountGive"], Web3.toBytes(hexstr=order["tokenGet"]), order["amountGet"], order["expires"], order["nonce"], Web3.toBytes(hexstr=order["user"]), OrderState.CANCELED.name, date, order["amountGet"], # Contract updates orderFills to amountGet when trade is cancelled date, 0 # Cancelled = 0 volume available ) async with App().db.acquire_connection() as connection: upsert_retval = await connection.execute(UPSERT_CANCELED_ORDER_STMT, *upsert_args) _, _, did_upsert = parse_insert_status(upsert_retval) if did_upsert: logger.debug("recorded order cancel signature=%s", signature) return bool(did_upsert)
async def record_order(order, block_number=0): signature = make_order_hash(order) if "r" in order and order["r"] is not None: source = OrderSource.OFFCHAIN date = datetime.utcnow() else: source = OrderSource.ONCHAIN date = datetime.fromtimestamp(block_timestamp(App().web3, block_number), tz=None) # if tokenGive is ZERO_ADDR, sort by (amount_give / amount_get) DESC # => -(amount_give / amount_get) ASC # if tokenGet is ZERO_ADDR, sort by (amount_get / amount_give) ASC if order["tokenGive"] == ZERO_ADDR: with localcontext() as decimal_ctx: decimal_ctx.prec = 10 sorting_price = -order["amountGive"] / order["amountGet"] else: with localcontext() as decimal_ctx: decimal_ctx.prec = 10 sorting_price = order["amountGet"] / order["amountGive"] insert_args = ( source.name, Web3.toBytes(hexstr=signature), Web3.toBytes(hexstr=order["tokenGive"]), order["amountGive"], Web3.toBytes(hexstr=order["tokenGet"]), order["amountGet"], order["expires"], order["nonce"], Web3.toBytes(hexstr=order["user"]), OrderState.OPEN.name, order.get("v"), order.get("r"), order.get("s"), date, sorting_price, ) async with App().db.acquire_connection() as connection: insert_retval = await connection.execute(INSERT_ORDER_STMT, *insert_args) _, _, did_insert = parse_insert_status(insert_retval) return did_insert
async def record_transfer(transfer_direction, event): block_number = coerce_to_int(event["blockNumber"]) log_index = coerce_to_int(event["logIndex"]) date = datetime.fromtimestamp(block_timestamp(App().web3, block_number), tz=None) insert_args = (block_number, Web3.toBytes(hexstr=event["transactionHash"]), log_index, transfer_direction, Web3.toBytes(hexstr=event["args"]["token"]), Web3.toBytes(hexstr=event["args"]["user"]), event["args"]["amount"], event["args"]["balance"], date) async with App().db.acquire_connection() as connection: insert_retval = await connection.execute(INSERT_TRANSFER_STMT, *insert_args) _, _, did_insert = parse_insert_status(insert_retval) return bool(did_insert)
async def record_order(order, block_number=0): signature = make_order_hash(order) if "r" in order and order["r"] is not None: source = OrderSource.OFFCHAIN date = datetime.utcnow() else: source = OrderSource.ONCHAIN date = datetime.fromtimestamp( block_timestamp(App().web3, block_number), tz=None) insert_args = (source.name, Web3.toBytes(hexstr=signature), Web3.toBytes(hexstr=order["tokenGive"]), order["amountGive"], Web3.toBytes(hexstr=order["tokenGet"]), order["amountGet"], order["expires"], order["nonce"], Web3.toBytes(hexstr=order["user"]), OrderState.OPEN.name, order.get("v"), order.get("r"), order.get("s"), date) async with App().db.acquire_connection() as connection: insert_retval = await connection.execute(INSERT_ORDER_STMT, *insert_args) _, _, did_insert = parse_insert_status(insert_retval) return did_insert