Skip to content

Commit

Permalink
Union Table for Bridge Operations added
Browse files Browse the repository at this point in the history
  • Loading branch information
igorsereda committed Feb 15, 2024
1 parent c23f985 commit ddd19d1
Show file tree
Hide file tree
Showing 7 changed files with 128 additions and 68 deletions.
65 changes: 49 additions & 16 deletions bridge_indexer/handlers/bridge_matcher.py
Original file line number Diff line number Diff line change
@@ -1,32 +1,48 @@
from datetime import timedelta

from bridge_indexer.models import BridgeDepositTransaction
from bridge_indexer.models import BridgeWithdrawTransaction
from bridge_indexer.models import EtherlinkDepositEvent
from bridge_indexer.models import EtherlinkWithdrawEvent
from bridge_indexer.models import TezosDepositEvent
from bridge_indexer.models import TezosWithdrawEvent
from bridge_indexer.models import BridgeDepositOperation
from bridge_indexer.models import BridgeOperation
from bridge_indexer.models import BridgeOperationType
from bridge_indexer.models import BridgeWithdrawOperation
from bridge_indexer.models import EtherlinkDepositOperation
from bridge_indexer.models import EtherlinkWithdrawOperation
from bridge_indexer.models import TezosDepositOperation
from bridge_indexer.models import TezosWithdrawOperation

LAYERS_TIMESTAMP_GAP = timedelta(minutes=5)


class BridgeMatcher:
@staticmethod
async def check_pending_tezos_deposits():
qs = TezosDepositEvent.filter(bridge_deposits__isnull=True)
qs = TezosDepositOperation.filter(bridge_deposits__isnull=True)
async for l1_deposit in qs:
await BridgeDepositTransaction.create(l1_transaction=l1_deposit)
bridge_deposit = await BridgeDepositOperation.create(l1_transaction=l1_deposit)
await BridgeOperation.create(
id=bridge_deposit.id,
type=BridgeOperationType.deposit,
l1_account=l1_deposit.l1_account,
l2_account=l1_deposit.l2_account,
)

@staticmethod
async def check_pending_etherlink_withdrawals():
qs = EtherlinkWithdrawEvent.filter(bridge_withdrawals__isnull=True)
qs = EtherlinkWithdrawOperation.filter(bridge_withdrawals__isnull=True)
async for l2_withdrawal in qs:
await BridgeWithdrawTransaction.create(l2_transaction=l2_withdrawal)
bridge_withdrawal = await BridgeWithdrawOperation.create(l2_transaction=l2_withdrawal)
await BridgeOperation.create(
id=bridge_withdrawal.id,
type=BridgeOperationType.withdrawal,
l1_account=l2_withdrawal.l1_account,
l2_account=l2_withdrawal.l2_account,
)

