Skip to content

Commit

Permalink
Fix deposits
Browse files Browse the repository at this point in the history
  • Loading branch information
igorsereda committed Aug 5, 2024
1 parent a953b2c commit 0b79e27
Show file tree
Hide file tree
Showing 7 changed files with 92 additions and 199 deletions.
59 changes: 17 additions & 42 deletions bridge_indexer/dipdup.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -79,25 +79,25 @@ indexes:
callback: tezos.on_head

tezos_rollup_cement:
# kind: tezos.operations
# datasources:
# - tzkt
# types:
# - sr_cement
# contracts:
# - tezos_smart_rollup
# handlers:
# - callback: tezos.on_cement_commitment
# pattern:
# - type: sr_cement
# destination: tezos_smart_rollup
#

tezos_deposit_operations:
kind: tezos.operations
datasources:
- tzkt
types:
- sr_cement
contracts:
- tezos_smart_rollup
handlers:
- callback: tezos.on_cement_commitment
pattern:
- type: sr_cement
destination: tezos_smart_rollup

tezos_rollup_operations:
kind: tezos.operations
datasources:
- tzkt
types:
- sr_execute
- transaction
contracts:
- tezos_smart_rollup
Expand All @@ -107,12 +107,8 @@ indexes:
- type: transaction
destination: tezos_smart_rollup
entrypoint: default
- callback: tezos.on_rollup_execute
pattern:
- type: sr_execute
destination: tezos_smart_rollup

etherlink_kernel_events:
etherlink_deposit_events:
kind: evm.events
datasources:
- etherlink_subsquid
Expand All @@ -122,28 +118,7 @@ indexes:
contract: etherlink_rollup_kernel
name: Deposit

- callback: etherlink.on_withdraw
contract: etherlink_rollup_kernel
name: Withdrawal

etherlink_token_events:
kind: evm.events
datasources:
- etherlink_subsquid
- etherlink_node
handlers:
- callback: etherlink.on_transfer
contract: l2_tzbtc_token
name: Transfer
- callback: etherlink.on_transfer
contract: l2_sirs_token
name: Transfer
- callback: etherlink.on_transfer
contract: l2_usdt_token
name: Transfer


etherlink_kernel_transactions:
etherlink_xtz_deposit_transactions:
kind: evm.transactions
datasources:
- etherlink_subsquid
Expand Down
102 changes: 38 additions & 64 deletions bridge_indexer/handlers/bridge_matcher.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ async def check_pending_tezos_deposits(cls):
else:
cls._pending_tezos_deposits = False

qs = TezosDepositOperation.filter(bridge_deposits__isnull=True)
qs = TezosDepositOperation.filter(bridge_deposits=None)
async for l1_deposit in qs:
l1_deposit: TezosDepositOperation
bridge_deposit = await BridgeDepositOperation.create(l1_transaction=l1_deposit)
Expand All @@ -65,80 +65,53 @@ async def check_pending_tezos_deposits(cls):
@classmethod
async def check_pending_inbox(cls):
qs = BridgeDepositOperation.filter(
l1_transaction__parameters_hash__isnull=False,
l2_transaction__isnull=True,
inbox_message__isnull=True,
status=BridgeOperationStatus.created,
).order_by('level', 'transaction_index', 'log_index')
async for l1_deposit in qs:
l1_deposit: TezosDepositOperation
inbox_message = (
await RollupInboxMessage.filter(
parameters_hash=l1_deposit.parameters_hash,
level=l1_deposit.level,
)
.order_by('index')
.first()
)
inbox_message=None,
).order_by(
'l1_transaction__level', 'l1_transaction__counter', 'l1_transaction__nonce',
).prefetch_related('l1_transaction')
async for bridge_deposit in qs:
bridge_deposit: BridgeDepositOperation
inbox_message = await RollupInboxMessage.filter(
parameters_hash=bridge_deposit.l1_transaction.parameters_hash,
level=bridge_deposit.l1_transaction.level,
).order_by('index').first()

if inbox_message:
l1_deposit.bridge_deposits.inbox_message = inbox_message
l1_deposit.parameters_hash = None
bridge_deposit.inbox_message = inbox_message
await bridge_deposit.save()
bridge_deposit.l1_transaction.parameters_hash = None
await bridge_deposit.l1_transaction.save()
inbox_message.parameters_hash = None
await l1_deposit.save()
await inbox_message.save()

@classmethod
async def check_pending_etherlink_withdrawals(cls):
if not cls._pending_etherlink_withdrawals:
return
else:
cls._pending_etherlink_withdrawals = False

