Skip to content

Commit

Permalink
XTZ Deposit support added
Browse files Browse the repository at this point in the history
  • Loading branch information
igorsereda committed Feb 13, 2024
1 parent d53bb9d commit 3038d94
Show file tree
Hide file tree
Showing 11 changed files with 136 additions and 19 deletions.
22 changes: 9 additions & 13 deletions bridge_indexer/dipdup.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -36,19 +36,6 @@ datasources:
etherlink_node:
kind: evm.node
url: ${ETHERLINK_NODE_URL}
http:
retry_count: 6
retry_sleep: 10
retry_multiplier: 1

ratelimit_rate: 50
ratelimit_period: 10
ratelimit_sleep: .2

connection_limit: 5
connection_timeout: 10
request_timeout: 10
batch_size: 99

etherlink_subsquid:
kind: evm.subsquid
Expand Down Expand Up @@ -144,6 +131,15 @@ indexes:
name: Transfer


etherlink_kernel_transactions:
kind: evm.subsquid.transactions
datasource: etherlink_subsquid
node_only: true
handlers:
- callback: etherlink.on_xtz_deposit
from: etherlink_rollup_kernel


advanced:
reindex:
config_modified: ignore
Expand Down
32 changes: 32 additions & 0 deletions bridge_indexer/handlers/bridge_matcher.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
from datetime import timedelta

from bridge_indexer.models import BridgeDepositTransaction
from bridge_indexer.models import BridgeWithdrawTransaction
from bridge_indexer.models import EtherlinkDepositEvent
Expand Down Expand Up @@ -37,6 +39,36 @@ async def check_pending_etherlink_deposits():
bridge_deposit.l2_transaction = l2_deposit
await bridge_deposit.save()

