diff --git a/src/dexorder/accounting.py b/src/dexorder/accounting.py index 14a39d6..9e2f66a 100644 --- a/src/dexorder/accounting.py +++ b/src/dexorder/accounting.py @@ -189,19 +189,25 @@ async def accounting_placement(order_placed: EventData): AccountingSubcategory.GasFee, NATIVE_TOKEN, gas_fee) -async def accounting_fill(fill: EventData, out_token: str): +async def accounting_fill(fill: EventData, out_token: str) -> dec: + """ + Returns the mark-to-market USD value of the transaction. + """ block_hash = hexstr(fill['blockHash']) tx_id = hexstr(fill['transactionHash']) fee = int(fill['args']['fillFee']) fm = await FeeManager.get() - await add_accounting_row(fm.fill_fee_account_addr, block_hash, tx_id, AccountingCategory.Income, + return await add_accounting_row(fm.fill_fee_account_addr, block_hash, tx_id, AccountingCategory.Income, AccountingSubcategory.FillFee, out_token, fee) async def add_accounting_row(account: str, block_hash: Optional[str], tx_id: Optional[str], category, subcategory, token, amount, note=None, - *, adjust_decimals=True): + *, adjust_decimals=True) -> dec: + """ + Returns the mark-to-market USD value of the transaction. + """ if amount == 0: - return + return dec(0) if adjust_decimals: amount = await adj_dec(token, amount) # noinspection PyTypeChecker @@ -221,10 +227,10 @@ async def add_accounting_row(account: str, block_hash: Optional[str], tx_id: Opt if new_amount < 0: log.error(f'negative balance for account {account} when applying accounting row {time} {category} {subcategory} {token} {amount} ${value}') account_db.balances[token] = new_amount - metric.account_balance.labels(address=account, token=token).set(new_amount) db.session.add(account_db) # deep changes would not be detected by the ORM else: log.warning(f'No db account found for {account}') + return value async def adjust_balance(account: DbAccount, token=NATIVE_TOKEN, subcategory=AccountingSubcategory.InitialBalance, note=None): true_balance = await get_balance(account.address, token) diff --git a/src/dexorder/event_handler.py b/src/dexorder/event_handler.py index d1a9e3a..d62cbfa 100644 --- a/src/dexorder/event_handler.py +++ b/src/dexorder/event_handler.py @@ -44,11 +44,12 @@ async def handle_order_placed(event: EventData): except KeyError: log.warning(f'Rogue DexorderSwapPlaced in tx {hexstr(event["transactionHash"])}') return - await accounting_placement(event) log.debug(f'DexorderSwapPlaced {addr} {start_index} {num_orders}') if not await verify_vault(addr): log.warning(f'Discarding order from rogue vault {addr}.') return + await accounting_placement(event) + metric.orders.inc() contract = None for index in range(start_index, start_index+num_orders): key = OrderKey(addr, index) @@ -82,7 +83,8 @@ async def handle_swap_filled(event: EventData): except KeyError: log.warning(f'DexorderSwapFilled IGNORED due to missing order {vault} {order_index}') return - await accounting_fill(event, order.order.tokenOut) + value = await accounting_fill(event, order.order.tokenOut) + metric.volume.inc(value) order.status.trancheStatus[tranche_index].activationTime = next_execution_time # update rate limit try: triggers = OrderTriggers.instances[order.key] diff --git a/src/dexorder/metric/metrics.py b/src/dexorder/metric/metrics.py index 51547aa..96714d6 100644 --- a/src/dexorder/metric/metrics.py +++ b/src/dexorder/metric/metrics.py @@ -11,10 +11,13 @@ runner_loops = Counter("runner_loops", "Number of times the runner loop has been runner_latency = Summary("runner_latency", "How old the current block being processed is, in seconds") vaults = Gauge("vaults", "Total vault count", ) +orders = Counter("orders", "Orders placed", ) open_orders = Gauge("open_orders", "Total active orders", ) triggers_time = Gauge("triggers_time", "Total active time triggers", ) triggers_line = Gauge("triggers_line", "Total active line triggers", ) +executions = Counter("executions", "Total executions attempted") +executions_failed = Counter("executions_failed", "Number of failed execution attempts") +volume = Counter("volume", "Total volume of successful executions in USD") -account_balance = Gauge("account_balance", "Account balance", ["address", "token"]) account_total = Gauge('account_total', 'Total number of accounts configured') account_available = Gauge('account_available', 'Number of accounts that do not have any pending transactions') diff --git a/src/dexorder/order/executionhandler.py b/src/dexorder/order/executionhandler.py index 171b483..190c4ae 100644 --- a/src/dexorder/order/executionhandler.py +++ b/src/dexorder/order/executionhandler.py @@ -6,7 +6,7 @@ from uuid import UUID from web3.exceptions import ContractPanicError, ContractLogicError from web3.types import EventData -from dexorder import db +from dexorder import db, metric from dexorder.accounting import accounting_transaction_gas from dexorder.base import TransactionReceiptDict, TransactionRequest, transaction_request_deserializers from dexorder.base.order import TrancheKey, OrderKey @@ -125,6 +125,11 @@ def finish_execution_request(tk: TrancheKey, error: Optional[str]=None): if trig is not None: trig.touch() + if error is None: + metric.executions.inc() + else: + metric.executions_failed.inc() + # # execute() error handling #