qs = EtherlinkWithdrawOperation.filter(bridge_withdrawals__isnull=True)
async for l2_withdrawal in qs:
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,
created_at=l2_withdrawal.timestamp,
updated_at=l2_withdrawal.timestamp,
status=BridgeOperationStatus.created,
)

@classmethod
async def check_pending_etherlink_deposits(cls):
if not cls._pending_etherlink_deposits:
return
else:
cls._pending_etherlink_deposits = False

qs = (
EtherlinkDepositOperation.filter(bridge_deposits__isnull=True)
.prefetch_related('l2_token')
.order_by('level', 'transaction_index')
)
qs = EtherlinkDepositOperation.filter(
bridge_deposits=None,
).prefetch_related('l2_token').order_by('level', 'transaction_index', 'log_index')

async for l2_deposit in qs:
bridge_deposit = (
await BridgeDepositOperation.filter(
l2_transaction=None,
l1_transaction__inbox_message_id=l2_deposit.inbox_message_id,
)
.prefetch_related()
.first()
)
l2_deposit: EtherlinkDepositOperation
bridge_deposit = await BridgeDepositOperation.filter(
inbox_message__level=l2_deposit.inbox_message_level,
inbox_message__index=l2_deposit.inbox_message_index,
l2_transaction=None,
).first()

if not bridge_deposit:
continue
bridge_deposit.l2_transaction = l2_deposit
await bridge_deposit.save()

bridge_operation = await BridgeOperation.get(id=bridge_deposit.id)
bridge_operation = await BridgeOperation.get(id=bridge_deposit.pk)
bridge_operation.is_completed = True
bridge_operation.is_successful = l2_deposit.l2_token is not None
bridge_operation.updated_at = max(bridge_operation.created_at, l2_deposit.timestamp)
bridge_operation.updated_at = l2_deposit.timestamp
match (l2_deposit.l2_token_id, l2_deposit.ticket_id, l2_deposit.ticket_owner):
case str(), str(), str():
bridge_operation.status = BridgeOperationStatus.finished
Expand All @@ -161,39 +134,40 @@ async def check_pending_etherlink_xtz_deposits(cls):
cls._pending_etherlink_xtz_deposits = False

qs = EtherlinkDepositOperation.filter(
bridge_deposits__isnull=True,
inbox_message_id__isnull=True,
bridge_deposits=None,
l2_token_id='xtz',
).order_by('level', 'transaction_index')
).order_by('level', 'transaction_index').prefetch_related('l2_token', 'l2_token__ticket')
async for l2_deposit in qs:
await l2_deposit.fetch_related('l2_token', 'l2_token__ticket')
l2_deposit: EtherlinkDepositOperation
bridge_deposit = (
await BridgeDepositOperation.filter(
l2_transaction=None,
l1_transaction__inbox_message_id__gt=0,
inbox_message_id__isnull=False,
l1_transaction__ticket=l2_deposit.l2_token.ticket,
l1_transaction__timestamp__lte=l2_deposit.timestamp,
l1_transaction__timestamp__gte=l2_deposit.timestamp - LAYERS_TIMESTAMP_GAP_MAX,
l1_transaction__l2_account=l2_deposit.l2_account,
l1_transaction__amount=l2_deposit.amount[:-12],
)
.order_by('l1_transaction__timestamp')
.prefetch_related('l1_transaction__inbox_message')
.prefetch_related('inbox_message', 'l1_transaction')
.first()
)

if not bridge_deposit:
continue

l2_deposit.inbox_message = bridge_deposit.l1_transaction.inbox_message
await l2_deposit.save()

bridge_deposit.l2_transaction = l2_deposit
await bridge_deposit.save()
bridge_deposit.l1_transaction.parameters_hash = None
await bridge_deposit.l1_transaction.save()
bridge_deposit.inbox_message.parameters_hash = None
await bridge_deposit.inbox_message.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
bridge_operation.updated_at = max(bridge_operation.created_at, l2_deposit.timestamp)
bridge_operation.updated_at = l2_deposit.timestamp
bridge_operation.status = BridgeOperationStatus.finished
await bridge_operation.save()

Expand Down
39 changes: 15 additions & 24 deletions bridge_indexer/handlers/etherlink/on_deposit.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,9 @@
import asyncio
from datetime import datetime
from datetime import timezone

from dipdup.context import HandlerContext
from dipdup.models.evm import EvmEvent
from tortoise.exceptions import DoesNotExist