qs = EtherlinkDepositEvent.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__tezos_ticket')
bridge_deposit = (
await BridgeDepositTransaction.filter(
l2_transaction=None,
l1_transaction__inbox_message_id__gt=0,
l1_transaction__ticket=l2_deposit.l2_token.tezos_ticket,
l1_transaction__timestamp__gt=l2_deposit.timestamp - timedelta(minutes=5),
l1_transaction__timestamp__lt=l2_deposit.timestamp + timedelta(minutes=5),
l1_transaction__l2_account=l2_deposit.l2_account,
l1_transaction__amount=l2_deposit.amount[:-12],
)
.prefetch_related('l1_transaction__inbox_message')
.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()

@staticmethod
async def check_pending_tezos_withdrawals():
qs = TezosWithdrawEvent.filter(bridge_withdrawals__isnull=True).order_by('level')
Expand Down
8 changes: 6 additions & 2 deletions bridge_indexer/handlers/etherlink/on_deposit.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,12 @@ async def on_deposit(
ctx: HandlerContext,
event: SubsquidEvent[Deposit],
) -> None:
token_contract = event.payload.ticket_owner[-40:]
etherlink_token = await register_etherlink_token(token_contract, event.payload.ticket_hash)
if event.payload.ticket_owner == event.payload.receiver:
ctx.logger.warning('Deposit routing info seems wrong')
etherlink_token = None
else:
token_contract = event.payload.ticket_owner[-40:]
etherlink_token = await register_etherlink_token(token_contract, event.payload.ticket_hash)

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

Expand Down
50 changes: 50 additions & 0 deletions bridge_indexer/handlers/etherlink/on_xtz_deposit.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
from datetime import datetime
from datetime import timezone

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 EtherlinkToken


async def on_xtz_deposit(
ctx: HandlerContext,
transaction: SubsquidTransactionData | EvmNodeTransactionData,
) -> None:
validators = [
transaction.value > 0,
transaction.from_ == '0x0000000000000000000000000000000000000000',
transaction.to != transaction.from_,
transaction.input == '0x',
transaction.sighash == '0x',
transaction.data is None,
]
if not all(validators):
return

etherlink_token = await EtherlinkToken.get(id='xtz')

await EtherlinkDepositEvent.create(
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,
amount=transaction.value,
inbox_message=None,
)

ctx.logger.info(f'Deposit Transaction registered: {transaction}')

sync_level = ctx.datasources['etherlink_node']._subscriptions._subscriptions[None]
status = await Index.get(name='etherlink_kernel_events').only('status').values_list('status', flat=True)
if status == IndexStatus.realtime or sync_level - transaction.level < 5:
await BridgeMatcher.check_pending_etherlink_deposits()
5 changes: 3 additions & 2 deletions bridge_indexer/handlers/tezos/on_cement_commitment.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
from dipdup.context import HandlerContext
from dipdup.models.tezos_tzkt import TzktSmartRollupCement

from bridge_indexer.handlers.bridge_matcher import BridgeMatcher
from bridge_indexer.handlers.rollup_message import OutboxMessageService
from bridge_indexer.models import RollupCommitment
from bridge_indexer.models import RollupOutboxMessage
from dipdup.context import HandlerContext
from dipdup.models.tezos_tzkt import TzktSmartRollupCement


async def on_cement_commitment(
Expand Down
4 changes: 4 additions & 0 deletions bridge_indexer/handlers/tezos/on_rollup_call.py
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,10 @@ async def on_rollup_call(
routing_info = bytes.fromhex(parameter.bytes)
l2_receiver = routing_info[:20]

if len(routing_info) not in [20, 40]:
ctx.logger.warning('Invalid routing_info', parameter)
return

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

await TezosDepositEvent.create(
Expand Down
1 change: 0 additions & 1 deletion bridge_indexer/hooks/on_restart.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
from dipdup.context import HookContext
from dipdup.models import Index

from bridge_indexer.handlers.bridge_matcher import BridgeMatcher

Expand Down
1 change: 0 additions & 1 deletion bridge_indexer/hooks/on_synchronized.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
from dipdup.context import HookContext
from dipdup.exceptions import IndexAlreadyExistsError

from bridge_indexer.handlers.bridge_matcher import BridgeMatcher

Expand Down
3 changes: 3 additions & 0 deletions bridge_indexer/models/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ class Meta:
model_name=TezosTicket.Meta.model,
source_field='tezos_ticket_id',
to_field='id',
unique=True,
null=True,
)
tezos_ticket_hash = fields.CharField(max_length=78, index=True)
Expand Down Expand Up @@ -196,13 +197,15 @@ class Meta:
model_name=EtherlinkToken.Meta.model,
source_field='token_id',
to_field='id',
null=True,
)
amount = fields.TextField()
inbox_message: ForeignKeyFieldInstance[RollupInboxMessage] = fields.ForeignKeyField(
model_name=RollupInboxMessage.Meta.model,
source_field='inbox_message_id',
to_field='id',
unique=True,
null=True,
)

bridge_deposits: fields.ReverseRelation['BridgeDepositTransaction']
Expand Down
17 changes: 17 additions & 0 deletions bridge_indexer/sql/on_reindex/00_xtz_asset.sql
Original file line number Diff line number Diff line change
@@ -1,2 +1,19 @@
insert into tezos_token (id, contract_address, token_id, name, symbol, decimals, type)
values ('xtz', 'KT1000000000000000000000000000000000', '0', 'Tezos', 'XTZ', 6, 'native');

insert into tezos_ticket (id, ticketer_address, ticket_id, ticket_hash, token_id)
values (
'KT1Q6aNZ9aGro4DvBKwhKvVdia2UmVGsS9zE_0',
'KT1Q6aNZ9aGro4DvBKwhKvVdia2UmVGsS9zE',
'0',
'10666650643273303508566200220257708314889526103361559239516955374962850039068',
'xtz'
);

insert into etherlink_token (id, name, tezos_ticket_hash, tezos_ticket_id)
values (
'xtz',
'ethXTZ',
'10666650643273303508566200220257708314889526103361559239516955374962850039068',
'KT1Q6aNZ9aGro4DvBKwhKvVdia2UmVGsS9zE_0'
);
12 changes: 12 additions & 0 deletions bridge_indexer/types/l2_token/evm_methods/transfer.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
from __future__ import annotations

from pydantic import BaseModel
from pydantic import Extra


class Transfer(BaseModel):
class Config:
extra = Extra.forbid

to: str
amount: int

0 comments on commit 3038d94

Please sign in to comment.