def handle_event(self, event_name: str, contract_address: str, log_data: dict, log_entry: dict): """Map incoming EVM log to database entry.""" opid = self.get_unique_transaction_id(log_entry) existing_op = self.get_existing_op(opid, CryptoOperationType.deposit) if existing_op: # Already in the database, all we need to do is to call blocknumber updater now return False network = self.dbsession.query(AssetNetwork).get(self.network_id) asset = self.dbsession.query(Asset).filter_by( network=network, external_id=eth_address_to_bin(contract_address)).one() if event_name == "Transfer": to_address = eth_address_to_bin(log_data["to"]) from_address = eth_address_to_bin(log_data["from"]) value = Decimal(log_data["value"]) self.logger.debug("Incoming transfer event %s %s %s", from_address, to_address, value) # Get destination address entry address = self.dbsession.query(CryptoAddress).filter_by( address=to_address).one_or_none() if not address: # Address not in our system return False # Create operation op = CryptoAddressDeposit(network=network) op.opid = opid op.txid = txid_to_bin(log_entry["transactionHash"]) op.external_address = from_address op.block = int(log_entry["blockNumber"], 16) op.required_confirmation_count = self.confirmation_count op.crypto_account = address.get_or_create_account(asset) # Create holding account that keeps the value until we receive N amount of confirmations acc = Account(asset=asset) self.dbsession.add(acc) self.dbsession.flush() acc.do_withdraw_or_deposit( value, "Token {} deposit from {} in tx {}".format( asset.symbol, log_data["from"], log_entry["transactionHash"])) op.holding_account = acc self.dbsession.add(op) self.notify_deposit(op) return True else: # Unmonitored event return False
def handle_event(self, event_name: str, contract_address: str, log_data: dict, log_entry: dict): """Map incoming EVM log to database entry.""" with self._get_tm(): opid = self.get_unique_transaction_id(log_entry) existing_op = self.get_existing_op(opid, CryptoOperationType.deposit) if existing_op: # Already in the database, all we need to do is to call blocknumber updater now return False network = self.dbsession.query(AssetNetwork).get(self.network_id) asset = self.dbsession.query(Asset).filter_by(network=network, external_id=eth_address_to_bin(contract_address)).one() if event_name == "Transfer": to_address = eth_address_to_bin(log_data["to"]) from_address = eth_address_to_bin(log_data["from"]) value = Decimal(log_data["value"]) # Get destination address entry address = self.dbsession.query(CryptoAddress).filter_by(address=to_address).one_or_none() if not address: # Address not in our system return False # Create operation op = CryptoAddressDeposit(network=network) op.opid = opid op.txid = txid_to_bin(log_entry["transactionHash"]) op.external_address = from_address op.block = int(log_entry["blockNumber"], 16) op.required_confirmation_count = self.confirmation_count op.crypto_account = address.get_or_create_account(asset) # Create holding account that keeps the value until we receive N amount of confirmations acc = Account(asset=asset) self.dbsession.add(acc) self.dbsession.flush() acc.do_withdraw_or_deposit(value, "Token {} deposit from {} in tx {}".format(asset.symbol, log_data["from"], log_entry["transactionHash"])) op.holding_account = acc self.dbsession.add(op) self.notify_deposit(op) return True else: # Unmonitored event return False