from bridge_indexer.handlers import setup_handler_logger
from bridge_indexer.handlers.bridge_matcher import BridgeMatcher
from bridge_indexer.models import EtherlinkDepositOperation
from bridge_indexer.models import EtherlinkToken
Expand Down Expand Up @@ -41,7 +38,6 @@ async def on_deposit(
ctx: HandlerContext,
event: EvmEvent[DepositPayload],
) -> None:
setup_handler_logger(ctx)
ctx.logger.info(f'Etherlink Deposit Event found: 0x{event.data.transaction_hash}')

try:
Expand All @@ -50,29 +46,23 @@ async def on_deposit(
ctx.logger.warning(
'Incorrect Deposit Routing Info: ' + exception.args[0].format(*exception.args[1:]) + '. Mark Operation as `Failed Deposit`.'
)
etherlink_token = None

if event.payload.ticket_owner == event.payload.receiver:
ctx.logger.warning('Incorrect Deposit Routing Info: `ticket_owner == receiver`. Mark Operation as `Revertable Deposit`.')
event.payload.ticket_hash = None
etherlink_token = None
else:
try:
token_contract = event.payload.ticket_owner.removeprefix('0x')
etherlink_token = await register_etherlink_token(token_contract, event.payload.ticket_hash)
except ValueError as exception:
ctx.logger.warning(
'Incorrect Deposit Routing Info: ' + exception.args[0].format(*exception.args[1:]) + '. Mark Operation as `Failed Deposit`.'
)
if event.payload.ticket_owner == event.payload.receiver:
ctx.logger.warning('Incorrect Deposit Routing Info: `ticket_owner == receiver`. Mark Operation as `Revertable Deposit`.')
etherlink_token = None

while True:
try:
inbox_message = await ctx.container.inbox_message_service.find_by_index(event.payload.inbox_level, event.payload.inbox_msg_id)
except DoesNotExist:
ctx.logger.warning('L2 deposit is matched before L1. Waiting for L1 deposit with inbox_message...')
await asyncio.sleep(1)
else:
break
try:
token_contract = event.payload.ticket_owner.removeprefix('0x')
etherlink_token = await register_etherlink_token(token_contract, event.payload.ticket_hash)
except ValueError as exception:
ctx.logger.warning(
'Incorrect Deposit Routing Info: '
+ exception.args[0].format(*exception.args[1:])
+ '. Mark Operation as `Failed Deposit`.'
)
etherlink_token = None

deposit = await EtherlinkDepositOperation.create(
timestamp=datetime.fromtimestamp(event.data.timestamp, tz=timezone.utc),
Expand All @@ -86,7 +76,8 @@ async def on_deposit(
ticket_id=event.payload.ticket_hash,
ticket_owner=event.payload.ticket_owner[-40:],
amount=event.payload.amount,
inbox_message=inbox_message,
inbox_message_level=event.payload.inbox_level,
inbox_message_index=event.payload.inbox_msg_id,
)

ctx.logger.info(f'Etherlink Deposit Event registered: {deposit.id}')
Expand Down
9 changes: 4 additions & 5 deletions bridge_indexer/handlers/etherlink/on_xtz_deposit.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
from dipdup.context import HandlerContext
from dipdup.models.evm import EvmTransactionData

from bridge_indexer.handlers import setup_handler_logger
from bridge_indexer.handlers.bridge_matcher import BridgeMatcher
from bridge_indexer.models import EtherlinkDepositOperation
from bridge_indexer.models import EtherlinkToken
Expand All @@ -27,8 +26,10 @@ async def on_xtz_deposit(
ctx: HandlerContext,
transaction: EvmTransactionData,
) -> None:
setup_handler_logger(ctx)
ctx.logger.info(f'Etherlink XTZ Deposit Transaction found: 0x{transaction.hash}')
if transaction.to == transaction.from_:
return

ctx.logger.info(f'Etherlink XTZ Deposit Transaction found: {transaction.hash}')

try:
await _validate_xtz_transaction(transaction)
Expand All @@ -43,15 +44,13 @@ async def on_xtz_deposit(
timestamp=datetime.fromtimestamp(transaction.timestamp, tz=timezone.utc),
level=transaction.level,
address=transaction.from_[-40:],
log_index=0,
transaction_hash=transaction.hash[-64:],
transaction_index=transaction.transaction_index,
l2_account=transaction.to[-40:],
l2_token=etherlink_token,
ticket=tezos_ticket,
ticket_owner=etherlink_token.id,
amount=transaction.value,
inbox_message=None,
)

ctx.logger.info(f'XTZ Deposit Transaction registered: {deposit.id}')
Expand Down
Loading

0 comments on commit 0b79e27

Please sign in to comment.