@staticmethod
async def check_pending_etherlink_deposits():
qs = EtherlinkDepositEvent.filter(bridge_deposits__isnull=True).order_by('level', 'transaction_index')
qs = EtherlinkDepositOperation.filter(bridge_deposits__isnull=True).order_by('level', 'transaction_index')
async for l2_deposit in qs:
bridge_deposit = (
await BridgeDepositTransaction.filter(
await BridgeDepositOperation.filter(
l2_transaction=None,
l1_transaction__inbox_message_id=l2_deposit.inbox_message_id,
)
Expand All @@ -39,15 +55,22 @@ async def check_pending_etherlink_deposits():
bridge_deposit.l2_transaction = l2_deposit
await bridge_deposit.save()

qs = EtherlinkDepositEvent.filter(
bridge_operation = await BridgeOperation.get(id=bridge_deposit.id)
bridge_operation.is_completed = True
bridge_operation.is_successful = l2_deposit.l2_token is not None
await bridge_operation.save()

@staticmethod
async def check_pending_etherlink_xtz_deposits():
qs = EtherlinkDepositOperation.filter(
bridge_deposits__isnull=True,
inbox_message_id__isnull=True,
l2_token_id='xtz',
).order_by('level', 'transaction_index')
async for l2_deposit in qs:
await l2_deposit.fetch_related('l2_token', 'l2_token__ticket')
bridge_deposit = (
await BridgeDepositTransaction.filter(
await BridgeDepositOperation.filter(
l2_transaction=None,
l1_transaction__inbox_message_id__gt=0,
l1_transaction__ticket=l2_deposit.l2_token.ticket,
Expand All @@ -69,11 +92,16 @@ async def check_pending_etherlink_deposits():
bridge_deposit.l2_transaction = l2_deposit
await bridge_deposit.save()

bridge_operation = await BridgeOperation.get(id=bridge_deposit.id)
bridge_operation.is_completed = True
bridge_operation.is_successful = l2_deposit.l2_token is not None
await bridge_operation.save()

@staticmethod
async def check_pending_tezos_withdrawals():
qs = TezosWithdrawEvent.filter(bridge_withdrawals__isnull=True).order_by('level')
qs = TezosWithdrawOperation.filter(bridge_withdrawals__isnull=True).order_by('level')
async for l1_withdrawal in qs:
bridge_withdrawal = await BridgeWithdrawTransaction.filter(
bridge_withdrawal = await BridgeWithdrawOperation.filter(
l1_transaction=None,
l2_transaction__outbox_message_id=l1_withdrawal.outbox_message_id,
).first()
Expand All @@ -84,6 +112,11 @@ async def check_pending_tezos_withdrawals():
bridge_withdrawal.l1_transaction = l1_withdrawal
await bridge_withdrawal.save()

bridge_operation = await BridgeOperation.get(id=bridge_withdrawal.id)
bridge_operation.is_completed = True
bridge_operation.is_successful = True
await bridge_operation.save()

@staticmethod
async def check_pending_transactions():
await BridgeMatcher.check_pending_tezos_deposits()
Expand Down
4 changes: 2 additions & 2 deletions bridge_indexer/handlers/etherlink/on_deposit.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

from bridge_indexer.handlers.bridge_matcher import BridgeMatcher
from bridge_indexer.handlers.rollup_message import InboxMessageService
from bridge_indexer.models import EtherlinkDepositEvent
from bridge_indexer.models import EtherlinkDepositOperation
from bridge_indexer.models import EtherlinkToken
from bridge_indexer.models import TezosTicket
from bridge_indexer.types.kernel.evm_events.deposit import Deposit
Expand Down Expand Up @@ -44,7 +44,7 @@ async def on_deposit(

inbox_message = await InboxMessageService.find_by_index(event.payload.inbox_level, event.payload.inbox_msg_id, ctx)

await EtherlinkDepositEvent.create(
await EtherlinkDepositOperation.create(
timestamp=datetime.fromtimestamp(event.data.timestamp, tz=timezone.utc),
level=event.data.level,
address=event.data.address[-40:],
Expand Down
12 changes: 6 additions & 6 deletions bridge_indexer/handlers/etherlink/on_withdraw.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
from dipdup.context import HandlerContext
from dipdup.models import Index
from dipdup.models import IndexStatus
from dipdup.models.evm_subsquid import SubsquidEvent
from tortoise.exceptions import DoesNotExist

from bridge_indexer.handlers.bridge_matcher import BridgeMatcher
from bridge_indexer.handlers.rollup_message import OutboxMessageService
from bridge_indexer.models import EtherlinkToken
from bridge_indexer.models import EtherlinkWithdrawEvent
from bridge_indexer.models import EtherlinkWithdrawOperation
from bridge_indexer.types.kernel.evm_events.withdrawal import Withdrawal
from dipdup.context import HandlerContext
from dipdup.models import Index
from dipdup.models import IndexStatus
from dipdup.models.evm_subsquid import SubsquidEvent


async def on_withdraw(
Expand All @@ -33,7 +33,7 @@ async def on_withdraw(
)
return

await EtherlinkWithdrawEvent.create(
await EtherlinkWithdrawOperation.create(
timestamp=event.data.timestamp,
level=event.data.level,
address=event.data.address[-40:],
Expand Down
6 changes: 3 additions & 3 deletions bridge_indexer/handlers/etherlink/on_xtz_deposit.py
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
from datetime import datetime
from datetime import timezone

from bridge_indexer.models import TezosTicket
from dipdup.context import HandlerContext
from dipdup.models import Index
from dipdup.models import IndexStatus
from dipdup.models.evm_node import EvmNodeTransactionData
from dipdup.models.evm_subsquid import SubsquidTransactionData

from bridge_indexer.handlers.bridge_matcher import BridgeMatcher
from bridge_indexer.models import EtherlinkDepositEvent
from bridge_indexer.models import EtherlinkDepositOperation
from bridge_indexer.models import EtherlinkToken
from bridge_indexer.models import TezosTicket


async def on_xtz_deposit(
Expand All @@ -31,7 +31,7 @@ async def on_xtz_deposit(
etherlink_token = await EtherlinkToken.get(id='xtz')
tezos_ticket = await TezosTicket.get(token_id='xtz')

await EtherlinkDepositEvent.create(
await EtherlinkDepositOperation.create(
timestamp=datetime.fromtimestamp(transaction.timestamp, tz=timezone.utc),
level=transaction.level,
address=transaction.from_[-40:],
Expand Down
4 changes: 2 additions & 2 deletions bridge_indexer/handlers/tezos/on_rollup_call.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@

from bridge_indexer.handlers.bridge_matcher import BridgeMatcher
from bridge_indexer.handlers.rollup_message import InboxMessageService
from bridge_indexer.models import TezosDepositEvent
from bridge_indexer.models import TezosDepositOperation
from bridge_indexer.models import TezosTicket
from bridge_indexer.models import TezosToken
from bridge_indexer.types.rollup.tezos_parameters.default import LL
Expand Down Expand Up @@ -116,7 +116,7 @@ async def on_rollup_call(

inbox_message = await InboxMessageService.match_transaction_with_inbox(default.data, ctx)

await TezosDepositEvent.create(
await TezosDepositOperation.create(
timestamp=default.data.timestamp,
level=default.data.level,
operation_hash=default.data.hash,
Expand Down
4 changes: 2 additions & 2 deletions bridge_indexer/handlers/tezos/on_rollup_execute.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

from bridge_indexer.handlers.bridge_matcher import BridgeMatcher
from bridge_indexer.handlers.rollup_message import OutboxMessageService
from bridge_indexer.models import TezosWithdrawEvent
from bridge_indexer.models import TezosWithdrawOperation
from bridge_indexer.types.output_proof.output_proof import OutputProofData
from bridge_indexer.types.ticketer.tezos_parameters.withdraw import WithdrawParameter
from bridge_indexer.types.ticketer.tezos_storage import TicketerStorage
Expand Down Expand Up @@ -51,7 +51,7 @@ async def on_rollup_execute(
)
return

await TezosWithdrawEvent.create(
await TezosWithdrawOperation.create(
timestamp=execute.data.timestamp,
level=execute.data.level,
operation_hash=execute.data.hash,
Expand Down
Loading

0 comments on commit ddd19d1

Please sign in to comment.